2019-02-09 09:49:27 +01:00
|
|
|
|
#include "CatalogTablesPage.h"
|
|
|
|
|
|
|
2022-04-09 07:10:29 +02:00
|
|
|
|
#include "catalog/widgets/CatalogConstraintPage.h"
|
|
|
|
|
|
#include "catalog/widgets/CatalogIndexPage.h"
|
|
|
|
|
|
#include "catalog/widgets/ColumnPage.h"
|
|
|
|
|
|
#include "catalog/widgets/DependantsPage.h"
|
2019-02-09 09:49:27 +01:00
|
|
|
|
#include "PropertiesPage.h"
|
2022-04-09 07:10:29 +02:00
|
|
|
|
#include "catalog/widgets/TriggerPage.h"
|
|
|
|
|
|
#include "catalog/models/ColumnTableModel.h"
|
|
|
|
|
|
#include "catalog/models/ConstraintModel.h"
|
2023-01-24 17:47:52 +00:00
|
|
|
|
#include "catalog/tables/TablesTableModel.h"
|
2022-04-09 07:10:29 +02:00
|
|
|
|
#include "util/PgLabTableView.h"
|
2019-02-09 09:49:27 +01:00
|
|
|
|
#include "ResultTableModelUtil.h"
|
2022-04-10 14:26:31 +02:00
|
|
|
|
#include "widgets/SqlCodePreview.h"
|
2021-03-08 16:59:13 +01:00
|
|
|
|
#include "SqlFormattingUtils.h"
|
2021-03-08 17:23:34 +01:00
|
|
|
|
#include "catalog/PgAttributeContainer.h"
|
2019-02-09 09:49:27 +01:00
|
|
|
|
#include "catalog/PgIndexContainer.h"
|
|
|
|
|
|
#include "catalog/PgTriggerContainer.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <QApplication>
|
2021-06-12 07:36:24 +02:00
|
|
|
|
#include <QHeaderView>
|
2019-02-09 09:49:27 +01:00
|
|
|
|
#include <QStringBuilder>
|
2022-01-20 20:13:56 +01:00
|
|
|
|
//#include <QSortFilterProxyModel>
|
2019-02-09 09:49:27 +01:00
|
|
|
|
#include <QTableWidget>
|
|
|
|
|
|
|
2022-01-17 17:12:07 +01:00
|
|
|
|
CatalogTablesPage::CatalogTablesPage(std::shared_ptr<OpenDatabase> opendatabase, QWidget *parent)
|
2019-02-09 09:49:27 +01:00
|
|
|
|
: QSplitter(Qt::Horizontal, parent)
|
2022-01-17 17:12:07 +01:00
|
|
|
|
, m_tablesTableView(this, new TablesTableModel(opendatabase, this))
|
2019-02-09 09:49:27 +01:00
|
|
|
|
{
|
2023-01-24 17:47:52 +00:00
|
|
|
|
auto tv = m_tablesTableView.itemView();
|
2021-12-30 18:54:26 +01:00
|
|
|
|
tv->setSelectionMode(QAbstractItemView::SingleSelection);
|
2019-02-09 09:49:27 +01:00
|
|
|
|
m_detailsTabs = new QTabWidget(this);
|
|
|
|
|
|
|
|
|
|
|
|
// Populate splitter
|
2021-12-30 18:54:26 +01:00
|
|
|
|
addWidget(tv);
|
2019-02-09 09:49:27 +01:00
|
|
|
|
addWidget(m_detailsTabs);
|
|
|
|
|
|
|
|
|
|
|
|
// - Columns page
|
|
|
|
|
|
m_columnsPage = new ColumnPage(this);
|
|
|
|
|
|
m_detailsTabs->addTab(m_columnsPage, "");
|
|
|
|
|
|
|
|
|
|
|
|
// constrainst
|
|
|
|
|
|
m_constraintPage = new CatalogConstraintPage(this);
|
|
|
|
|
|
m_detailsTabs->addTab(m_constraintPage, "");
|
|
|
|
|
|
|
|
|
|
|
|
// - Index page
|
|
|
|
|
|
m_indexPage = new CatalogIndexPage(this);
|
|
|
|
|
|
m_detailsTabs->addTab(m_indexPage, "");
|
|
|
|
|
|
|
|
|
|
|
|
// - Properties page
|
2022-01-16 18:43:09 +01:00
|
|
|
|
// m_propertiesPage = new PropertiesPage(this);
|
2021-12-30 18:54:26 +01:00
|
|
|
|
// m_propertiesPage->setSourceModel(m_tablesTableView.dataModel());
|
2022-01-16 18:43:09 +01:00
|
|
|
|
// m_detailsTabs->addTab(m_propertiesPage, "");
|
2019-02-09 09:49:27 +01:00
|
|
|
|
|
|
|
|
|
|
// - Trigger page
|
|
|
|
|
|
m_triggerPage = new TriggerPage(this);
|
|
|
|
|
|
m_detailsTabs->addTab(m_triggerPage, "");
|
|
|
|
|
|
|
2019-11-17 10:27:11 +01:00
|
|
|
|
m_dependentsPage = new DependantsPage(this);
|
|
|
|
|
|
m_detailsTabs->addTab(m_dependentsPage, "");
|
|
|
|
|
|
|
2019-02-09 09:49:27 +01:00
|
|
|
|
// SQL tab
|
|
|
|
|
|
m_tableSql = new SqlCodePreview(this);
|
|
|
|
|
|
m_detailsTabs->addTab(m_tableSql, "");
|
|
|
|
|
|
|
|
|
|
|
|
// Force focus on columns tab by default
|
|
|
|
|
|
m_detailsTabs->setCurrentIndex(0);
|
|
|
|
|
|
|
|
|
|
|
|
// Signals
|
2021-12-30 18:54:26 +01:00
|
|
|
|
connect(tv->selectionModel(), &QItemSelectionModel::currentRowChanged, this,
|
2019-02-09 09:49:27 +01:00
|
|
|
|
&CatalogTablesPage::tableListTable_currentRowChanged);
|
|
|
|
|
|
|
2021-12-30 18:54:26 +01:00
|
|
|
|
connect(m_tablesTableView.dataModel(), &QAbstractItemModel::layoutChanged,
|
2019-02-09 09:49:27 +01:00
|
|
|
|
this, &CatalogTablesPage::tableListTable_layoutChanged);
|
2019-08-16 10:49:38 +02:00
|
|
|
|
|
2021-12-30 18:54:26 +01:00
|
|
|
|
connect(tv, &QTableView::doubleClicked,
|
2019-08-16 10:49:38 +02:00
|
|
|
|
this, &CatalogTablesPage::on_tableListTable_doubleClicked);
|
2019-02-09 09:49:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-02-09 14:59:33 +01:00
|
|
|
|
void CatalogTablesPage::retranslateUi(bool /*all*/)
|
2019-02-09 09:49:27 +01:00
|
|
|
|
{
|
|
|
|
|
|
auto set_tabtext = [this] (QWidget *widget, QString translation) {
|
|
|
|
|
|
m_detailsTabs->setTabText(m_detailsTabs->indexOf(widget), translation);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
set_tabtext(m_columnsPage, QApplication::translate("TablesPage", "Columns", nullptr));
|
|
|
|
|
|
set_tabtext(m_constraintPage, QApplication::translate("TablesPage", "Constraints", nullptr));
|
|
|
|
|
|
set_tabtext(m_indexPage, QApplication::translate("TablesPage", "Indexes", nullptr));
|
2022-01-16 18:43:09 +01:00
|
|
|
|
// set_tabtext(m_propertiesPage, QApplication::translate("TablesPage", "Properties", nullptr));
|
2019-02-09 09:49:27 +01:00
|
|
|
|
set_tabtext(m_triggerPage, QApplication::translate("TablesPage", "Triggers", nullptr));
|
2019-11-17 10:27:11 +01:00
|
|
|
|
set_tabtext(m_dependentsPage, QApplication::translate("TablesPage", "Dependants", nullptr));
|
2019-02-09 09:49:27 +01:00
|
|
|
|
set_tabtext(m_tableSql, QApplication::translate("TablesPage", "SQL", nullptr));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CatalogTablesPage::setCatalog(std::shared_ptr<PgDatabaseCatalog> cat)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_catalog = cat;
|
2021-12-30 18:54:26 +01:00
|
|
|
|
m_tablesTableView.setCatalog(cat);
|
2019-02-09 09:49:27 +01:00
|
|
|
|
|
|
|
|
|
|
m_constraintPage->setCatalog(cat);
|
|
|
|
|
|
m_indexPage->setCatalog(cat);
|
|
|
|
|
|
m_triggerPage->setCatalog(cat);
|
2019-11-17 10:27:11 +01:00
|
|
|
|
m_dependentsPage->setCatalog(cat);
|
2019-02-09 09:49:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-02-09 20:37:34 +01:00
|
|
|
|
void CatalogTablesPage::setNamespaceFilter(NamespaceFilter filter)
|
2019-02-09 09:49:27 +01:00
|
|
|
|
{
|
2021-12-30 18:54:26 +01:00
|
|
|
|
m_tablesTableView.dataModel()->setNamespaceFilter(filter);
|
2023-01-24 17:47:52 +00:00
|
|
|
|
//m_tablesTableView.tableView()->resizeColumnsToContents();
|
2019-02-09 09:49:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CatalogTablesPage::tableListTable_currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous)
|
|
|
|
|
|
{
|
2022-01-20 20:13:56 +01:00
|
|
|
|
if (current.row() == previous.row())
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
2023-01-24 17:47:52 +00:00
|
|
|
|
auto tableNode = TablesTableModel::nodeFromIndex(m_tablesTableView.sortFilter()->mapToSource(current)); // m_tablesTableView.rowItemForProxyIndex(current);
|
|
|
|
|
|
selectedTableChanged(tableNode->_class);
|
2019-02-09 09:49:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CatalogTablesPage::tableListTable_layoutChanged(const QList<QPersistentModelIndex> &, QAbstractItemModel::LayoutChangeHint )
|
|
|
|
|
|
{
|
2021-12-30 18:54:26 +01:00
|
|
|
|
auto table = m_tablesTableView.currentRowItem();
|
|
|
|
|
|
selectedTableChanged(table);
|
2019-02-09 09:49:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CatalogTablesPage::on_tableListTable_doubleClicked(const QModelIndex &index)
|
|
|
|
|
|
{
|
2023-01-24 17:47:52 +00:00
|
|
|
|
auto sourceIndex = m_tablesTableView.sortFilter()->mapToSource(index);
|
|
|
|
|
|
auto tableNode = TablesTableModel::nodeFromIndex(sourceIndex);
|
|
|
|
|
|
if (tableNode)
|
|
|
|
|
|
{
|
|
|
|
|
|
Oid oid = tableNode->_class.oid();
|
|
|
|
|
|
if (oid != InvalidOid) {
|
|
|
|
|
|
tableSelected(oid);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-02-09 09:49:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CatalogTablesPage::selectedTableChanged(const std::optional<PgClass> &table)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_columnsPage->setData(m_catalog, table);
|
|
|
|
|
|
|
|
|
|
|
|
m_constraintPage->setFilter(table);
|
|
|
|
|
|
m_indexPage->setFilter(table);
|
|
|
|
|
|
m_triggerPage->setFilter(table);
|
2019-11-17 10:27:11 +01:00
|
|
|
|
m_dependentsPage->setFilter(table);
|
2019-02-09 09:49:27 +01:00
|
|
|
|
|
|
|
|
|
|
updateSqlTab(table);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CatalogTablesPage::updateSqlTab(const std::optional<PgClass> &table)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!table.has_value()) {
|
|
|
|
|
|
m_tableSql->clear();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString drop_sql;
|
|
|
|
|
|
QString create_sql;
|
2022-01-20 20:13:56 +01:00
|
|
|
|
// table
|
|
|
|
|
|
drop_sql += table->dropSql() % "\n";
|
|
|
|
|
|
create_sql += table->createSql() % "\n";
|
2019-02-09 09:49:27 +01:00
|
|
|
|
// - columns
|
|
|
|
|
|
// - constraints
|
|
|
|
|
|
// table details (inherits etc)
|
|
|
|
|
|
|
|
|
|
|
|
// Indexes
|
|
|
|
|
|
auto && indexes = m_catalog->indexes()->getIndexesForTable(table->oid());
|
2021-03-08 16:59:13 +01:00
|
|
|
|
if (!indexes.empty()) {
|
|
|
|
|
|
drop_sql += "-- drop Indexes\n";
|
|
|
|
|
|
create_sql += "-- create Indexes\n";
|
|
|
|
|
|
for (auto && index : indexes) {
|
|
|
|
|
|
drop_sql += index.dropSql() % "\n";
|
|
|
|
|
|
create_sql += index.createSql() % "\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-02-09 09:49:27 +01:00
|
|
|
|
|
|
|
|
|
|
// Triggers
|
2021-03-08 16:59:13 +01:00
|
|
|
|
auto && triggers = m_catalog->triggers()->getTriggersForRelation(table->oid());
|
|
|
|
|
|
if (!triggers.empty()) {
|
|
|
|
|
|
drop_sql += "-- drop Triggers\n";
|
|
|
|
|
|
create_sql += "-- create Triggers\n";
|
|
|
|
|
|
for (auto && trg : triggers) {
|
|
|
|
|
|
drop_sql += trg.dropSql() % "\n";
|
|
|
|
|
|
create_sql += trg.createSql() % "\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-02-09 09:49:27 +01:00
|
|
|
|
|
|
|
|
|
|
// Privileges
|
|
|
|
|
|
create_sql += "-- set Privileges\n";
|
|
|
|
|
|
create_sql += table->grantSql() % "\n";
|
|
|
|
|
|
|
|
|
|
|
|
// Comments
|
|
|
|
|
|
create_sql += "-- set Comments table + columns\n";
|
2022-01-20 20:13:56 +01:00
|
|
|
|
create_sql += table->commentSql() % "\n";
|
2021-03-08 17:23:34 +01:00
|
|
|
|
|
|
|
|
|
|
auto && cols = m_catalog->attributes()->getColumnsForRelation(table->oid());
|
|
|
|
|
|
for (auto && col : cols) {
|
|
|
|
|
|
if (!col.description.isEmpty()) {
|
|
|
|
|
|
create_sql += "COMMENT ON COLUMN " + table->fullyQualifiedQuotedObjectName()
|
|
|
|
|
|
+ "." + quoteIdent(col.name) + " IS " + dollarQuoteString(col.description) + ";\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-08 16:59:13 +01:00
|
|
|
|
//
|
2019-02-09 09:49:27 +01:00
|
|
|
|
m_tableSql->setPlainText(drop_sql % "\n\n" % create_sql);
|
|
|
|
|
|
}
|