Skip to content

Commit 4dfabe4

Browse files
committed
VacuumDialog: Add dialog to allow compacting single objects individually
Add a new dialog which is shown when compacting the database. This dialog allows selecting single objects individually to avoid vacuuming the entire database. It also shows a new warning if the database is dirty as changes are going to be saved before vacuuming.
1 parent 6f8e988 commit 4dfabe4

7 files changed

Lines changed: 230 additions & 40 deletions

File tree

‎src/MainWindow.cpp‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "sqlitetablemodel.h"
2626
#include "FilterTableHeader.h"
2727
#include "SqlExecutionArea.h"
28+
#include "VacuumDialog.h"
2829

2930
MainWindow::MainWindow(QWidget* parent)
3031
: QMainWindow(parent),
@@ -530,18 +531,12 @@ void MainWindow::createIndex()
530531

531532
void MainWindow::compact()
532533
{
533-
QApplication::setOverrideCursor( Qt::WaitCursor );
534-
if (!db.compact()){
535-
QString error = tr("Error: could not compact the database file. Message from database engine: %1").arg(db.lastErrorMessage);
536-
QApplication::restoreOverrideCursor( );
537-
QMessageBox::warning( this, QApplication::applicationName(), error );
538-
} else {
539-
QApplication::restoreOverrideCursor( );
540-
QMessageBox::information(this, QApplication::applicationName(), tr("Database successfully compacted."));
534+
VacuumDialog dialog(&db, this);
535+
if(dialog.exec())
536+
{
537+
populateStructure();
538+
resetBrowser();
541539
}
542-
db.open(db.curDBFilename);
543-
populateStructure();
544-
resetBrowser();
545540
}
546541

547542
void MainWindow::deleteObject()

‎src/VacuumDialog.cpp‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "VacuumDialog.h"
2+
#include "ui_VacuumDialog.h"
3+
#include "sqlitedb.h"
4+
#include <QTreeWidgetItem>
5+
6+
VacuumDialog::VacuumDialog(DBBrowserDB* _db, QWidget* parent) :
7+
QDialog(parent),
8+
ui(new Ui::VacuumDialog),
9+
db(_db)
10+
{
11+
// Create UI
12+
ui->setupUi(this);
13+
14+
// Show warning if DB is dirty
15+
ui->labelSavepointWarning->setVisible(db->getDirty());
16+
17+
// Populate list of objects to compact
18+
QList<DBBrowserObject> objects = db->objMap.values("table");
19+
objects.append(db->objMap.values("index"));
20+
for(QList<DBBrowserObject>::const_iterator i=objects.constBegin();i!=objects.constEnd();++i)
21+
{
22+
QTreeWidgetItem* item = new QTreeWidgetItem(ui->treeSelectedObjects);
23+
item->setText(0, (*i).getname());
24+
item->setIcon(0, QIcon(QString(":icons/%1").arg((*i).gettype())));
25+
ui->treeSelectedObjects->addTopLevelItem(item);
26+
}
27+
28+
// Sort objects and select them all
29+
ui->treeSelectedObjects->sortByColumn(0, Qt::AscendingOrder);
30+
ui->treeSelectedObjects->selectAll();
31+
}
32+
33+
VacuumDialog::~VacuumDialog()
34+
{
35+
delete ui;
36+
}
37+
38+
void VacuumDialog::accept()
39+
{
40+
if(ui->treeSelectedObjects->selectedItems().count() == 0)
41+
QDialog::accept();
42+
43+
QApplication::setOverrideCursor(Qt::WaitCursor);
44+
45+
// Commit all changes first
46+
db->save();
47+
48+
// All items selected?
49+
if(ui->treeSelectedObjects->selectedItems().count() == ui->treeSelectedObjects->topLevelItemCount())
50+
{
51+
// Yes, so just execute a simple vacuum command for all objects
52+
db->executeSQL("VACUUM;", false);
53+
} else {
54+
// No, so execute a vacuum command for each selected object individually
55+
QList<QTreeWidgetItem*> selection = ui->treeSelectedObjects->selectedItems();
56+
foreach(QTreeWidgetItem* item, selection)
57+
db->executeSQL(QString("VACUUM `%1`;").arg(item->text(0)), false);
58+
}
59+
60+
db->setDirty(false);
61+
QApplication::restoreOverrideCursor();
62+
QDialog::accept();
63+
}

‎src/VacuumDialog.h‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef __VACUUMDIALOG_H__
2+
#define __VACUUMDIALOG_H__
3+
4+
#include <QDialog>
5+
6+
namespace Ui {
7+
class VacuumDialog;
8+
}
9+
10+
class DBBrowserDB;
11+
12+
class VacuumDialog : public QDialog
13+
{
14+
Q_OBJECT
15+
16+
public:
17+
explicit VacuumDialog(DBBrowserDB* _db, QWidget* parent = 0);
18+
~VacuumDialog();
19+
20+
private:
21+
Ui::VacuumDialog* ui;
22+
DBBrowserDB* db;
23+
24+
protected slots:
25+
virtual void accept();
26+
};
27+
28+
#endif

‎src/VacuumDialog.ui‎

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>VacuumDialog</class>
4+
<widget class="QDialog" name="VacuumDialog">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>475</width>
10+
<height>439</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>Compact Database</string>
15+
</property>
16+
<layout class="QVBoxLayout" name="verticalLayout">
17+
<item>
18+
<widget class="QLabel" name="labelSavepointWarning">
19+
<property name="font">
20+
<font>
21+
<weight>75</weight>
22+
<bold>true</bold>
23+
</font>
24+
</property>
25+
<property name="text">
26+
<string>Warning: Compacting the database will commit all changes you made.</string>
27+
</property>
28+
<property name="wordWrap">
29+
<bool>true</bool>
30+
</property>
31+
<property name="margin">
32+
<number>5</number>
33+
</property>
34+
</widget>
35+
</item>
36+
<item>
37+
<widget class="QLabel" name="label">
38+
<property name="text">
39+
<string>Please select the objects to compact:</string>
40+
</property>
41+
<property name="buddy">
42+
<cstring>treeSelectedObjects</cstring>
43+
</property>
44+
</widget>
45+
</item>
46+
<item>
47+
<widget class="QTreeWidget" name="treeSelectedObjects">
48+
<property name="editTriggers">
49+
<set>QAbstractItemView::NoEditTriggers</set>
50+
</property>
51+
<property name="showDropIndicator" stdset="0">
52+
<bool>false</bool>
53+
</property>
54+
<property name="selectionMode">
55+
<enum>QAbstractItemView::ExtendedSelection</enum>
56+
</property>
57+
<property name="rootIsDecorated">
58+
<bool>false</bool>
59+
</property>
60+
<property name="itemsExpandable">
61+
<bool>false</bool>
62+
</property>
63+
<property name="expandsOnDoubleClick">
64+
<bool>false</bool>
65+
</property>
66+
<attribute name="headerVisible">
67+
<bool>false</bool>
68+
</attribute>
69+
<column>
70+
<property name="text">
71+
<string notr="true">1</string>
72+
</property>
73+
</column>
74+
</widget>
75+
</item>
76+
<item>
77+
<widget class="QDialogButtonBox" name="buttonBox">
78+
<property name="orientation">
79+
<enum>Qt::Horizontal</enum>
80+
</property>
81+
<property name="standardButtons">
82+
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
83+
</property>
84+
</widget>
85+
</item>
86+
</layout>
87+
</widget>
88+
<tabstops>
89+
<tabstop>treeSelectedObjects</tabstop>
90+
<tabstop>buttonBox</tabstop>
91+
</tabstops>
92+
<resources/>
93+
<connections>
94+
<connection>
95+
<sender>buttonBox</sender>
96+
<signal>accepted()</signal>
97+
<receiver>VacuumDialog</receiver>
98+
<slot>accept()</slot>
99+
<hints>
100+
<hint type="sourcelabel">
101+
<x>222</x>
102+
<y>374</y>
103+
</hint>
104+
<hint type="destinationlabel">
105+
<x>157</x>
106+
<y>274</y>
107+
</hint>
108+
</hints>
109+
</connection>
110+
<connection>
111+
<sender>buttonBox</sender>
112+
<signal>rejected()</signal>
113+
<receiver>VacuumDialog</receiver>
114+
<slot>reject()</slot>
115+
<hints>
116+
<hint type="sourcelabel">
117+
<x>290</x>
118+
<y>380</y>
119+
</hint>
120+
<hint type="destinationlabel">
121+
<x>286</x>
122+
<y>274</y>
123+
</hint>
124+
</hints>
125+
</connection>
126+
</connections>
127+
</ui>

‎src/sqlitedb.cpp‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -198,31 +198,6 @@ void DBBrowserDB::close (){
198198
objMap.clear();
199199
}
200200

201-
bool DBBrowserDB::compact ( )
202-
{
203-
char *errmsg;
204-
bool ok=false;
205-
206-
if (!isOpen()) return false;
207-
208-
if (_db){
209-
save();
210-
logSQL(QString("VACUUM;"), kLogMsg_App);
211-
if (SQLITE_OK==sqlite3_exec(_db,"VACUUM;",
212-
NULL,NULL,&errmsg)){
213-
ok=true;
214-
setDirty(false);
215-
}
216-
}
217-
218-
if (!ok){
219-
lastErrorMessage = QString(errmsg);
220-
return false;
221-
}else{
222-
return true;
223-
}
224-
}
225-
226201
bool DBBrowserDB::dump(const QString& filename)
227202
{
228203
// Open file

‎src/sqlitedb.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ class DBBrowserDB
6969
bool open ( const QString & db);
7070
bool create ( const QString & db);
7171
void close ();
72-
bool compact ();
7372
bool setRestorePoint(const QString& pointname = "RESTOREPOINT");
7473
bool save (const QString& pointname = "RESTOREPOINT");
7574
bool revert (const QString& pointname = "RESTOREPOINT");

‎src/src.pro‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ HEADERS += \
4444
sqlitetablemodel.h \
4545
FilterTableHeader.h \
4646
gen_version.h \
47-
SqlExecutionArea.h
47+
SqlExecutionArea.h \
48+
VacuumDialog.h
4849

4950
SOURCES += \
5051
sqlitedb.cpp \
@@ -64,7 +65,8 @@ SOURCES += \
6465
grammar/Sqlite3Parser.cpp \
6566
sqlitetablemodel.cpp \
6667
FilterTableHeader.cpp \
67-
SqlExecutionArea.cpp
68+
SqlExecutionArea.cpp \
69+
VacuumDialog.cpp
6870

6971
RESOURCES += icons/icons.qrc
7072

@@ -77,7 +79,8 @@ FORMS += \
7779
EditDialog.ui \
7880
ExportCsvDialog.ui \
7981
ImportCsvDialog.ui \
80-
SqlExecutionArea.ui
82+
SqlExecutionArea.ui \
83+
VacuumDialog.ui
8184

8285
LIBPATH_QHEXEDIT=$$PWD/../libs/qhexedit
8386
LIBPATH_ANTLR=$$PWD/../libs/antlr-2.7.7

0 commit comments

Comments
 (0)