Skip to content
Permalink
Browse files

Fix spatialite aspatial add features

Fixes #34379
  • Loading branch information
elpaso committed Feb 12, 2020
1 parent 0fd99f3 commit ac65ff2d40e7b74cd605601a36469b01b68a5a8d
Showing with 42 additions and 6 deletions.
  1. +3 −6 src/providers/spatialite/qgsspatialiteprovider.cpp
  2. +39 −0 tests/src/python/test_provider_spatialite.py
@@ -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;
@@ -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 )
@@ -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;
@@ -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.
You can’t perform that action at this time.