Skip to content
Permalink
Browse files

Merge pull request #7951 from elpaso/bugfix-19802-dbmanager-saved-que…

…ries

Bugfix 19802 dbmanager saved queries
  • Loading branch information
elpaso committed Sep 19, 2018
2 parents b748767 + 9473d63 commit 826056e42c97155587c5949ea375b48f6e9e66a7
@@ -24,6 +24,7 @@
from builtins import zip
from builtins import next
from builtins import str
from hashlib import md5

from qgis.PyQt.QtCore import Qt, pyqtSignal
from qgis.PyQt.QtWidgets import QDialog, QWidget, QAction, QApplication, QStyledItemDelegate
@@ -181,6 +182,9 @@ def __init__(self, iface, layer, parent=None):
if uri.selectAtIdDisabled():
self.avoidSelectById.setCheckState(Qt.Checked)

def getQueryHash(self, name):
return 'q%s' % md5(name.encode('utf8')).hexdigest()

def updatePresetsCombobox(self):
self.presetCombo.clear()

@@ -199,8 +203,8 @@ def storePreset(self):
if query == "":
return
name = self.presetName.text()
QgsProject.instance().writeEntry('DBManager', 'savedQueries/q' + str(name.__hash__()) + '/name', name)
QgsProject.instance().writeEntry('DBManager', 'savedQueries/q' + str(name.__hash__()) + '/query', query)
QgsProject.instance().writeEntry('DBManager', 'savedQueries/' + self.getQueryHash(name) + '/name', name)
QgsProject.instance().writeEntry('DBManager', 'savedQueries/' + self.getQueryHash(name) + '/query', query)
index = self.presetCombo.findText(name)
if index == -1:
self.presetCombo.addItem(name)
@@ -210,13 +214,13 @@ def storePreset(self):

def deletePreset(self):
name = self.presetCombo.currentText()
QgsProject.instance().removeEntry('DBManager', 'savedQueries/q' + str(name.__hash__()))
QgsProject.instance().removeEntry('DBManager', 'savedQueries/q' + self.getQueryHash(name))
self.presetCombo.removeItem(self.presetCombo.findText(name))
self.presetCombo.setCurrentIndex(-1)

def loadPreset(self, name):
query = QgsProject.instance().readEntry('DBManager', 'savedQueries/q' + str(name.__hash__()) + '/query')[0]
name = QgsProject.instance().readEntry('DBManager', 'savedQueries/q' + str(name.__hash__()) + '/name')[0]
query = QgsProject.instance().readEntry('DBManager', 'savedQueries/' + self.getQueryHash(name) + '/query')[0]
name = QgsProject.instance().readEntry('DBManager', 'savedQueries/' + self.getQueryHash(name) + '/name')[0]
self.editSql.setText(query)

def clearSql(self):
@@ -24,6 +24,7 @@
from builtins import zip
from builtins import next
from builtins import str
from hashlib import md5

from qgis.PyQt.QtCore import Qt, pyqtSignal
from qgis.PyQt.QtWidgets import QDialog, QWidget, QAction, QApplication, QInputDialog, QStyledItemDelegate, QTableWidgetItem
@@ -197,6 +198,9 @@ def writeQueryHistory(self, sql, affectedRows, secs):

self.populateQueryHistory()

def getQueryHash(self, name):
return 'q%s' % md5(name.encode('utf8')).hexdigest()

def updatePresetsCombobox(self):
self.presetCombo.clear()

@@ -214,9 +218,9 @@ def storePreset(self):
query = self._getSqlQuery()
if query == "":
return
name = self.presetName.text()
QgsProject.instance().writeEntry('DBManager', 'savedQueries/q' + str(name.__hash__()) + '/name', name)
QgsProject.instance().writeEntry('DBManager', 'savedQueries/q' + str(name.__hash__()) + '/query', query)
name = str(self.presetName.text())
QgsProject.instance().writeEntry('DBManager', 'savedQueries/' + self.getQueryHash(name) + '/name', name)
QgsProject.instance().writeEntry('DBManager', 'savedQueries/' + self.getQueryHash(name) + '/query', query)
index = self.presetCombo.findText(name)
if index == -1:
self.presetCombo.addItem(name)
@@ -226,13 +230,13 @@ def storePreset(self):

def deletePreset(self):
name = self.presetCombo.currentText()
QgsProject.instance().removeEntry('DBManager', 'savedQueries/q' + str(name.__hash__()))
QgsProject.instance().removeEntry('DBManager', 'savedQueries/' + self.getQueryHash(name))
self.presetCombo.removeItem(self.presetCombo.findText(name))
self.presetCombo.setCurrentIndex(-1)

def loadPreset(self, name):
query = QgsProject.instance().readEntry('DBManager', 'savedQueries/q' + str(name.__hash__()) + '/query')[0]
name = QgsProject.instance().readEntry('DBManager', 'savedQueries/q' + str(name.__hash__()) + '/name')[0]
query = QgsProject.instance().readEntry('DBManager', 'savedQueries/' + self.getQueryHash(name) + '/query')[0]
name = QgsProject.instance().readEntry('DBManager', 'savedQueries/' + self.getQueryHash(name) + '/name')[0]
self.editSql.setText(query)

def loadAsLayerToggled(self, checked):
@@ -32,7 +32,7 @@
QgsMapCanvas)

from qgis.PyQt.QtTest import QSignalSpy
from qgis.PyQt.QtCore import QT_VERSION_STR, QTemporaryFile, QTemporaryDir
from qgis.PyQt.QtCore import QT_VERSION_STR, QTemporaryDir
import sip

from qgis.testing import start_app, unittest
@@ -1105,6 +1105,22 @@ def testProjectTitleWithPeriod(self):
self.assertEqual(p0.baseName(), '2.18.21')
self.assertEqual(p1.baseName(), 'qgis-3.2.0')

def testWriteEntry(self):

tmpDir = QTemporaryDir()
tmpFile = "{}/project.qgs".format(tmpDir.path())

# zip with existing file
project = QgsProject()
query = 'select * from "sample DH" where "sample DH"."Elev" > 130 and "sample DH"."Elev" < 140'
self.assertTrue(project.writeEntry('myscope', 'myentry', query))
self.assertTrue(project.write(tmpFile))

self.assertTrue(project.read(tmpFile))
q, ok = project.readEntry('myscope', 'myentry')
self.assertTrue(ok)
self.assertEqual(q, query)


if __name__ == '__main__':
unittest.main()

0 comments on commit 826056e

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