diff options
Diffstat (limited to 'SQLiteStudio3/guiSQLiteStudio/dialogs/indexdialog.cpp')
| -rw-r--r-- | SQLiteStudio3/guiSQLiteStudio/dialogs/indexdialog.cpp | 133 |
1 files changed, 68 insertions, 65 deletions
diff --git a/SQLiteStudio3/guiSQLiteStudio/dialogs/indexdialog.cpp b/SQLiteStudio3/guiSQLiteStudio/dialogs/indexdialog.cpp index e3e7701..ebf9beb 100644 --- a/SQLiteStudio3/guiSQLiteStudio/dialogs/indexdialog.cpp +++ b/SQLiteStudio3/guiSQLiteStudio/dialogs/indexdialog.cpp @@ -9,10 +9,11 @@ #include "uiconfig.h" #include "services/config.h" #include "uiutils.h" -#include "sqlite3.h" +#include "db/sqlite3.h" #include "indexexprcolumndialog.h" #include "windows/editorwindow.h" #include "services/codeformatter.h" +#include "common/compatibility.h" #include <QDebug> #include <QGridLayout> #include <QSignalMapper> @@ -64,7 +65,7 @@ void IndexDialog::init() { qCritical() << "Created IndexDialog for null or closed database."; notifyError(tr("Tried to open index dialog for closed or inexisting database.")); - reject(); + preReject(); return; } @@ -91,7 +92,7 @@ void IndexDialog::init() connect(columnStateSignalMapping, SIGNAL(mapped(QString)), this, SLOT(updateColumnState(QString))); SchemaResolver resolver(db); - ui->tableCombo->addItem(QString::null); + ui->tableCombo->addItem(QString()); ui->tableCombo->addItems(resolver.getTables()); connect(ui->tableCombo, SIGNAL(currentTextChanged(QString)), this, SLOT(updateTable(QString))); connect(ui->tableCombo, SIGNAL(currentTextChanged(QString)), this, SLOT(updateValidation())); @@ -99,29 +100,15 @@ void IndexDialog::init() 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(2, false); - } - else - { - ui->partialIndexCheck->setVisible(false); - ui->partialIndexEdit->setVisible(false); - ui->columnsTable->setColumnHidden(2, true); - ui->addExprColumnButton->setVisible(false); - ui->editExprColumnButton->setVisible(false); - ui->delExprColumnButton->setVisible(false); - } + 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(2, false); readCollations(); - ui->ddlEdit->setSqliteVersion(db->getVersion()); - if (index.isNull()) createIndex = SqliteCreateIndexPtr::create(); else @@ -146,8 +133,9 @@ void IndexDialog::readIndex() SqliteQueryPtr parsedObject = resolver.getParsedObject(index, SchemaResolver::INDEX); if (!parsedObject.dynamicCast<SqliteCreateIndex>()) { + createIndex = SqliteCreateIndexPtr::create(); notifyError(tr("Could not process index %1 correctly. Unable to open an index dialog.").arg(index)); - reject(); + preReject(); return; } @@ -160,9 +148,6 @@ void IndexDialog::buildColumns() clearColumns(); ui->columnsTable->setRowCount(0); - totalColumns = tableColumns.size(); - ui->columnsTable->setRowCount(totalColumns); - int row = 0; for (const QString& column : tableColumns) buildColumn(column, row++); @@ -235,8 +220,12 @@ void IndexDialog::buildColumn(const QString& name, int row) IndexDialog::Column* IndexDialog::buildColumn(SqliteOrderBy* orderBy, int row) { - SqliteExpr* expr = dynamic_cast<SqliteExpr*>(orderBy->expr->clone()); - return buildColumn(expr, row); + Column* column = orderBy->isSimpleColumn() ? + new Column(orderBy->getColumnName(), ui->columnsTable) : + new Column(dynamic_cast<SqliteExpr*>(orderBy->expr->clone()), ui->columnsTable); + + buildColumn(column, row); + return column; } IndexDialog::Column* IndexDialog::buildColumn(SqliteExpr* expr, int row) @@ -248,6 +237,9 @@ IndexDialog::Column* IndexDialog::buildColumn(SqliteExpr* expr, int row) void IndexDialog::buildColumn(Column* column, int row) { + totalColumns++; + ui->columnsTable->setRowCount(totalColumns); + QString key = column->getKey(); columns[key] = column; columnsByRow << column; @@ -262,20 +254,17 @@ void IndexDialog::buildColumn(Column* column, int row) layout->setContentsMargins(margins); column->getCheckParent()->setLayout(layout); - column->setCheck(new QCheckBox(key)); + column->setCheck(new QCheckBox(column->getKey())); column->getCheckParent()->layout()->addWidget(column->getCheck()); columnStateSignalMapping->setMapping(column->getCheck(), key); connect(column->getCheck(), SIGNAL(toggled(bool)), columnStateSignalMapping, SLOT(map())); connect(column->getCheck(), SIGNAL(toggled(bool)), this, SLOT(updateValidation())); - if (db->getDialect() == Dialect::Sqlite3) - { - column->setCollation(new QComboBox()); - column->getCollation()->setEditable(true); - column->getCollation()->lineEdit()->setPlaceholderText(tr("default", "index dialog")); - column->getCollation()->setModel(&collations); - } + column->setCollation(new QComboBox()); + column->getCollation()->setEditable(true); + column->getCollation()->lineEdit()->setPlaceholderText(tr("default", "index dialog")); + column->getCollation()->setModel(&collations); column->setSort(new QComboBox()); column->getSort()->setToolTip(tr("Sort order", "table constraints")); @@ -286,8 +275,6 @@ void IndexDialog::buildColumn(Column* column, int row) column->prepareForNewRow(); column->assignToNewRow(row); - totalColumns++; - updateColumnState(key); } @@ -297,8 +284,7 @@ void IndexDialog::updateColumnState(const QString& columnKey) bool enabled = col->getCheck()->isChecked(); col->getSort()->setEnabled(enabled); - if (col->hasCollation()) - col->getCollation()->setEnabled(enabled); + col->getCollation()->setEnabled(enabled); } void IndexDialog::updatePartialConditionState() @@ -482,9 +468,11 @@ void IndexDialog::applyColumnValues() int row = 0; int totalRows = tableColumns.size(); bool orderChanged = false; + QStringList orderedIndexKeys; for (SqliteOrderBy* idxCol : createIndex->indexedColumns) { key = getKey(idxCol); + orderedIndexKeys << key; if (idxCol->isSimpleColumn()) { @@ -502,14 +490,13 @@ void IndexDialog::applyColumnValues() column->getCheck()->setChecked(true); updateColumnState(key); column->getSort()->setCurrentText(sqliteSortOrder(idxCol->order)); - if (column->hasCollation()) - column->getCollation()->setCurrentText(idxCol->getCollation()); + column->getCollation()->setCurrentText(idxCol->getCollation()); // Setting proper order - int currentRow = columnsByRow.indexOf(column); - if (currentRow != row) + int intendedRow = columnsByRow.indexOf(column); + if (intendedRow != row) { - columnsByRow.move(currentRow, row); + columnsByRow.move(intendedRow, row); orderChanged = true; } @@ -587,7 +574,7 @@ void IndexDialog::rebuildCreateIndex() else idxCol = addIndexedColumn(column->getName()); - if (column->hasCollation() && !column->getCollation()->currentText().isEmpty()) + if (!column->getCollation()->currentText().isEmpty()) addCollation(idxCol, column->getCollation()->currentText()); if (column->getSort()->currentIndex() > 0) @@ -599,7 +586,7 @@ void IndexDialog::rebuildCreateIndex() if (createIndex->where) delete createIndex->where; - Parser parser(db->getDialect()); + Parser parser; SqliteExpr* expr = parser.parseExpr(ui->partialIndexEdit->toPlainText()); if (expr) @@ -624,8 +611,6 @@ void IndexDialog::queryDuplicates() static QString countColNameTpl = QStringLiteral("count(%1)"); static QString countConditionTpl = QStringLiteral("count(%1) > 1"); - Dialect dialect = db->getDialect(); - QStringList cols; QStringList grpCols; QStringList countCols; @@ -636,10 +621,10 @@ void IndexDialog::queryDuplicates() if (!columns[column]->getCheck()->isChecked()) continue; - wrappedCol = wrapObjIfNeeded(column, dialect); + wrappedCol = wrapObjIfNeeded(column); cols << wrappedCol; grpCols << wrappedCol; - countColName = wrapObjIfNeeded(countColNameTpl.arg(column), dialect); + countColName = wrapObjIfNeeded(countColNameTpl.arg(column)); cols << countTpl.arg(wrappedCol, countColName); countCols << countConditionTpl.arg(wrappedCol); } @@ -650,7 +635,7 @@ void IndexDialog::queryDuplicates() QString sqlCols = cols.join(", "); QString sqlGrpCols = grpCols.join(", "); QString sqlCntCols = countCols.join(" AND "); - QString sqlTable = wrapObjIfNeeded(ui->tableCombo->currentText(), dialect); + QString sqlTable = wrapObjIfNeeded(ui->tableCombo->currentText()); editor->setContents(queryTpl.arg(sqlCols, sqlTable, sqlGrpCols, sqlCntCols)); editor->execute(); } @@ -679,7 +664,15 @@ QString IndexDialog::getKey(SqliteOrderBy* col) const if (col->isSimpleColumn()) return col->getColumnName(); - return col->expr->tokens.filterWhiteSpaces(false).detokenize(); + return buildKey(col->expr); +} + +QString IndexDialog::buildKey(SqliteExpr* expr) +{ + if (expr->mode == SqliteExpr::Mode::COLLATE && expr->expr1) + return expr->expr1->tokens.filterWhiteSpaces(false).detokenize().trimmed(); + + return expr->tokens.filterWhiteSpaces(false).detokenize().trimmed(); } QStringList IndexDialog::getExistingColumnExprs(const QString& exceptThis) const @@ -711,15 +704,18 @@ QStringList IndexDialog::getTableColumns() const return cols; } +void IndexDialog::preReject() +{ + preRejected = true; +} + void IndexDialog::accept() { rebuildCreateIndex(); - Dialect dialect = db->getDialect(); - QStringList sqls; if (existingIndex) - sqls << QString("DROP INDEX %1").arg(wrapObjIfNeeded(originalCreateIndex->index, dialect)); + sqls << QString("DROP INDEX %1").arg(wrapObjIfNeeded(originalCreateIndex->index)); sqls << createIndex->detokenize(); @@ -767,6 +763,14 @@ void IndexDialog::accept() } } +int IndexDialog::exec() +{ + if (preRejected) + return Rejected; + + return QDialog::exec(); +} + IndexDialog::Column::Column(const QString& name, QTableWidget* table) { this->name = name; @@ -795,8 +799,7 @@ void IndexDialog::Column::prepareForNewRow() { column1Contrainer = defineContainer(checkParent); column2Contrainer = defineContainer(sort); - if (collation) - column3Contrainer = defineContainer(collation); + column3Contrainer = defineContainer(collation); } QCheckBox* IndexDialog::Column::getCheck() const @@ -839,11 +842,6 @@ void IndexDialog::Column::setCollation(QComboBox* cb) collation = cb; } -bool IndexDialog::Column::hasCollation() const -{ - return collation != nullptr; -} - QString IndexDialog::Column::getName() const { return name; @@ -851,6 +849,11 @@ QString IndexDialog::Column::getName() const SqliteExpr* IndexDialog::Column::getExpr() const { + // If column's expression contains collation at top level, + // the EXPR for processing is inner expr of collation. + if (expr->mode == SqliteExpr::Mode::COLLATE) + return expr->expr1; + return expr; } @@ -868,7 +871,7 @@ bool IndexDialog::Column::isExpr() const QString IndexDialog::Column::getKey() const { if (expr) - return expr->tokens.filterWhiteSpaces(false).detokenize(); + return IndexDialog::buildKey(expr); else return name; } |
