Skip to content

Commit 1f4e889

Browse files
author
Jim Hamill
committed
Lots of updates
1 parent 758e997 commit 1f4e889

16 files changed

+579
-158
lines changed

BrowseWidget.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ bool BrowseWidget::setTable(QString table)
6767

6868
// Neaten it up
6969
ui->tableView->setSortingEnabled(true);
70+
ui->tableView->sortByColumn(0, Qt::AscendingOrder);
7071
ui->tableView->setAlternatingRowColors(true);
7172

7273
return true;

DatabaseConnectionWidget.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44
#include <QUrl>
55
#include <QThread>
66
#include <QDebug>
7+
#include <QProgressDialog>
8+
#include <QFileDialog>
9+
#include <QMessageBox>
10+
#include <QSqlQuery>
711

812
#include "Extension/MySQLExtension.h"
13+
#include "Utilities/SQLSplitter.h"
914

1015
DatabaseConnectionWidget::DatabaseConnectionWidget(QWidget *parent) :
1116
QWidget(parent),
@@ -114,6 +119,57 @@ bool DatabaseConnectionWidget::connectToDatabase(QString name, QString driver, Q
114119
return true;
115120
}
116121

122+
bool DatabaseConnectionWidget::importDatabase()
123+
{
124+
// Show dialog and get filename
125+
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::homePath(), tr("SQL Files (*.sql)"));
126+
127+
// If no filename was specified, just return
128+
if (fileName.isEmpty()) {
129+
return false;
130+
}
131+
132+
// Get the query string
133+
QFile file(fileName);
134+
if (!file.open(QFile::ReadOnly | QFile::Text)) {
135+
return false;
136+
}
137+
138+
// Read string from file and feed into splitter
139+
QTextStream in(&file);
140+
SQLSplitter splitter(in.readAll());
141+
142+
// Create Progress Dialog
143+
QProgressDialog progress("Importing database...", "Abort", 0, splitter.getLength());
144+
progress.setWindowModality(Qt::WindowModal);
145+
progress.setValue(0);
146+
progress.show();
147+
148+
while (!splitter.atEnd()) {
149+
// Create query
150+
QSqlQuery query(m_database);
151+
152+
// Get next query
153+
QString statement = splitter.getNext();
154+
155+
// Execute the query string
156+
if (!query.exec(statement)) {
157+
qDebug() << "Error: " << statement;
158+
}
159+
160+
// Increment progress bar value
161+
progress.setValue(splitter.getPosition());
162+
}
163+
164+
// Let the user know the code has been run
165+
QMessageBox::information(this, "Operation Successful", "Database has been imported successfully.");
166+
167+
// Refresh the database
168+
refresh();
169+
170+
return true;
171+
}
172+
117173
Extension *DatabaseConnectionWidget::getExtension()
118174
{
119175
return m_extension;

DatabaseConnectionWidget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class DatabaseConnectionWidget : public QWidget
2828

2929
bool connectToDatabase(QString name, QString driver, QString host, QString database, QString username, QString password, int port = 0);
3030

31+
bool importDatabase();
32+
3133
QSqlError lastError();
3234

3335
Extension *getExtension();

Extension/MySQLExtension.cpp

Lines changed: 85 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,19 @@
99
#include <QProcess>
1010
#include <QMessageBox>
1111
#include <QInputDialog>
12+
#include <QProgressDialog>
1213
#include <QSqlQuery>
14+
#include <QSqlDriver>
1315
#include <QSqlError>
16+
#include <QSqlField>
17+
#include <QSqlRecord>
1418
#include <QDebug>
1519

20+
#define CREATE_TABLE_COLUMN 1
21+
1622
MySQLExtension::MySQLExtension(QObject *parent, QSqlDatabase *database) :
1723
Extension(parent, database)
1824
{
19-
// Backup actions
20-
m_backupAction = new QAction(QIcon::fromTheme("document-export"), QT_TR_NOOP("&Export..."), &m_toolsToolBar);
21-
m_backupAction->setStatusTip(QT_TR_NOOP("Export database"));
22-
connect(m_backupAction, SIGNAL(triggered()), &m_toolsWidget, SLOT(on_backupDatabaseButton_clicked()));
23-
24-
// Restore actions
25-
m_restoreAction = new QAction(QIcon::fromTheme("document-import"), QT_TR_NOOP("&Import..."), &m_toolsToolBar);
26-
m_restoreAction->setStatusTip(QT_TR_NOOP("Import Database"));
27-
connect(m_restoreAction, SIGNAL(triggered()), &m_toolsWidget, SLOT(on_restoreDatabaseButton_clicked()));
28-
29-
// Clear actions
30-
m_clearAction = new QAction(QIcon::fromTheme("edit-clear"), QT_TR_NOOP("&Clear..."), &m_toolsToolBar);
31-
m_clearAction->setStatusTip(QT_TR_NOOP("Clear Database"));
32-
connect(m_clearAction, SIGNAL(triggered()), &m_toolsWidget, SLOT(on_clearDatabaseButton_clicked()));
33-
34-
// Add actions to "Tools" toolbar
35-
m_toolsToolBar.addAction(m_backupAction);
36-
m_toolsToolBar.addAction(m_restoreAction);
37-
m_toolsToolBar.addAction(m_clearAction);
38-
39-
// Append "Tools" toolbar
40-
m_toolBars.append(&m_toolsToolBar);
41-
4225
// Create Tools Tab
4326
m_toolsWidget.setDatabase(m_database);
4427
m_toolsTab = new ExtensionTab(&m_toolsWidget, QIcon::fromTheme("applications-utilities"), "Tools");
@@ -51,14 +34,14 @@ MySQLExtension::MySQLExtension(QObject *parent, QSqlDatabase *database) :
5134
MySQLExtension::~MySQLExtension()
5235
{
5336
delete m_toolsTab;
54-
delete m_clearAction;
55-
delete m_backupAction;
56-
delete m_restoreAction;
5737
}
5838

5939
int MySQLExtension::hasCapability(Capability capability)
6040
{
6141
switch (capability) {
42+
case EXPORT_DATABASE: return true;
43+
case CLEAR_DATABASE: return true;
44+
6245
case ADD_TABLE: return true;
6346
case REMOVE_TABLE: return true;
6447
case RENAME_TABLE: return true;
@@ -71,80 +54,93 @@ int MySQLExtension::hasCapability(Capability capability)
7154
return false;
7255
}
7356

74-
int MySQLExtension::importDatabase()
57+
int MySQLExtension::exportDatabase()
7558
{
76-
/*// Show dialog and get filename
77-
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::homePath(), tr("SQL Files (*.sql)"));
59+
// Show dialog and get filename
60+
61+
QString fileName = QFileDialog::getSaveFileName(qApp->activeWindow(), tr("Save File"), QDir::homePath(), tr("SQL Files (*.sql)"));
7862

7963
// If no filename was specified, just return
8064
if (fileName.isEmpty()) {
81-
return;
65+
return false;
8266
}
8367

84-
// Create mysqldump proces and set file to save to
85-
QProcess mysql;
86-
mysql.setStandardInputFile(fileName);
87-
88-
// Compile Arguments
89-
QStringList arguments;
90-
arguments << "-h" << m_database->hostName() << "-P" << QString::number(m_database->port())
91-
<< "-u" << m_database->userName() << QString("-p" + m_database->password()) << m_database->databaseName();
92-
93-
// Execute dump command
94-
mysql.start("mysql", arguments);
95-
96-
// Wait until finished
97-
if (!mysql.waitForFinished(240000)) {
68+
// Setup file
69+
QFile outputFile(fileName);
70+
outputFile.open(QFile::WriteOnly | QFile::Text);
9871

99-
// Show message box if dump failed
100-
QMessageBox::critical(this, "Database Restore Failed", "Please ensure that you have mysql-client installed on this system.");
101-
102-
// Prevent further execution
103-
return;
72+
// Get number of records we'll need to insert (for the progress dialog) as they are what takes times
73+
int insertsCount = 0;
74+
foreach (QString table, m_database->tables()) {
75+
QSqlQuery tableSizeQuery("SELECT COUNT(*) FROM " + table, *m_database);
76+
tableSizeQuery.next();
77+
insertsCount += tableSizeQuery.record().value(0).toInt();
10478
}
10579

106-
// Let user know backup was successful
107-
QMessageBox::information(this, "Restore Successful", "Database has been successfully restored.");*/
108-
}
109-
110-
int MySQLExtension::exportDatabase()
111-
{
112-
/*// Show dialog and get filename
113-
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), QDir::homePath(), tr("SQL Files (*.sql)"));
114-
115-
// If no filename was specified, just return
116-
if (fileName.isEmpty()) {
117-
return;
80+
// Create Progress Dialog
81+
QProgressDialog progress("Dumping database...", "Abort", 0, insertsCount);
82+
progress.setWindowModality(Qt::WindowModal);
83+
progress.setValue(0);
84+
progress.show();
85+
86+
// Create text stream and about headers
87+
QTextStream outputStream(&outputFile);
88+
outputStream << "-- QuteSQL Dump - V" << qApp->applicationVersion() << endl;
89+
outputStream << "SET NAMES utf8;" << endl;
90+
outputStream << "SET time_zone = '+00:00';" << endl;
91+
outputStream << "SET foreign_key_checks = 0;" << endl;
92+
outputStream << "SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';" << endl << endl;
93+
94+
// Dump each table
95+
foreach (QString table, m_database->tables()) {
96+
97+
// If the cancel button was clicked, abort
98+
if (progress.wasCanceled())
99+
return false;
100+
101+
// Dump the create table statement
102+
QSqlQuery createTableQuery("SHOW CREATE TABLE " + table, *m_database);
103+
outputStream << "DROP TABLE IF EXISTS `" + table + "`;" << endl;
104+
createTableQuery.next();
105+
outputStream << createTableQuery.record().value(CREATE_TABLE_COLUMN).toString() + ";" << endl << endl;
106+
107+
// Dump the insert queries
108+
QSqlQuery selectTableQuery("SELECT * FROM " + table, *m_database);
109+
if (selectTableQuery.size()) {
110+
outputStream << "INSERT INTO `" + table + "` VALUES " << endl;
111+
while (selectTableQuery.next()) {
112+
QString row = "(";
113+
for (int column = 0; column < selectTableQuery.record().count(); column++) {
114+
row += m_database->driver()->formatValue(selectTableQuery.record().field(column)) + ",";
115+
}
116+
row.truncate(row.size()-1);
117+
row += "),";
118+
outputStream << row << endl;
119+
120+
// If the cancel button was clicked, abort
121+
progress.setValue(progress.value()+1);
122+
if (progress.wasCanceled())
123+
return false;
124+
125+
// Update our event loop so that progress dialog updates
126+
QCoreApplication::processEvents();
127+
}
128+
outputStream.seek(outputStream.pos()-2);
129+
outputStream << ";" << endl << endl;
130+
}
118131
}
119132

120-
// Create mysqldump proces and set file to save to
121-
QProcess mysqldump;
122-
mysqldump.setStandardOutputFile(fileName);
123-
124-
// Compile Arguments
125-
QStringList arguments;
126-
arguments << "-h" << m_database->hostName() << "-P" << QString::number(m_database->port())
127-
<< "-u" << m_database->userName() << QString("-p" + m_database->password()) << m_database->databaseName();
133+
// Close our output file
134+
outputFile.close();
128135

129-
// Execute dump command
130-
mysqldump.start("mysqldump", arguments);
136+
QMessageBox::information(qApp->activeWindow(), "Backup Successful", "Database has been successfully exported to:\n " + fileName);
131137

132-
// Wait until finished
133-
if (!mysqldump.waitForFinished(240000)) {
134-
135-
// Show message box if dump failed
136-
QMessageBox::critical(this, "Database Backup Failed", "Please ensure that you have mysqldump installed on this system.");
137-
138-
// Prevent further execution
139-
return;
140-
}
141-
142-
QMessageBox::information(this, "Backup Successful", "Database has been successfully backed up to:\n " + fileName);*/
138+
return true;
143139
}
144140

145141
int MySQLExtension::clearDatabase()
146142
{
147-
/*if (QMessageBox::Yes == QMessageBox::question(this, "Clear Database", "Are you sure you want to clear the database?\n\nThis action cannot be undone.")) {
143+
if (QMessageBox::Yes == QMessageBox::question(qApp->activeWindow(), "Clear Database", "Are you sure you want to clear the database?\n\nThis action cannot be undone.")) {
148144

149145
QSqlQuery query(*m_database);
150146

@@ -165,18 +161,20 @@ int MySQLExtension::clearDatabase()
165161
if (!query.exec(statements)) {
166162

167163
// Only show error if the box is ticked
168-
QMessageBox::critical(this, "Could not execute query", query.lastError().text());
164+
QMessageBox::critical(qApp->activeWindow(), "Could not execute query", query.lastError().text());
169165

170166
// Prevent further execution
171-
return;
167+
return false;
172168
}
173169

174170
// Let the user know it all went splendidly
175-
QMessageBox::information(this, "Clear Successful", "Database has been successfully cleared.");
171+
QMessageBox::information(qApp->activeWindow(), "Clear Successful", "Database has been successfully cleared.");
176172

177173
// Let the app know a refresh is needed
178174
emit refreshNeeded();
179-
}*/
175+
176+
return true;
177+
}
180178
}
181179

182180
int MySQLExtension::createTable(QString table)

Extension/MySQLExtension.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ Q_OBJECT
2020

2121
int hasCapability(Capability capability);
2222

23-
int importDatabase();
2423
int exportDatabase();
2524
int clearDatabase();
2625

@@ -38,10 +37,6 @@ Q_OBJECT
3837
ExtensionTab *m_toolsTab;
3938
QToolBar m_toolsToolBar;
4039
MySQLTools m_toolsWidget;
41-
42-
QAction *m_backupAction;
43-
QAction *m_restoreAction;
44-
QAction *m_clearAction;
4540
};
4641

4742
#endif // MYSQLEXTENSION_H

0 commit comments

Comments
 (0)