mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-01-19 18:40:13 -06:00
Add option to execute arbitrary SQL when loading any database
This adds an option to execute arbitrary SQL statements whenever a database is opened or created. See issue #451 for a possible use case.
This commit is contained in:
@@ -17,6 +17,8 @@ PreferencesDialog::PreferencesDialog(QWidget* parent)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
ui->treeSyntaxHighlighting->setColumnHidden(0, true);
|
||||
ui->labelDatabaseDefaultSqlText->setVisible(false);
|
||||
ui->editDatabaseDefaultSqlText->setVisible(false);
|
||||
|
||||
ui->fr_bin_bg->installEventFilter(this);
|
||||
ui->fr_bin_fg->installEventFilter(this);
|
||||
@@ -61,6 +63,7 @@ void PreferencesDialog::loadSettings()
|
||||
ui->checkHideSchemaLinebreaks->setChecked(getSettingsValue("db", "hideschemalinebreaks").toBool());
|
||||
ui->foreignKeysCheckBox->setChecked(getSettingsValue("db", "foreignkeys").toBool());
|
||||
ui->spinPrefetchSize->setValue(getSettingsValue("db", "prefetchsize").toInt());
|
||||
ui->editDatabaseDefaultSqlText->setText(getSettingsValue("db", "defaultsqltext").toString());
|
||||
|
||||
ui->comboDataBrowserFont->setCurrentIndex(ui->comboEditorFont->findText(getSettingsValue("databrowser", "font").toString()));
|
||||
ui->spinDataBrowserFontSize->setValue(getSettingsValue("databrowser", "fontsize").toInt());
|
||||
@@ -108,6 +111,7 @@ void PreferencesDialog::saveSettings()
|
||||
setSettingsValue("db", "hideschemalinebreaks", ui->checkHideSchemaLinebreaks->isChecked());
|
||||
setSettingsValue("db", "foreignkeys", ui->foreignKeysCheckBox->isChecked());
|
||||
setSettingsValue("db", "prefetchsize", ui->spinPrefetchSize->value());
|
||||
setSettingsValue("db", "defaultsqltext", ui->editDatabaseDefaultSqlText->text());
|
||||
|
||||
setSettingsValue("checkversion", "enabled", ui->checkUpdates->isChecked());
|
||||
|
||||
@@ -219,6 +223,10 @@ QVariant PreferencesDialog::getSettingsDefaultValue(const QString& group, const
|
||||
if(group == "db" && name == "prefetchsize")
|
||||
return 50000;
|
||||
|
||||
// db/defaultsqltext?
|
||||
if(group == "db" && name == "defaultsqltext")
|
||||
return "";
|
||||
|
||||
// MainWindow/geometry?
|
||||
if(group == "MainWindow" && name == "geometry")
|
||||
return "";
|
||||
|
||||
@@ -233,6 +233,46 @@
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="1">
|
||||
<widget class="QPushButton" name="buttonDatabaseAdvanced">
|
||||
<property name="text">
|
||||
<string>Advanced</string>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset resource="icons/icons.qrc">
|
||||
<normaloff>:/icons/down</normaloff>:/icons/down</iconset>
|
||||
</property>
|
||||
<property name="checkable">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="6" column="1">
|
||||
<widget class="SqlTextEdit" name="editDatabaseDefaultSqlText" native="true">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>20</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>16777215</width>
|
||||
<height>200</height>
|
||||
</size>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="6" column="0">
|
||||
<widget class="QLabel" name="labelDatabaseDefaultSqlText">
|
||||
<property name="text">
|
||||
<string>SQL to execute after opening database</string>
|
||||
</property>
|
||||
<property name="buddy">
|
||||
<cstring>editDatabaseDefaultSqlText</cstring>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="7" column="1">
|
||||
<spacer name="horizontalSpacer_2">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
@@ -884,6 +924,14 @@
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
<class>SqlTextEdit</class>
|
||||
<extends>QWidget</extends>
|
||||
<header>sqltextedit.h</header>
|
||||
<container>1</container>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<tabstops>
|
||||
<tabstop>tabWidget</tabstop>
|
||||
<tabstop>comboDefaultLocation</tabstop>
|
||||
@@ -927,8 +975,8 @@
|
||||
<slot>saveSettings()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>269</x>
|
||||
<y>395</y>
|
||||
<x>275</x>
|
||||
<y>488</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>155</x>
|
||||
@@ -943,8 +991,8 @@
|
||||
<slot>reject()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>337</x>
|
||||
<y>395</y>
|
||||
<x>343</x>
|
||||
<y>488</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>286</x>
|
||||
@@ -960,7 +1008,7 @@
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>571</x>
|
||||
<y>103</y>
|
||||
<y>99</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>245</x>
|
||||
@@ -976,7 +1024,7 @@
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>571</x>
|
||||
<y>141</y>
|
||||
<y>137</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>245</x>
|
||||
@@ -1016,6 +1064,38 @@
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
<connection>
|
||||
<sender>buttonDatabaseAdvanced</sender>
|
||||
<signal>toggled(bool)</signal>
|
||||
<receiver>labelDatabaseDefaultSqlText</receiver>
|
||||
<slot>setVisible(bool)</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>367</x>
|
||||
<y>189</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>205</x>
|
||||
<y>220</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
<connection>
|
||||
<sender>buttonDatabaseAdvanced</sender>
|
||||
<signal>toggled(bool)</signal>
|
||||
<receiver>editDatabaseDefaultSqlText</receiver>
|
||||
<slot>setVisible(bool)</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>376</x>
|
||||
<y>191</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>331</x>
|
||||
<y>219</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
</connections>
|
||||
<slots>
|
||||
<slot>saveSettings()</slot>
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "sqlite.h"
|
||||
#include "sqlitetablemodel.h"
|
||||
#include "CipherDialog.h"
|
||||
#include "PreferencesDialog.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QMessageBox>
|
||||
@@ -143,6 +144,14 @@ bool DBBrowserDB::open(const QString& db)
|
||||
QFileInfo fid(fi.absoluteDir().absolutePath());
|
||||
isReadOnly = !fi.isWritable() || !fid.isWritable();
|
||||
|
||||
// Execute default SQL
|
||||
if(!isReadOnly)
|
||||
{
|
||||
QString default_sql = PreferencesDialog::getSettingsValue("db", "defaultsqltext").toString();
|
||||
if(!default_sql.isEmpty())
|
||||
executeMultiSQL(default_sql, false, true);
|
||||
}
|
||||
|
||||
curDBFilename = db;
|
||||
return true;
|
||||
} else {
|
||||
@@ -360,6 +369,11 @@ bool DBBrowserDB::create ( const QString & db)
|
||||
executeSQL("DROP TABLE notempty;", false, false);
|
||||
executeSQL("COMMIT;", false, false);
|
||||
|
||||
// Execute default SQL
|
||||
QString default_sql = PreferencesDialog::getSettingsValue("db", "defaultsqltext").toString();
|
||||
if(!default_sql.isEmpty())
|
||||
executeMultiSQL(default_sql, false, true);
|
||||
|
||||
curDBFilename = db;
|
||||
isEncrypted = false;
|
||||
isReadOnly = false;
|
||||
|
||||
Reference in New Issue
Block a user