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
This commit is contained in:
rldhont 2017-10-14 15:58:11 +02:00 committed by GitHub
commit 949d216744
2 changed files with 14 additions and 11 deletions

View File

@ -79,10 +79,8 @@ class DBManagerPlugin(object):
self.dlg.close() self.dlg.close()
def onLayerWasAdded(self, aMapLayer): def onLayerWasAdded(self, aMapLayer):
# Be able to update every Db layer from Postgres, Spatialite and Oracle
if hasattr(aMapLayer, 'dataProvider') and aMapLayer.dataProvider().name() in ['postgres', 'spatialite', 'oracle']: if hasattr(aMapLayer, 'dataProvider') and aMapLayer.dataProvider().name() in ['postgres', 'spatialite', 'oracle']:
uri = QgsDataSourceUri(aMapLayer.source())
table = uri.table()
if table.startswith('(') and table.endswith(')'):
self.iface.addCustomActionForLayer(self.layerAction, aMapLayer) self.iface.addCustomActionForLayer(self.layerAction, aMapLayer)
# virtual has QUrl source # virtual has QUrl source
# url = QUrl(QUrl.fromPercentEncoding(l.source())) # url = QUrl(QUrl.fromPercentEncoding(l.source()))
@ -91,10 +89,9 @@ class DBManagerPlugin(object):
# url.queryItemValue('geometry') # url.queryItemValue('geometry')
def onUpdateSqlLayer(self): def onUpdateSqlLayer(self):
# Be able to update every Db layer from Postgres, Spatialite and Oracle
l = self.iface.activeLayer() l = self.iface.activeLayer()
if l.dataProvider().name() in ['postgres', 'spatialite', 'oracle']: if l.dataProvider().name() in ['postgres', 'spatialite', 'oracle']:
table = QgsDataSourceUri(l.source()).table()
if table.startswith('(') and table.endswith(')'):
self.run() self.run()
self.dlg.runSqlLayerWindow(l) self.dlg.runSqlLayerWindow(l)
# virtual has QUrl source # virtual has QUrl source

View File

@ -31,7 +31,7 @@ from qgis.PyQt.QtGui import QKeySequence, QCursor, QClipboard, QIcon, QStandardI
from qgis.PyQt.Qsci import QsciAPIs from qgis.PyQt.Qsci import QsciAPIs
from qgis.PyQt.QtXml import QDomDocument from qgis.PyQt.QtXml import QDomDocument
from qgis.core import QgsProject, QgsDataSourceUri from qgis.core import QgsProject, QgsDataSourceUri, QgsReadWriteContext
from qgis.utils import OverrideCursor from qgis.utils import OverrideCursor
from .db_plugins import createDbPlugin from .db_plugins import createDbPlugin
@ -151,6 +151,12 @@ class DlgSqlLayerWindow(QWidget, Ui_Dialog):
match = re.search('^\((SELECT .+ FROM .+)\)$', sql, re.S) match = re.search('^\((SELECT .+ FROM .+)\)$', sql, re.S)
if match: if match:
sql = match.group(1) sql = match.group(1)
if not sql.startswith('(') and not sql.endswith(')'):
schema = uri.schema()
if schema and schema.upper() != 'PUBLIC':
sql = 'SELECT * FROM ' + schema + '.' + sql
else
sql = 'SELECT * FROM ' + sql
self.editSql.setText(sql) self.editSql.setText(sql)
self.executeSql() self.executeSql()
@ -328,11 +334,11 @@ class DlgSqlLayerWindow(QWidget, Ui_Dialog):
XMLDocument = QDomDocument("style") XMLDocument = QDomDocument("style")
XMLMapLayers = XMLDocument.createElement("maplayers") XMLMapLayers = XMLDocument.createElement("maplayers")
XMLMapLayer = XMLDocument.createElement("maplayer") XMLMapLayer = XMLDocument.createElement("maplayer")
self.layer.writeLayerXML(XMLMapLayer, XMLDocument) self.layer.writeLayerXml(XMLMapLayer, XMLDocument, QgsReadWriteContext())
XMLMapLayer.firstChildElement("datasource").firstChild().setNodeValue(layer.source()) XMLMapLayer.firstChildElement("datasource").firstChild().setNodeValue(layer.source())
XMLMapLayers.appendChild(XMLMapLayer) XMLMapLayers.appendChild(XMLMapLayer)
XMLDocument.appendChild(XMLMapLayers) XMLDocument.appendChild(XMLMapLayers)
self.layer.readLayerXML(XMLMapLayer) self.layer.readLayerXml(XMLMapLayer, QgsReadWriteContext())
self.layer.reload() self.layer.reload()
self.iface.actionDraw().trigger() self.iface.actionDraw().trigger()
self.iface.mapCanvas().refresh() self.iface.mapCanvas().refresh()