Skip to content

Commit 636e0cf

Browse files
committed
[MSSQL] Refactor provider for schema checks
1 parent 3213d01 commit 636e0cf

File tree

1 file changed

+18
-64
lines changed

1 file changed

+18
-64
lines changed

src/providers/mssql/qgsmssqlprovider.cpp

Lines changed: 18 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,7 @@ QVariant QgsMssqlProvider::minimumValue( int index )
471471
QString sql = QString( "select min([%1]) from " )
472472
.arg( fld.name() );
473473

474-
if ( !mSchemaName.isEmpty() )
475-
sql += "[" + mSchemaName + "].";
476-
477-
sql += "[" + mTableName + "]";
474+
sql += QString("[%1].[%2]").arg( mSchemaName, mTableName );
478475

479476
if ( !mSqlWhereClause.isEmpty() )
480477
{
@@ -506,10 +503,7 @@ QVariant QgsMssqlProvider::maximumValue( int index )
506503
QString sql = QString( "select max([%1]) from " )
507504
.arg( fld.name() );
508505

509-
if ( !mSchemaName.isEmpty() )
510-
sql += "[" + mSchemaName + "].";
511-
512-
sql += "[" + mTableName + "]";
506+
sql += QString("[%1].[%2]").arg( mSchemaName, mTableName );
513507

514508
if ( !mSqlWhereClause.isEmpty() )
515509
{
@@ -550,10 +544,7 @@ void QgsMssqlProvider::uniqueValues( int index, QList<QVariant> &uniqueValues, i
550544
sql += QString( "[%1] from " )
551545
.arg( fld.name() );
552546

553-
if ( !mSchemaName.isEmpty() )
554-
sql += "[" + mSchemaName + "].";
555-
556-
sql += "[" + mTableName + "]";
547+
sql += QString("[%1].[%2]").arg( mSchemaName, mTableName );
557548

558549
if ( !mSqlWhereClause.isEmpty() )
559550
{
@@ -631,10 +622,7 @@ void QgsMssqlProvider::UpdateStatistics( bool estimate )
631622
}
632623
}
633624

634-
if ( mSchemaName.isEmpty() )
635-
statement += QString( " from [%1]" ).arg( mTableName );
636-
else
637-
statement += QString( " from [%1].[%2]" ).arg( mSchemaName, mTableName );
625+
statement += QString( " from [%1].[%2]" ).arg( mSchemaName, mTableName );
638626

639627
if ( !mSqlWhereClause.isEmpty() )
640628
{
@@ -752,10 +740,7 @@ bool QgsMssqlProvider::addFeatures( QgsFeatureList & flist )
752740
{
753741
QString statement;
754742
QString values;
755-
if ( mSchemaName.isEmpty() )
756-
statement = QString( "INSERT INTO [%1].[%2] (" ).arg( QString( "dbo" ), mTableName );
757-
else
758-
statement = QString( "INSERT INTO [%1].[%2] (" ).arg( mSchemaName, mTableName );
743+
statement = QString( "INSERT INTO [%1].[%2] (" ).arg( mSchemaName, mTableName );
759744

760745
bool first = true;
761746
QSqlQuery query = QSqlQuery( mDatabase );
@@ -906,10 +891,7 @@ bool QgsMssqlProvider::addFeatures( QgsFeatureList & flist )
906891
}
907892

908893

909-
if ( mSchemaName.isEmpty() )
910-
statement = QString( "SELECT IDENT_CURRENT('dbo.%1')" ).arg( mTableName );
911-
else
912-
statement = QString( "SELECT IDENT_CURRENT('%1.%2')" ).arg( mSchemaName, mTableName );
894+
statement = QString( "SELECT IDENT_CURRENT('%1.%2')" ).arg( mSchemaName, mTableName );
913895

914896
if ( !query.exec( statement ) )
915897
{
@@ -949,12 +931,8 @@ bool QgsMssqlProvider::addAttributes( const QList<QgsField> &attributes )
949931

950932
if ( statement.isEmpty() )
951933
{
952-
if ( mSchemaName.isEmpty() )
953-
statement = QString( "ALTER TABLE [%1].[%2] ADD " ).arg(
954-
QString( "dbo" ), mTableName );
955-
else
956-
statement = QString( "ALTER TABLE [%1].[%2] ADD " ).arg(
957-
mSchemaName, mTableName );
934+
statement = QString( "ALTER TABLE [%1].[%2] ADD " ).arg(
935+
mSchemaName, mTableName );
958936
}
959937
else
960938
statement += ",";
@@ -982,10 +960,7 @@ bool QgsMssqlProvider::deleteAttributes( const QgsAttributeIds &attributes )
982960
{
983961
if ( statement.isEmpty() )
984962
{
985-
if ( mSchemaName.isEmpty() )
986-
statement = QString( "ALTER TABLE [%1].[%2] DROP COLUMN " ).arg( QString( "dbo" ), mTableName );
987-
else
988-
statement = QString( "ALTER TABLE [%1].[%2] DROP COLUMN " ).arg( mSchemaName, mTableName );
963+
statement = QString( "ALTER TABLE [%1].[%2] DROP COLUMN " ).arg( mSchemaName, mTableName );
989964
}
990965
else
991966
statement += ",";
@@ -1026,10 +1001,7 @@ bool QgsMssqlProvider::changeAttributeValues( const QgsChangedAttributesMap & at
10261001
continue;
10271002

10281003
QString statement;
1029-
if ( mSchemaName.isEmpty() )
1030-
statement = QString( "UPDATE [%1].[%2] SET " ).arg( QString( "dbo" ), mTableName );
1031-
else
1032-
statement = QString( "UPDATE [%1].[%2] SET " ).arg( mSchemaName, mTableName );
1004+
statement = QString( "UPDATE [%1].[%2] SET " ).arg( mSchemaName, mTableName );
10331005

10341006
bool first = true;
10351007
QSqlQuery query = QSqlQuery( mDatabase );
@@ -1136,10 +1108,7 @@ bool QgsMssqlProvider::changeGeometryValues( QgsGeometryMap & geometry_map )
11361108
continue;
11371109

11381110
QString statement;
1139-
if ( mSchemaName.isEmpty() )
1140-
statement = QString( "UPDATE [%1].[%2] SET " ).arg( QString( "dbo" ), mTableName );
1141-
else
1142-
statement = QString( "UPDATE [%1].[%2] SET " ).arg( mSchemaName, mTableName );
1111+
statement = QString( "UPDATE [%1].[%2] SET " ).arg( mSchemaName, mTableName );
11431112

11441113
QSqlQuery query = QSqlQuery( mDatabase );
11451114
query.setForwardOnly( true );
@@ -1213,12 +1182,8 @@ bool QgsMssqlProvider::deleteFeatures( const QgsFeatureIds & id )
12131182
QSqlQuery query = QSqlQuery( mDatabase );
12141183
query.setForwardOnly( true );
12151184
QString statement;
1216-
if ( mSchemaName.isEmpty() )
1217-
statement = QString( "DELETE FROM [%1].[%2] WHERE [%3] IN (%4)" ).arg( QString( "dbo" ),
1218-
mTableName, mFidColName, featureIds );
1219-
else
1220-
statement = QString( "DELETE FROM [%1].[%2] WHERE [%3] IN (%4)" ).arg( mSchemaName,
1221-
mTableName, mFidColName, featureIds );
1185+
statement = QString( "DELETE FROM [%1].[%2] WHERE [%3] IN (%4)" ).arg( mSchemaName,
1186+
mTableName, mFidColName, featureIds );
12221187

12231188
if ( !query.exec( statement ) )
12241189
{
@@ -1248,12 +1213,8 @@ bool QgsMssqlProvider::createSpatialIndex()
12481213
QSqlQuery query = QSqlQuery( mDatabase );
12491214
query.setForwardOnly( true );
12501215
QString statement;
1251-
if ( mSchemaName.isEmpty() )
1252-
statement = QString( "CREATE SPATIAL INDEX [qgs_%1_sidx] ON [%2].[%3] ( [%4] )" ).arg(
1253-
mGeometryColName, QString( "dbo" ), mTableName, mGeometryColName );
1254-
else
1255-
statement = QString( "CREATE SPATIAL INDEX [qgs_%1_sidx] ON [%2].[%3] ( [%4] )" ).arg(
1256-
mGeometryColName, mSchemaName, mTableName, mGeometryColName );
1216+
statement = QString( "CREATE SPATIAL INDEX [qgs_%1_sidx] ON [%2].[%3] ( [%4] )" ).arg(
1217+
mGeometryColName, mSchemaName, mTableName, mGeometryColName );
12571218

12581219
if ( mGeometryColType == "geometry" )
12591220
{
@@ -1288,12 +1249,8 @@ bool QgsMssqlProvider::createAttributeIndex( int field )
12881249
return false;
12891250
}
12901251

1291-
if ( mSchemaName.isEmpty() )
1292-
statement = QString( "CREATE NONCLUSTERED INDEX [qgs_%1_idx] ON [%2].[%3] ( [%4] )" ).arg(
1293-
mGeometryColName, QString( "dbo" ), mTableName, mAttributeFields[field].name() );
1294-
else
1295-
statement = QString( "CREATE NONCLUSTERED INDEX [qgs_%1_idx] ON [%2].[%3] ( [%4] )" ).arg(
1296-
mGeometryColName, mSchemaName, mTableName, mAttributeFields[field].name() );
1252+
statement = QString( "CREATE NONCLUSTERED INDEX [qgs_%1_idx] ON [%2].[%3] ( [%4] )" ).arg(
1253+
mGeometryColName, mSchemaName, mTableName, mAttributeFields[field].name() );
12971254

12981255
if ( !query.exec( statement ) )
12991256
{
@@ -1346,10 +1303,7 @@ bool QgsMssqlProvider::setSubsetString( QString theSQL, bool )
13461303

13471304
QString sql = QString( "select count(*) from " );
13481305

1349-
if ( !mSchemaName.isEmpty() )
1350-
sql += "[" + mSchemaName + "].";
1351-
1352-
sql += "[" + mTableName + "]";
1306+
sql += QString("[%1].[%2]").arg( mSchemaName, mTableName );
13531307

13541308
if ( !mSqlWhereClause.isEmpty() )
13551309
{

0 commit comments

Comments
 (0)