Skip to content

Commit

Permalink
Add flag to opt into including system tables in QgsAbstractDatabasePr…
Browse files Browse the repository at this point in the history
…oviderConnection tables listings
  • Loading branch information
nyalldawson authored and troopa81 committed Nov 29, 2022
1 parent 3864de6 commit 44361ba
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
Expand Up @@ -41,6 +41,7 @@ is not supported or cannot be performed without errors.
View,
MaterializedView,
Foreign,
IncludeSystemTables,
};

typedef QFlags<QgsAbstractDatabaseProviderConnection::TableFlag> TableFlags;
Expand Down
5 changes: 4 additions & 1 deletion src/core/providers/ogr/qgsogrproviderconnection.cpp
Expand Up @@ -186,7 +186,10 @@ QList<QgsAbstractDatabaseProviderConnection::TableProperty> QgsOgrProviderConnec
QList<QgsAbstractDatabaseProviderConnection::TableProperty> tableInfo;

QgsProviderMetadata *metadata = QgsProviderRegistry::instance()->providerMetadata( QStringLiteral( "ogr" ) );
const QList< QgsProviderSublayerDetails > subLayers = metadata->querySublayers( uri(), Qgis::SublayerQueryFlag::IncludeSystemTables, nullptr );
const QList< QgsProviderSublayerDetails > subLayers = metadata->querySublayers(
uri(),
( flags & TableFlag::IncludeSystemTables ) ? Qgis::SublayerQueryFlag::IncludeSystemTables : Qgis::SublayerQueryFlags(),
nullptr );

tableInfo.reserve( subLayers.size() );
for ( const QgsProviderSublayerDetails &subLayer : subLayers )
Expand Down
1 change: 1 addition & 0 deletions src/core/providers/qgsabstractdatabaseproviderconnection.h
Expand Up @@ -65,6 +65,7 @@ class CORE_EXPORT QgsAbstractDatabaseProviderConnection : public QgsAbstractProv
View = 1 << 4, //!< View table
MaterializedView = 1 << 5, //!< Materialized view table
Foreign = 1 << 6, //!< Foreign data wrapper
IncludeSystemTables = 1 << 7, //!< Include system tables (since QGIS 3.30)
};

Q_ENUM( TableFlag )
Expand Down
25 changes: 25 additions & 0 deletions tests/src/python/test_provider_ogr.py
Expand Up @@ -2787,6 +2787,31 @@ def testDiscoverRelationships(self):
self.assertEqual(rel.strength(), QgsRelation.Composition)
self.assertEqual(rel.fieldPairs(), {'REL_OBJECTID': 'OBJECTID'})

def test_provider_connection_tables(self):
"""
Test retrieving tables via the connections API
"""
metadata = QgsProviderRegistry.instance().providerMetadata('ogr')
# start with a connection which only supports one layer
conn = metadata.createConnection(TEST_DATA_DIR + '/' + 'relationships.gdb', {})
self.assertTrue(conn)

# don't want system tables included
self.assertCountEqual([t.tableName() for t in conn.tables()],
['table1', 'table2', 'table3', 'table4', 'table6', 'table7',
'table8', 'table9', 'points', 'points__ATTACH',
'composite_many_to_many', 'simple_attributed',
'simple_many_to_many'])
# DO want system tables included
self.assertCountEqual([t.tableName() for t in conn.tables('',
QgsAbstractDatabaseProviderConnection.TableFlag.Aspatial | QgsAbstractDatabaseProviderConnection.TableFlag.Vector | QgsAbstractDatabaseProviderConnection.TableFlag.IncludeSystemTables)],
['table1', 'table2', 'table3', 'table4', 'table6', 'table7',
'table8', 'table9', 'points', 'points__ATTACH',
'composite_many_to_many', 'simple_attributed',
'simple_many_to_many', 'GDB_DBTune', 'GDB_ItemRelationshipTypes',
'GDB_ItemRelationships', 'GDB_ItemTypes', 'GDB_Items',
'GDB_SpatialRefs', 'GDB_SystemCatalog'])

@unittest.skipIf(int(gdal.VersionInfo('VERSION_NUM')) < GDAL_COMPUTE_VERSION(3, 6, 0), "GDAL 3.6 required")
def test_provider_connection_relationships(self):
"""
Expand Down

0 comments on commit 44361ba

Please sign in to comment.