Skip to content

Commit

Permalink
oracle provider: reassign feature id after inserting new features (fixes
Browse files Browse the repository at this point in the history
 #12690)

(cherry picked from commit d3df4ed)
  • Loading branch information
jef-n committed Jun 29, 2015
1 parent a728341 commit 9754353
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions src/providers/oracle/qgsoracleprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ bool QgsOracleProvider::addFeatures( QgsFeatureList &flist )

try
{
QSqlQuery qry( db );
QSqlQuery ins( db ), getfid( db );

if ( !db.transaction() )
{
Expand All @@ -1193,14 +1193,24 @@ bool QgsOracleProvider::addFeatures( QgsFeatureList &flist )

if ( mPrimaryKeyType == pktInt || mPrimaryKeyType == pktFidMap )
{
QString keys, kdelim = "";

foreach ( int idx, mPrimaryKeyAttrs )
{
insert += delim + quotedIdentifier( field( idx ).name() );
const QgsField &fld = field( idx );
insert += delim + quotedIdentifier( fld.name() );
keys += kdelim + quotedIdentifier( fld.name() );
values += delim + "?";
delim = ",";
kdelim = ",";
fieldId << idx;
defaultValues << defaultValue( idx ).toString();
}

if ( !getfid.prepare( QString( "SELECT %1 FROM %2 WHERE ROWID=?" ).arg( keys ).arg( mQuery ) ) )
{
throw OracleException( tr( "Could not prepare get feature id statement" ), getfid );
}
}

const QgsAttributes &attributevec = flist[0].attributes();
Expand Down Expand Up @@ -1276,9 +1286,9 @@ bool QgsOracleProvider::addFeatures( QgsFeatureList &flist )
insert += values + ")";

QgsDebugMsgLevel( QString( "SQL prepare: %1" ).arg( insert ), 4 );
if ( !qry.prepare( insert ) )
if ( !ins.prepare( insert ) )
{
throw OracleException( tr( "Could not prepare insert statement" ), qry );
throw OracleException( tr( "Could not prepare insert statement" ), ins );
}

for ( QgsFeatureList::iterator features = flist.begin(); features != flist.end(); ++features )
Expand All @@ -1289,7 +1299,7 @@ bool QgsOracleProvider::addFeatures( QgsFeatureList &flist )

if ( !mGeometryColumn.isNull() )
{
appendGeomParam( features->geometry(), qry );
appendGeomParam( features->geometry(), ins );
}

for ( int i = 0; i < fieldId.size(); i++ )
Expand All @@ -1315,20 +1325,37 @@ bool QgsOracleProvider::addFeatures( QgsFeatureList &flist )
}

QgsDebugMsgLevel( QString( "addBindValue: %1" ).arg( v ), 4 );
qry.addBindValue( v );
ins.addBindValue( v );
}

if ( !qry.exec() )
throw OracleException( tr( "Could not insert feature %1" ).arg( features->id() ), qry );
if ( !ins.exec() )
throw OracleException( tr( "Could not insert feature %1" ).arg( features->id() ), ins );

if ( mPrimaryKeyType == pktRowId )
{
features->setFeatureId( mShared->lookupFid( QList<QVariant>() << QVariant( qry.lastInsertId() ) ) );
features->setFeatureId( mShared->lookupFid( QList<QVariant>() << QVariant( ins.lastInsertId() ) ) );
QgsDebugMsgLevel( QString( "new fid=%1" ).arg( features->id() ), 4 );
}
else if ( mPrimaryKeyType == pktInt || mPrimaryKeyType == pktFidMap )
{
getfid.addBindValue( QVariant( ins.lastInsertId() ) );
if ( !getfid.exec() || !getfid.next() )
throw OracleException( tr( "Could not retrieve feature id %1" ).arg( features->id() ), getfid );

int col = 0;
foreach ( int idx, mPrimaryKeyAttrs )
{
const QgsField &fld = field( idx );

QVariant v = getfid.value( col++ );
if ( v.type() != fld.type() )
v = QgsVectorDataProvider::convertValue( fld.type(), v.toString() );
features->setAttribute( idx, v );
}
}
}

qry.finish();
ins.finish();

if ( !db.commit() )
{
Expand Down

0 comments on commit 9754353

Please sign in to comment.