Skip to content

Commit

Permalink
[dbmanager] Fix encoding issues with virtual layers
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Mercier committed Feb 27, 2017
1 parent 23c305b commit f56e6b9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
10 changes: 6 additions & 4 deletions python/plugins/db_manager/db_plugins/vlayers/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from ..connector import DBConnector
from ..plugin import Table

from qgis.core import Qgis, QgsDataSourceUri, QgsVirtualLayerDefinition, QgsProject, QgsMapLayer, QgsVectorLayer, QgsCoordinateReferenceSystem, QgsWkbTypes
from qgis.core import Qgis, QgsDataSourceUri, QgsVirtualLayerDefinition, QgsProject, QgsMapLayer, QgsVectorLayer, QgsCoordinateReferenceSystem, QgsWkbTypes, QgsVirtualLayerDefinition

import sqlite3

Expand All @@ -48,7 +48,7 @@ def getQueryGeometryName(sqlite_file):
with sqlite3_connection(sqlite_file) as conn:
c = conn.cursor()
for r in c.execute("SELECT url FROM _meta"):
d = QgsVirtualLayerDefinition.fromUrl(QUrl.fromEncoded(r[0]))
d = QgsVirtualLayerDefinition.fromUrl(QUrl(r[0]))
if d.hasDefinedGeometry():
return d.geometryField()
return None
Expand Down Expand Up @@ -127,8 +127,10 @@ def _get_cursor_columns(self, c):
tmp = tf.fileName()
tf.close()

q = QUrl.toPercentEncoding(c.sql)
p = QgsVectorLayer("%s?query=%s" % (QUrl.fromLocalFile(tmp).toString(), q), "vv", "virtual")
df = QgsVirtualLayerDefinition()
df.setFilePath(tmp)
df.setQuery(c.sql)
p = QgsVectorLayer(df.toString(), "vv", "virtual")
if not p.isValid():
return []
f = [f.name() for f in p.fields()]
Expand Down
8 changes: 5 additions & 3 deletions python/plugins/db_manager/db_plugins/vlayers/data_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from ..plugin import DbError

from qgis.PyQt.QtCore import QUrl, QTime, QTemporaryFile
from qgis.core import Qgis, QgsVectorLayer, QgsWkbTypes, QgsWkbTypes
from qgis.core import Qgis, QgsVectorLayer, QgsWkbTypes, QgsWkbTypes, QgsVirtualLayerDefinition


class LTableDataModel(TableDataModel):
Expand Down Expand Up @@ -68,7 +68,6 @@ class LSqlResultModel(BaseTableModel):

def __init__(self, db, sql, parent=None):
# create a virtual layer with non-geometry results
q = QUrl.toPercentEncoding(sql)
t = QTime()
t.start()

Expand All @@ -77,7 +76,10 @@ def __init__(self, db, sql, parent=None):
tmp = tf.fileName()
tf.close()

p = QgsVectorLayer("%s?query=%s" % (QUrl.fromLocalFile(tmp).toString(), q), "vv", "virtual")
df = QgsVirtualLayerDefinition()
df.setFilePath(tmp)
df.setQuery(sql)
p = QgsVectorLayer(df.toString(), "vv", "virtual")
self._secs = t.elapsed() / 1000.0

if not p.isValid():
Expand Down

0 comments on commit f56e6b9

Please sign in to comment.