-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSchemaWidget.cpp
More file actions
171 lines (133 loc) · 5.09 KB
/
SchemaWidget.cpp
File metadata and controls
171 lines (133 loc) · 5.09 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
#include "SchemaWidget.h"
#include "ui_SchemaWidget.h"
#include <QMessageBox>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QDebug>
#include "ColumnDialog.h"
SchemaWidget::SchemaWidget(QWidget *parent, QSqlDatabase *database, Extension *extension) :
QWidget(parent),
ui(new Ui::SchemaWidget)
{
ui->setupUi(this);
// Set the database
m_database = database;
// Set the extension
m_extension = extension;
connect(ui->addColumnButton, SIGNAL(clicked()), this, SLOT(addColumn()));
connect(ui->removeColumnButton, SIGNAL(clicked()), this, SLOT(removeColumn()));
}
SchemaWidget::~SchemaWidget()
{
delete ui;
}
void SchemaWidget::init()
{
// Setup the model
m_model = new QSqlQueryModel(this);
// Hide add column if not supported
if (!m_extension || !m_extension->hasCapability(ADD_COLUMN)) {
ui->addColumnButton->setVisible(false);
}
// Hide remove column if not supported
if (!m_extension || !m_extension->hasCapability(REMOVE_COLUMN)) {
ui->removeColumnButton->setVisible(false);
}
}
bool SchemaWidget::setTable(QString table)
{
// Set current table name
m_tableName = table;
// Get the schema
m_model->setQuery(m_extension->viewSchemaQuery(table), *m_database);
// Set the query from the query input box
if (m_database->driverName() == "QSQLITE") {
m_model->setQuery("PRAGMA table_info(" + table + ")", *m_database);
}
// Attach the result table to the model
ui->tableView->setEnabled(true);
ui->tableView->setModel(m_model);
return true;
}
void SchemaWidget::refresh()
{
setTable(m_tableName);
}
void SchemaWidget::addColumn()
{
// Show add column dialog
ColumnDialog columnDialog;
columnDialog.setDataTypes(m_extension->getDataTypes());
columnDialog.exec();
// Actually add the column
m_extension->addColumn(m_tableName, columnDialog.getName(), columnDialog.getType(), columnDialog.getLength(),
columnDialog.getNullable(), columnDialog.getDefault());
// Refresh schema
refresh();
// Emit refresh needed
emit refreshNeeded();
}
void SchemaWidget::removeColumn()
{
// Display messagebox for confirmation
if (QMessageBox::Yes == QMessageBox::question(this, "Drop Column", "Are you sure you want to remove this column?")) {
// Get column name
QModelIndex index = ui->tableView->model()->index(ui->tableView->currentIndex().row(), m_extension->getSchemaColumn(NAME));
QString column = ui->tableView->model()->data(index).toString();
// Drop column
m_extension->removeColumn(m_tableName, column);
// Refresh schema
refresh();
// Emit refresh needed
emit refreshNeeded();
}
}
void SchemaWidget::on_tableView_activated(const QModelIndex &index)
{
// Only open dialog if supports altering column
if (!m_extension->hasCapability(ALTER_COLUMN)) {
return;
}
// Get column name
QModelIndex nameIndex = ui->tableView->model()->index(ui->tableView->currentIndex().row(), m_extension->getSchemaColumn(NAME));
QString name = ui->tableView->model()->data(nameIndex).toString();
// Get column type
QModelIndex typeIndex = ui->tableView->model()->index(ui->tableView->currentIndex().row(), m_extension->getSchemaColumn(TYPE));
QString type = ui->tableView->model()->data(typeIndex).toString();
qDebug() << type;
// Extract parameters from type
QRegularExpression bracketsExp("/\((.*?)\)/");
QString parameters = bracketsExp.match(type).captured();
qDebug() << parameters;
// Remove parameters from type
//type.remove()
// Extract length from type
QRegularExpression lengthExp("[0-9]+");
int length = lengthExp.match(type).captured().toInt();
qDebug() << length;
// Remove digits and other characters so that only type remains
type.remove(QRegularExpression("[^A-Za-z ]"));
qDebug() << type;
// Get nullable
QModelIndex nullableIndex = ui->tableView->model()->index(ui->tableView->currentIndex().row(), m_extension->getSchemaColumn(NULLABLE));
bool nullable = (ui->tableView->model()->data(nullableIndex).toString() == "YES") ? true : false;
// Get default value
QModelIndex defaultValueIndex = ui->tableView->model()->index(ui->tableView->currentIndex().row(), m_extension->getSchemaColumn(DEFAULT_VALUE));
QString defaultValue = ui->tableView->model()->data(defaultValueIndex).toString();
// Show column dialog
ColumnDialog columnDialog;
columnDialog.setDataTypes(m_extension->getDataTypes());
columnDialog.setName(name);
columnDialog.setType(type);
columnDialog.setLength(length);
columnDialog.setNullable(nullable);
columnDialog.setDefaultValue(defaultValue);
columnDialog.exec();
// Actually add the column
m_extension->alterColumn(m_tableName, name, columnDialog.getName(), columnDialog.getType(), columnDialog.getLength(),
columnDialog.getNullable(), columnDialog.getDefault());
// Refresh schema
refresh();
// Emit refresh needed
emit refreshNeeded();
}