Skip to content

Commit d3df4ed

Browse files
committedJun 17, 2015
oracle provider: reassign feature id after inserting new features (fixes #12690)
1 parent a786042 commit d3df4ed

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed
 

‎src/providers/oracle/qgsoracleprovider.cpp

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ bool QgsOracleProvider::addFeatures( QgsFeatureList &flist )
11691169

11701170
try
11711171
{
1172-
QSqlQuery qry( db );
1172+
QSqlQuery ins( db ), getfid( db );
11731173

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

11941194
if ( mPrimaryKeyType == pktInt || mPrimaryKeyType == pktFidMap )
11951195
{
1196+
QString keys, kdelim = "";
1197+
11961198
foreach ( int idx, mPrimaryKeyAttrs )
11971199
{
1198-
insert += delim + quotedIdentifier( field( idx ).name() );
1200+
const QgsField &fld = field( idx );
1201+
insert += delim + quotedIdentifier( fld.name() );
1202+
keys += kdelim + quotedIdentifier( fld.name() );
11991203
values += delim + "?";
12001204
delim = ",";
1205+
kdelim = ",";
12011206
fieldId << idx;
12021207
defaultValues << defaultValue( idx ).toString();
12031208
}
1209+
1210+
if ( !getfid.prepare( QString( "SELECT %1 FROM %2 WHERE ROWID=?" ).arg( keys ).arg( mQuery ) ) )
1211+
{
1212+
throw OracleException( tr( "Could not prepare get feature id statement" ), getfid );
1213+
}
12041214
}
12051215

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

12781288
QgsDebugMsgLevel( QString( "SQL prepare: %1" ).arg( insert ), 4 );
1279-
if ( !qry.prepare( insert ) )
1289+
if ( !ins.prepare( insert ) )
12801290
{
1281-
throw OracleException( tr( "Could not prepare insert statement" ), qry );
1291+
throw OracleException( tr( "Could not prepare insert statement" ), ins );
12821292
}
12831293

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

12901300
if ( !mGeometryColumn.isNull() )
12911301
{
1292-
appendGeomParam( features->geometry(), qry );
1302+
appendGeomParam( features->geometry(), ins );
12931303
}
12941304

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

13171327
QgsDebugMsgLevel( QString( "addBindValue: %1" ).arg( v ), 4 );
1318-
qry.addBindValue( v );
1328+
ins.addBindValue( v );
13191329
}
13201330

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

13241334
if ( mPrimaryKeyType == pktRowId )
13251335
{
1326-
features->setFeatureId( mShared->lookupFid( QList<QVariant>() << QVariant( qry.lastInsertId() ) ) );
1336+
features->setFeatureId( mShared->lookupFid( QList<QVariant>() << QVariant( ins.lastInsertId() ) ) );
13271337
QgsDebugMsgLevel( QString( "new fid=%1" ).arg( features->id() ), 4 );
13281338
}
1339+
else if ( mPrimaryKeyType == pktInt || mPrimaryKeyType == pktFidMap )
1340+
{
1341+
getfid.addBindValue( QVariant( ins.lastInsertId() ) );
1342+
if ( !getfid.exec() || !getfid.next() )
1343+
throw OracleException( tr( "Could not retrieve feature id %1" ).arg( features->id() ), getfid );
1344+
1345+
int col = 0;
1346+
foreach ( int idx, mPrimaryKeyAttrs )
1347+
{
1348+
const QgsField &fld = field( idx );
1349+
1350+
QVariant v = getfid.value( col++ );
1351+
if ( v.type() != fld.type() )
1352+
v = QgsVectorDataProvider::convertValue( fld.type(), v.toString() );
1353+
features->setAttribute( idx, v );
1354+
}
1355+
}
13291356
}
13301357

1331-
qry.finish();
1358+
ins.finish();
13321359

13331360
if ( !db.commit() )
13341361
{

0 commit comments

Comments
 (0)
Please sign in to comment.