Skip to content
Permalink
Browse files

Fix virtual layers fail if table field names have special characters

Fixes #16943

Cherry-picked from 6c39212
  • Loading branch information
nyalldawson committed Jan 19, 2018
1 parent 4b1d98b commit d95fc60b048f37c8c6ddcd486161db7076764bac
@@ -215,7 +215,7 @@ struct VTable
typeName = "text";
break;
}
sqlFields << field.name() + " " + typeName;
sqlFields << QString( "\"%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,
@@ -841,6 +842,29 @@ def test_joined_layers_conversion(self):

QgsMapLayerRegistry.instance().removeMapLayers([v1, v2, v3])

def testFieldsWithSpecialCharacters(self):
ml = QgsVectorLayer("Point?srid=EPSG:4326&field=123:int", "mem_with_nontext_fieldnames", "memory")
self.assertEqual(ml.isValid(), True)
QgsMapLayerRegistry.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)

QgsMapLayerRegistry.instance().removeMapLayer(ml)


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

0 comments on commit d95fc60

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