Skip to content
Permalink
Browse files

Merge pull request #3322 from rldhont/db_manager_update_sql_layer

[DB Manager] Add the ability to update SQL Layer
  • Loading branch information
rldhont committed Jul 26, 2016
2 parents b3a38f4 + c1cdfe8 commit 153b1c1d0ec157a7aba6b170174ba6f61c339aa1
@@ -199,6 +199,15 @@ def runSqlWindow(self):
self.tabs.setCurrentIndex(index)
query.nameChanged.connect(functools.partial(self.update_query_tab_name, index, dbname))

def runSqlLayerWindow(self, layer):
from dlg_sql_layer_window import DlgSqlLayerWindow
query = DlgSqlLayerWindow(self.iface, layer, self)
lname = layer.name()
tabname = self.tr("Layer") + u" (%s)" % lname
index = self.tabs.addTab(query, tabname)
#self.tabs.setTabIcon(index, db.connection().icon())
self.tabs.setCurrentIndex(index)

def update_query_tab_name(self, index, dbname, queryname):
if not queryname:
queryname = self.tr("Query")
@@ -24,6 +24,9 @@
from qgis.PyQt.QtWidgets import QAction, QApplication
from qgis.PyQt.QtGui import QIcon

from qgis.core import QgsMapLayerRegistry, QgsMapLayer, QgsDataSourceURI
import re

from . import resources_rc # NOQA


@@ -48,6 +51,15 @@ def initGui(self):
else:
self.iface.addPluginToMenu(QApplication.translate("DBManagerPlugin", "DB Manager"), self.action)

self.layerAction = QAction(QIcon(":/db_manager/icon"), QApplication.translate("DBManagerPlugin", "Update Sql Layer"),
self.iface.mainWindow())
self.layerAction.setObjectName("dbManagerUpdateSqlLayer")
QObject.connect(self.layerAction, SIGNAL("triggered()"), self.onUpdateSqlLayer)
self.iface.legendInterface().addLegendLayerAction(self.layerAction, "", "dbManagerUpdateSqlLayer", QgsMapLayer.VectorLayer, False)
for l in QgsMapLayerRegistry.instance().mapLayers().values():
self.onLayerWasAdded(l)
QgsMapLayerRegistry.instance().layerWasAdded.connect(self.onLayerWasAdded)

def unload(self):
# Remove the plugin menu item and icon
if hasattr(self.iface, 'removePluginDatabaseMenu'):
@@ -59,9 +71,36 @@ def unload(self):
else:
self.iface.removeToolBarIcon(self.action)

self.iface.legendInterface().removeLegendLayerAction(self.layerAction)
QgsMapLayerRegistry.instance().layerWasAdded.disconnect(self.onLayerWasAdded)

if self.dlg is not None:
self.dlg.close()

def onLayerWasAdded(self, aMapLayer):
if aMapLayer.dataProvider().name() in ['postgres', 'spatialite', 'oracle']:
uri = QgsDataSourceURI(aMapLayer.source())
if re.search('^\(SELECT .+ FROM .+\)$', uri.table(), re.S):
self.iface.legendInterface().addLegendLayerActionForLayer(self.layerAction, aMapLayer)
# virtual has QUrl source
# url = QUrl(QUrl.fromPercentEncoding(l.source()))
# url.queryItemValue('query')
# url.queryItemValue('uid')
# url.queryItemValue('geometry')

def onUpdateSqlLayer(self):
l = self.iface.legendInterface().currentLayer()
if l.dataProvider().name() in ['postgres', 'spatialite', 'oracle']:
uri = QgsDataSourceURI(l.source())
if re.search('^\(SELECT .+ FROM .+\)$', uri.table(), re.S):
self.run()
self.dlg.runSqlLayerWindow(l)
# virtual has QUrl source
# url = QUrl(QUrl.fromPercentEncoding(l.source()))
# url.queryItemValue('query')
# url.queryItemValue('uid')
# url.queryItemValue('geometry')

def run(self):
# keep opened only one instance
if self.dlg is None:

0 comments on commit 153b1c1

Please sign in to comment.
You can’t perform that action at this time.