Skip to content

Commit

Permalink
Merge pull request #6749 from slarosa/db_quey_history_1
Browse files Browse the repository at this point in the history
[FEATURE][needs-docs] DB Manager: adds SQL query history
  • Loading branch information
m-kuhn authored May 7, 2018
2 parents 0cc0fed + 84d8693 commit 9d12b0a
Show file tree
Hide file tree
Showing 2 changed files with 278 additions and 166 deletions.
63 changes: 59 additions & 4 deletions python/plugins/db_manager/dlg_sql_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
from builtins import str

from qgis.PyQt.QtCore import Qt, pyqtSignal
from qgis.PyQt.QtWidgets import QDialog, QWidget, QAction, QApplication, QInputDialog, QStyledItemDelegate
from qgis.PyQt.QtWidgets import QDialog, QWidget, QAction, QApplication, QInputDialog, QStyledItemDelegate, QTableWidgetItem
from qgis.PyQt.QtGui import QKeySequence, QCursor, QClipboard, QIcon, QStandardItemModel, QStandardItem
from qgis.PyQt.Qsci import QsciAPIs

from qgis.core import QgsProject, QgsApplication, QgsTask
from qgis.core import QgsProject, QgsApplication, QgsTask, QgsSettings
from qgis.utils import OverrideCursor

from .db_plugins.plugin import BaseError
Expand All @@ -53,19 +53,22 @@

class DlgSqlWindow(QWidget, Ui_Dialog):
nameChanged = pyqtSignal(str)
QUERY_HISTORY_LIMIT = 20

def __init__(self, iface, db, parent=None):
QWidget.__init__(self, parent)
self.mainWindow = parent
self.iface = iface
self.db = db
self.dbType = db.connection().typeNameString()
self.connectionName = db.connection().connectionName()
self.filter = ""
self.modelAsync = None
self.allowMultiColumnPk = isinstance(db, PGDatabase) # at the moment only PostgreSQL allows a primary key to span multiple columns, SpatiaLite doesn't
self.aliasSubQuery = isinstance(db, PGDatabase) # only PostgreSQL requires subqueries to be aliases
self.setupUi(self)
self.setWindowTitle(
self.tr(u"{0} - {1} [{2}]").format(self.windowTitle(), db.connection().connectionName(), db.connection().typeNameString()))
self.tr(u"{0} - {1} [{2}]").format(self.windowTitle(), self.connectionName, self.dbType))

self.defaultLayerName = 'QueryLayer'

Expand All @@ -79,6 +82,17 @@ def __init__(self, iface, db, parent=None):
self.editSql.setMarginVisible(True)
self.initCompleter()

settings = QgsSettings()
self.history = settings.value('DB_Manager/queryHistory/' + self.dbType, {self.connectionName: []})
if self.connectionName not in self.history:
self.history[self.connectionName] = []

self.queryHistoryWidget.setVisible(False)
self.queryHistoryTableWidget.verticalHeader().hide()
self.queryHistoryTableWidget.doubleClicked.connect(self.insertQueryInEditor)
self.populateQueryHistory()
self.btnQueryHistory.toggled.connect(self.showHideQueryHistory)

self.btnCancel.setText(self.tr("Cancel (ESC)"))
self.btnCancel.setEnabled(False)
self.btnCancel.clicked.connect(self.executeSqlCanceled)
Expand Down Expand Up @@ -140,6 +154,45 @@ def __init__(self, iface, db, parent=None):

self.presetName.textChanged.connect(self.nameChanged)

def insertQueryInEditor(self, item):
sql = item.data(Qt.DisplayRole)
self.editSql.insertText(sql)

def showHideQueryHistory(self, visible):
self.queryHistoryWidget.setVisible(visible)

def populateQueryHistory(self):
self.queryHistoryTableWidget.clearContents()
self.queryHistoryTableWidget.setRowCount(0)
dictlist = self.history[self.connectionName]

if not dictlist:
return

for i in range(len(dictlist)):
self.queryHistoryTableWidget.insertRow(0)
queryItem = QTableWidgetItem(dictlist[i]['query'])
rowsItem = QTableWidgetItem(str(dictlist[i]['rows']))
durationItem = QTableWidgetItem(str(dictlist[i]['secs']))
self.queryHistoryTableWidget.setItem(0, 0, queryItem)
self.queryHistoryTableWidget.setItem(0, 1, rowsItem)
self.queryHistoryTableWidget.setItem(0, 2, durationItem)

self.queryHistoryTableWidget.resizeColumnsToContents()
self.queryHistoryTableWidget.resizeRowsToContents()

def writeQueryHistory(self, sql, affectedRows, secs):
if len(self.history[self.connectionName]) >= self.QUERY_HISTORY_LIMIT:
self.history[self.connectionName].pop(0)

settings = QgsSettings()
self.history[self.connectionName].append({'query': sql,
'rows': affectedRows,
'secs': secs})
settings.setValue('DB_Manager/queryHistory/' + self.dbType, self.history)

self.populateQueryHistory()

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

Expand Down Expand Up @@ -236,12 +289,14 @@ def executeSqlCompleted(self):
quotedCols = []

self.viewResult.setModel(model)
self.lblResult.setText(self.tr("{0} rows, {1:.1f} seconds").format(model.affectedRows(), model.secs()))
self.lblResult.setText(self.tr("{0} rows, {1:.3f} seconds").format(model.affectedRows(), model.secs()))
cols = self.viewResult.model().columnNames()
for col in cols:
quotedCols.append(self.db.connector.quoteId(col))

self.setColumnCombos(cols, quotedCols)

self.writeQueryHistory(self.modelAsync.task.sql, model.affectedRows(), model.secs())
self.update()
elif not self.modelAsync.canceled:
DlgDbError.showError(self.modelAsync.error, self)
Expand Down
Loading

0 comments on commit 9d12b0a

Please sign in to comment.