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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
|
#include "indexdialog.h"
#include "ui_indexdialog.h"
#include "schemaresolver.h"
#include "parser/ast/sqliteindexedcolumn.h"
#include "services/notifymanager.h"
#include "common/utils_sql.h"
#include "db/chainexecutor.h"
#include "dbtree/dbtree.h"
#include "ddlpreviewdialog.h"
#include "uiconfig.h"
#include "services/config.h"
#include "uiutils.h"
#include "sqlite3.h"
#include "windows/editorwindow.h"
#include "services/codeformatter.h"
#include <QDebug>
#include <QGridLayout>
#include <QSignalMapper>
#include <QScrollBar>
#include <QPushButton>
#include <QMessageBox>
IndexDialog::IndexDialog(Db* db, QWidget *parent) :
QDialog(parent),
db(db),
ui(new Ui::IndexDialog)
{
init();
}
IndexDialog::IndexDialog(Db* db, const QString& index, QWidget* parent) :
QDialog(parent),
db(db),
index(index),
ui(new Ui::IndexDialog)
{
existingIndex = true;
init();
}
IndexDialog::~IndexDialog()
{
delete ui;
}
void IndexDialog::changeEvent(QEvent *e)
{
QDialog::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void IndexDialog::init()
{
ui->setupUi(this);
limitDialogWidth(this);
if (!db || !db->isOpen())
{
qCritical() << "Created IndexDialog for null or closed database.";
notifyError(tr("Tried to open index dialog for closed or inexisting database."));
reject();
return;
}
ui->columnsTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
ui->partialIndexEdit->setDb(db);
connect(ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(tabChanged(int)));
columnStateSignalMapping = new QSignalMapper(this);
connect(columnStateSignalMapping, SIGNAL(mapped(int)), this, SLOT(updateColumnState(int)));
SchemaResolver resolver(db);
ui->tableCombo->addItem(QString::null);
ui->tableCombo->addItems(resolver.getTables());
connect(ui->tableCombo, SIGNAL(currentTextChanged(QString)), this, SLOT(updateTable(QString)));
connect(ui->tableCombo, SIGNAL(currentTextChanged(QString)), this, SLOT(updateValidation()));
if (existingIndex)
ui->tableCombo->setEnabled(false);
if (db->getDialect() == Dialect::Sqlite3)
{
connect(ui->partialIndexCheck, SIGNAL(toggled(bool)), this, SLOT(updatePartialConditionState()));
connect(ui->partialIndexEdit, SIGNAL(errorsChecked(bool)), this, SLOT(updateValidation()));
connect(ui->partialIndexEdit, SIGNAL(textChanged()), this, SLOT(updateValidation()));
ui->partialIndexEdit->setVirtualSqlExpression("SELECT %1");
updatePartialConditionState();
ui->columnsTable->setColumnHidden(1, false);
}
else
{
ui->partialIndexCheck->setVisible(false);
ui->partialIndexEdit->setVisible(false);
ui->columnsTable->setColumnHidden(1, true);
}
readCollations();
ui->ddlEdit->setSqliteVersion(db->getVersion());
if (index.isNull())
createIndex = SqliteCreateIndexPtr::create();
else
readIndex();
originalCreateIndex = SqliteCreateIndexPtr::create(*createIndex);
ui->nameEdit->setText(index);
setTable(createIndex->table);
if (!index.isNull())
applyIndex();
updateValidation();
ui->nameEdit->setFocus();
}
void IndexDialog::readIndex()
{
SchemaResolver resolver(db);
SqliteQueryPtr parsedObject = resolver.getParsedObject(index, SchemaResolver::INDEX);
if (!parsedObject.dynamicCast<SqliteCreateIndex>())
{
notifyError(tr("Could not process index %1 correctly. Unable to open an index dialog.").arg(index));
reject();
return;
}
createIndex = parsedObject.dynamicCast<SqliteCreateIndex>();
}
void IndexDialog::buildColumns()
{
// Clean up
ui->columnsTable->setRowCount(0);
columnCheckBoxes.clear();
sortComboBoxes.clear();
collateComboBoxes.clear();
totalColumns = tableColumns.size();
ui->columnsTable->setRowCount(totalColumns);
int row = 0;
foreach (const QString& column, tableColumns)
buildColumn(column, row++);
}
void IndexDialog::updateTable(const QString& value)
{
table = value;
SchemaResolver resolver(db);
tableColumns = resolver.getTableColumns(table);
buildColumns();
}
void IndexDialog::updateValidation()
{
bool tableOk = ui->tableCombo->currentIndex() > 0;
bool colSelected = false;
if (tableOk)
{
foreach (QCheckBox* cb, columnCheckBoxes)
{
if (cb->isChecked())
{
colSelected = true;
break;
}
}
}
bool partialConditionOk = (!ui->partialIndexCheck->isChecked() ||
(ui->partialIndexEdit->isSyntaxChecked() && !ui->partialIndexEdit->haveErrors()));
setValidState(ui->tableCombo, tableOk, tr("Pick the table for the index."));
setValidState(ui->columnsTable, colSelected, tr("Select at least one column."));
setValidState(ui->partialIndexCheck, partialConditionOk, tr("Enter a valid condition."));
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(colSelected && partialConditionOk);
}
void IndexDialog::setTable(const QString& value)
{
ui->tableCombo->setCurrentText(value);
}
void IndexDialog::readCollations()
{
SchemaResolver resolver(db);
QStringList collList = resolver.getCollations();
if (collList.size() > 0)
collList.prepend("");
collations.setStringList(collList);
}
void IndexDialog::buildColumn(const QString& name, int row)
{
int col = 0;
QWidget* checkParent = new QWidget();
QHBoxLayout* layout = new QHBoxLayout();
QMargins margins = layout->contentsMargins();
margins.setTop(0);
margins.setBottom(0);
margins.setLeft(4);
margins.setRight(4);
layout->setContentsMargins(margins);
checkParent->setLayout(layout);
QCheckBox* check = new QCheckBox(name);
checkParent->layout()->addWidget(check);
ui->columnsTable->setCellWidget(row, col++, checkParent);
columnStateSignalMapping->setMapping(check, row);
connect(check, SIGNAL(toggled(bool)), columnStateSignalMapping, SLOT(map()));
connect(check, SIGNAL(toggled(bool)), this, SLOT(updateValidation()));
columnCheckBoxes << check;
QComboBox* collation = nullptr;
if (db->getDialect() == Dialect::Sqlite3)
{
collation = new QComboBox();
collation->setEditable(true);
collation->lineEdit()->setPlaceholderText(tr("default", "index dialog"));
collation->setModel(&collations);
ui->columnsTable->setCellWidget(row, col++, collation);
collateComboBoxes << collation;
}
else
{
col++;
}
QComboBox* sortOrder = new QComboBox();
sortOrder->setToolTip(tr("Sort order", "table constraints"));
ui->columnsTable->setCellWidget(row, col++, sortOrder);
sortComboBoxes << sortOrder;
QStringList sortList = {"", sqliteSortOrder(SqliteSortOrder::ASC), sqliteSortOrder(SqliteSortOrder::DESC)};
sortOrder->addItems(sortList);
totalColumns++;
updateColumnState(row);
}
void IndexDialog::updateColumnState(int row)
{
bool enabled = columnCheckBoxes[row]->isChecked();
sortComboBoxes[row]->setEnabled(enabled);
if (db->getDialect() == Dialect::Sqlite3)
collateComboBoxes[row]->setEnabled(enabled);
}
void IndexDialog::updatePartialConditionState()
{
ui->partialIndexEdit->setEnabled(ui->partialIndexCheck->isChecked());
updateValidation();
}
void IndexDialog::updateDdl()
{
rebuildCreateIndex();
QString formatted = FORMATTER->format("sql", createIndex->detokenize(), db);
ui->ddlEdit->setPlainText(formatted);
}
void IndexDialog::tabChanged(int tab)
{
if (tab == 1)
updateDdl();
}
void IndexDialog::applyColumnValues()
{
Dialect dialect = db->getDialect();
int row;
foreach (SqliteIndexedColumn* idxCol, createIndex->indexedColumns)
{
row = indexOf(tableColumns, idxCol->name, Qt::CaseInsensitive);
if (row == -1)
{
qCritical() << "Cannot find column from index in the table columns! Indexed column:" << idxCol->name
<< ", table columns:" << tableColumns << ", index name:" << index << ", table name:" << table;
continue;
}
columnCheckBoxes[row]->setChecked(true);
updateColumnState(row);
sortComboBoxes[row]->setCurrentText(sqliteSortOrder(idxCol->sortOrder));
if (dialect == Dialect::Sqlite3)
collateComboBoxes[row]->setCurrentText(idxCol->collate);
}
}
void IndexDialog::applyIndex()
{
applyColumnValues();
ui->partialIndexCheck->setChecked(createIndex->where != nullptr);
if (createIndex->where)
ui->partialIndexEdit->setPlainText(createIndex->where->detokenize());
}
SqliteIndexedColumn* IndexDialog::addIndexedColumn(const QString& name)
{
SqliteIndexedColumn* idxCol = new SqliteIndexedColumn();
idxCol->name = name;
idxCol->setParent(createIndex.data());
createIndex->indexedColumns << idxCol;
return idxCol;
}
void IndexDialog::rebuildCreateIndex()
{
createIndex = SqliteCreateIndexPtr::create();
createIndex->index = ui->nameEdit->text();
if (ui->tableCombo->currentIndex() > -1)
createIndex->table = ui->tableCombo->currentText();
createIndex->uniqueKw = ui->uniqueCheck->isChecked();
SqliteIndexedColumn* idxCol = nullptr;
int i = -1;
for (const QString& column : tableColumns)
{
i++;
if (!columnCheckBoxes[i]->isChecked())
continue;
idxCol = addIndexedColumn(column);
if (!collateComboBoxes[i]->currentText().isEmpty())
idxCol->collate = collateComboBoxes[i]->currentText();
if (sortComboBoxes[i]->currentIndex() > 0)
idxCol->sortOrder = sqliteSortOrder(sortComboBoxes[i]->currentText());
}
if (ui->partialIndexCheck->isChecked())
{
if (createIndex->where)
delete createIndex->where;
Parser parser(db->getDialect());
SqliteExpr* expr = parser.parseExpr(ui->partialIndexEdit->toPlainText());
if (expr)
{
expr->setParent(createIndex.data());
createIndex->where = expr;
}
else
{
qCritical() << "Could not parse expression from partial index condition: " << ui->partialIndexEdit->toPlainText()
<< ", the CREATE INDEX statement will be incomplete.";
}
}
createIndex->rebuildTokens();
}
void IndexDialog::queryDuplicates()
{
static QString queryTpl = QStringLiteral("SELECT %1 FROM %2 GROUP BY %3 HAVING %4;\n");
static QString countTpl = QStringLiteral("count(%1) AS %2");
static QString countColNameTpl = QStringLiteral("count(%1)");
static QString countConditionTpl = QStringLiteral("count(%1) > 1");
Dialect dialect = db->getDialect();
QStringList cols;
QStringList grpCols;
QStringList countCols;
QString wrappedCol;
QString countColName;
int i = 0;
for (const QString& column : tableColumns)
{
if (!columnCheckBoxes[i++]->isChecked())
continue;
wrappedCol = wrapObjIfNeeded(column, dialect);
cols << wrappedCol;
grpCols << wrappedCol;
countColName = wrapObjIfNeeded(countColNameTpl.arg(column), dialect);
cols << countTpl.arg(wrappedCol, countColName);
countCols << countConditionTpl.arg(wrappedCol);
}
EditorWindow* editor = MAINWINDOW->openSqlEditor();
editor->setCurrentDb(db);
QString sqlCols = cols.join(", ");
QString sqlGrpCols = grpCols.join(", ");
QString sqlCntCols = countCols.join(" AND ");
QString sqlTable = wrapObjIfNeeded(ui->tableCombo->currentText(), dialect);
editor->setContents(queryTpl.arg(sqlCols, sqlTable, sqlGrpCols, sqlCntCols));
editor->execute();
}
void IndexDialog::accept()
{
rebuildCreateIndex();
Dialect dialect = db->getDialect();
QStringList sqls;
if (existingIndex)
sqls << QString("DROP INDEX %1").arg(wrapObjIfNeeded(originalCreateIndex->index, dialect));
sqls << createIndex->detokenize();
if (!CFG_UI.General.DontShowDdlPreview.get())
{
DdlPreviewDialog dialog(db, this);
dialog.setDdl(sqls);
if (dialog.exec() != QDialog::Accepted)
return;
}
ChainExecutor executor;
executor.setDb(db);
executor.setAsync(false);
executor.setQueries(sqls);
executor.exec();
if (executor.getSuccessfulExecution())
{
CFG->addDdlHistory(sqls.join("\n"), db->getName(), db->getPath());
QDialog::accept();
DBTREE->refreshSchema(db);
return;
}
if (executor.getErrors().size() == 1 && executor.getErrors().first().first == SQLITE_CONSTRAINT)
{
int res = QMessageBox::critical(this,
tr("Error", "index dialog"),
tr("Cannot create unique index, because values in selected columns are not unique. "
"Would you like to execute SELECT query to see problematic values?"),
QMessageBox::Yes,
QMessageBox::No);
if (res == QMessageBox::Yes)
{
QDialog::reject();
queryDuplicates();
}
}
else
{
QMessageBox::critical(this, tr("Error", "index dialog"), tr("An error occurred while executing SQL statements:\n%1")
.arg(executor.getErrorsMessages().join(",\n")), QMessageBox::Ok);
}
}
|