Add support for SQLite extension loading

Enable the extension support in SQLite.

Add code to allow loading of extension.

Add menu entry for easy extension loading.
This commit is contained in:
Martin Kleusberg
2013-05-03 15:26:34 +02:00
parent a031386edd
commit e36b17a485
7 changed files with 75 additions and 1 deletions

View File

@@ -988,6 +988,7 @@ void MainWindow::activateFields(bool enable)
ui->buttonDeleteRecord->setEnabled(enable);
ui->buttonNewRecord->setEnabled(enable);
ui->actionExecuteSql->setEnabled(enable);
ui->actionLoadExtension->setEnabled(enable);
}
void MainWindow::browseTableHeaderClicked(int logicalindex)
@@ -1161,3 +1162,17 @@ void MainWindow::saveSqlFile()
QFileInfo fileinfo(file);
ui->tabSqlAreas->setTabText(ui->tabSqlAreas->currentIndex(), fileinfo.fileName());
}
void MainWindow::loadExtension()
{
QString file = QFileDialog::getOpenFileName(
this,
tr("Select extension file"),
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(),
tr("Extensions(*.so *.dll);;All files(*)"));
if(db.loadExtension(file))
QMessageBox::information(this, QApplication::applicationName(), tr("Extension successfully loaded."));
else
QMessageBox::warning(this, QApplication::applicationName(), tr("Error loading extension: %1").arg(db.lastErrorMessage));
}

View File

@@ -140,6 +140,7 @@ private slots:
virtual void closeSqlTab(int index, bool force = false);
virtual void openSqlFile();
virtual void saveSqlFile();
virtual void loadExtension();
};
#endif

View File

@@ -292,7 +292,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>278</width>
<width>763</width>
<height>444</height>
</rect>
</property>
@@ -802,6 +802,7 @@
<addaction name="fileSaveAction"/>
<addaction name="fileRevertAction"/>
<addaction name="fileCompactAction"/>
<addaction name="actionLoadExtension"/>
<addaction name="separator"/>
<addaction name="menuImport"/>
<addaction name="menuExport"/>
@@ -1292,6 +1293,18 @@
<string>Save SQL file</string>
</property>
</action>
<action name="actionLoadExtension">
<property name="enabled">
<bool>false</bool>
</property>
<property name="icon">
<iconset resource="icons/icons.qrc">
<normaloff>:/icons/load_extension</normaloff>:/icons/load_extension</iconset>
</property>
<property name="text">
<string>Load extension</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
@@ -2024,6 +2037,22 @@
</hint>
</hints>
</connection>
<connection>
<sender>actionLoadExtension</sender>
<signal>activated()</signal>
<receiver>MainWindow</receiver>
<slot>loadExtension()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>399</x>
<y>299</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>fileOpen()</slot>
@@ -2069,5 +2098,6 @@
<slot>openSqlTab()</slot>
<slot>openSqlFile()</slot>
<slot>saveSqlFile()</slot>
<slot>loadExtension()</slot>
</slots>
</ui>

View File

@@ -28,6 +28,7 @@
<file alias="run">resultset_next.png</file>
<file alias="save_sql">page_save.png</file>
<file alias="open_sql">page_white_database.png</file>
<file alias="load_extension">plugin_add.png</file>
</qresource>
<qresource prefix="/oldimages">
<file alias="128">oldimages/128.png</file>

BIN
src/icons/plugin_add.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 691 B

View File

@@ -81,6 +81,9 @@ bool DBBrowserDB::open ( const QString & db)
}
curDBFilename = db;
}
// Enable extension loading
sqlite3_enable_load_extension(_db, 1);
}
return ok;
@@ -1009,3 +1012,24 @@ bool DBBrowserDB::setPragma(const QString& pragma, int value, int& originalvalue
}
return false;
}
bool DBBrowserDB::loadExtension(const QString& filename)
{
// Check if file exists
if(!QFile::exists(filename))
{
lastErrorMessage = QObject::tr("File not found.");
return false;
}
// Try to load extension
char* error;
if(sqlite3_load_extension(_db, filename.toUtf8(), 0, &error) == SQLITE_OK)
{
return true;
} else {
lastErrorMessage = QString::fromUtf8(error);
sqlite3_free(error);
return false;
}
}

View File

@@ -108,6 +108,9 @@ public:
bool setPragma(const QString& pragma, const QString& value);
bool setPragma(const QString& pragma, const QString& value, QString& originalvalue);
bool setPragma(const QString& pragma, int value, int& originalvalue);
bool loadExtension(const QString& filename);
sqlite3 * _db;