Skip to content

Commit

Permalink
[MSSQL] Refactor provider for schema checks
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanW2 committed May 21, 2014
1 parent 3213d01 commit 636e0cf
Showing 1 changed file with 18 additions and 64 deletions.
82 changes: 18 additions & 64 deletions src/providers/mssql/qgsmssqlprovider.cpp
Expand Up @@ -471,10 +471,7 @@ QVariant QgsMssqlProvider::minimumValue( int index )
QString sql = QString( "select min([%1]) from " )
.arg( fld.name() );

if ( !mSchemaName.isEmpty() )
sql += "[" + mSchemaName + "].";

sql += "[" + mTableName + "]";
sql += QString("[%1].[%2]").arg( mSchemaName, mTableName );

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

if ( !mSchemaName.isEmpty() )
sql += "[" + mSchemaName + "].";

sql += "[" + mTableName + "]";
sql += QString("[%1].[%2]").arg( mSchemaName, mTableName );

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

if ( !mSchemaName.isEmpty() )
sql += "[" + mSchemaName + "].";

sql += "[" + mTableName + "]";
sql += QString("[%1].[%2]").arg( mSchemaName, mTableName );

if ( !mSqlWhereClause.isEmpty() )
{
Expand Down Expand Up @@ -631,10 +622,7 @@ void QgsMssqlProvider::UpdateStatistics( bool estimate )
}
}

if ( mSchemaName.isEmpty() )
statement += QString( " from [%1]" ).arg( mTableName );
else
statement += QString( " from [%1].[%2]" ).arg( mSchemaName, mTableName );
statement += QString( " from [%1].[%2]" ).arg( mSchemaName, mTableName );

