Skip to content
Permalink
Browse files

Add a button to save the query as an external sql file

  • Loading branch information
lbartoletti committed Oct 19, 2018
1 parent f23b09b commit 74f7cb5414169fd8553ec5e2777e21a4c76a2087
Showing with 30 additions and 5 deletions.
  1. +18 −2 python/plugins/db_manager/dlg_sql_window.py
  2. +12 −3 python/plugins/db_manager/ui/DlgSqlWindow.ui
@@ -26,8 +26,8 @@
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
from qgis.PyQt.QtCore import Qt, pyqtSignal, QDir
from qgis.PyQt.QtWidgets import QDialog, QWidget, QAction, QApplication, QInputDialog, QStyledItemDelegate, QTableWidgetItem, QFileDialog
from qgis.PyQt.QtGui import QKeySequence, QCursor, QClipboard, QIcon, QStandardItemModel, QStandardItem
from qgis.PyQt.Qsci import QsciAPIs

@@ -120,6 +120,7 @@ def __init__(self, iface, db, parent=None):
self.btnClear.clicked.connect(self.clearSql)

self.presetStore.clicked.connect(self.storePreset)
self.presetSaveAsFile.clicked.connect(self.saveAsFilePreset)
self.presetDelete.clicked.connect(self.deletePreset)
self.presetCombo.activated[str].connect(self.loadPreset)
self.presetCombo.activated[str].connect(self.presetName.setText)
@@ -228,6 +229,21 @@ def storePreset(self):
else:
self.presetCombo.setCurrentIndex(index)

def saveAsFilePreset(self):
query = self._getSqlQuery()
if query == "":
return

filename, ext = QFileDialog.getSaveFileName(
self,
'Save SQL Query',
QDir.homePath(),
"SQL File (*.sql)")

if filename:
with open(filename, 'w') as f:
f.write(query)

def deletePreset(self):
name = self.presetCombo.currentText()
QgsProject.instance().removeEntry('DBManager', 'savedQueries/' + self.getQueryHash(name))
@@ -25,7 +25,7 @@
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<widget class="QWidget" name="">
<widget class="QWidget" name="layoutWidget">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
@@ -100,6 +100,16 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="presetSaveAsFile">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Save the query as SQL file&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Save As File</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
@@ -226,7 +236,7 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="">
<widget class="QWidget" name="layoutWidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTableView" name="viewResult">
@@ -498,7 +508,6 @@ unique values</string>
<tabstop>queryBuilderBtn</tabstop>
<tabstop>presetCombo</tabstop>
<tabstop>presetName</tabstop>
<tabstop>presetStore</tabstop>
<tabstop>presetDelete</tabstop>
<tabstop>editSql</tabstop>
<tabstop>btnExecute</tabstop>

0 comments on commit 74f7cb5

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