Skip to content

Commit

Permalink
[mssql] Use output instead of IDENT_CURRENT to retrieve newly added p…
Browse files Browse the repository at this point in the history
…rimary keys

It's safer, works with non-identity columns, and potentially allows
us to optimise in future and bulk insert features instead of
one by one
  • Loading branch information
nyalldawson committed Oct 25, 2018
1 parent 72946c3 commit 972250d
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions src/providers/mssql/qgsmssqlprovider.cpp
Expand Up @@ -944,7 +944,12 @@ bool QgsMssqlProvider::addFeatures( QgsFeatureList &flist, Flags flags )
}
}

statement += ") VALUES (" + values + ')';
statement += QStringLiteral( ") " );
if ( !( flags & QgsFeatureSink::FastInsert ) )
{
statement += QStringLiteral( " OUTPUT inserted." ) + mFidColName;
}
statement += QStringLiteral( " VALUES (" ) + values + ')';

// use prepared statement to prevent from sql injection
if ( !query.prepare( statement ) )
Expand Down Expand Up @@ -1067,19 +1072,6 @@ bool QgsMssqlProvider::addFeatures( QgsFeatureList &flist, Flags flags )

if ( !( flags & QgsFeatureSink::FastInsert ) )
{
statement = QStringLiteral( "SELECT IDENT_CURRENT('%1.%2')" ).arg( mSchemaName, mTableName );

if ( !query.exec( statement ) )
{
QString msg = query.lastError().text();
QgsDebugMsg( msg );
if ( !mSkipFailures )
{
pushError( msg );
return false;
}
}

if ( !query.next() )
{
QString msg = query.lastError().text();
Expand Down

0 comments on commit 972250d

Please sign in to comment.