if ( !mSqlWhereClause.isEmpty() )
{
Expand Down Expand Up @@ -752,10 +740,7 @@ bool QgsMssqlProvider::addFeatures( QgsFeatureList & flist )
{
QString statement;
QString values;
if ( mSchemaName.isEmpty() )
statement = QString( "INSERT INTO [%1].[%2] (" ).arg( QString( "dbo" ), mTableName );
else
statement = QString( "INSERT INTO [%1].[%2] (" ).arg( mSchemaName, mTableName );
statement = QString( "INSERT INTO [%1].[%2] (" ).arg( mSchemaName, mTableName );

bool first = true;
QSqlQuery query = QSqlQuery( mDatabase );
Expand Down Expand Up @@ -906,10 +891,7 @@ bool QgsMssqlProvider::addFeatures( QgsFeatureList & flist )
}


if ( mSchemaName.isEmpty() )
statement = QString( "SELECT IDENT_CURRENT('dbo.%1')" ).arg( mTableName );
else
statement = QString( "SELECT IDENT_CURRENT('%1.%2')" ).arg( mSchemaName, mTableName );
statement = QString( "SELECT IDENT_CURRENT('%1.%2')" ).arg( mSchemaName, mTableName );

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

if ( statement.isEmpty() )
{
if ( mSchemaName.isEmpty() )
statement = QString( "ALTER TABLE [%1].[%2] ADD " ).arg(
QString( "dbo" ), mTableName );
else
statement = QString( "ALTER TABLE [%1].[%2] ADD " ).arg(
mSchemaName, mTableName );
statement = QString( "ALTER TABLE [%1].[%2] ADD " ).arg(
mSchemaName, mTableName );
}
else
statement += ",";
Expand Down Expand Up @@ -982,10 +960,7 @@ bool QgsMssqlProvider::deleteAttributes( const QgsAttributeIds &attributes )
{
if ( statement.isEmpty() )
{
if ( mSchemaName.isEmpty() )
statement = QString( "ALTER TABLE [%1].[%2] DROP COLUMN " ).arg( QString( "dbo" ), mTableName );
else
statement = QString( "ALTER TABLE [%1].[%2] DROP COLUMN " ).arg( mSchemaName, mTableName );
statement = QString( "ALTER TABLE [%1].[%2] DROP COLUMN " ).arg( mSchemaName, mTableName );
}
else
statement += ",";
Expand Down Expand Up @@ -1026,10 +1001,7 @@ bool QgsMssqlProvider::changeAttributeValues( const QgsChangedAttributesMap & at
continue;

QString statement;
if ( mSchemaName.isEmpty() )
statement = QString( "UPDATE [%1].[%2] SET " ).arg( QString( "dbo" ), mTableName );
else
statement = QString( "UPDATE [%1].[%2] SET " ).arg( mSchemaName, mTableName );
statement = QString( "UPDATE [%1].[%2] SET " ).arg( mSchemaName, mTableName );

bool first = true;
QSqlQuery query = QSqlQuery( mDatabase );
Expand Down Expand Up @@ -1136,10 +1108,7 @@ bool QgsMssqlProvider::changeGeometryValues( QgsGeometryMap & geometry_map )
continue;

QString statement;
if ( mSchemaName.isEmpty() )
statement = QString( "UPDATE [%1].[%2] SET " ).arg( QString( "dbo" ), mTableName );
else
statement = QString( "UPDATE [%1].[%2] SET " ).arg( mSchemaName, mTableName );
statement = QString( "UPDATE [%1].[%2] SET " ).arg( mSchemaName, mTableName );

QSqlQuery query = QSqlQuery( mDatabase );
query.setForwardOnly( true );
Expand Down Expand Up @@ -1213,12 +1182,8 @@ bool QgsMssqlProvider::deleteFeatures( const QgsFeatureIds & id )
QSqlQuery query = QSqlQuery( mDatabase );
query.setForwardOnly( true );
QString statement;
if ( mSchemaName.isEmpty() )
statement = QString( "DELETE FROM [%1].[%2] WHERE [%3] IN (%4)" ).arg( QString( "dbo" ),
mTableName, mFidColName, featureIds );
else
statement = QString( "DELETE FROM [%1].[%2] WHERE [%3] IN (%4)" ).arg( mSchemaName,
mTableName, mFidColName, featureIds );
statement = QString( "DELETE FROM [%1].[%2] WHERE [%3] IN (%4)" ).arg( mSchemaName,
mTableName, mFidColName, featureIds );

if ( !query.exec( statement ) )
{
Expand Down Expand Up @@ -1248,12 +1213,8 @@ bool QgsMssqlProvider::createSpatialIndex()
QSqlQuery query = QSqlQuery( mDatabase );
query.setForwardOnly( true );
QString statement;
if ( mSchemaName.isEmpty() )
statement = QString( "CREATE SPATIAL INDEX [qgs_%1_sidx] ON [%2].[%3] ( [%4] )" ).arg(
mGeometryColName, QString( "dbo" ), mTableName, mGeometryColName );
else
statement = QString( "CREATE SPATIAL INDEX [qgs_%1_sidx] ON [%2].[%3] ( [%4] )" ).arg(
mGeometryColName, mSchemaName, mTableName, mGeometryColName );
statement = QString( "CREATE SPATIAL INDEX [qgs_%1_sidx] ON [%2].[%3] ( [%4] )" ).arg(
mGeometryColName, mSchemaName, mTableName, mGeometryColName );

if ( mGeometryColType == "geometry" )
{
Expand Down Expand Up @@ -1288,12 +1249,8 @@ bool QgsMssqlProvider::createAttributeIndex( int field )
return false;
}

if ( mSchemaName.isEmpty() )
statement = QString( "CREATE NONCLUSTERED INDEX [qgs_%1_idx] ON [%2].[%3] ( [%4] )" ).arg(
mGeometryColName, QString( "dbo" ), mTableName, mAttributeFields[field].name() );
else
statement = QString( "CREATE NONCLUSTERED INDEX [qgs_%1_idx] ON [%2].[%3] ( [%4] )" ).arg(
mGeometryColName, mSchemaName, mTableName, mAttributeFields[field].name() );
statement = QString( "CREATE NONCLUSTERED INDEX [qgs_%1_idx] ON [%2].[%3] ( [%4] )" ).arg(
mGeometryColName, mSchemaName, mTableName, mAttributeFields[field].name() );

if ( !query.exec( statement ) )
{
Expand Down Expand Up @@ -1346,10 +1303,7 @@ bool QgsMssqlProvider::setSubsetString( QString theSQL, bool )

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

if ( !mSchemaName.isEmpty() )
sql += "[" + mSchemaName + "].";

sql += "[" + mTableName + "]";
sql += QString("[%1].[%2]").arg( mSchemaName, mTableName );

if ( !mSqlWhereClause.isEmpty() )
{
Expand Down

0 comments on commit 636e0cf

Please sign in to comment.