Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Load SQLite extensions earlier
In the Preferences dialog we allow the user to configure a list of
SQLite extensions which should be loaded whenever a new database file is
create or an existing database file is loaded. This commit changes the
order of actions after creating or opening a file so that the extensions
are loaded significantly earlier. This way they are already loaded for
most of the time during the process.

This fixes some subtle bugs. One of the more prominent ones was
triggered when there is a view which uses some function which is
defined in such an extension. After loading the file, parsing would fail
and you couldn't see the fields of the view. Browsing it would work but
trying to edit the display format crashed the application.
  • Loading branch information
MKleusberg committed Sep 4, 2018
1 parent 60195c7 commit f3e6aec
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
19 changes: 1 addition & 18 deletions src/MainWindow.cpp
Expand Up @@ -401,7 +401,6 @@ bool MainWindow::fileOpen(const QString& fileName, bool dontAddToRecentFiles, bo
if(!dontAddToRecentFiles)
addToRecentFilesMenu(wFile);
openSqlTab(true);
loadExtensionsFromSettings();
if(ui->mainTab->currentIndex() == BrowseTab)
populateTable();
else if(ui->mainTab->currentIndex() == PragmaTab)
Expand Down Expand Up @@ -432,7 +431,6 @@ void MainWindow::fileNew()
statusEncodingLabel->setText(db.getPragma("encoding"));
statusEncryptionLabel->setVisible(false);
statusReadOnlyLabel->setVisible(false);
loadExtensionsFromSettings();
populateTable();
openSqlTab(true);
createTable();
Expand All @@ -446,7 +444,6 @@ void MainWindow::fileNewInMemoryDatabase()
statusEncodingLabel->setText(db.getPragma("encoding"));
statusEncryptionLabel->setVisible(false);
statusReadOnlyLabel->setVisible(false);
loadExtensionsFromSettings();
populateTable();
openSqlTab(true);
createTable();
Expand Down Expand Up @@ -1597,7 +1594,6 @@ void MainWindow::importDatabaseFromSQL()
}

db.create(newDbFile);
loadExtensionsFromSettings();
}

// Defer foreign keys. Just deferring them instead of disabling them should work fine because in the import we only expect CREATE and INSERT
Expand Down Expand Up @@ -2125,19 +2121,6 @@ void MainWindow::loadExtension()
QMessageBox::warning(this, QApplication::applicationName(), tr("Error loading extension: %1").arg(db.lastError()));
}

void MainWindow::loadExtensionsFromSettings()
{
if(!db.isOpen())
return;

QStringList list = Settings::getValue("extensions", "list").toStringList();
for(const QString& ext : list)
{
if(db.loadExtension(ext) == false)
QMessageBox::warning(this, QApplication::applicationName(), tr("Error loading extension: %1").arg(db.lastError()));
}
}

void MainWindow::reloadSettings()
{
// Set data browser font
Expand All @@ -2163,7 +2146,7 @@ void MainWindow::reloadSettings()
editDock->reloadSettings();

// Load extensions
loadExtensionsFromSettings();
db.loadExtensionsFromSettings();

// Refresh view
dbStructureModel->reloadData();
Expand Down
1 change: 0 additions & 1 deletion src/MainWindow.h
Expand Up @@ -184,7 +184,6 @@ class MainWindow : public QMainWindow
void addToRecentFilesMenu(const QString& filename);
void activateFields(bool enable = true);
void enableEditing(bool enable_edit);
void loadExtensionsFromSettings();
void saveAsView(QString query);
void duplicateRecord(int currentRow);
void selectTableLine(int lineToSelect);
Expand Down
20 changes: 20 additions & 0 deletions src/sqlitedb.cpp
Expand Up @@ -156,6 +156,9 @@ bool DBBrowserDB::open(const QString& db, bool readOnly)
QFileInfo fid(fi.absoluteDir().absolutePath());
isReadOnly = readOnly || !fi.isWritable() || !fid.isWritable();

// Load extensions
loadExtensionsFromSettings();

// Execute default SQL
if(!isReadOnly)
{
Expand Down Expand Up @@ -532,6 +535,9 @@ bool DBBrowserDB::create ( const QString & db)
executeSQL("DROP TABLE notempty;", false, false);
}

// Load extensions
loadExtensionsFromSettings();

// Execute default SQL
QString default_sql = Settings::getValue("db", "defaultsqltext").toString();
if(!default_sql.isEmpty())
Expand Down Expand Up @@ -1808,6 +1814,20 @@ bool DBBrowserDB::loadExtension(const QString& filePath)
}
}


void DBBrowserDB::loadExtensionsFromSettings()
{
if(!_db)
return;

QStringList list = Settings::getValue("extensions", "list").toStringList();
for(const QString& ext : list)
{
if(loadExtension(ext) == false)
QMessageBox::warning(nullptr, QApplication::applicationName(), tr("Error loading extension: %1").arg(lastError()));
}
}

QVector<QPair<QString, QString>> DBBrowserDB::queryColumnInformation(const QString& schema_name, const QString& object_name)
{
waitForDbRelease();
Expand Down
1 change: 1 addition & 0 deletions src/sqlitedb.h
Expand Up @@ -171,6 +171,7 @@ class DBBrowserDB : public QObject
bool setPragma(const QString& pragma, int value, int& originalvalue);

bool loadExtension(const QString& filename);
void loadExtensionsFromSettings();

private:
QVector<QPair<QString, QString>> queryColumnInformation(const QString& schema_name, const QString& object_name);
Expand Down

0 comments on commit f3e6aec

Please sign in to comment.