Skip to content

Commit

Permalink
Add a button to save the query as an external sql file
Browse files Browse the repository at this point in the history
  • Loading branch information
lbartoletti committed Oct 22, 2018
1 parent f23b09b commit 74f7cb5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
20 changes: 18 additions & 2 deletions python/plugins/db_manager/dlg_sql_window.py
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
15 changes: 12 additions & 3 deletions python/plugins/db_manager/ui/DlgSqlWindow.ui
Expand Up @@ -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">
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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">
Expand Down Expand Up @@ -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>
Expand Down

0 comments on commit 74f7cb5

Please sign in to comment.