Skip to content

Commit 51b4554

Browse files
author
Jim Hamill
committed
Alter Column now works for MySQL + other updates
1 parent 46fcfb4 commit 51b4554

8 files changed

+118
-14
lines changed

DatabaseConnectionWidget.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <QSqlQuery>
1111

1212
#include "Extension/MySQLExtension.h"
13+
#include "Utilities/QCompressor.h"
1314
#include "Utilities/SQLSplitter.h"
1415

1516
DatabaseConnectionWidget::DatabaseConnectionWidget(QWidget *parent) :
@@ -122,7 +123,7 @@ bool DatabaseConnectionWidget::connectToDatabase(QString name, QString driver, Q
122123
bool DatabaseConnectionWidget::importDatabase()
123124
{
124125
// Show dialog and get filename
125-
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::homePath(), tr("SQL Files (*.sql)"));
126+
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::homePath(), tr("SQL Files (*.sql *gz)"));
126127

127128
// If no filename was specified, just return
128129
if (fileName.isEmpty()) {
@@ -135,9 +136,17 @@ bool DatabaseConnectionWidget::importDatabase()
135136
return false;
136137
}
137138

139+
// File data
140+
QByteArray fileData = file.readAll();
141+
142+
// Decompress if it is a gzip file
143+
QFileInfo fileInfo(fileName);
144+
if (fileInfo.suffix() == "gz") {
145+
fileData = QCompressor::gzDecompress(fileData);//if (!QCompressor::gzipDecompress(file.readAll(), fileData)) {
146+
}
147+
138148
// Read string from file and feed into splitter
139-
QTextStream in(&file);
140-
SQLSplitter splitter(in.readAll());
149+
SQLSplitter splitter(fileData);
141150

142151
// Create Progress Dialog
143152
QProgressDialog progress("Importing database...", "Abort", 0, splitter.getLength());

Extension/Extension.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ int Extension::removeColumn(QString table, QString column)
101101
return false;
102102
}
103103

104+
int Extension::alterColumn(QString table, QString oldName, QString newName, QString type, int length, bool nullable, QString defaultValue)
105+
{
106+
qDebug() << "Alter column not implemented.";
107+
108+
return false;
109+
}
110+
104111
void Extension::addRelations(QSqlRelationalTableModel *model)
105112
{
106113

Extension/Extension.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ enum Capability
3939
VIEW_SCHEMA = 21,
4040
ADD_COLUMN = 22,
4141
REMOVE_COLUMN = 23,
42-
EDIT_COLUMN = 24
42+
ALTER_COLUMN = 24
4343
};
4444

4545
enum SchemaColumn
@@ -56,7 +56,8 @@ enum ColumnFlags
5656
NO_PROPERTIES = 0,
5757
HAS_LENGTH = 1,
5858
HAS_NULLABLE = 2,
59-
HAS_DEFAULT = 4
59+
HAS_DEFAULT = 4,
60+
HAS_ENUM = 5
6061
};
6162

6263
class Extension : public QObject
@@ -86,6 +87,7 @@ Q_OBJECT
8687
virtual QMap<QString, int> getDataTypes();
8788
virtual int addColumn(QString table, QString name, QString type, int length = 0, bool nullable = false, QString defaultValue = "");
8889
virtual int removeColumn(QString table, QString column);
90+
virtual int alterColumn(QString table, QString oldName, QString newName, QString type, int length = 0, bool nullable = false, QString defaultValue = "");
8991

9092
virtual void addRelations(QSqlRelationalTableModel *model);
9193

Extension/MySQLExtension.cpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ int MySQLExtension::hasCapability(Capability capability)
4949
case VIEW_SCHEMA: return true;
5050
case ADD_COLUMN: return true;
5151
case REMOVE_COLUMN: return true;
52+
case ALTER_COLUMN: return true;
5253
}
5354

5455
return false;
@@ -107,14 +108,13 @@ int MySQLExtension::exportDatabase()
107108
// Dump the insert queries
108109
QSqlQuery selectTableQuery("SELECT * FROM " + table, *m_database);
109110
if (selectTableQuery.size()) {
110-
outputStream << "INSERT INTO `" + table + "` VALUES " << endl;
111111
while (selectTableQuery.next()) {
112-
QString row = "(";
112+
QString row = "INSERT INTO `" + table + "` VALUES (";
113113
for (int column = 0; column < selectTableQuery.record().count(); column++) {
114114
row += m_database->driver()->formatValue(selectTableQuery.record().field(column)) + ",";
115115
}
116116
row.truncate(row.size()-1);
117-
row += "),";
117+
row += ");";
118118
outputStream << row << endl;
119119

120120
// If the cancel button was clicked, abort
@@ -125,8 +125,6 @@ int MySQLExtension::exportDatabase()
125125
// Update our event loop so that progress dialog updates
126126
QCoreApplication::processEvents();
127127
}
128-
outputStream.seek(outputStream.pos()-2);
129-
outputStream << ";" << endl << endl;
130128
}
131129
}
132130

@@ -278,6 +276,34 @@ int MySQLExtension::removeColumn(QString table, QString column)
278276
return true;
279277
}
280278

