Skip to content
Permalink
Browse files

[mssql] Use output instead of IDENT_CURRENT to retrieve newly added p…

…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 22, 2018
1 parent 72946c3 commit 972250d01f1cd19c12d9cbd5f8179a4c0424fbf3
Showing with 6 additions and 14 deletions.
  1. +6 −14 src/providers/mssql/qgsmssqlprovider.cpp
@@ -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 ) )
@@ -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();

0 comments on commit 972250d

Please sign in to comment.
You can’t perform that action at this time.