Skip to content

Commit

Permalink
[mssql] Ignore computed columns in insert/update
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanW2 committed May 22, 2017
1 parent 2b39c1e commit 7fee0b2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/providers/mssql/qgsmssqlprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,27 @@ void QgsMssqlProvider::loadFields()
{
mAttributeFields.clear();
mDefaultValues.clear();
mComputedColumns.clear();

// get field spec
QSqlQuery query = QSqlQuery( mDatabase );
query.setForwardOnly( true );

// Get computed columns which need to be ignored on insert or update.
if ( !query.exec( QStringLiteral( "SELECT name FROM sys.columns WHERE is_computed = 1 AND object_id = OBJECT_ID('[%1].[%2]')" ).arg( mSchemaName, mTableName ) ) )
{
QgsDebugMsg( query.lastError().text( ) );
return;
}

if ( query.isActive() )
{
while ( query.next() )
{
mComputedColumns.append( query.value( 0 ).toString() );
}
}

if ( !query.exec( QStringLiteral( "exec sp_columns @table_name = N'%1', @table_owner = '%2'" ).arg( mTableName, mSchemaName ) ) )
{
QgsDebugMsg( query.lastError().text( ) );
Expand Down Expand Up @@ -432,6 +450,7 @@ void QgsMssqlProvider::loadFields()
sqlTypeName ) );
}

//COLUMN_DEF
if ( !query.value( 12 ).isNull() )
{
mDefaultValues.insert( i, query.value( 12 ).toString() );
Expand Down Expand Up @@ -835,6 +854,9 @@ bool QgsMssqlProvider::addFeatures( QgsFeatureList &flist )
if ( mDefaultValues.contains( i ) && mDefaultValues.value( i ) == attrs.at( i ).toString() )
continue; // skip fields having default values

if ( mComputedColumns.contains( fld.name() ) )
continue; // skip computed columns because they are done server side.

if ( !first )
{
statement += ',';
Expand Down Expand Up @@ -912,6 +934,9 @@ bool QgsMssqlProvider::addFeatures( QgsFeatureList &flist )
if ( mDefaultValues.contains( i ) && mDefaultValues.value( i ) == attrs.at( i ).toString() )
continue; // skip fields having default values

if ( mComputedColumns.contains( fld.name() ) )
continue; // skip computed columns because they are done server side.

QVariant::Type type = fld.type();
if ( attrs.at( i ).isNull() || !attrs.at( i ).isValid() )
{
Expand Down Expand Up @@ -1142,6 +1167,9 @@ bool QgsMssqlProvider::changeAttributeValues( const QgsChangedAttributesMap &att
if ( fld.name().isEmpty() )
continue; // invalid

if ( mComputedColumns.contains( fld.name() ) )
continue; // skip computed columns because they are done server side.

if ( !first )
statement += ',';
else
Expand Down Expand Up @@ -1176,6 +1204,9 @@ bool QgsMssqlProvider::changeAttributeValues( const QgsChangedAttributesMap &att
if ( fld.name().isEmpty() )
continue; // invalid

if ( mComputedColumns.contains( fld.name() ) )
continue; // skip computed columns because they are done server side.

QVariant::Type type = fld.type();
if ( it2->isNull() || !it2->isValid() )
{
Expand Down
1 change: 1 addition & 0 deletions src/providers/mssql/qgsmssqlprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class QgsMssqlProvider : public QgsVectorDataProvider
//! Fields
QgsFields mAttributeFields;
QMap<int, QString> mDefaultValues;
QList<QString> mComputedColumns;

mutable QgsMssqlGeometryParser mParser;

Expand Down

0 comments on commit 7fee0b2

Please sign in to comment.