diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 719ec3fe..4e0d2ae1 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -25,6 +25,7 @@ #include "sqlitetablemodel.h" #include "FilterTableHeader.h" #include "SqlExecutionArea.h" +#include "VacuumDialog.h" MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), @@ -530,18 +531,12 @@ void MainWindow::createIndex() void MainWindow::compact() { - QApplication::setOverrideCursor( Qt::WaitCursor ); - if (!db.compact()){ - QString error = tr("Error: could not compact the database file. Message from database engine: %1").arg(db.lastErrorMessage); - QApplication::restoreOverrideCursor( ); - QMessageBox::warning( this, QApplication::applicationName(), error ); - } else { - QApplication::restoreOverrideCursor( ); - QMessageBox::information(this, QApplication::applicationName(), tr("Database successfully compacted.")); + VacuumDialog dialog(&db, this); + if(dialog.exec()) + { + populateStructure(); + resetBrowser(); } - db.open(db.curDBFilename); - populateStructure(); - resetBrowser(); } void MainWindow::deleteObject() diff --git a/src/VacuumDialog.cpp b/src/VacuumDialog.cpp new file mode 100644 index 00000000..c82d00fc --- /dev/null +++ b/src/VacuumDialog.cpp @@ -0,0 +1,63 @@ +#include "VacuumDialog.h" +#include "ui_VacuumDialog.h" +#include "sqlitedb.h" +#include + +VacuumDialog::VacuumDialog(DBBrowserDB* _db, QWidget* parent) : + QDialog(parent), + ui(new Ui::VacuumDialog), + db(_db) +{ + // Create UI + ui->setupUi(this); + + // Show warning if DB is dirty + ui->labelSavepointWarning->setVisible(db->getDirty()); + + // Populate list of objects to compact + QList objects = db->objMap.values("table"); + objects.append(db->objMap.values("index")); + for(QList::const_iterator i=objects.constBegin();i!=objects.constEnd();++i) + { + QTreeWidgetItem* item = new QTreeWidgetItem(ui->treeSelectedObjects); + item->setText(0, (*i).getname()); + item->setIcon(0, QIcon(QString(":icons/%1").arg((*i).gettype()))); + ui->treeSelectedObjects->addTopLevelItem(item); + } + + // Sort objects and select them all + ui->treeSelectedObjects->sortByColumn(0, Qt::AscendingOrder); + ui->treeSelectedObjects->selectAll(); +} + +VacuumDialog::~VacuumDialog() +{ + delete ui; +} + +void VacuumDialog::accept() +{ + if(ui->treeSelectedObjects->selectedItems().count() == 0) + QDialog::accept(); + + QApplication::setOverrideCursor(Qt::WaitCursor); + + // Commit all changes first + db->save(); + + // All items selected? + if(ui->treeSelectedObjects->selectedItems().count() == ui->treeSelectedObjects->topLevelItemCount()) + { + // Yes, so just execute a simple vacuum command for all objects + db->executeSQL("VACUUM;", false); + } else { + // No, so execute a vacuum command for each selected object individually + QList selection = ui->treeSelectedObjects->selectedItems(); + foreach(QTreeWidgetItem* item, selection) + db->executeSQL(QString("VACUUM `%1`;").arg(item->text(0)), false); + } + + db->setDirty(false); + QApplication::restoreOverrideCursor(); + QDialog::accept(); +} diff --git a/src/VacuumDialog.h b/src/VacuumDialog.h new file mode 100644 index 00000000..e5e03726 --- /dev/null +++ b/src/VacuumDialog.h @@ -0,0 +1,28 @@ +#ifndef __VACUUMDIALOG_H__ +#define __VACUUMDIALOG_H__ + +#include + +namespace Ui { +class VacuumDialog; +} + +class DBBrowserDB; + +class VacuumDialog : public QDialog +{ + Q_OBJECT + +public: + explicit VacuumDialog(DBBrowserDB* _db, QWidget* parent = 0); + ~VacuumDialog(); + +private: + Ui::VacuumDialog* ui; + DBBrowserDB* db; + +protected slots: + virtual void accept(); +}; + +#endif diff --git a/src/VacuumDialog.ui b/src/VacuumDialog.ui new file mode 100644 index 00000000..81f63b95 --- /dev/null +++ b/src/VacuumDialog.ui @@ -0,0 +1,127 @@ + + + VacuumDialog + + + + 0 + 0 + 475 + 439 + + + + Compact Database + + + + + + + 75 + true + + + + Warning: Compacting the database will commit all changes you made. + + + true + + + 5 + + + + + + + Please select the objects to compact: + + + treeSelectedObjects + + + + + + + QAbstractItemView::NoEditTriggers + + + false + + + QAbstractItemView::ExtendedSelection + + + false + + + false + + + false + + + false + + + + 1 + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + treeSelectedObjects + buttonBox + + + + + buttonBox + accepted() + VacuumDialog + accept() + + + 222 + 374 + + + 157 + 274 + + + + + buttonBox + rejected() + VacuumDialog + reject() + + + 290 + 380 + + + 286 + 274 + + + + + diff --git a/src/sqlitedb.cpp b/src/sqlitedb.cpp index e55bb983..4ff0a582 100644 --- a/src/sqlitedb.cpp +++ b/src/sqlitedb.cpp @@ -198,31 +198,6 @@ void DBBrowserDB::close (){ objMap.clear(); } -bool DBBrowserDB::compact ( ) -{ - char *errmsg; - bool ok=false; - - if (!isOpen()) return false; - - if (_db){ - save(); - logSQL(QString("VACUUM;"), kLogMsg_App); - if (SQLITE_OK==sqlite3_exec(_db,"VACUUM;", - NULL,NULL,&errmsg)){ - ok=true; - setDirty(false); - } - } - - if (!ok){ - lastErrorMessage = QString(errmsg); - return false; - }else{ - return true; - } -} - bool DBBrowserDB::dump(const QString& filename) { // Open file diff --git a/src/sqlitedb.h b/src/sqlitedb.h index c7b0ae9a..8496ec92 100644 --- a/src/sqlitedb.h +++ b/src/sqlitedb.h @@ -69,7 +69,6 @@ public: bool open ( const QString & db); bool create ( const QString & db); void close (); - bool compact (); bool setRestorePoint(const QString& pointname = "RESTOREPOINT"); bool save (const QString& pointname = "RESTOREPOINT"); bool revert (const QString& pointname = "RESTOREPOINT"); diff --git a/src/src.pro b/src/src.pro index 25dce698..5c3a6d60 100644 --- a/src/src.pro +++ b/src/src.pro @@ -44,7 +44,8 @@ HEADERS += \ sqlitetablemodel.h \ FilterTableHeader.h \ gen_version.h \ - SqlExecutionArea.h + SqlExecutionArea.h \ + VacuumDialog.h SOURCES += \ sqlitedb.cpp \ @@ -64,7 +65,8 @@ SOURCES += \ grammar/Sqlite3Parser.cpp \ sqlitetablemodel.cpp \ FilterTableHeader.cpp \ - SqlExecutionArea.cpp + SqlExecutionArea.cpp \ + VacuumDialog.cpp RESOURCES += icons/icons.qrc @@ -77,7 +79,8 @@ FORMS += \ EditDialog.ui \ ExportCsvDialog.ui \ ImportCsvDialog.ui \ - SqlExecutionArea.ui + SqlExecutionArea.ui \ + VacuumDialog.ui LIBPATH_QHEXEDIT=$$PWD/../libs/qhexedit LIBPATH_ANTLR=$$PWD/../libs/antlr-2.7.7