Skip to content

Commit

Permalink
Provide a public API for invalidating connections.
Browse files Browse the repository at this point in the history
QgsVectorDataProvider::forceReload() is the virtual method. It is implemented
in the OGR provider.
See here for reference:
http://gis.stackexchange.com/questions/159950/make-a-qgis-layer-update-from-a-changed-data-source
  • Loading branch information
m-kuhn committed Aug 29, 2015
1 parent 812b012 commit aeeca48
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python/core/qgsvectordataprovider.sip
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ class QgsVectorDataProvider : QgsDataProvider
*/
virtual QgsTransaction* transaction() const;

virtual void forceReload();

protected:
void clearMinMaxCache();
void fillMinMaxCache();
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsvectordataprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,15 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
*/
virtual QgsTransaction* transaction() const { return 0; }

/**
* Forces a reload of the underlying datasource if the provider implements this
* method.
* In particular on the OGR provider, a pooled connection will be invalidated.
* This forces QGIS to reopen a file or connection.
* This can be required if the underlying file is replaced.
*/
virtual void forceReload() {}

protected:
void clearMinMaxCache();
void fillMinMaxCache();
Expand Down
5 changes: 5 additions & 0 deletions src/providers/ogr/qgsogrprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2439,6 +2439,11 @@ QByteArray QgsOgrProvider::quotedIdentifier( QByteArray field )
return QgsOgrUtils::quotedIdentifier( field, ogrDriverName );
}

void QgsOgrProvider::forceReload()
{
QgsOgrConnPool::instance()->invalidateConnections( filePath() );
}

QByteArray QgsOgrUtils::quotedIdentifier( QByteArray field, const QString& ogrDriverName )
{
if ( ogrDriverName == "MySQL" )
Expand Down
7 changes: 7 additions & 0 deletions src/providers/ogr/qgsogrprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@ class QgsOgrProvider : public QgsVectorDataProvider

QByteArray quotedIdentifier( QByteArray field );

/**
* A forced reload invalidates the underlying connection.
* E.g. in case a shapefile is replaced, the old file will be closed
* and the new file will be opened.
*/
void forceReload();

protected:
/** Loads fields from input file to member attributeFields */
void loadFields();
Expand Down

0 comments on commit aeeca48

Please sign in to comment.