Skip to content
Permalink
Browse files

Fix virtual layers fail if table field names have special characters

Fixes #16943
  • Loading branch information
nyalldawson committed Jan 18, 2018
1 parent a1618c7 commit 6c392124ca4684c433518a40b81e34200672c77c
@@ -218,7 +218,7 @@ struct VTable
typeName = QStringLiteral( "text" );
break;
}
sqlFields << field.name() + " " + typeName;
sqlFields << QStringLiteral( "\"%1\" %2" ).arg( field.name(), typeName );
}

QgsVectorDataProvider *provider = mLayer ? mLayer->dataProvider() : mProvider;
@@ -16,6 +16,7 @@
import os

from qgis.core import (QgsVectorLayer,
QgsField,
QgsFeature,
QgsFeatureRequest,
QgsGeometry,
@@ -847,6 +848,29 @@ def test_joined_layers_conversion(self):

QgsProject.instance().removeMapLayers([v1.id(), v2.id(), v3.id()])

def testFieldsWithSpecialCharacters(self):
ml = QgsVectorLayer("Point?srid=EPSG:4326&field=123:int", "mem_with_nontext_fieldnames", "memory")
self.assertEqual(ml.isValid(), True)
QgsProject.instance().addMapLayer(ml)

ml.startEditing()
self.assertTrue(ml.addAttribute(QgsField('abc:123', QVariant.String)))
f1 = QgsFeature(ml.fields())
f1.setGeometry(QgsGeometry.fromWkt('POINT(0 0)'))
f2 = QgsFeature(ml.fields())
f2.setGeometry(QgsGeometry.fromWkt('POINT(1 1)'))
ml.addFeatures([f1, f2])
ml.commitChanges()

vl = QgsVectorLayer("?query=select * from mem_with_nontext_fieldnames", "vl", "virtual")
self.assertEqual(vl.isValid(), True)
self.assertEqual(vl.fields().at(0).name(), '123')
self.assertEqual(vl.fields().at(1).name(), 'abc:123')

self.assertEqual(vl.featureCount(), 2)

QgsProject.instance().removeMapLayer(ml)


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

0 comments on commit 6c39212

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