Skip to content
Permalink
Browse files

Merge pull request #34718 from elpaso/bugfix-gh34696-spatialite-nofie…

…lds-3_10

[backport] Fix spatialite editing with nofields (or def values)
  • Loading branch information
rouault committed Mar 11, 2020
2 parents 190390d + fa1ff55 commit 4ed2c48383ca1bc3b3fa52c97cd99391279c7990
Showing with 33 additions and 5 deletions.
  1. +12 −5 src/providers/spatialite/qgsspatialiteprovider.cpp
  2. +21 −0 tests/src/python/test_provider_spatialite.py
@@ -4015,6 +4015,7 @@ bool QgsSpatiaLiteProvider::addFeatures( QgsFeatureList &flist, Flags flags )

if ( flist.isEmpty() )
return true;

QgsAttributes attributevec = flist[0].attributes();

ret = sqlite3_exec( mSqliteHandle, "BEGIN", nullptr, nullptr, &errMsg );
@@ -4025,15 +4026,19 @@ bool QgsSpatiaLiteProvider::addFeatures( QgsFeatureList &flist, Flags flags )
QString baseSql { QStringLiteral( "INSERT INTO %1(" ).arg( QgsSqliteUtils::quotedIdentifier( mTableName ) ) };
baseValues = QStringLiteral( ") VALUES (" );

QChar baseSeparator { ' ' };

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

for ( QgsFeatureList::iterator feature = flist.begin(); feature != flist.end(); ++feature )
{

QChar separator { baseSeparator };
QString values { baseValues };
sql = baseSql;

@@ -4045,7 +4050,9 @@ bool QgsSpatiaLiteProvider::addFeatures( QgsFeatureList &flist, Flags flags )

for ( int i = 0; i < attributevec.count(); ++i )
{
if ( mDefaultValues.contains( i ) && mDefaultValues.value( i ) == attributevec.at( i ).toString() )
if ( mDefaultValues.contains( i ) && (
mDefaultValues.value( i ) == attributevec.at( i ).toString() ||
! attributevec.at( i ).isValid() ) )
{
defaultIndexes.push_back( i );
continue;
@@ -4060,9 +4067,9 @@ bool QgsSpatiaLiteProvider::addFeatures( QgsFeatureList &flist, Flags flags )
continue;
}

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

sql += values;
@@ -4105,7 +4112,7 @@ bool QgsSpatiaLiteProvider::addFeatures( QgsFeatureList &flist, Flags flags )
continue;
}

QVariant v = attributevec.at( i );
const QVariant v = attributevec.at( i );

// binding values for each attribute
if ( i >= mAttributeFields.count() )
@@ -237,9 +237,15 @@ def setUpClass(cls):
INSERT INTO test_bigint (id, value, position) VALUES
(987654321012345, 1, ST_GeomFromtext('LINESTRINGM(10.416255 55.3786316 1577093516, 10.516255 55.4786316 157709)', 4326) ),
(987654321012346, 2, ST_GeomFromtext('LINESTRINGM(10.316255 55.3786316 1577093516, 11.216255 56.3786316 157709)', 4326) )"""
cur.execute(sql)

# no fields table
sql = "CREATE TABLE \"test_nofields\"(pkuid integer primary key autoincrement)"
cur.execute(sql)
sql = "SELECT AddGeometryColumn('test_nofields', 'geometry', 4326, 'POINT', 'XY')"
cur.execute(sql)

# Commit all test data
cur.execute("COMMIT")
con.close()

@@ -1264,6 +1270,21 @@ def testSpatialiteAspatialMultipleAdd(self):
self.assertEqual(vl.getFeature(1).attributes(), [1, 123, 'a note'])
self.assertEqual(vl.getFeature(2).attributes(), [2, 456, 'another note'])

def testAddFeatureNoFields(self):
"""Test regression #34696"""

vl = QgsVectorLayer("dbname=%s table='test_nofields' (geometry)" % self.dbname, "test_nofields", "spatialite")
self.assertTrue(vl.isValid())
self.assertTrue(vl.startEditing())
f = QgsFeature(vl.fields())
g = QgsGeometry.fromWkt('point(9 45)')
f.setGeometry(g)
self.assertTrue(vl.addFeatures([f]))
self.assertTrue(vl.commitChanges())
vl = QgsVectorLayer("dbname=%s table='test_nofields' (geometry)" % self.dbname, "test_nofields", "spatialite")
self.assertEqual(vl.featureCount(), 1)
self.assertEqual(vl.getFeature(1).geometry().asWkt().upper(), 'POINT (9 45)')


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

0 comments on commit 4ed2c48

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