Skip to content

Commit

Permalink
Merge pull request #4195 from mhugo/fix_vlayers
Browse files Browse the repository at this point in the history
[virtual layers] Fix encoding issues in dbmanager
  • Loading branch information
Hugo Mercier authored Feb 27, 2017
2 parents 0eccc89 + f56e6b9 commit 887ca6f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 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
2 changes: 1 addition & 1 deletion src/providers/virtual/qgsvirtuallayerqueryparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ namespace QgsVirtualLayerQueryParser
int pos = geometryTypeRx.indexIn( columnType, 0 );
if ( pos != -1 )
{
QgsWkbTypes::Type type = static_cast<QgsWkbTypes::Type>( geometryTypeRx.cap( 1 ).toInt() );
QgsWkbTypes::Type type = static_cast<QgsWkbTypes::Type>( geometryTypeRx.cap( 1 ).toLong() );
long srid = geometryTypeRx.cap( 2 ).toLong();
d.setGeometry( type );
d.setSrid( srid );
Expand Down

0 comments on commit 887ca6f

Please sign in to comment.