Skip to content

Commit 0e70452

Browse files
author
Hugo Mercier
committed
Fix text encoding issues in virtual layers (fixes #14350)
1 parent 3dea491 commit 0e70452

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/providers/virtual/qgsvirtuallayersqlitemodule.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ int vtableFilter( sqlite3_vtab_cursor * cursor, int idxNum, const char *idxStr,
613613
{
614614
int n = sqlite3_value_bytes( argv[0] );
615615
const char* t = reinterpret_cast<const char*>( sqlite3_value_text( argv[0] ) );
616-
QString str( QByteArray::fromRawData( t, n ) );
616+
QString str = QString::fromUtf8( t, n );
617617
expr += "'" + str.replace( "'", "''" ) + "'";
618618
break;
619619
}
@@ -738,7 +738,7 @@ void qgisFunctionWrapper( sqlite3_context* ctxt, int nArgs, sqlite3_value** args
738738
{
739739
int n = sqlite3_value_bytes( args[i] );
740740
const char* t = reinterpret_cast<const char*>( sqlite3_value_text( args[i] ) );
741-
QString str( QByteArray::fromRawData( t, n ) ); // don't copy data
741+
QString str = QString::fromUtf8( t, n );
742742
variants << QVariant( str );
743743
break;
744744
}

tests/src/python/test_provider_virtual.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,6 @@ def test_recursiveLayer(self):
334334
QgsMapLayerRegistry.instance().removeMapLayer(l.id())
335335

336336
def test_no_geometry(self):
337-
source = QUrl.toPercentEncoding(os.path.join(self.testDataDir, "france_parts.shp"))
338337
df = QgsVirtualLayerDefinition()
339338
df.addSource("vtab", os.path.join(self.testDataDir, "france_parts.shp"), "ogr")
340339
df.setGeometryWkbType(QgsWKBTypes.NoGeometry)
@@ -720,6 +719,24 @@ def test_qgisExpressionFunctions(self):
720719
for f in l.getFeatures():
721720
self.assertEqual(f.attributes(), ['hello world', 2016, u'This', u'project'])
722721

722+
def test_query_with_accents(self):
723+
# shapefile with accents and latin1 encoding
724+
df = QgsVirtualLayerDefinition()
725+
df.addSource("vtab", os.path.join(self.testDataDir, "france_parts.shp"), "ogr", "ISO-8859-1")
726+
df.setQuery(u"SELECT * FROM vtab WHERE TYPE_1 = 'Région'")
727+
vl = QgsVectorLayer(df.toString(), "testq", "virtual")
728+
self.assertEqual(vl.isValid(), True)
729+
ids = [f.id() for f in vl.getFeatures()]
730+
self.assertEqual(len(ids), 4)
731+
732+
# the same shapefile with a wrong encoding
733+
df.addSource("vtab", os.path.join(self.testDataDir, "france_parts.shp"), "ogr", "UTF-8")
734+
df.setQuery(u"SELECT * FROM vtab WHERE TYPE_1 = 'Région'")
735+
vl2 = QgsVectorLayer(df.toString(), "testq", "virtual")
736+
self.assertEqual(vl2.isValid(), True)
737+
ids = [f.id() for f in vl2.getFeatures()]
738+
self.assertEqual(ids, [])
739+
723740

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

0 commit comments

Comments
 (0)