9
9
#include < QProcess>
10
10
#include < QMessageBox>
11
11
#include < QInputDialog>
12
+ #include < QProgressDialog>
12
13
#include < QSqlQuery>
14
+ #include < QSqlDriver>
13
15
#include < QSqlError>
16
+ #include < QSqlField>
17
+ #include < QSqlRecord>
14
18
#include < QDebug>
15
19
20
+ #define CREATE_TABLE_COLUMN 1
21
+
16
22
MySQLExtension::MySQLExtension (QObject *parent, QSqlDatabase *database) :
17
23
Extension(parent, database)
18
24
{
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
-
42
25
// Create Tools Tab
43
26
m_toolsWidget.setDatabase (m_database);
44
27
m_toolsTab = new ExtensionTab (&m_toolsWidget, QIcon::fromTheme (" applications-utilities" ), " Tools" );
@@ -51,14 +34,14 @@ MySQLExtension::MySQLExtension(QObject *parent, QSqlDatabase *database) :
51
34
MySQLExtension::~MySQLExtension ()
52
35
{
53
36
delete m_toolsTab;
54
- delete m_clearAction;
55
- delete m_backupAction;
56
- delete m_restoreAction;
57
37
}
58
38
59
39
int MySQLExtension::hasCapability (Capability capability)
60
40
{
61
41
switch (capability) {
42
+ case EXPORT_DATABASE: return true ;
43
+ case CLEAR_DATABASE: return true ;
44
+
62
45
case ADD_TABLE: return true ;
63
46
case REMOVE_TABLE: return true ;
64
47
case RENAME_TABLE: return true ;
@@ -71,80 +54,93 @@ int MySQLExtension::hasCapability(Capability capability)
71
54
return false ;
72
55
}
73
56
74
- int MySQLExtension::importDatabase ()
57
+ int MySQLExtension::exportDatabase ()
75
58
{
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)" ));
78
62
79
63
// If no filename was specified, just return
80
64
if (fileName.isEmpty ()) {
81
- return;
65
+ return false ;
82
66
}
83
67
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);
98
71
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 ();
104
78
}
105
79
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
+ }
118
131
}
119
132
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 ();
128
135
129
- // Execute dump command
130
- mysqldump.start("mysqldump", arguments);
136
+ QMessageBox::information (qApp->activeWindow (), " Backup Successful" , " Database has been successfully exported to:\n " + fileName);
131
137
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 ;
143
139
}
144
140
145
141
int MySQLExtension::clearDatabase ()
146
142
{
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\n This action cannot be undone." )) {
148
144
149
145
QSqlQuery query (*m_database);
150
146
@@ -165,18 +161,20 @@ int MySQLExtension::clearDatabase()
165
161
if (!query.exec (statements)) {
166
162
167
163
// 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 ());
169
165
170
166
// Prevent further execution
171
- return;
167
+ return false ;
172
168
}
173
169
174
170
// 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." );
176
172
177
173
// Let the app know a refresh is needed
178
174
emit refreshNeeded ();
179
- }*/
175
+
176
+ return true ;
177
+ }
180
178
}
181
179
182
180
int MySQLExtension::createTable (QString table)
0 commit comments