-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatabaseConnectionWidget.cpp
More file actions
209 lines (160 loc) · 5.65 KB
/
DatabaseConnectionWidget.cpp
File metadata and controls
209 lines (160 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "DatabaseConnectionWidget.h"
#include "ui_DatabaseConnectionWidget.h"
#include <QUrl>
#include <QThread>
#include <QDebug>
#include <QProgressDialog>
#include <QFileDialog>
#include <QMessageBox>
#include <QSqlQuery>
#include "Extension/MySQLExtension.h"
#include "Utilities/QCompressor.h"
#include "Utilities/SQLSplitter.h"
DatabaseConnectionWidget::DatabaseConnectionWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::DatabaseConnectionWidget)
{
m_extension = NULL;
ui->setupUi(this);
}
DatabaseConnectionWidget::~DatabaseConnectionWidget()
{
// Close the ssh tunnel
m_sshTunnel.terminate();
// Close this connection
m_database.close();
// Remove the actual database (is this necessary if I close it?)
QSqlDatabase::removeDatabase(m_database.connectionName());
delete m_extension;
delete ui;
}
QSqlDatabase DatabaseConnectionWidget::database()
{
return m_database;
}
QString DatabaseConnectionWidget::name()
{
return m_database.connectionName();
}
QSqlError DatabaseConnectionWidget::lastError()
{
return m_database.lastError();
}
bool DatabaseConnectionWidget::createSshTunnel(QString hostname, int remotePort, int forwardedPort, int sshPort)
{
// Compile Arguments (of the form: -fNg -L 16111:127.0.0.1:3306 user@www.hostname.com
QStringList arguments;
arguments << "-fNg" << "-L" << QString(QString::number(forwardedPort) + ":" + "127.0.0.1" + ":" + QString::number(remotePort)) << QString(hostname) << "-p" << QString::number(sshPort);
// Execute SSH Command
m_sshTunnel.start("ssh", arguments);
// Wait until finished
if (!m_sshTunnel.waitForFinished(30000)) {
return false;
}
// Print useful information
qDebug() << QString("SSH tunnel binded to 127.0.0.1:" + QString::number(forwardedPort));
return true;
}
bool DatabaseConnectionWidget::connectToDatabase(QString name, QString driver, QString host, QString database, QString username, QString password, int port)
{
m_database = QSqlDatabase::addDatabase(driver, name);
m_database.setHostName(host);
m_database.setDatabaseName(database);
m_database.setUserName(username);
m_database.setPassword(password);
m_database.setPort(port);
// Attempt to connect
if (!m_database.open()) {
return false;
}
// If there's extra support for this driver, add it
if (driver == "QMYSQL" || driver == "QMYSQL3") {
// Create the extension (adds Tools widget, etc)
m_extension = new MySQLExtension(this, &m_database);
connect(m_extension, SIGNAL(refreshNeeded()), this, SLOT(refresh()));
}
// Setup the extensions if they exist
if (m_extension) {
// Add each tab provided by this extension
QList<ExtensionTab*> tabs = m_extension->getTabs();
QList<ExtensionTab*>::iterator i;
for (i = tabs.begin(); i != tabs.end(); ++i) {
ui->tabWidget->addTab((*i)->getWidget(), (*i)->getIcon(), (*i)->getLabel());
}
}
// Setup Explorer Widget
ui->explorerTab->init(&m_database, m_extension);
// Setup Query Widget
ui->queryTab->setDatabase(&m_database);
connect(ui->queryTab, SIGNAL(refreshNeeded()), this, SLOT(refresh()));
// Setup SQL Widget
ui->sqlTab->setDatabase(m_database);
connect(ui->sqlTab, SIGNAL(refreshNeeded()), this, SLOT(refresh()));
return true;
}
bool DatabaseConnectionWidget::importDatabase()
{
// Show dialog and get filename
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::homePath(), tr("SQL Files (*.sql *gz)"));
// If no filename was specified, just return
if (fileName.isEmpty()) {
return false;
}
// Get the query string
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
return false;
}
// File data
//QByteArray fileData = file.readAll();
QByteArray decompressed;
// Decompress if it is a gzip file
QFileInfo fileInfo(fileName);
if (fileInfo.suffix() == "gz") {
/*if (!QCompressor::gzipDecompress(fileData, decompressed)) {
return false;
}*/
//decompressed = QCompressor::gzDecompress(fileData);
//qDebug() << decompressed;
// Let the user know that this is broken
QMessageBox::warning(this, "Operation Failed", "Compressed databases are not supported at this time.");
return false;
}
// Read string from file and feed into splitter
SQLSplitter splitter(&file);
// Create Progress Dialog
QProgressDialog progress("Importing database...", "Abort", 0, splitter.getLength());
progress.setWindowModality(Qt::ApplicationModal);
progress.setValue(0);
progress.show();
while (!splitter.atEnd()) {
// Create query
QSqlQuery query(m_database);
// Get next query
QString statement = splitter.getNext();
// Execute the query string
if (!query.exec(statement)) {
qDebug() << "Error: " << statement;
}
// If the cancel button was clicked, abort
if (progress.wasCanceled())
return false;
// Increment progress bar value
progress.setValue(splitter.getPosition());
// Update our event loop so that progress dialog updates
QCoreApplication::processEvents();
}
// Let the user know the code has been run
QMessageBox::information(this, "Operation Successful", "Database has been imported successfully.");
// Refresh the database
refresh();
return true;
}
Extension *DatabaseConnectionWidget::getExtension()
{
return m_extension;
}
void DatabaseConnectionWidget::refresh()
{
ui->explorerTab->refresh();
}