279+
int MySQLExtension::alterColumn(QString table, QString oldName, QString newName, QString type, int length, bool nullable, QString defaultValue)
280+
{
281+
QString queryString = "ALTER TABLE " + table + " CHANGE " + oldName + " " + newName;
282+
283+
// If there's a length, embed that in type
284+
if (length) {
285+
type = type + "(" + QString::number(length) + ")";
286+
}
287+
288+
// Add type
289+
queryString += " " + type;
290+
291+
// If nullable is set, add that
292+
if (!nullable) {
293+
queryString += " NOT NULL";
294+
}
295+
296+
// if default value is set, add that
297+
if (defaultValue.length()) {
298+
queryString += " DEFAULT '" + defaultValue + "'";
299+
}
300+
301+
// Run the add column query
302+
QSqlQuery query = m_database->exec(queryString);
303+
304+
return true;
305+
}
306+
281307
SchemaWidget* MySQLExtension::createSchemaWidget(QWidget *parent)
282308
{
283309
return new MySQLSchemaWidget(parent, m_database);

Extension/MySQLExtension.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Q_OBJECT
3232
QMap<QString, int> getDataTypes();
3333
int addColumn(QString table, QString name, QString type, int length = 0, bool nullable = false, QString defaultValue = "");
3434
int removeColumn(QString table, QString column);
35+
int alterColumn(QString table, QString oldName, QString newName, QString type, int length = 0, bool nullable = false, QString defaultValue = "");
3536

3637
private:
3738
ExtensionTab *m_toolsTab;

SchemaWidget.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ void SchemaWidget::removeColumn()
110110

111111
void SchemaWidget::on_tableView_activated(const QModelIndex &index)
112112
{
113+
// Only open dialog if supports altering column
114+
if (!m_extension->hasCapability(ALTER_COLUMN)) {
115+
return;
116+
}
117+
113118
// Get column name
114119
QModelIndex nameIndex = ui->tableView->model()->index(ui->tableView->currentIndex().row(), m_extension->getSchemaColumn(NAME));
115120
QString name = ui->tableView->model()->data(nameIndex).toString();
@@ -129,7 +134,7 @@ void SchemaWidget::on_tableView_activated(const QModelIndex &index)
129134

130135
// Get nullable
131136
QModelIndex nullableIndex = ui->tableView->model()->index(ui->tableView->currentIndex().row(), m_extension->getSchemaColumn(NULLABLE));
132-
bool nullable = ui->tableView->model()->data(nullableIndex).toBool();
137+
bool nullable = (ui->tableView->model()->data(nullableIndex).toString() == "YES") ? true : false;
133138

134139
// Get default value
135140
QModelIndex defaultValueIndex = ui->tableView->model()->index(ui->tableView->currentIndex().row(), m_extension->getSchemaColumn(DEFAULT_VALUE));
@@ -146,7 +151,7 @@ void SchemaWidget::on_tableView_activated(const QModelIndex &index)
146151
columnDialog.exec();
147152

148153
// Actually add the column
149-
m_extension->addColumn(m_tableName, columnDialog.getName(), columnDialog.getType(), columnDialog.getLength(),
154+
m_extension->alterColumn(m_tableName, name, columnDialog.getName(), columnDialog.getType(), columnDialog.getLength(),
150155
columnDialog.getNullable(), columnDialog.getDefault());
151156

152157
// Refresh schema

Utilities/QCompressor.cpp

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "QCompressor.h"
22

3+
#include <QDebug>
4+
35
/**
46
* @brief Compresses the given buffer using the standard GZIP algorithm
57
* @param input The buffer to be compressed
@@ -169,7 +171,7 @@ bool QCompressor::gzipDecompress(QByteArray input, QByteArray &output)
169171
case Z_STREAM_ERROR:
170172
// Clean-up
171173
inflateEnd(&strm);
172-
174+
qDebug() << "ZStreamError";
173175
// Return
174176
return(false);
175177
}
@@ -194,3 +196,53 @@ bool QCompressor::gzipDecompress(QByteArray input, QByteArray &output)
194196
else
195197
return(true);
196198
}
199+
200+
QByteArray QCompressor::gzDecompress(QByteArray &data)
201+
{
202+
if (data.size() <= 4) {
203+
qWarning("gUncompress: Input data is truncated");
204+
return QByteArray();
205+
}
206+
207+
QByteArray result;
208+
209+
int ret;
210+
z_stream strm;
211+
static const int CHUNK_SIZE = 1024;
212+
char out[CHUNK_SIZE];
213+
214+
/* allocate inflate state */
215+
strm.zalloc = Z_NULL;
216+
strm.zfree = Z_NULL;
217+
strm.opaque = Z_NULL;
218+
strm.avail_in = data.size();
219+
strm.next_in = (Bytef*)(data.data());
220+
221+
ret = inflateInit2(&strm, 15 + 32); // gzip decoding
222+
if (ret != Z_OK)
223+
return QByteArray();
224+
225+
// run inflate()
226+
do {
227+
strm.avail_out = CHUNK_SIZE;
228+
strm.next_out = (Bytef*)(out);
229+
230+
ret = inflate(&strm, Z_NO_FLUSH);
231+
Q_ASSERT(ret != Z_STREAM_ERROR); // state not clobbered
232+
233+
switch (ret) {
234+
case Z_NEED_DICT:
235+
ret = Z_DATA_ERROR; // and fall through
236+
case Z_DATA_ERROR:
237+
case Z_MEM_ERROR:
238+
(void)inflateEnd(&strm);
239+
return QByteArray();
240+
}
241+
242+
result.append(out, CHUNK_SIZE - strm.avail_out);
243+
} while (strm.avail_out == 0);
244+
245+
// clean up and return
246+
inflateEnd(&strm);
247+
return result;
248+
}

Utilities/QCompressor.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class QCompressor
1212
public:
1313
static bool gzipCompress(QByteArray input, QByteArray &output, int level = -1);
1414
static bool gzipDecompress(QByteArray input, QByteArray &output);
15+
16+
static QByteArray gzDecompress(QByteArray &data);
1517
};
1618

17-
#endif // QCOMPRESSOR_H
19+
#endif // QCOMPRESSOR_H

0 commit comments

Comments
 (0)