From 61645d44ac4ca41f260070aa9eeda9298f718f99 Mon Sep 17 00:00:00 2001 From: eelke Date: Sun, 8 Apr 2018 09:04:38 +0200 Subject: [PATCH] Show moer information about the indexes. --- pglab/TablesPage.cpp | 59 +++++++++++++++++++++++---------- pglab/TablesPage.h | 5 +-- pglab/TablesPage.ui | 14 +++++--- pglablib/SqlFormattingUtils.cpp | 54 +++++++++++++++++++++++++++++- pglablib/SqlFormattingUtils.h | 3 +- 5 files changed, 110 insertions(+), 25 deletions(-) diff --git a/pglab/TablesPage.cpp b/pglab/TablesPage.cpp index 363b838..e4a97b5 100644 --- a/pglab/TablesPage.cpp +++ b/pglab/TablesPage.cpp @@ -7,7 +7,7 @@ #include "ResultTableModelUtil.h" #include "ColumnTableModel.h" #include "ConstraintModel.h" -#include "NamespaceFilterWidget.h" +//#include "NamespaceFilterWidget.h" #include "IconColumnDelegate.h" #include "IndexModel.h" #include "SqlFormattingUtils.h" @@ -43,6 +43,7 @@ TablesPage::TablesPage(MainWindow *parent) font.setFixedPitch(true); font.setPointSize(10); ui->constraintSqlEdit->setFont(font); + ui->indexSqlEdit->setFont(font); SetTableViewDefault(ui->indexesTable); m_indexModel = new IndexModel(this); @@ -50,8 +51,8 @@ TablesPage::TablesPage(MainWindow *parent) ui->indexesTable->setItemDelegate(new PgLabItemDelegate(ui->indexesTable)); ui->indexesTable->setItemDelegateForColumn(0, delegate); - m_namespaceFilterWidget = new NamespaceFilterWidget(this); - ui->verticalLayoutTableView->addWidget(m_namespaceFilterWidget); + //m_namespaceFilterWidget = new NamespaceFilterWidget(this); + //ui->verticalLayoutTableView->addWidget(m_namespaceFilterWidget); connect(ui->tableListTable->selectionModel(), &QItemSelectionModel::currentRowChanged, this, @@ -63,6 +64,9 @@ TablesPage::TablesPage(MainWindow *parent) connect(ui->constraintsTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &TablesPage::constraintsTable_selectionChanged); + connect(ui->indexesTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, + &TablesPage::indexesTable_selectionChanged); + } TablesPage::~TablesPage() @@ -75,11 +79,12 @@ void TablesPage::setCatalog(std::shared_ptr cat) m_catalog = cat; m_tablesModel->setCatalog(cat); ui->tableListTable->resizeColumnsToContents(); - m_namespaceFilterWidget->init(cat->namespaces()); +// m_namespaceFilterWidget->init(cat->namespaces()); auto highlighter = new SqlSyntaxHighlighter(ui->constraintSqlEdit->document()); highlighter->setTypes(*cat->types()); - + highlighter = new SqlSyntaxHighlighter(ui->indexSqlEdit->document()); + highlighter->setTypes(*cat->types()); } void TablesPage::tableListTable_currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous) @@ -98,24 +103,26 @@ void TablesPage::tableListTable_currentRowChanged(const QModelIndex ¤t, co } } -void TablesPage::constraintsTable_currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous) -{ - if (current.row() != previous.row()) { -// QString drop_definition = m_constraintModel->dropDefinition(current.row()); -// QString create_definition = m_constraintModel->createDefinition(current.row()); - const PgConstraint& constraint = m_constraintModel->constraint(current.row()); - QString drop = getDropConstraintDefinition(*m_catalog, constraint); - QString add = getConstraintDefinition(*m_catalog, constraint); +//void TablesPage::constraintsTable_currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous) +//{ +// if (current.row() != previous.row()) { +//// QString drop_definition = m_constraintModel->dropDefinition(current.row()); +//// QString create_definition = m_constraintModel->createDefinition(current.row()); +// const PgConstraint& constraint = m_constraintModel->constraint(current.row()); +// QString drop = getDropConstraintDefinition(*m_catalog, constraint); +// QString add = getConstraintDefinition(*m_catalog, constraint); - ui->constraintSqlEdit->setPlainText(drop % QString::fromUtf16(u"\n") % add); - } -} +// ui->constraintSqlEdit->setPlainText(drop % QString::fromUtf16(u"\n") % add); +// } +//} void TablesPage::constraintsTable_selectionChanged(const QItemSelection &/*selected*/, const QItemSelection &/*deselected*/) { const auto indexes = ui->constraintsTable->selectionModel()->selectedIndexes(); boost::container::flat_set rijen; - for (const auto e : indexes) rijen.insert(e.row()); + for (const auto e : indexes) + rijen.insert(e.row()); + QString drops; QString creates; for (auto rij : rijen) { @@ -126,6 +133,24 @@ void TablesPage::constraintsTable_selectionChanged(const QItemSelection &/*selec ui->constraintSqlEdit->setPlainText(drops % "\n" % creates); } +void TablesPage::indexesTable_selectionChanged(const QItemSelection &/*selected*/, const QItemSelection &/*deselected*/) +{ + const auto indexes = ui->indexesTable->selectionModel()->selectedIndexes(); + boost::container::flat_set rijen; + for (const auto e : indexes) + rijen.insert(e.row()); + + QString drops; + QString creates; + for (auto rij : rijen) { + const PgIndex index = m_indexModel->getIndex(rij); + //drops += getDropIndexDefinition(*m_catalog, index) % "\n"; + creates += getIndexDefinition(*m_catalog, index) % "\n"; + } + ui->indexSqlEdit->setPlainText(drops % "\n" % creates); + +} + void TablesPage::on_tableListTable_doubleClicked(const QModelIndex &index) { PgClass table = m_tablesModel->getTable(index.row()); diff --git a/pglab/TablesPage.h b/pglab/TablesPage.h index dd97b4c..7681e05 100644 --- a/pglab/TablesPage.h +++ b/pglab/TablesPage.h @@ -34,13 +34,14 @@ private: ColumnTableModel* m_columnsModel = nullptr; ConstraintModel* m_constraintModel = nullptr; IndexModel* m_indexModel = nullptr; - NamespaceFilterWidget* m_namespaceFilterWidget; + //NamespaceFilterWidget* m_namespaceFilterWidget; private slots: void tableListTable_currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous); - void constraintsTable_currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous); +// void constraintsTable_currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous); void constraintsTable_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected); + void indexesTable_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected); void on_tableListTable_doubleClicked(const QModelIndex &index); }; diff --git a/pglab/TablesPage.ui b/pglab/TablesPage.ui index 248fb56..d2a9ee2 100644 --- a/pglab/TablesPage.ui +++ b/pglab/TablesPage.ui @@ -6,8 +6,8 @@ 0 0 - 1041 - 830 + 993 + 754 @@ -38,7 +38,7 @@ - 1 + 2 @@ -76,7 +76,13 @@ - + + + Qt::Vertical + + + + diff --git a/pglablib/SqlFormattingUtils.cpp b/pglablib/SqlFormattingUtils.cpp index eb43dc7..fb3addc 100644 --- a/pglablib/SqlFormattingUtils.cpp +++ b/pglablib/SqlFormattingUtils.cpp @@ -8,6 +8,7 @@ #include "PgAttributeContainer.h" #include "PgClass.h" #include "PgClassContainer.h" +#include "PgIndex.h" #include "PgNamespace.h" #include "PgNamespaceContainer.h" #include "PgDatabaseCatalog.h" @@ -19,7 +20,7 @@ bool identNeedsQuotes(QString ident) if (ident[0].isDigit()) return true; for (auto c : ident) - if (c < 'a' && c > 'z' && c != '_') + if ((c < 'a' || c > 'z') && c != '_') return true; auto kw = getPgsqlKeyword(ident); @@ -170,6 +171,57 @@ QString getConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstr return result; } +QString getIndexDefinition(const PgDatabaseCatalog &catalog, const PgIndex &index) +{ + PgClass table_class = catalog.classes()->getByKey(index.relid); + PgClass index_class = catalog.classes()->getByKey(index.indexrelid); + + + + + QString result; + result = "CREATE "; + if (index.isunique) + result += "UNIQUE "; + result += "INDEX " +// % quoteIdent(getIndexDisplayString(catalog, index.indexrelid)) + % quoteIdent(index_class.name) + % "\n ON " % genFQTableName(catalog, table_class); +// % "\n USING " % index_class.am lookup in pg_am table + return result; + +#if 0 + + wxT("\n USING ") + GetIndexType() + + wxT("\n ("); + if (GetProcName().IsNull()) + str += GetQuotedColumns(); + else + { + str += GetQuotedSchemaPrefix(GetProcNamespace()) + qtIdent(GetProcName()) + wxT("(") + GetQuotedColumns() + wxT(")"); + if (!this->GetOperatorClasses().IsNull()) + str += wxT(" ") + GetOperatorClasses(); + } + + str += wxT(")"); + + if (GetConnection()->BackendMinimumVersion(8, 2) && GetFillFactor().Length() > 0) + str += wxT("\n WITH (FILLFACTOR=") + GetFillFactor() + wxT(")"); + + if (GetConnection()->BackendMinimumVersion(8, 0) && tablespace != GetDatabase()->GetDefaultTablespace()) + str += wxT("\nTABLESPACE ") + qtIdent(tablespace); + + AppendIfFilled(str, wxT("\n WHERE "), GetConstraint()); + + str += wxT(";\n"); + + if (GetConnection()->BackendMinimumVersion(7, 5)) + if (GetIsClustered()) + str += wxT("ALTER TABLE ") + GetQuotedSchemaPrefix(GetIdxSchema()) + qtIdent(GetIdxTable()) + + wxT(" CLUSTER ON ") + qtIdent(GetName()) + + wxT(";\n"); +#endif +} + /* wxString sql; diff --git a/pglablib/SqlFormattingUtils.h b/pglablib/SqlFormattingUtils.h index 4379f04..cec320d 100644 --- a/pglablib/SqlFormattingUtils.h +++ b/pglablib/SqlFormattingUtils.h @@ -6,6 +6,7 @@ class PgClass; class PgConstraint; class PgDatabaseCatalog; +class PgIndex; bool identNeedsQuotes(QString ident); QString quoteIdent(QString ident); @@ -13,7 +14,7 @@ QString quoteIdent(QString ident); QString genFQTableName(const PgDatabaseCatalog &catalog, const PgClass &cls); QString getDropConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint); QString getConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint); - +QString getIndexDefinition(const PgDatabaseCatalog &catalog, const PgIndex &index); QString getForeignKeyConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint); #endif // SQLFORMATTINGUTILS_H