mirror of
https://github.com/qgis/QGIS.git
synced 2025-10-18 00:06:00 -04:00
schemas filtering for QgsMssqlProviderConnection
This commit is contained in:
parent
e6a2e1f6f3
commit
f81737c960
@ -429,6 +429,10 @@ QgsDataSourceUri QgsMssqlConnection::connUri( const QString &connName )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QStringList excludedSchemas = QgsMssqlConnection::excludedSchemasList( connName );
|
||||||
|
if ( !excludedSchemas.isEmpty() )
|
||||||
|
uri.setParam( QStringLiteral( "excludedSchemas" ), excludedSchemas.join( ',' ) );
|
||||||
|
|
||||||
return uri;
|
return uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -469,59 +473,80 @@ QList<QgsVectorDataProvider::NativeType> QgsMssqlConnection::nativeTypes()
|
|||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString QgsMssqlConnection::buildQueryForSchemas( const QString &connName, bool allowTablesWithNoGeometry )
|
QStringList QgsMssqlConnection::excludedSchemasList( const QString &connName )
|
||||||
{
|
{
|
||||||
QgsSettings settings;
|
QgsSettings settings;
|
||||||
|
|
||||||
QString selectedSchemas;
|
|
||||||
|
|
||||||
QString databaseName = settings.value( QStringLiteral( "/MSSQL/connections/" ) + connName + "/database" ).toString();
|
QString databaseName = settings.value( QStringLiteral( "/MSSQL/connections/" ) + connName + "/database" ).toString();
|
||||||
|
|
||||||
|
return excludedSchemasList( connName, databaseName );
|
||||||
|
}
|
||||||
|
|
||||||
|
QStringList QgsMssqlConnection::excludedSchemasList( const QString &connName, const QString &database )
|
||||||
|
{
|
||||||
|
QgsSettings settings;
|
||||||
bool schemaFilteringEnabled = settings.value( QStringLiteral( "/MSSQL/connections/" ) + connName + "/schemasFiltering" ).toBool();
|
bool schemaFilteringEnabled = settings.value( QStringLiteral( "/MSSQL/connections/" ) + connName + "/schemasFiltering" ).toBool();
|
||||||
|
|
||||||
if ( schemaFilteringEnabled )
|
if ( schemaFilteringEnabled )
|
||||||
{
|
{
|
||||||
QVariant schemaSettingsVariant = settings.value( QStringLiteral( "/MSSQL/connections/" ) + connName + "/schemasFiltered" );
|
QVariant schemaSettingsVariant = settings.value( QStringLiteral( "/MSSQL/connections/" ) + connName + "/excludedSchemas" );
|
||||||
|
|
||||||
if ( schemaSettingsVariant.type() == QVariant::Map )
|
if ( schemaSettingsVariant.type() == QVariant::Map )
|
||||||
{
|
{
|
||||||
|
const QVariantMap schemaSettings = schemaSettingsVariant.toMap();
|
||||||
|
if ( schemaSettings.contains( database ) && schemaSettings.value( database ).type() == QVariant::StringList )
|
||||||
|
return schemaSettings.value( database ).toStringList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return QStringList();
|
||||||
|
}
|
||||||
|
|
||||||
|
void QgsMssqlConnection::setExcludedSchemasList( const QString &connName, const QStringList &excludedSchemas )
|
||||||
|
{
|
||||||
|
QgsSettings settings;
|
||||||
|
|
||||||
|
QString currentDatabaseName = settings.value( QStringLiteral( "/MSSQL/connections/" ) + connName + "/database" ).toString();
|
||||||
|
setExcludedSchemasList( connName, currentDatabaseName, excludedSchemas );
|
||||||
|
}
|
||||||
|
|
||||||
|
void QgsMssqlConnection::setExcludedSchemasList( const QString &connName, const QString &database, const QStringList &excludedSchemas )
|
||||||
|
{
|
||||||
|
QgsSettings settings;
|
||||||
|
settings.setValue( QStringLiteral( "/MSSQL/connections/" ) + connName + "/schemasFiltering", excludedSchemas.isEmpty() ? 0 : 1 );
|
||||||
|
|
||||||
|
QVariant schemaSettingsVariant = settings.value( QStringLiteral( "/MSSQL/connections/" ) + connName + "/excludedSchemas" );
|
||||||
QVariantMap schemaSettings = schemaSettingsVariant.toMap();
|
QVariantMap schemaSettings = schemaSettingsVariant.toMap();
|
||||||
QVariantMap schemaSettingsForDatabase = schemaSettings.value( databaseName ).toMap();
|
schemaSettings.insert( database, excludedSchemas );
|
||||||
//schema filter
|
settings.setValue( QStringLiteral( "/MSSQL/connections/" ) + connName + "/excludedSchemas", schemaSettings );
|
||||||
|
}
|
||||||
|
|
||||||
|
QString QgsMssqlConnection::buildQueryForTables( bool allowTablesWithNoGeometry, bool geometryColumnOnly, const QStringList &excludedSchemaList )
|
||||||
QStringList schemaNames;
|
{
|
||||||
for ( const QString &schemaName : schemaSettingsForDatabase.keys() )
|
QString notSelectedSchemas;
|
||||||
|
if ( !excludedSchemaList.isEmpty() )
|
||||||
{
|
{
|
||||||
if ( schemaSettingsForDatabase.value( schemaName ).toBool() )
|
QStringList quotedSchemas;
|
||||||
{
|
for ( const QString &sch : excludedSchemaList )
|
||||||
schemaNames.append( QgsMssqlProvider::quotedValue( schemaName ) );
|
quotedSchemas.append( QgsMssqlProvider::quotedValue( sch ) );
|
||||||
}
|
notSelectedSchemas = quotedSchemas.join( ',' );
|
||||||
}
|
notSelectedSchemas.prepend( QStringLiteral( "( " ) );
|
||||||
if ( !schemaNames.empty() )
|
notSelectedSchemas.append( QStringLiteral( " )" ) );
|
||||||
selectedSchemas = schemaNames.join( ',' );
|
|
||||||
|
|
||||||
selectedSchemas.prepend( QStringLiteral( "( " ) );
|
|
||||||
selectedSchemas.append( QStringLiteral( " )" ) );
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// build sql statement
|
|
||||||
QString query( QStringLiteral( "SELECT " ) );
|
QString query( QStringLiteral( "SELECT " ) );
|
||||||
if ( geometryColumnsOnly( connName ) )
|
if ( geometryColumnOnly )
|
||||||
{
|
{
|
||||||
query += QStringLiteral( "f_table_schema, f_table_name, f_geometry_column, srid, geometry_type, 0 FROM geometry_columns" );
|
query += QStringLiteral( "f_table_schema, f_table_name, f_geometry_column, srid, geometry_type, 0 FROM geometry_columns" );
|
||||||
if ( !selectedSchemas.isEmpty() )
|
if ( !notSelectedSchemas.isEmpty() )
|
||||||
query += QStringLiteral( " WHERE f_table_schema IN %1" ).arg( selectedSchemas );
|
query += QStringLiteral( " WHERE f_table_schema NOT IN %1" ).arg( notSelectedSchemas );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
query += QStringLiteral( "sys.schemas.name, sys.objects.name, sys.columns.name, null, 'GEOMETRY', CASE when sys.objects.type = 'V' THEN 1 ELSE 0 END \n"
|
query += QStringLiteral( "sys.schemas.name, sys.objects.name, sys.columns.name, null, 'GEOMETRY', CASE when sys.objects.type = 'V' THEN 1 ELSE 0 END \n"
|
||||||
"FROM sys.columns JOIN sys.types ON sys.columns.system_type_id = sys.types.system_type_id AND sys.columns.user_type_id = sys.types.user_type_id JOIN sys.objects ON sys.objects.object_id = sys.columns.object_id JOIN sys.schemas ON sys.objects.schema_id = sys.schemas.schema_id \n"
|
"FROM sys.columns JOIN sys.types ON sys.columns.system_type_id = sys.types.system_type_id AND sys.columns.user_type_id = sys.types.user_type_id JOIN sys.objects ON sys.objects.object_id = sys.columns.object_id JOIN sys.schemas ON sys.objects.schema_id = sys.schemas.schema_id \n"
|
||||||
"WHERE (sys.types.name = 'geometry' OR sys.types.name = 'geography') AND (sys.objects.type = 'U' OR sys.objects.type = 'V')" );
|
"WHERE (sys.types.name = 'geometry' OR sys.types.name = 'geography') AND (sys.objects.type = 'U' OR sys.objects.type = 'V')" );
|
||||||
if ( !selectedSchemas.isEmpty() )
|
if ( !notSelectedSchemas.isEmpty() )
|
||||||
query += QStringLiteral( " AND (sys.schemas.name IN %1)" ).arg( selectedSchemas );
|
query += QStringLiteral( " AND (sys.schemas.name NOT IN %1)" ).arg( notSelectedSchemas );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( allowTablesWithNoGeometry )
|
if ( allowTablesWithNoGeometry )
|
||||||
@ -530,16 +555,21 @@ QString QgsMssqlConnection::buildQueryForSchemas( const QString &connName, bool
|
|||||||
"SELECT sys.schemas.name, sys.objects.name, null, null, 'NONE', case when sys.objects.type = 'V' THEN 1 ELSE 0 END \n"
|
"SELECT sys.schemas.name, sys.objects.name, null, null, 'NONE', case when sys.objects.type = 'V' THEN 1 ELSE 0 END \n"
|
||||||
"FROM sys.objects JOIN sys.schemas ON sys.objects.schema_id = sys.schemas.schema_id "
|
"FROM sys.objects JOIN sys.schemas ON sys.objects.schema_id = sys.schemas.schema_id "
|
||||||
"WHERE NOT EXISTS (SELECT * FROM sys.columns sc1 JOIN sys.types ON sc1.system_type_id = sys.types.system_type_id WHERE (sys.types.name = 'geometry' OR sys.types.name = 'geography') AND sys.objects.object_id = sc1.object_id) AND (sys.objects.type = 'U' or sys.objects.type = 'V')" );
|
"WHERE NOT EXISTS (SELECT * FROM sys.columns sc1 JOIN sys.types ON sc1.system_type_id = sys.types.system_type_id WHERE (sys.types.name = 'geometry' OR sys.types.name = 'geography') AND sys.objects.object_id = sc1.object_id) AND (sys.objects.type = 'U' or sys.objects.type = 'V')" );
|
||||||
if ( !selectedSchemas.isEmpty() )
|
if ( !notSelectedSchemas.isEmpty() )
|
||||||
query += QStringLiteral( " AND sys.schemas.name IN %1" ).arg( selectedSchemas );
|
query += QStringLiteral( " AND sys.schemas.name NOT IN %1" ).arg( notSelectedSchemas );
|
||||||
}
|
}
|
||||||
|
|
||||||
return query;
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString QgsMssqlConnection::buildQueryForSchemas( const QString &connName )
|
QString QgsMssqlConnection::buildQueryForTables( const QString &connName, bool allowTablesWithNoGeometry )
|
||||||
{
|
{
|
||||||
return buildQueryForSchemas( connName, allowGeometrylessTables( connName ) );
|
return buildQueryForTables( allowTablesWithNoGeometry, geometryColumnsOnly( connName ), excludedSchemasList( connName ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
QString QgsMssqlConnection::buildQueryForTables( const QString &connName )
|
||||||
|
{
|
||||||
|
return buildQueryForTables( allowGeometrylessTables( connName ), geometryColumnsOnly( connName ), excludedSchemasList( connName ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
QString QgsMssqlConnection::dbConnectionName( const QString &name )
|
QString QgsMssqlConnection::dbConnectionName( const QString &name )
|
||||||
|
@ -45,7 +45,6 @@ class QgsMssqlConnection
|
|||||||
*/
|
*/
|
||||||
static QSqlDatabase getDatabase( const QString &service, const QString &host, const QString &database, const QString &username, const QString &password );
|
static QSqlDatabase getDatabase( const QString &service, const QString &host, const QString &database, const QString &username, const QString &password );
|
||||||
|
|
||||||
|
|
||||||
static bool openDatabase( QSqlDatabase &db );
|
static bool openDatabase( QSqlDatabase &db );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -151,6 +150,7 @@ class QgsMssqlConnection
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of all schemas on the \a dataBase.
|
* Returns a list of all schemas on the \a dataBase.
|
||||||
|
* \since QGIS 3.18
|
||||||
*/
|
*/
|
||||||
static QStringList schemas( QSqlDatabase &dataBase, QString *errorMessage );
|
static QStringList schemas( QSqlDatabase &dataBase, QString *errorMessage );
|
||||||
|
|
||||||
@ -176,16 +176,47 @@ class QgsMssqlConnection
|
|||||||
static QList<QgsVectorDataProvider::NativeType> nativeTypes();
|
static QList<QgsVectorDataProvider::NativeType> nativeTypes();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds and returns a sql query string to obtain schemas list depending on settings and \a allowTablesWithNoGeometry
|
* Returns a list of excluded schemas for connection \a connName depending on settings, returns empty list if nothing is set for this connection
|
||||||
* \since QGIS 3.18
|
* \since QGIS 3.18
|
||||||
*/
|
*/
|
||||||
static QString buildQueryForSchemas( const QString &connName, bool allowTablesWithNoGeometry );
|
static QStringList excludedSchemasList( const QString &connName );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of excluded schemas for connection \a connName for a specific \a database depending on settings, returns empty list if nothing is set for this connection
|
||||||
|
* \since QGIS 3.18
|
||||||
|
*/
|
||||||
|
static QStringList excludedSchemasList( const QString &connName, const QString &database );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a list of excluded schemas for connection \a connName depending on settings, returns empty list if nothing is set for this connection
|
||||||
|
* \since QGIS 3.18
|
||||||
|
*/
|
||||||
|
static void setExcludedSchemasList( const QString &connName, const QStringList &excludedSchemas );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a list of excluded schemas for connection \a connName for a specific \a database depending on settings, returns empty list if nothing is set for this connection
|
||||||
|
* \since QGIS 3.18
|
||||||
|
*/
|
||||||
|
static void setExcludedSchemasList( const QString &connName, const QString &database, const QStringList &excludedSchemas );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds and returns a sql query string to obtain tables list depending on \a allowTablesWithNoGeometry, \a geometryColumnOnly and on \a notSelectedSchemasList
|
||||||
|
* \since QGIS 3.18
|
||||||
|
*/
|
||||||
|
static QString buildQueryForTables( bool allowTablesWithNoGeometry, bool geometryColumnOnly, const QStringList &excludedSchemaList = QStringList() );
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds and returns a sql query string to obtain tables list depending on settings and \a allowTablesWithNoGeometry
|
||||||
|
* \since QGIS 3.18
|
||||||
|
*/
|
||||||
|
static QString buildQueryForTables( const QString &connName, bool allowTablesWithNoGeometry );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds and returns a sql query string to obtain schemas list depending only on settings
|
* Builds and returns a sql query string to obtain schemas list depending only on settings
|
||||||
* \since QGIS 3.18
|
* \since QGIS 3.18
|
||||||
*/
|
*/
|
||||||
static QString buildQueryForSchemas( const QString &connName );
|
static QString buildQueryForTables( const QString &connName );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
@ -74,9 +74,10 @@ void QgsMssqlConnectionItem::readConnectionSettings()
|
|||||||
|
|
||||||
mSchemaSettings.clear();
|
mSchemaSettings.clear();
|
||||||
mSchemasFilteringEnabled = settings.value( key + "/schemasFiltering" ).toBool();
|
mSchemasFilteringEnabled = settings.value( key + "/schemasFiltering" ).toBool();
|
||||||
|
|
||||||
if ( mSchemasFilteringEnabled )
|
if ( mSchemasFilteringEnabled )
|
||||||
{
|
{
|
||||||
QVariant schemasSettingsVariant = settings.value( key + "/schemasFiltered" );
|
QVariant schemasSettingsVariant = settings.value( key + "/excludedSchemas" );
|
||||||
if ( schemasSettingsVariant.isValid() && schemasSettingsVariant.type() == QVariant::Map )
|
if ( schemasSettingsVariant.isValid() && schemasSettingsVariant.type() == QVariant::Map )
|
||||||
mSchemaSettings = schemasSettingsVariant.toMap();
|
mSchemaSettings = schemasSettingsVariant.toMap();
|
||||||
}
|
}
|
||||||
@ -144,7 +145,7 @@ QVector<QgsDataItem *> QgsMssqlConnectionItem::createChildren()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// build sql statement
|
// build sql statement
|
||||||
QString query = QgsMssqlConnection::buildQueryForSchemas( mName );
|
QString query = QgsMssqlConnection::buildQueryForTables( mName );
|
||||||
|
|
||||||
const bool disableInvalidGeometryHandling = QgsMssqlConnection::isInvalidGeometryHandlingDisabled( mName );
|
const bool disableInvalidGeometryHandling = QgsMssqlConnection::isInvalidGeometryHandlingDisabled( mName );
|
||||||
|
|
||||||
@ -252,14 +253,13 @@ QVector<QgsDataItem *> QgsMssqlConnectionItem::createChildren()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// add missing schemas (i.e., empty schemas)
|
// add missing schemas (i.e., empty schemas)
|
||||||
const QString uri = connInfo();
|
const QString uri = connInfo();
|
||||||
const QStringList allSchemas = QgsMssqlConnection::schemas( uri, nullptr );
|
const QStringList allSchemas = QgsMssqlConnection::schemas( uri, nullptr );
|
||||||
QVariantMap schemaSettings = mSchemaSettings.value( mDatabase ).toMap();
|
QStringList excludedSchema = QgsMssqlConnection::excludedSchemasList( mName );
|
||||||
for ( const QString &schema : allSchemas )
|
for ( const QString &schema : allSchemas )
|
||||||
{
|
{
|
||||||
if ( mSchemasFilteringEnabled && !schemaSettings.value( schema ).toBool() )
|
if ( mSchemasFilteringEnabled && excludedSchema.contains( schema ) )
|
||||||
continue; // user does not want it to be shown
|
continue; // user does not want it to be shown
|
||||||
|
|
||||||
if ( addedSchemas.contains( schema ) )
|
if ( addedSchemas.contains( schema ) )
|
||||||
@ -274,8 +274,6 @@ QVector<QgsDataItem *> QgsMssqlConnectionItem::createChildren()
|
|||||||
children.append( schemaItem );
|
children.append( schemaItem );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// spawn threads (new layers will be added later on)
|
// spawn threads (new layers will be added later on)
|
||||||
if ( mColumnTypeThread )
|
if ( mColumnTypeThread )
|
||||||
{
|
{
|
||||||
|
@ -61,7 +61,7 @@ QgsMssqlNewConnection::QgsMssqlNewConnection( QWidget *parent, const QString &co
|
|||||||
txtHost->setText( settings.value( key + "/host" ).toString() );
|
txtHost->setText( settings.value( key + "/host" ).toString() );
|
||||||
listDatabase->addItem( settings.value( key + "/database" ).toString() );
|
listDatabase->addItem( settings.value( key + "/database" ).toString() );
|
||||||
groupBoxSchemasFilter->setChecked( settings.value( key + "/schemasFiltering" ).toBool() );
|
groupBoxSchemasFilter->setChecked( settings.value( key + "/schemasFiltering" ).toBool() );
|
||||||
QVariant schemasVariant = settings.value( key + "/schemasFiltered" );
|
QVariant schemasVariant = settings.value( key + "/excludedSchemas" );
|
||||||
if ( schemasVariant.isValid() && schemasVariant.type() == QVariant::Map )
|
if ( schemasVariant.isValid() && schemasVariant.type() == QVariant::Map )
|
||||||
mSchemaSettings = schemasVariant.toMap();
|
mSchemaSettings = schemasVariant.toMap();
|
||||||
|
|
||||||
@ -90,14 +90,8 @@ QgsMssqlNewConnection::QgsMssqlNewConnection( QWidget *parent, const QString &co
|
|||||||
cb_trustedConnection_clicked();
|
cb_trustedConnection_clicked();
|
||||||
|
|
||||||
schemaView->setModel( &mSchemaModel );
|
schemaView->setModel( &mSchemaModel );
|
||||||
if ( listDatabase->currentItem() )
|
|
||||||
{
|
|
||||||
QString dataBaseName = listDatabase->currentItem()->text();
|
|
||||||
mSchemaModel.setDataBaseName( dataBaseName );
|
|
||||||
mSchemaModel.setSchemasSetting( mSchemaSettings.value( dataBaseName ).toMap() );
|
|
||||||
}
|
|
||||||
|
|
||||||
onCurrentDataBaseChange();
|
onCurrentDataBaseChange();
|
||||||
|
|
||||||
groupBoxSchemasFilter->setCollapsed( !groupBoxSchemasFilter->isChecked() );
|
groupBoxSchemasFilter->setCollapsed( !groupBoxSchemasFilter->isChecked() );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,8 +141,8 @@ void QgsMssqlNewConnection::accept()
|
|||||||
if ( groupBoxSchemasFilter->isChecked() )
|
if ( groupBoxSchemasFilter->isChecked() )
|
||||||
{
|
{
|
||||||
if ( !mSchemaModel.dataBaseName().isEmpty() )
|
if ( !mSchemaModel.dataBaseName().isEmpty() )
|
||||||
mSchemaSettings.insert( mSchemaModel.dataBaseName(), mSchemaModel.schemasSettings() );
|
mSchemaSettings.insert( mSchemaModel.dataBaseName(), mSchemaModel.uncheckedSchemas() );
|
||||||
settings.setValue( baseKey + "/schemasFiltered", mSchemaSettings );
|
settings.setValue( baseKey + "/excludedSchemas", mSchemaSettings );
|
||||||
}
|
}
|
||||||
|
|
||||||
settings.setValue( baseKey + "/schemasFiltering", groupBoxSchemasFilter->isChecked() );
|
settings.setValue( baseKey + "/schemasFiltering", groupBoxSchemasFilter->isChecked() );
|
||||||
@ -303,9 +297,8 @@ void QgsMssqlNewConnection::updateOkButtonState()
|
|||||||
void QgsMssqlNewConnection::onCurrentDataBaseChange()
|
void QgsMssqlNewConnection::onCurrentDataBaseChange()
|
||||||
{
|
{
|
||||||
//First store the schema settings for the previous dataBase
|
//First store the schema settings for the previous dataBase
|
||||||
QVariantMap vm = mSchemaModel.schemasSettings();
|
|
||||||
if ( !mSchemaModel.dataBaseName().isEmpty() )
|
if ( !mSchemaModel.dataBaseName().isEmpty() )
|
||||||
mSchemaSettings.insert( mSchemaModel.dataBaseName(), mSchemaModel.schemasSettings() );
|
mSchemaSettings.insert( mSchemaModel.dataBaseName(), mSchemaModel.uncheckedSchemas() );
|
||||||
|
|
||||||
QString databaseName;
|
QString databaseName;
|
||||||
if ( listDatabase->currentItem() )
|
if ( listDatabase->currentItem() )
|
||||||
@ -318,23 +311,16 @@ void QgsMssqlNewConnection::onCurrentDataBaseChange()
|
|||||||
txtPassword->text().trimmed() );
|
txtPassword->text().trimmed() );
|
||||||
|
|
||||||
QStringList schemasList = QgsMssqlConnection::schemas( db, nullptr );
|
QStringList schemasList = QgsMssqlConnection::schemas( db, nullptr );
|
||||||
|
int i = 0;
|
||||||
QVariantMap newSchemaSettings = mSchemaSettings.value( databaseName ).toMap();
|
while ( i < schemasList.count() )
|
||||||
|
|
||||||
for ( const QString &sch : newSchemaSettings.keys() )
|
|
||||||
{
|
{
|
||||||
if ( !schemasList.contains( sch ) )
|
if ( QgsMssqlConnection::isSystemSchema( schemasList.at( i ) ) )
|
||||||
newSchemaSettings.remove( sch );
|
schemasList.removeAt( i );
|
||||||
|
else
|
||||||
|
++i;
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( const QString &sch : schemasList )
|
mSchemaModel.setSettings( databaseName, schemasList, QgsMssqlConnection::excludedSchemasList( txtName->text(), databaseName ) );
|
||||||
{
|
|
||||||
if ( !newSchemaSettings.contains( sch ) && !QgsMssqlConnection::isSystemSchema( sch ) )
|
|
||||||
newSchemaSettings.insert( sch, true );
|
|
||||||
}
|
|
||||||
|
|
||||||
mSchemaModel.setDataBaseName( databaseName );
|
|
||||||
mSchemaModel.setSchemasSetting( newSchemaSettings );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QgsMssqlNewConnection::SchemaModel::SchemaModel( QObject *parent ): QAbstractListModel( parent )
|
QgsMssqlNewConnection::SchemaModel::SchemaModel( QObject *parent ): QAbstractListModel( parent )
|
||||||
@ -351,18 +337,17 @@ QVariant QgsMssqlNewConnection::SchemaModel::data( const QModelIndex &index, int
|
|||||||
if ( !index.isValid() || index.row() >= mSchemas.count() )
|
if ( !index.isValid() || index.row() >= mSchemas.count() )
|
||||||
return QVariant();
|
return QVariant();
|
||||||
|
|
||||||
QList<QString> schemasName = mSchemas.keys();
|
|
||||||
|
|
||||||
switch ( role )
|
switch ( role )
|
||||||
{
|
{
|
||||||
case Qt::CheckStateRole:
|
case Qt::CheckStateRole:
|
||||||
if ( mSchemas.value( schemasName.at( index.row() ) ).toBool() )
|
if ( mExcludedSchemas.contains( mSchemas.at( index.row() ) ) )
|
||||||
return Qt::CheckState::Checked;
|
|
||||||
else
|
|
||||||
return Qt::CheckState::Unchecked;
|
return Qt::CheckState::Unchecked;
|
||||||
|
else
|
||||||
|
return Qt::CheckState::Checked;
|
||||||
break;
|
break;
|
||||||
case Qt::DisplayRole:
|
case Qt::DisplayRole:
|
||||||
return schemasName.at( index.row() );
|
return mSchemas.at( index.row() );
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return QVariant();
|
return QVariant();
|
||||||
@ -376,11 +361,13 @@ bool QgsMssqlNewConnection::SchemaModel::setData( const QModelIndex &index, cons
|
|||||||
if ( !index.isValid() || index.row() >= mSchemas.count() )
|
if ( !index.isValid() || index.row() >= mSchemas.count() )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
QList<QString> schemasName = mSchemas.keys();
|
|
||||||
switch ( role )
|
switch ( role )
|
||||||
{
|
{
|
||||||
case Qt::CheckStateRole:
|
case Qt::CheckStateRole:
|
||||||
mSchemas[schemasName.at( index.row() )] = value;
|
if ( value == Qt::Checked && mExcludedSchemas.contains( mSchemas.at( index.row() ) ) )
|
||||||
|
mExcludedSchemas.removeOne( mSchemas.at( index.row() ) );
|
||||||
|
else if ( value == Qt::Unchecked && !mExcludedSchemas.contains( mSchemas.at( index.row() ) ) )
|
||||||
|
mExcludedSchemas.append( mSchemas.at( index.row() ) );
|
||||||
return true;
|
return true;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -395,17 +382,11 @@ Qt::ItemFlags QgsMssqlNewConnection::SchemaModel::flags( const QModelIndex &inde
|
|||||||
return QAbstractListModel::flags( index ) | Qt::ItemFlag::ItemIsUserCheckable;
|
return QAbstractListModel::flags( index ) | Qt::ItemFlag::ItemIsUserCheckable;
|
||||||
}
|
}
|
||||||
|
|
||||||
void QgsMssqlNewConnection::SchemaModel::setSchemasSetting( const QVariantMap &schemas )
|
QStringList QgsMssqlNewConnection::SchemaModel::uncheckedSchemas() const
|
||||||
{
|
{
|
||||||
beginResetModel();
|
return mExcludedSchemas;
|
||||||
mSchemas = schemas;
|
|
||||||
endResetModel();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariantMap QgsMssqlNewConnection::SchemaModel::schemasSettings() const
|
|
||||||
{
|
|
||||||
return mSchemas;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString QgsMssqlNewConnection::SchemaModel::dataBaseName() const
|
QString QgsMssqlNewConnection::SchemaModel::dataBaseName() const
|
||||||
{
|
{
|
||||||
@ -416,3 +397,12 @@ void QgsMssqlNewConnection::SchemaModel::setDataBaseName( const QString &dataBas
|
|||||||
{
|
{
|
||||||
mDataBaseName = dataBaseName;
|
mDataBaseName = dataBaseName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QgsMssqlNewConnection::SchemaModel::setSettings( const QString &database, const QStringList &schemas, const QStringList &excludedSchemas )
|
||||||
|
{
|
||||||
|
beginResetModel();
|
||||||
|
mDataBaseName = database;
|
||||||
|
mSchemas = schemas;
|
||||||
|
mExcludedSchemas = excludedSchemas;
|
||||||
|
endResetModel();
|
||||||
|
}
|
||||||
|
@ -62,11 +62,8 @@ class QgsMssqlNewConnection : public QDialog, private Ui::QgsMssqlNewConnectionB
|
|||||||
bool setData( const QModelIndex &index, const QVariant &value, int role ) override;
|
bool setData( const QModelIndex &index, const QVariant &value, int role ) override;
|
||||||
Qt::ItemFlags flags( const QModelIndex &index ) const override;
|
Qt::ItemFlags flags( const QModelIndex &index ) const override;
|
||||||
|
|
||||||
//! Sets the schema settings (keyd : schema names, value : bool that represents whether the schema is checked)
|
//! Returns the unchecked schemas
|
||||||
void setSchemasSetting( const QVariantMap &schemas );
|
QStringList uncheckedSchemas() const;
|
||||||
|
|
||||||
//! Returns the schema settings (keyd : schema names, value : bool that represents whether the schema is checked)
|
|
||||||
QVariantMap schemasSettings() const;
|
|
||||||
|
|
||||||
//! Returns the database nale represented by the model
|
//! Returns the database nale represented by the model
|
||||||
QString dataBaseName() const;
|
QString dataBaseName() const;
|
||||||
@ -74,9 +71,13 @@ class QgsMssqlNewConnection : public QDialog, private Ui::QgsMssqlNewConnectionB
|
|||||||
//! Sets the database nale represented by the model
|
//! Sets the database nale represented by the model
|
||||||
void setDataBaseName( const QString &dataBaseName );
|
void setDataBaseName( const QString &dataBaseName );
|
||||||
|
|
||||||
|
//! Sets the settings for \a database
|
||||||
|
void setSettings( const QString &database, const QStringList &schemas, const QStringList &excludedSchemas );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QVariantMap mSchemas;
|
|
||||||
QString mDataBaseName;
|
QString mDataBaseName;
|
||||||
|
QStringList mSchemas;
|
||||||
|
QStringList mExcludedSchemas;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -66,6 +66,9 @@ QgsMssqlProviderConnection::QgsMssqlProviderConnection( const QString &uri, cons
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( inputUri.hasParam( QStringLiteral( "excludedSchemas" ) ) )
|
||||||
|
currentUri.setParam( QStringLiteral( "excludedSchemas" ), inputUri.param( QStringLiteral( "excludedSchemas" ) ) );
|
||||||
|
|
||||||
setUri( currentUri.uri() );
|
setUri( currentUri.uri() );
|
||||||
setDefaultCapabilities();
|
setDefaultCapabilities();
|
||||||
}
|
}
|
||||||
@ -456,8 +459,13 @@ QStringList QgsMssqlProviderConnection::schemas( ) const
|
|||||||
{
|
{
|
||||||
checkCapability( Capability::Schemas );
|
checkCapability( Capability::Schemas );
|
||||||
QStringList schemas;
|
QStringList schemas;
|
||||||
|
|
||||||
|
QgsDataSourceUri connUri( uri() );
|
||||||
|
|
||||||
const QgsDataSourceUri dsUri { uri() };
|
const QgsDataSourceUri dsUri { uri() };
|
||||||
const QString sql { QStringLiteral(
|
const QString sql
|
||||||
|
{
|
||||||
|
QStringLiteral(
|
||||||
R"raw(
|
R"raw(
|
||||||
SELECT s.name AS schema_name,
|
SELECT s.name AS schema_name,
|
||||||
s.schema_id,
|
s.schema_id,
|
||||||
@ -467,12 +475,22 @@ QStringList QgsMssqlProviderConnection::schemas( ) const
|
|||||||
ON u.uid = s.principal_id
|
ON u.uid = s.principal_id
|
||||||
WHERE u.issqluser = 1
|
WHERE u.issqluser = 1
|
||||||
AND u.name NOT IN ('sys', 'guest', 'INFORMATION_SCHEMA')
|
AND u.name NOT IN ('sys', 'guest', 'INFORMATION_SCHEMA')
|
||||||
)raw" )};
|
)raw" )
|
||||||
|
};
|
||||||
|
|
||||||
const QList<QVariantList> result { executeSqlPrivate( sql, false ).rows() };
|
const QList<QVariantList> result { executeSqlPrivate( sql, false ).rows() };
|
||||||
|
|
||||||
|
QStringList excludedSchemaList;
|
||||||
|
if ( connUri.hasParam( QStringLiteral( "excludedSchemas" ) ) )
|
||||||
|
excludedSchemaList = QgsDataSourceUri( uri() ).param( QStringLiteral( "excludedSchemas" ) ).split( ',' );
|
||||||
for ( const auto &row : result )
|
for ( const auto &row : result )
|
||||||
{
|
{
|
||||||
if ( row.size() > 0 )
|
if ( row.size() > 0 )
|
||||||
schemas.push_back( row.at( 0 ).toString() );
|
{
|
||||||
|
QString schema = row.at( 0 ).toString();
|
||||||
|
if ( !excludedSchemaList.contains( schema ) )
|
||||||
|
schemas.push_back( schema );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return schemas;
|
return schemas;
|
||||||
}
|
}
|
||||||
@ -498,13 +516,14 @@ void QgsMssqlProviderConnection::store( const QString &name ) const
|
|||||||
settings.setValue( "password", dsUri.password() );
|
settings.setValue( "password", dsUri.password() );
|
||||||
settings.setValue( "estimatedMetadata", dsUri.useEstimatedMetadata() );
|
settings.setValue( "estimatedMetadata", dsUri.useEstimatedMetadata() );
|
||||||
|
|
||||||
|
QgsMssqlConnection::setExcludedSchemasList( name, dsUri.database(), dsUri.param( QStringLiteral( "excludedSchemas" ) ).split( ',' ) );
|
||||||
|
|
||||||
for ( const auto ¶m : EXTRA_CONNECTION_PARAMETERS )
|
for ( const auto ¶m : EXTRA_CONNECTION_PARAMETERS )
|
||||||
{
|
{
|
||||||
if ( dsUri.hasParam( param ) )
|
if ( dsUri.hasParam( param ) )
|
||||||
{
|
{
|
||||||
settings.setValue( param, dsUri.param( param ) == QStringLiteral( "true" )
|
settings.setValue( param, dsUri.param( param ) == QStringLiteral( "true" )
|
||||||
|| dsUri.param( param ) == '1' );
|
|| dsUri.param( param ) == '1' );
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -540,7 +540,7 @@ void QgsMssqlSourceSelect::btnConnect_clicked()
|
|||||||
// Read supported layers from database
|
// Read supported layers from database
|
||||||
QApplication::setOverrideCursor( Qt::WaitCursor );
|
QApplication::setOverrideCursor( Qt::WaitCursor );
|
||||||
|
|
||||||
QString query = QgsMssqlConnection::buildQueryForSchemas( cmbConnections->currentText(), allowGeometrylessTables );
|
QString query = QgsMssqlConnection::buildQueryForTables( cmbConnections->currentText(), allowGeometrylessTables );
|
||||||
|
|
||||||
// issue the sql query
|
// issue the sql query
|
||||||
q = QSqlQuery( db );
|
q = QSqlQuery( db );
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
<width>772</width>
|
<width>772</width>
|
||||||
<height>691</height>
|
<height>687</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
<property name="sizePolicy">
|
<property name="sizePolicy">
|
||||||
@ -272,19 +272,6 @@ Untick save if you don't wish to be the case.</string>
|
|||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="7" column="0" colspan="2">
|
<item row="7" column="0" colspan="2">
|
||||||
<widget class="Line" name="line">
|
|
||||||
<property name="minimumSize">
|
|
||||||
<size>
|
|
||||||
<width>0</width>
|
|
||||||
<height>0</height>
|
|
||||||
</size>
|
|
||||||
</property>
|
|
||||||
<property name="orientation">
|
|
||||||
<enum>Qt::Horizontal</enum>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item row="8" column="0" colspan="2">
|
|
||||||
<widget class="QgsCollapsibleGroupBoxBasic" name="groupBoxSchemasFilter">
|
<widget class="QgsCollapsibleGroupBoxBasic" name="groupBoxSchemasFilter">
|
||||||
<property name="title">
|
<property name="title">
|
||||||
<string>Use Only a Subset of Schemas</string>
|
<string>Use Only a Subset of Schemas</string>
|
||||||
@ -317,7 +304,7 @@ Untick save if you don't wish to be the case.</string>
|
|||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="9" column="0" colspan="2">
|
<item row="8" column="0" colspan="2">
|
||||||
<widget class="QPushButton" name="btnConnect">
|
<widget class="QPushButton" name="btnConnect">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Test Connection</string>
|
<string>Test Connection</string>
|
||||||
|
@ -102,6 +102,30 @@ class TestPyQgsProviderConnectionMssql(unittest.TestCase, TestPyQgsProviderConne
|
|||||||
fields = conn.fields('qgis_test', 'someData')
|
fields = conn.fields('qgis_test', 'someData')
|
||||||
self.assertEqual(fields.names(), ['pk', 'cnt', 'name', 'name2', 'num_char', 'dt', 'date', 'time'])
|
self.assertEqual(fields.names(), ['pk', 'cnt', 'name', 'name2', 'num_char', 'dt', 'date', 'time'])
|
||||||
|
|
||||||
|
def test_schemas_filtering(self):
|
||||||
|
"""Test schemas filtering"""
|
||||||
|
|
||||||
|
md = QgsProviderRegistry.instance().providerMetadata('mssql')
|
||||||
|
|
||||||
|
conn = md.createConnection(self.uri, {})
|
||||||
|
schemas = conn.schemas()
|
||||||
|
self.assertEqual(len(schemas), 2)
|
||||||
|
self.assertEqual(schemas, ['dbo', 'qgis_test'])
|
||||||
|
filterUri = QgsDataSourceUri(self.uri)
|
||||||
|
filterUri.setParam('excludedSchemas', 'dbo')
|
||||||
|
conn = md.createConnection(filterUri.uri(), {})
|
||||||
|
schemas = conn.schemas()
|
||||||
|
self.assertEqual(len(schemas), 1)
|
||||||
|
self.assertEqual(schemas, ['qgis_test'])
|
||||||
|
|
||||||
|
# Store the connection
|
||||||
|
conn.store('filteredConnection')
|
||||||
|
|
||||||
|
otherConn = md.createConnection('filteredConnection')
|
||||||
|
schemas = otherConn.schemas()
|
||||||
|
self.assertEqual(len(schemas), 1)
|
||||||
|
self.assertEqual(schemas, ['qgis_test'])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user