[MSSQL] Refactor provider for schema checks

This commit is contained in:
Nathan Woodrow 2014-05-21 13:46:49 +10:00
parent 3213d014f3
commit 636e0cf224

View File

@ -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() )
{
@ -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() )
{
@ -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() )
{
@ -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() )
{
@ -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 );
@ -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 ) )
{
@ -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 += ",";
@ -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 += ",";
@ -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 );
@ -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 );
@ -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 ) )
{
@ -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" )
{
@ -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 ) )
{
@ -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() )
{