Skip to content

Commit 949d216

Browse files
authored
Merge pull request #5316 from rldhont/db_manager_update_dblayer
[FEATURE][needs-docs][DB Manager] Be able to update every Db layer from Postgres, Spatialite and Oracle
2 parents b91b854 + e6c64f6 commit 949d216

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

python/plugins/db_manager/db_manager_plugin.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,21 @@ def unload(self):
7979
self.dlg.close()
8080

8181
def onLayerWasAdded(self, aMapLayer):
82+
# Be able to update every Db layer from Postgres, Spatialite and Oracle
8283
if hasattr(aMapLayer, 'dataProvider') and aMapLayer.dataProvider().name() in ['postgres', 'spatialite', 'oracle']:
83-
uri = QgsDataSourceUri(aMapLayer.source())
84-
table = uri.table()
85-
if table.startswith('(') and table.endswith(')'):
86-
self.iface.addCustomActionForLayer(self.layerAction, aMapLayer)
84+
self.iface.addCustomActionForLayer(self.layerAction, aMapLayer)
8785
# virtual has QUrl source
8886
# url = QUrl(QUrl.fromPercentEncoding(l.source()))
8987
# url.queryItemValue('query')
9088
# url.queryItemValue('uid')
9189
# url.queryItemValue('geometry')
9290

9391
def onUpdateSqlLayer(self):
92+
# Be able to update every Db layer from Postgres, Spatialite and Oracle
9493
l = self.iface.activeLayer()
9594
if l.dataProvider().name() in ['postgres', 'spatialite', 'oracle']:
96-
table = QgsDataSourceUri(l.source()).table()
97-
if table.startswith('(') and table.endswith(')'):
98-
self.run()
99-
self.dlg.runSqlLayerWindow(l)
95+
self.run()
96+
self.dlg.runSqlLayerWindow(l)
10097
# virtual has QUrl source
10198
# url = QUrl(QUrl.fromPercentEncoding(l.source()))
10299
# url.queryItemValue('query')

python/plugins/db_manager/dlg_sql_layer_window.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from qgis.PyQt.Qsci import QsciAPIs
3232
from qgis.PyQt.QtXml import QDomDocument
3333

34-
from qgis.core import QgsProject, QgsDataSourceUri
34+
from qgis.core import QgsProject, QgsDataSourceUri, QgsReadWriteContext
3535
from qgis.utils import OverrideCursor
3636

3737
from .db_plugins import createDbPlugin
@@ -151,6 +151,12 @@ def __init__(self, iface, layer, parent=None):
151151
match = re.search('^\((SELECT .+ FROM .+)\)$', sql, re.S)
152152
if match:
153153
sql = match.group(1)
154+
if not sql.startswith('(') and not sql.endswith(')'):
155+
schema = uri.schema()
156+
if schema and schema.upper() != 'PUBLIC':
157+
sql = 'SELECT * FROM ' + schema + '.' + sql
158+
else
159+
sql = 'SELECT * FROM ' + sql
154160
self.editSql.setText(sql)
155161
self.executeSql()
156162

@@ -328,11 +334,11 @@ def updateSqlLayer(self):
328334
XMLDocument = QDomDocument("style")
329335
XMLMapLayers = XMLDocument.createElement("maplayers")
330336
XMLMapLayer = XMLDocument.createElement("maplayer")
331-
self.layer.writeLayerXML(XMLMapLayer, XMLDocument)
337+
self.layer.writeLayerXml(XMLMapLayer, XMLDocument, QgsReadWriteContext())
332338
XMLMapLayer.firstChildElement("datasource").firstChild().setNodeValue(layer.source())
333339
XMLMapLayers.appendChild(XMLMapLayer)
334340
XMLDocument.appendChild(XMLMapLayers)
335-
self.layer.readLayerXML(XMLMapLayer)
341+
self.layer.readLayerXml(XMLMapLayer, QgsReadWriteContext())
336342
self.layer.reload()
337343
self.iface.actionDraw().trigger()
338344
self.iface.mapCanvas().refresh()

0 commit comments

Comments
 (0)