Skip to content

Commit 758e997

Browse files
author
Jim Hamill
committed
SQL Splitter Dialog added
1 parent a0663c7 commit 758e997

11 files changed

+313
-35
lines changed

MainWindow.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "OpenConnectionDialog.h"
1313
#include "DatabaseConnectionWidget.h"
14+
#include "Tools/SQLSplitterDialog.h"
1415

1516
MainWindow::MainWindow(QWidget *parent) :
1617
QMainWindow(parent),
@@ -177,3 +178,9 @@ void MainWindow::on_actionRefresh_triggered()
177178
m_currentDatabase->refresh();
178179
}
179180
}
181+
182+
void MainWindow::on_actionSplit_SQL_File_triggered()
183+
{
184+
SQLSplitterDialog dialog;
185+
dialog.exec();
186+
}

MainWindow.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ private slots:
3434

3535
void on_actionRefresh_triggered();
3636

37+
void on_actionSplit_SQL_File_triggered();
38+
3739
signals:
3840
void statusEvent(const QString&);
3941

MainWindow.ui

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
<string>QuteSQL</string>
1515
</property>
1616
<property name="windowIcon">
17-
<iconset theme="server-database"/>
17+
<iconset theme="server-database">
18+
<normaloff/>
19+
</iconset>
1820
</property>
1921
<widget class="QWidget" name="centralWidget">
2022
<property name="sizePolicy">
@@ -103,6 +105,8 @@
103105
<property name="title">
104106
<string>Tools</string>
105107
</property>
108+
<addaction name="separator"/>
109+
<addaction name="actionSplit_SQL_File"/>
106110
</widget>
107111
<widget class="QMenu" name="menuHelp">
108112
<property name="title">
@@ -141,7 +145,9 @@
141145
</widget>
142146
<action name="actionNew_Connection">
143147
<property name="icon">
144-
<iconset theme="connect"/>
148+
<iconset theme="connect">
149+
<normaloff/>
150+
</iconset>
145151
</property>
146152
<property name="text">
147153
<string>New Connection</string>
@@ -152,15 +158,19 @@
152158
</action>
153159
<action name="actionClose_Connection">
154160
<property name="icon">
155-
<iconset theme="disconnect"/>
161+
<iconset theme="disconnect">
162+
<normaloff/>
163+
</iconset>
156164
</property>
157165
<property name="text">
158166
<string>Close Connection</string>
159167
</property>
160168
</action>
161169
<action name="actionQuit">
162170
<property name="icon">
163-
<iconset theme="application-exit"/>
171+
<iconset theme="application-exit">
172+
<normaloff/>
173+
</iconset>
164174
</property>
165175
<property name="text">
166176
<string>Quit</string>
@@ -191,7 +201,9 @@
191201
</action>
192202
<action name="actionRefresh">
193203
<property name="icon">
194-
<iconset theme="view-refresh"/>
204+
<iconset theme="view-refresh">
205+
<normaloff/>
206+
</iconset>
195207
</property>
196208
<property name="text">
197209
<string>Refresh</string>
@@ -200,6 +212,14 @@
200212
<string>Refresh the current database</string>
201213
</property>
202214
</action>
215+
<action name="actionSplit_SQL_File">
216+
<property name="icon">
217+
<iconset theme="edit-table-cell-split"/>
218+
</property>
219+
<property name="text">
220+
<string>Split SQL File</string>
221+
</property>
222+
</action>
203223
</widget>
204224
<layoutdefault spacing="6" margin="11"/>
205225
<resources/>

QuteSQL.pro

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ SOURCES += main.cpp\
3131
SavedQueryWidget.cpp \
3232
ColumnDialog.cpp \
3333
Database.cpp \
34-
Utilities/SQLSplitter.cpp
34+
Utilities/SQLSplitter.cpp \
35+
Tools/SQLSplitterDialog.cpp
3536

3637
HEADERS += MainWindow.h \
3738
DatabaseConnectionWidget.h \
@@ -51,7 +52,8 @@ HEADERS += MainWindow.h \
5152
SavedQueryWidget.h \
5253
ColumnDialog.h \
5354
Database.h \
54-
Utilities/SQLSplitter.h
55+
Utilities/SQLSplitter.h \
56+
Tools/SQLSplitterDialog.h
5557

5658
FORMS += MainWindow.ui \
5759
DatabaseConnectionWidget.ui \
@@ -65,7 +67,8 @@ FORMS += MainWindow.ui \
6567
ExportSelectionDialog.ui \
6668
QueryHistoryWidget.ui \
6769
SavedQueryWidget.ui \
68-
ColumnDialog.ui
70+
ColumnDialog.ui \
71+
Tools/SQLSplitterDialog.ui
6972

7073
target.path = /usr/bin
7174

