From 8290a035e8be4dbae38e2b7da0bce46a79c848f7 Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Thu, 3 Jan 2013 14:36:28 +0100 Subject: [PATCH] Add basic and shaky pragma editing Since quite a few problems are fixed by now and the program is moving towards rudeimentary usability again it's time to add some new features and problems! --- src/MainWindow.cpp | 68 +++++ src/MainWindow.h | 2 + src/MainWindow.ui | 603 +++++++++++++++++++++++++++++++++++++++++++++ src/sqlitedb.cpp | 115 +++++++++ src/sqlitedb.h | 2 + 5 files changed, 790 insertions(+) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index aaef2d80..ccb7bceb 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -81,6 +81,7 @@ MainWindow::MainWindow(QWidget* parent) setAcceptDrops(true); (void)statusBar(); + activateFields(false); init(); } @@ -166,6 +167,8 @@ void MainWindow::fileOpen(const QString & fileName) } populateStructure(); resetBrowser(); + if(ui->mainTab->currentIndex() == 2) + loadPragmas(); } } @@ -322,6 +325,7 @@ void MainWindow::fileClose() this->setWindowTitle(QApplication::applicationName()); resetBrowser(); populateStructure(); + loadPragmas(); activateFields(false); } @@ -895,6 +899,8 @@ void MainWindow::mainTabSelected(int tabindex) populateStructure(); else if(tabindex == 1) resetBrowser(); + else if(tabindex == 2) + loadPragmas(); } void MainWindow::importTableFromCSV() @@ -1316,6 +1322,12 @@ void MainWindow::activateFields(bool enable) ui->buttonNext->setEnabled(enable); ui->buttonPrevious->setEnabled(enable); ui->executeQueryButton->setEnabled(enable); + ui->scrollareaPragmas->setEnabled(enable); + ui->buttonGoto->setEnabled(enable); + ui->editGoto->setEnabled(enable); + ui->buttonRefresh->setEnabled(enable); + ui->buttonDeleteRecord->setEnabled(enable); + ui->buttonNewRecord->setEnabled(enable); } void MainWindow::browseTableHeaderClicked(int logicalindex) @@ -1334,3 +1346,59 @@ void MainWindow::resizeEvent(QResizeEvent*) { setRecordsetLabel(); } + +void MainWindow::loadPragmas() +{ + ui->comboboxPragmaAutoVacuum->setCurrentIndex(db.getPragma("auto_vacuum").toInt()); + ui->checkboxPragmaAutomaticIndex->setChecked(db.getPragma("automatic_index").toInt()); + ui->spinPragmaBusyTimeout->setValue(db.getPragma("busy_timeout").toInt()); + ui->spinPragmaCacheSize->setValue(db.getPragma("cache_size").toInt()); + ui->checkboxPragmaCheckpointFullFsync->setChecked(db.getPragma("checkpoint_fullfsync").toInt()); + ui->comboboxPragmaEncoding->setCurrentIndex(db.getPragma("encoding").toInt()); + ui->checkboxPragmaForeignKeys->setChecked(db.getPragma("foreign_keys").toInt()); + ui->checkboxPragmaFullFsync->setChecked(db.getPragma("fullfsync").toInt()); + ui->checkboxPragmaIgnoreCheckConstraints->setChecked(db.getPragma("ignore_check_constraints").toInt()); + ui->comboboxPragmaJournalMode->setCurrentIndex(db.getPragma("journal_mode").toInt()); + ui->spinPragmaJournalSizeLimit->setValue(db.getPragma("journal_size_limit").toInt()); + ui->checkboxPragmaLegacyFileFormat->setChecked(db.getPragma("legacy_file_format").toInt()); + ui->comboboxPragmaLockingMode->setCurrentIndex(db.getPragma("locking_mode").toInt()); + ui->spinPragmaMaxPageCount->setValue(db.getPragma("max_page_count").toInt()); + ui->spinPragmaPageSize->setValue(db.getPragma("page_size").toInt()); + ui->checkboxPragmaReadUncommitted->setChecked(db.getPragma("read_uncommitted").toInt()); + ui->checkboxPragmaRecursiveTriggers->setChecked(db.getPragma("recursive_triggers").toInt()); + ui->checkboxPragmaReverseUnorderedSelects->setChecked(db.getPragma("reverse_unordered_selects").toInt()); + ui->spinPragmaSchemaVersion->setValue(db.getPragma("schema_version").toInt()); + ui->checkboxPragmaSecureDelete->setChecked(db.getPragma("secure_delete").toInt()); + ui->comboboxPragmaSynchronous->setCurrentIndex(db.getPragma("synchronous").toInt()); + ui->comboboxPragmaTempStore->setCurrentIndex(db.getPragma("temp_store").toInt()); + ui->spinPragmaUserVersion->setValue(db.getPragma("user_version").toInt()); + ui->spinPragmaWalAutoCheckpoint->setValue(db.getPragma("wal_autocheckpoint").toInt()); +} + +void MainWindow::savePragmas() +{ + db.setPragma("auto_vacuum", QString::number(ui->comboboxPragmaAutoVacuum->currentIndex())); + db.setPragma("automatic_index", QString::number(ui->checkboxPragmaAutomaticIndex->isChecked())); + db.setPragma("busy_timeout", QString::number(ui->spinPragmaBusyTimeout->value())); + db.setPragma("cache_size", QString::number(ui->spinPragmaCacheSize->value())); + db.setPragma("checkpoint_fullfsync", QString::number(ui->checkboxPragmaCheckpointFullFsync->isChecked())); + db.setPragma("encoding", QString::number(ui->comboboxPragmaEncoding->currentIndex())); + db.setPragma("foreign_keys", QString::number(ui->checkboxPragmaForeignKeys->isChecked())); + db.setPragma("fullfsync", QString::number(ui->checkboxPragmaFullFsync->isChecked())); + db.setPragma("ignore_check_constraints", QString::number(ui->checkboxPragmaIgnoreCheckConstraints->isChecked())); + db.setPragma("journal_mode", QString::number(ui->comboboxPragmaJournalMode->currentIndex())); + db.setPragma("journal_size_limit", QString::number(ui->spinPragmaJournalSizeLimit->value())); + db.setPragma("legacy_file_format", QString::number(ui->checkboxPragmaLegacyFileFormat->isChecked())); + db.setPragma("locking_mode", QString::number(ui->comboboxPragmaLockingMode->currentIndex())); + db.setPragma("max_page_count", QString::number(ui->spinPragmaMaxPageCount->value())); + db.setPragma("page_size", QString::number(ui->spinPragmaPageSize->value())); + db.setPragma("read_uncommitted", QString::number(ui->checkboxPragmaReadUncommitted->isChecked())); + db.setPragma("recursive_triggers", QString::number(ui->checkboxPragmaRecursiveTriggers->isChecked())); + db.setPragma("reverse_unordered_selects", QString::number(ui->checkboxPragmaReverseUnorderedSelects->isChecked())); + db.setPragma("schema_version", QString::number(ui->spinPragmaSchemaVersion->value())); + db.setPragma("secure_delete", QString::number(ui->checkboxPragmaSecureDelete->isChecked())); + db.setPragma("synchronous", QString::number(ui->comboboxPragmaSynchronous->currentIndex())); + db.setPragma("temp_store", QString::number(ui->comboboxPragmaTempStore->currentIndex())); + db.setPragma("user_version", QString::number(ui->spinPragmaUserVersion->value())); + db.setPragma("wal_autocheckpoint", QString::number(ui->spinPragmaWalAutoCheckpoint->value())); +} diff --git a/src/MainWindow.h b/src/MainWindow.h index b2a60a5f..7992ead3 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -117,6 +117,8 @@ public slots: virtual void openPreferences(); virtual void updatePreferences(); virtual void openRecentFile(); + virtual void loadPragmas(); + virtual void savePragmas(); protected: DBBrowserDB db; diff --git a/src/MainWindow.ui b/src/MainWindow.ui index cccbaa31..9a93255c 100644 --- a/src/MainWindow.ui +++ b/src/MainWindow.ui @@ -276,6 +276,549 @@ + + + Edit &Pragmas + + + + + + true + + + + + 0 + -165 + 763 + 653 + + + + + QFormLayout::ExpandingFieldsGrow + + + + + Auto Vacuum + + + comboboxPragmaAutoVacuum + + + + + + + + None + + + + + Full + + + + + Incremental + + + + + + + + <html><head/><body><p>Automatic Index</p></body></html> + + + checkboxPragmaAutomaticIndex + + + + + + + + + + + + + + Busy Timeout + + + spinPragmaBusyTimeout + + + + + + + Cache Size + + + spinPragmaCacheSize + + + + + + + Checkpoint Full FSYNC + + + checkboxPragmaCheckpointFullFsync + + + + + + + + + + + + + + Encoding + + + comboboxPragmaEncoding + + + + + + + Foreign Keys + + + checkboxPragmaForeignKeys + + + + + + + + + + + + + + Full FSYNC + + + checkboxPragmaFullFsync + + + + + + + + + + + + + + Ignore Check Constraints + + + checkboxPragmaIgnoreCheckConstraints + + + + + + + + + + + + + + Journal Mode + + + comboboxPragmaJournalMode + + + + + + + + Delete + + + + + Truncate + + + + + Persist + + + + + Memory + + + + + WAL + + + + + Off + + + + + + + + Journal Size Limit + + + spinPragmaJournalSizeLimit + + + + + + + Legacy File Format + + + checkboxPragmaLegacyFileFormat + + + + + + + + + + + + + + Locking Mode + + + comboboxPragmaLockingMode + + + + + + + + Normal + + + + + Exclusive + + + + + + + + Max Page Count + + + spinPragmaMaxPageCount + + + + + + + Page Size + + + spinPragmaPageSize + + + + + + + Read Uncommitted + + + checkboxPragmaReadUncommitted + + + + + + + + + + + + + + Recursive Triggers + + + checkboxPragmaRecursiveTriggers + + + + + + + + + + + + + + Reverse Unordered Selects + + + checkboxPragmaReverseUnorderedSelects + + + + + + + + + + + + + + Schema Version + + + spinPragmaSchemaVersion + + + + + + + Secure Delete + + + checkboxPragmaSecureDelete + + + + + + + + + + + + + + Synchronous + + + comboboxPragmaSynchronous + + + + + + + + Off + + + + + Normal + + + + + Full + + + + + + + + Temp Store + + + comboboxPragmaTempStore + + + + + + + + Default + + + + + File + + + + + Memory + + + + + + + + User Version + + + spinPragmaUserVersion + + + + + + + WAL Auto Checkpoint + + + spinPragmaWalAutoCheckpoint + + + + + + + 10000 + + + + + + + -100000 + + + 100000 + + + + + + + + UTF-8 + + + + + UTF-16 + + + + + UTF-16le + + + + + UTF-16be + + + + + + + + -1 + + + 100000 + + + + + + + 2000000000 + + + + + + + 512 + + + 65536 + + + + + + + 10000 + + + + + + + 10000 + + + + + + + 10000 + + + + + + + QDialogButtonBox::Cancel|QDialogButtonBox::Save + + + false + + + + + + + + + E&xecute SQL @@ -874,6 +1417,32 @@ executeQueryButton queryErrorLineEdit queryResultTableView + scrollareaPragmas + comboboxPragmaAutoVacuum + checkboxPragmaAutomaticIndex + spinPragmaBusyTimeout + spinPragmaCacheSize + checkboxPragmaCheckpointFullFsync + comboboxPragmaEncoding + checkboxPragmaForeignKeys + checkboxPragmaFullFsync + checkboxPragmaIgnoreCheckConstraints + comboboxPragmaJournalMode + spinPragmaJournalSizeLimit + checkboxPragmaLegacyFileFormat + comboboxPragmaLockingMode + spinPragmaMaxPageCount + spinPragmaPageSize + checkboxPragmaReadUncommitted + checkboxPragmaRecursiveTriggers + checkboxPragmaReverseUnorderedSelects + spinPragmaSchemaVersion + checkboxPragmaSecureDelete + comboboxPragmaSynchronous + comboboxPragmaTempStore + spinPragmaUserVersion + spinPragmaWalAutoCheckpoint + buttonBoxPragmas @@ -1503,6 +2072,38 @@ + + buttonBoxPragmas + rejected() + MainWindow + loadPragmas() + + + 243 + 551 + + + -1 + 470 + + + + + buttonBoxPragmas + accepted() + MainWindow + savePragmas() + + + 715 + 550 + + + 802 + 522 + + + fileOpen() @@ -1543,5 +2144,7 @@ fileNew() helpAbout() on_delete_field() + loadPragmas() + savePragmas() diff --git a/src/sqlitedb.cpp b/src/sqlitedb.cpp index 397a7311..adae46be 100644 --- a/src/sqlitedb.cpp +++ b/src/sqlitedb.cpp @@ -778,7 +778,122 @@ QStringList DBBrowserDB::decodeCSV(const QString & csvfilename, char sep, char q return result; } +QString DBBrowserDB::getPragma(QString pragma) +{ + if(!isOpen()) + return ""; + QString sql = QString("PRAGMA %1").arg(pragma); + sqlite3_stmt* vm; + const char* tail; + QString retval = ""; + // Get value from DB + int err = sqlite3_prepare(_db, sql.toStdString().c_str(), sql.length(), &vm, &tail); + if(err == SQLITE_OK){ + logSQL(sql, kLogMsg_App); + if(sqlite3_step(vm) == SQLITE_ROW) + { + retval = QString((const char *) sqlite3_column_text(vm, 0)); + sqlite3_finalize(vm); + } else { + sqlite3_finalize(vm); + qDebug("didn't receive any output from pragma %s", pragma.toStdString().c_str()); + } + } else { + qDebug ("could not execute pragma command: %d, %s", err, sqlite3_errmsg(_db)); + } + // Make changes to the value when needed + if(pragma == "journal_mode") + { + retval = retval.toLower(); + if(retval == "delete") + retval = "0"; + else if(retval == "truncate") + retval = "1"; + else if(retval == "persist") + retval = "2"; + else if(retval == "memory") + retval = "3"; + else if(retval == "wal") + retval = "4"; + else if(retval == "off") + retval = "5"; + else + retval = "0"; + } else if(pragma == "locking_mode") { + retval = retval.toLower(); + if(retval == "normal") + retval = "0"; + else if(retval == "exclusive") + retval = "1"; + else + retval = "0"; + } else if(pragma == "encoding") { + retval = retval.toLower(); + if(retval == "utf-8") + retval = "0"; + else if(retval == "utf-16") + retval = "1"; + else if(retval == "utf-16le") + retval = "2"; + else if(retval == "utf-16be") + retval = "3"; + else + retval = "0"; + } + // Return it + return retval; +} + +bool DBBrowserDB::setPragma(QString pragma, QString value) +{ + // Make changes to the value when needed + if(pragma == "journal_mode") + { + if(value == "0") + value = "delete"; + else if(value == "1") + value = "truncate"; + else if(value == "2") + value = "persist"; + else if(value == "3") + value = "memory"; + else if(value == "4") + value = "wal"; + else if(value == "5") + value = "off"; + else + value = "delete"; + } else if(value == "locking_mode") { + if(value == "0") + value = "normal"; + else if(value == "1") + value = "exclusive"; + else + value = "normal"; + } else if(pragma == "encoding") { + if(value == "0") + value = "\"utf-8\""; + else if(value == "1") + value = "\"utf-16\""; + else if(value == "2") + value = "\"utf-16le\""; + else if(value == "3") + value = "\"utf-16be\""; + else + value = "\"utf-8\""; + } + + // Set the pragma value + QString sql = QString("PRAGMA %1 = %2;").arg(pragma).arg(value); + if(!executeSQL(sql)) + { + qDebug("Error setting pragma %s to %s: %s", pragma.toStdString().c_str(), value.toStdString().c_str(), lastErrorMessage.toStdString().c_str()); + return false; + } else { + return true; + } +} diff --git a/src/sqlitedb.h b/src/sqlitedb.h index 5bca269f..c1828ed6 100644 --- a/src/sqlitedb.h +++ b/src/sqlitedb.h @@ -120,6 +120,8 @@ public: char * GetEncodedQStringAsPointer( const QString & input); QString GetEncodedQString( const QString & input); QString GetDecodedQString( const QString & input); + QString getPragma(QString pragma); + bool setPragma(QString pragma, QString value); sqlite3 * _db;