Skip to content

Commit

Permalink
[DB Manager] Add the ability to update SQL Layer
Browse files Browse the repository at this point in the history
  • Loading branch information
rldhont committed Jul 20, 2016
1 parent cd33ccf commit c1cdfe8
Show file tree
Hide file tree
Showing 4 changed files with 985 additions and 0 deletions.
9 changes: 9 additions & 0 deletions python/plugins/db_manager/db_manager.py
Expand Up @@ -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")
Expand Down
39 changes: 39 additions & 0 deletions python/plugins/db_manager/db_manager_plugin.py
Expand Up @@ -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


Expand All @@ -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'):
Expand All @@ -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:
Expand Down

0 comments on commit c1cdfe8

Please sign in to comment.