-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqleditor.cpp
246 lines (196 loc) · 7.12 KB
/
sqleditor.cpp
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <QtSql>
#include <QtGui>
#include "sqleditor.h"
#include "ui_sqleditor.h"
#include "simplesqlparser.h"
void sqleditor::updateTitle() {
this->setWindowTitle((isUnsaved ? QString("*") : QString("")) + queryName);
}
sqleditor::sqleditor(QWidget *parent, QString name, QString sql, bool is_new) :
QSplitter(parent),
queryName(name),
originalSql(sql),
isNew(is_new),
ui(new Ui::sqleditor)
{
ui->setupUi(this);
connect(parent, SIGNAL(querySaved(QString,QString)),this, SLOT(_on_querySaved(QString,QString)));
ui->statusLine->setText(tr("Ready."));
ui->sqlEdit->setText(sql);
isUnsaved = false;
updateTitle();
}
sqleditor::~sqleditor()
{
delete ui;
}
void sqleditor::exec()
{
QSqlQueryModel *model = new QSqlQueryModel(ui->table);
model->setQuery(QSqlQuery(ui->sqlEdit->toPlainText(), QSqlDatabase::database(CONNECTION_NAME)));
ui->table->setModel(model);
if (model->lastError().type() != QSqlError::NoError)
ui->statusLine->setText(model->lastError().text());
else if (model->query().isSelect()) {
SimpleSqlParser p;
p.parse(ui->sqlEdit->toPlainText());
ui->statusLine->setText(tr("Query might have issues."));
if(p.detected_error_unaggregated_args())
QMessageBox::critical(this, tr("ERROR!"), tr("detected selection of un-aggregated column select in an aggregated query!\nIn an aggregated query you should select only aggregated columns or use aggregation functions"));
else if(p.detected_error_select_nested_in_select())
QMessageBox::critical(this, tr("ERROR!"), tr("Nested select query in the select line!"));
else if(p.detected_error_nested_call_to_aggregated_func())
QMessageBox::critical(this, tr("ERROR!"), tr("Cannot nest aggregated function calls!"));
else ui->statusLine->setText(tr("Query OK."));
} else
ui->statusLine->setText(tr("Query OK, number of affected rows: %1").arg(model->query().numRowsAffected()));
updateActions();
emit sqlQueryExecuted();
}
void sqleditor::updateActions()
{
}
QString sqleditor::getQueryName() {
return queryName;
}
void sqleditor::insertRow()
{
QSqlTableModel *model = qobject_cast<QSqlTableModel *>(ui->table->model());
if (!model)
return;
QModelIndex insertIndex = ui->table->currentIndex();
int row = insertIndex.row() == -1 ? 0 : insertIndex.row();
model->insertRow(row);
insertIndex = model->index(row, 0);
ui->table->setCurrentIndex(insertIndex);
ui->table->edit(insertIndex);
}
void sqleditor::deleteRow()
{
QSqlTableModel *model = qobject_cast<QSqlTableModel *>(ui->table->model());
if (!model)
return;
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
QModelIndexList currentSelection = ui->table->selectionModel()->selectedIndexes();
for (int i = 0; i < currentSelection.count(); ++i) {
if (currentSelection.at(i).column() != 0)
continue;
model->removeRow(currentSelection.at(i).row());
}
model->submitAll();
model->setEditStrategy(QSqlTableModel::OnRowChange);
updateActions();
}
void sqleditor::showTable(const QString &t)
{
QSqlTableModel *model = new QSqlTableModel(ui->table);
model->setEditStrategy(QSqlTableModel::OnRowChange);
model->setTable(QSqlDatabase::database(CONNECTION_NAME).driver()->escapeIdentifier(t, QSqlDriver::TableName));
model->select();
if (model->lastError().type() != QSqlError::NoError)
//emit statusMessage(model->lastError().text());
ui->statusLine->setText(model->lastError().text());
ui->table->setModel(model);
ui->table->setEditTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::EditKeyPressed);
connect(ui->table->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
this, SLOT(currentChanged()));
updateActions();
}
void sqleditor::showMetaData(const QString &t)
{
QSqlRecord rec = QSqlDatabase::database(CONNECTION_NAME).record(t);
QStandardItemModel *model = new QStandardItemModel(ui->table);
model->insertRows(0, rec.count());
model->insertColumns(0, 7);
model->setHeaderData(0, Qt::Horizontal, "Fieldname");
model->setHeaderData(1, Qt::Horizontal, "Type");
model->setHeaderData(2, Qt::Horizontal, "Length");
model->setHeaderData(3, Qt::Horizontal, "Precision");
model->setHeaderData(4, Qt::Horizontal, "Required");
model->setHeaderData(5, Qt::Horizontal, "AutoValue");
model->setHeaderData(6, Qt::Horizontal, "DefaultValue");
for (int i = 0; i < rec.count(); ++i) {
QSqlField fld = rec.field(i);
model->setData(model->index(i, 0), fld.name());
model->setData(model->index(i, 1), fld.typeID() == -1
? QString(QVariant::typeToName(fld.type()))
: QString("%1 (%2)").arg(QVariant::typeToName(fld.type())).arg(fld.typeID()));
model->setData(model->index(i, 2), fld.length());
model->setData(model->index(i, 3), fld.precision());
model->setData(model->index(i, 4), fld.requiredStatus() == -1 ? QVariant("?")
: QVariant(bool(fld.requiredStatus())));
model->setData(model->index(i, 5), fld.isAutoValue());
model->setData(model->index(i, 6), fld.defaultValue());
}
ui->table->setModel(model);
ui->table->setEditTriggers(QAbstractItemView::NoEditTriggers);
updateActions();
}
void sqleditor::on_submitButton_clicked()
{
exec();
ui->sqlEdit->setFocus();
}
void sqleditor::on_clearButton_clicked()
{
ui->sqlEdit->clear();
ui->sqlEdit->setFocus();
}
void sqleditor::on_saveButton_clicked()
{
bool ok;
QString oldName = queryName;
QString newName = queryName;
if(isNew) {
QString text = QInputDialog::getText(this, "Set Query Name",
"New name for query", QLineEdit::Normal,
queryName, &ok);
if (!ok || text.isEmpty())
return;
newName = text;
}
emit saveQuery(newName, ui->sqlEdit->toPlainText(), oldName);
}
void sqleditor::_on_querySaved(QString name, QString oldName)
{
if(oldName != this->queryName)
return;
this->queryName = name;
this->isNew = false;
this->isUnsaved = false;
updateTitle();
}
void sqleditor::closeEvent(QCloseEvent *event)
{
int ret = QMessageBox::Discard;
if(isNew && this->ui->sqlEdit->toPlainText().isEmpty())
{
}
else if(isUnsaved)
{
QMessageBox msgBox;
msgBox.setText("The query has been modified.");
msgBox.setInformativeText("Do you want to save your changes?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
ret = msgBox.exec();
}
if(ret == QMessageBox::Cancel)
{
event->ignore();
return;
}
else if(ret == QMessageBox::Save)
{
on_saveButton_clicked();
}
emit sqleditorClosed(event, queryName);
event->accept();
}
void sqleditor::on_sqlEdit_textChanged()
{
if(!isUnsaved) {
isUnsaved = true;
updateTitle();
}
}