Skip to content

Commit

Permalink
Fix spatialite aspatial add features
Browse files Browse the repository at this point in the history
Fixes #34379
  • Loading branch information
elpaso committed Feb 12, 2020
1 parent 0fd99f3 commit ac65ff2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/providers/spatialite/qgsspatialiteprovider.cpp
Expand Up @@ -4006,7 +4006,6 @@ bool QgsSpatiaLiteProvider::addFeatures( QgsFeatureList &flist, Flags flags )
char *errMsg = nullptr;
bool toCommit = false;
QString baseValues;
QString separator;
int ia, ret;
// SQL for single row
QString sql;
Expand All @@ -4022,13 +4021,11 @@ bool QgsSpatiaLiteProvider::addFeatures( QgsFeatureList &flist, Flags flags )

QString baseSql { QStringLiteral( "INSERT INTO %1(" ).arg( QgsSqliteUtils::quotedIdentifier( mTableName ) ) };
baseValues = QStringLiteral( ") VALUES (" );
separator.clear();

if ( !mGeometryColumn.isEmpty() )
{
baseSql += separator + QgsSqliteUtils::quotedIdentifier( mGeometryColumn );
baseValues += separator + geomParam();
separator = ',';
baseSql += QgsSqliteUtils::quotedIdentifier( mGeometryColumn ) + ',';
baseValues += geomParam() + ',';
}

for ( QgsFeatureList::iterator feature = flist.begin(); feature != flist.end(); ++feature )
Expand Down Expand Up @@ -4060,9 +4057,9 @@ bool QgsSpatiaLiteProvider::addFeatures( QgsFeatureList &flist, Flags flags )
continue;
}

const QChar separator { i > 0 ? ',' : ' ' };
sql += separator + QgsSqliteUtils::quotedIdentifier( fieldname );
values += separator + '?';
separator = ',';
}

sql += values;
Expand Down
39 changes: 39 additions & 0 deletions tests/src/python/test_provider_spatialite.py
Expand Up @@ -1235,6 +1235,45 @@ def testSpatialiteDefaultValues(self):
self.assertEqual(feature.attribute(4), 123)
self.assertEqual(feature.attribute(5), 'My default')

def testSpatialiteAspatialMultipleAdd(self):
"""Add multiple features in aspatial table. See GH #34379"""

# Create the test table

dbname = os.path.join(tempfile.gettempdir(), "test_aspatial_multiple_edits.sqlite")
if os.path.exists(dbname):
os.remove(dbname)
con = spatialite_connect(dbname, isolation_level=None)
cur = con.cursor()
cur.execute("BEGIN")
sql = "SELECT InitSpatialMetadata()"
cur.execute(sql)

# simple table with primary key
sql = """
CREATE TABLE "test_aspatial_multiple_edits"(pkuid integer primary key autoincrement,"id" integer,"note" text)
"""
cur.execute(sql)
cur.execute("COMMIT")
con.close()

vl = QgsVectorLayer("dbname='%s' table='test_aspatial_multiple_edits'" % dbname, 'test_aspatial_multiple_edits', 'spatialite')
self.assertTrue(vl.isValid())

self.assertTrue(vl.startEditing())
f1 = QgsFeature(vl.fields())
f1.setAttribute('note', 'a note')
f1.setAttribute('id', 123)
f2 = QgsFeature(vl.fields())
f2.setAttribute('note', 'another note')
f2.setAttribute('id', 456)
self.assertTrue(vl.addFeatures([f1, f2]))
self.assertTrue(vl.commitChanges())

# Verify
self.assertEqual(vl.getFeature(1).attributes(), [1, 123, 'a note'])
self.assertEqual(vl.getFeature(2).attributes(), [2, 456, 'another note'])


if __name__ == '__main__':
unittest.main()

0 comments on commit ac65ff2

Please sign in to comment.