diff options
Diffstat (limited to 'SQLiteStudio3/guiSQLiteStudio/windows/tablewindow.cpp')
| -rw-r--r-- | SQLiteStudio3/guiSQLiteStudio/windows/tablewindow.cpp | 99 |
1 files changed, 74 insertions, 25 deletions
diff --git a/SQLiteStudio3/guiSQLiteStudio/windows/tablewindow.cpp b/SQLiteStudio3/guiSQLiteStudio/windows/tablewindow.cpp index 526ae1b..c9356d8 100644 --- a/SQLiteStudio3/guiSQLiteStudio/windows/tablewindow.cpp +++ b/SQLiteStudio3/guiSQLiteStudio/windows/tablewindow.cpp @@ -38,6 +38,7 @@ #include "dialogs/importdialog.h" #include "dialogs/populatedialog.h" #include "datagrid/sqlqueryitem.h" +#include "common/dbcombobox.h" #include <QMenu> #include <QToolButton> #include <QLabel> @@ -164,6 +165,7 @@ void TableWindow::init() initActions(); updateTabsOrder(); + createDbCombo(); connect(dataModel, SIGNAL(executionSuccessful()), this, SLOT(executionSuccessful())); connect(dataModel, SIGNAL(executionFailed(QString)), this, SLOT(executionFailed(QString))); @@ -225,6 +227,9 @@ void TableWindow::createStructureActions() createAction(MOVE_COLUMN_UP, ICONS.MOVE_UP, tr("Move column up", "table window"), this, SLOT(moveColumnUp()), ui->structureToolBar, ui->structureView); createAction(MOVE_COLUMN_DOWN, ICONS.MOVE_DOWN, tr("Move column down", "table window"), this, SLOT(moveColumnDown()), ui->structureToolBar, ui->structureView); ui->structureToolBar->addSeparator(); + createAction(ADD_INDEX_STRUCT, ICONS.INDEX_ADD, tr("Create index", "table window"), this, SLOT(addIndex()), ui->structureToolBar, ui->structureView); + createAction(ADD_TRIGGER_STRUCT, ICONS.TRIGGER_ADD, tr("Create trigger", "table window"), this, SLOT(addTrigger()), ui->structureToolBar, ui->structureView); + ui->structureToolBar->addSeparator(); ui->structureToolBar->addAction(actionMap[IMPORT]); ui->structureToolBar->addAction(actionMap[EXPORT]); ui->structureToolBar->addAction(actionMap[POPULATE]); @@ -289,12 +294,16 @@ void TableWindow::editColumn(const QModelIndex& idx) SqliteCreateTable::Column* column = structureModel->getColumn(idx.row()); ColumnDialog columnDialog(db, this); columnDialog.setColumn(column); + if (hasAnyPkDefined() && !column->hasConstraint(SqliteCreateTable::Column::Constraint::PRIMARY_KEY)) + columnDialog.disableConstraint(ConstraintDialog::Constraint::PK); + if (columnDialog.exec() != QDialog::Accepted) return; SqliteCreateTable::Column* modifiedColumn = columnDialog.getModifiedColumn(); structureModel->replaceColumn(idx.row(), modifiedColumn); resizeStructureViewColumns(); + updateTableConstraintsToolbarState(); } void TableWindow::delColumn(const QModelIndex& idx) @@ -314,6 +323,7 @@ void TableWindow::delColumn(const QModelIndex& idx) structureModel->delColumn(idx.row()); resizeStructureViewColumns(); + updateTableConstraintsToolbarState(); } void TableWindow::executeStructureChanges() @@ -441,6 +451,7 @@ void TableWindow::updateTableConstraintsToolbarState() actionMap[DEL_TABLE_CONSTRAINT]->setEnabled(anyColumn && validIdx); actionMap[MOVE_CONSTRAINT_UP]->setEnabled(anyColumn && validIdx && !isFirst); actionMap[MOVE_CONSTRAINT_DOWN]->setEnabled(anyColumn && validIdx && !isLast); + actionMap[ADD_TABLE_PK]->setEnabled(!hasAnyPkDefined()); } void TableWindow::setupDefShortcuts() @@ -480,21 +491,15 @@ void TableWindow::executionFailed(const QString& errorText) void TableWindow::initDbAndTable() { - int totalConstrCols = 6; - if (db->getVersion() == 2) - { - ui->withoutRowIdCheck->setVisible(false); - totalConstrCols -= 2; - } - - totalConstrCols += 2; // we start at 3rd column - for (int colIdx = 2; colIdx < totalConstrCols; colIdx++) + for (int colIdx = 2; colIdx < 9; colIdx++) ui->structureView->setItemDelegateForColumn(colIdx, constraintColumnsDelegate); + ui->dbCombo->setCurrentDb(db); if (existingTable) { dataModel->setDb(db); dataModel->setDatabaseAndTable(database, table); + ui->dbCombo->setDisabled(true); } ui->tableNameEdit->setText(table); // TODO no attached/temp db name support here @@ -563,7 +568,6 @@ void TableWindow::initDbAndTable() connect(ui->withoutRowIdCheck, SIGNAL(clicked()), this, SLOT(withOutRowIdChanged())); - ui->ddlEdit->setSqliteVersion(db->getVersion()); parseDdl(); updateIndexes(); updateTriggers(); @@ -605,7 +609,6 @@ void TableWindow::parseDdl() { createTable = SqliteCreateTablePtr::create(); createTable->table = table; - createTable->dialect = db->getDialect(); } originalCreateTable = SqliteCreateTablePtr::create(*createTable); structureModel->setCreateTable(createTable.data()); @@ -621,6 +624,13 @@ void TableWindow::parseDdl() updateDdlTab(); } +void TableWindow::createDbCombo() +{ + ui->dbCombo->setFixedWidth(100); + ui->dbCombo->setToolTip(tr("Database")); + connect(ui->dbCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(dbChanged())); +} + void TableWindow::changeEvent(QEvent *e) { QWidget::changeEvent(e); @@ -904,6 +914,9 @@ void TableWindow::addColumn() ColumnDialog columnDialog(db, this); columnDialog.setColumn(&column); + if (hasAnyPkDefined()) + columnDialog.disableConstraint(ConstraintDialog::Constraint::PK); + if (columnDialog.exec() != QDialog::Accepted) return; @@ -913,6 +926,7 @@ void TableWindow::addColumn() ui->structureView->setCurrentIndex(structureModel->index(structureModel->rowCount()-1, 0)); resizeStructureViewColumns(); + updateTableConstraintsToolbarState(); } void TableWindow::editColumn() @@ -954,6 +968,9 @@ void TableWindow::moveColumnDown() void TableWindow::addConstraint(ConstraintDialog::Constraint mode) { NewConstraintDialog dialog(mode, createTable.data(), db, this); + if (hasAnyPkDefined()) + dialog.disableMode(ConstraintDialog::PK); + if (dialog.exec() != QDialog::Accepted) return; @@ -968,6 +985,7 @@ void TableWindow::addConstraint(ConstraintDialog::Constraint mode) structureConstraintsModel->appendConstraint(tableConstr); ui->tableConstraintsView->resizeColumnToContents(0); ui->tableConstraintsView->resizeColumnToContents(1); + updateTableConstraintsToolbarState(); } bool TableWindow::validate(bool skipWarning) @@ -1060,7 +1078,7 @@ QString TableWindow::getCurrentIndex() const int row = ui->indexList->currentRow(); QTableWidgetItem* item = ui->indexList->item(row, 0); if (!item) - return QString::null; + return QString(); return item->text(); } @@ -1070,7 +1088,7 @@ QString TableWindow::getCurrentTrigger() const int row = ui->triggerList->currentRow(); QTableWidgetItem* item = ui->triggerList->item(row, 0); if (!item) - return QString::null; + return QString(); return item->text(); } @@ -1100,6 +1118,31 @@ int TableWindow::getStructureTabIdx() const return ui->tabWidget->indexOf(ui->structureTab); } +bool TableWindow::hasAnyPkDefined() const +{ + if (structureConstraintsModel) + { + for (int i = 0, total = structureConstraintsModel->rowCount(); i < total; ++i) + { + SqliteCreateTable::Constraint* constraint = structureConstraintsModel->getConstraint(i); + if (constraint->type == SqliteCreateTable::Constraint::PRIMARY_KEY) + return true; + } + } + + if (structureModel) + { + for (int i = 0, total = structureModel->rowCount(); i < total; ++i) + { + SqliteCreateTable::Column* column = structureModel->getColumn(i); + if (column->hasConstraint(SqliteCreateTable::Column::Constraint::PRIMARY_KEY)) + return true; + } + } + + return false; +} + void TableWindow::updateDdlTab() { createTable->rebuildTokens(); @@ -1121,6 +1164,8 @@ void TableWindow::updateNewTableState() actionMap[CREATE_SIMILAR]->setEnabled(existingTable); actionMap[RESET_AUTOINCREMENT]->setEnabled(existingTable); actionMap[REFRESH_STRUCTURE]->setEnabled(existingTable); + actionMap[ADD_INDEX_STRUCT]->setEnabled(existingTable); + actionMap[ADD_TRIGGER_STRUCT]->setEnabled(existingTable); } void TableWindow::addConstraint() @@ -1173,6 +1218,7 @@ void TableWindow::delConstraint(const QModelIndex& idx) structureConstraintsModel->delConstraint(idx.row()); ui->structureView->resizeColumnToContents(0); + updateTableConstraintsToolbarState(); } void TableWindow::moveConstraintUp() @@ -1309,7 +1355,7 @@ void TableWindow::withOutRowIdChanged() if (!createTable) return; - createTable->withOutRowId = ui->withoutRowIdCheck->isChecked() ? QStringLiteral("ROWID") : QString::null; + createTable->withOutRowId = ui->withoutRowIdCheck->isChecked() ? QStringLiteral("ROWID") : QString(); updateDdlTab(); emit modifyStatusChanged(); } @@ -1446,7 +1492,7 @@ void TableWindow::updateIndexes() return; SchemaResolver resolver(db); - resolver.setIgnoreSystemObjects(true); + resolver.setIgnoreSystemObjects(false); QList<SqliteCreateIndexPtr> indexes = resolver.getParsedIndexesForTable(database, table); ui->indexList->setColumnCount(4); @@ -1458,10 +1504,6 @@ void TableWindow::updateIndexes() tr("Partial index condition", "table window indexes"), }); - Dialect dialect= db->getDialect(); - if (dialect == Dialect::Sqlite2) - ui->indexList->setColumnCount(3); - ui->indexList->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch); QTableWidgetItem* item = nullptr; @@ -1482,12 +1524,9 @@ void TableWindow::updateIndexes() item->setFlags(Qt::ItemIsEnabled|Qt::ItemIsSelectable); ui->indexList->setItem(row, 2, item); - if (dialect == Dialect::Sqlite3) - { - item = new QTableWidgetItem(index->where ? index->where->detokenize() : ""); - item->setFlags(Qt::ItemIsEnabled|Qt::ItemIsSelectable); - ui->indexList->setItem(row, 3, item); - } + item = new QTableWidgetItem(index->where ? index->where->detokenize() : ""); + item->setFlags(Qt::ItemIsEnabled|Qt::ItemIsSelectable); + ui->indexList->setItem(row, 3, item); row++; } @@ -1653,3 +1692,13 @@ void TableWindow::updateFont() view->verticalHeader()->setDefaultSectionSize(fm.height() + 4); } } + +void TableWindow::dbChanged() +{ + disconnect(db, SIGNAL(dbObjectDeleted(QString,QString,DbObjectType)), this, SLOT(checkIfTableDeleted(QString,QString,DbObjectType))); + + db = ui->dbCombo->currentDb(); + dataModel->setDb(db); + + connect(db, SIGNAL(dbObjectDeleted(QString,QString,DbObjectType)), this, SLOT(checkIfTableDeleted(QString,QString,DbObjectType))); +} |