QuteSQL.pro.user

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 3.0.1, 2015-07-23T12:15:23. -->
3+
<!-- Written by QtCreator 3.0.1, 2015-07-23T13:11:03. -->
44
<qtcreator>
55
<data>
66
<variable>ProjectExplorer.Project.ActiveTarget</variable>

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,23 @@
11
# QuteSQL
2-
A Qt-based SQL Editor and Explorer
2+
3+
## Todo
4+
5+
- Create wrapper QSqlDatabase class (with functions like executeQuery(), getName(), etc)
6+
- Create log tab that shows history of all SQL commands executed.
7+
- Store settings in SQLite Database (Storing using QSettings is inconvenient with arrays.)
8+
- Export selected
9+
- SQL Splitter
10+
11+
## SQLite Database Schema
12+
13+
databases
14+
id | host | port | username | password | ssh_hostname | ssh_port
15+
16+
queries
17+
id | database_id | type (SAVED/HISTORY) | created_at | query
18+
19+
relations
20+
id | database_id | table | column | foreign_table | foreign_column | display_column*
21+
22+
* This field is important and is the reason we need to store this in the database (as opposed to detecting it each time upon opening the database)
23+

SqlWidget.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void SqlWidget::setDatabase(QSqlDatabase database)
3333
void SqlWidget::on_loadFromFileButton_clicked()
3434
{
3535
// Show dialog and get filename
36-
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "/home/jana", tr("SQL Files (*.sql)"));
36+
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::homePath(), tr("SQL Files (*.sql)"));
3737

3838
// If no filename was specified, just return
3939
if (fileName.isEmpty()) {
@@ -74,7 +74,7 @@ void SqlWidget::on_runSqlButton_clicked()
7474
if (!query.exec(statement)) {
7575

7676
// Always show debug message
77-
//qDebug() << "Error: " << statement;
77+
qDebug() << "Error: " << statement;
7878

7979
// Only show error if the box is ticked
8080
if (ui->showErrorsCheckBox->isChecked()) {

Tools/SQLSplitterDialog.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include "SQLSplitterDialog.h"
2+
#include "ui_SQLSplitterDialog.h"
3+
4+
#include <QFileDialog>
5+
#include <QString>
6+
#include <QMessageBox>
7+
8+
#include "Utilities/SQLSplitter.h"
9+
10+
SQLSplitterDialog::SQLSplitterDialog(QWidget *parent) :
11+
QDialog(parent),
12+
ui(new Ui::SQLSplitterDialog)
13+
{
14+
ui->setupUi(this);
15+
}
16+
17+
SQLSplitterDialog::~SQLSplitterDialog()
18+
{
19+
delete ui;
20+
}
21+
22+
void SQLSplitterDialog::on_inputFileButton_clicked()
23+
{
24+
// Show dialog and get filename
25+
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::homePath(), tr("SQL Files (*.sql)"));
26+
27+
// Set the input box filename
28+
ui->inputFileEdit->setText(fileName);
29+
}
30+
31+
void SQLSplitterDialog::on_outputFileButton_clicked()
32+
{
33+
// Show dialog and get filename
34+
QString fileName = QFileDialog::getSaveFileName(this, tr("Open File"), QDir::homePath(), tr("SQL Files (*.sql)"));
35+
36+
// Set the input box filename
37+
ui->outputFileEdit->setText(fileName);
38+
}
39+
40+
void SQLSplitterDialog::on_buttonBox_accepted()
41+
{
42+
// Setup files and variables
43+
int part = 1;
44+
int maxSize = ui->sizeSpinBox->text().toInt() * 1000;
45+
QFile inputFile(ui->inputFileEdit->text());
46+
QFile outputFile(ui->outputFileEdit->text() + "." + QString::number(part));
47+
48+
// If the file exists, open it
49+
if (inputFile.open(QFile::ReadOnly | QFile::Text) && outputFile.open(QFile::WriteOnly | QFile::Text)) {
50+
SQLSplitter splitter(inputFile.readAll());
51+
52+
// Split the statements until the end
53+
while (!splitter.atEnd()) {
54+
QString nextStatement = splitter.getNext();
55+
56+
// If the next statement is bigger than the max permitted size, abort!
57+
if (nextStatement.length() > maxSize) {
58+
QMessageBox::critical(this, "Unable to split file", "A statement in this file is larger than the permitted max file size.");
59+
inputFile.close();
60+
outputFile.close();
61+
return;
62+
}
63+
64+
// Check if we need to start a new file
65+
if (outputFile.size() + nextStatement.length() > maxSize) {
66+
part++;
67+
outputFile.close();
68+
outputFile.setFileName(ui->outputFileEdit->text() + "." + QString::number(part));
69+
outputFile.open(QFile::WriteOnly | QFile::Text);
70+
}
71+
72+
// Write the statement to the file
73+
outputFile.write(nextStatement.toUtf8());
74+
}
75+
76+
// Close our files
77+
inputFile.close();
78+
outputFile.close();
79+
80+
QMessageBox::information(this, "Operation Successful", "File has been successfully split.");
81+
}
82+
}

Tools/SQLSplitterDialog.h

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

0 commit comments

Comments
 (0)