2019-02-09 09:49:27 +01:00
|
|
|
|
#include "CatalogTablesPage.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "ColumnPage.h"
|
|
|
|
|
|
#include "ColumnTableModel.h"
|
|
|
|
|
|
#include "ConstraintModel.h"
|
|
|
|
|
|
#include "PgLabTableView.h"
|
|
|
|
|
|
#include "PropertiesPage.h"
|
|
|
|
|
|
#include "ResultTableModelUtil.h"
|
|
|
|
|
|
#include "SqlCodePreview.h"
|
2019-02-09 20:37:34 +01:00
|
|
|
|
#include "TablesTableModel.h"
|
2019-02-09 09:49:27 +01:00
|
|
|
|
#include "TriggerPage.h"
|
|
|
|
|
|
#include "catalog/PgIndexContainer.h"
|
|
|
|
|
|
#include "catalog/PgTriggerContainer.h"
|
|
|
|
|
|
#include "widgets/CatalogConstraintPage.h"
|
|
|
|
|
|
#include "widgets/CatalogIndexPage.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
|
#include <QStringBuilder>
|
2019-02-09 14:59:33 +01:00
|
|
|
|
#include <QSortFilterProxyModel>
|
2019-02-09 09:49:27 +01:00
|
|
|
|
#include <QTableWidget>
|
|
|
|
|
|
|
|
|
|
|
|
CatalogTablesPage::CatalogTablesPage(QWidget *parent)
|
|
|
|
|
|
: QSplitter(Qt::Horizontal, parent)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_tableView = new PgLabTableView(this);
|
2019-02-09 14:59:33 +01:00
|
|
|
|
m_tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
|
|
|
|
m_tableView->setSelectionMode(QAbstractItemView::SingleSelection);
|
2019-02-09 09:49:27 +01:00
|
|
|
|
m_detailsTabs = new QTabWidget(this);
|
|
|
|
|
|
|
|
|
|
|
|
// Populate splitter
|
|
|
|
|
|
addWidget(m_tableView);
|
|
|
|
|
|
addWidget(m_detailsTabs);
|
|
|
|
|
|
|
|
|
|
|
|
// Setup model(s)
|
|
|
|
|
|
m_tablesModel = new TablesTableModel(this);
|
2019-02-09 14:59:33 +01:00
|
|
|
|
m_tablesSortFilter = new QSortFilterProxyModel(this);
|
|
|
|
|
|
m_tablesSortFilter->setSourceModel(m_tablesModel);
|
|
|
|
|
|
m_tableView->setModel(m_tablesSortFilter);
|
|
|
|
|
|
m_tableView->setSortingEnabled(true);
|
2019-02-09 09:49:27 +01:00
|
|
|
|
|
|
|
|
|
|
// - 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
|
|
|
|
|
|
m_propertiesPage = new PropertiesPage(this);
|
|
|
|
|
|
m_propertiesPage->setSourceModel(m_tablesModel);
|
|
|
|
|
|
m_detailsTabs->addTab(m_propertiesPage, "");
|
|
|
|
|
|
|
|
|
|
|
|
connect(m_tableView->selectionModel(), &QItemSelectionModel::currentRowChanged,
|
|
|
|
|
|
m_propertiesPage, &PropertiesPage::setActiveRow);
|
|
|
|
|
|
|
|
|
|
|
|
// - Trigger page
|
|
|
|
|
|
m_triggerPage = new TriggerPage(this);
|
|
|
|
|
|
m_detailsTabs->addTab(m_triggerPage, "");
|
|
|
|
|
|
|
|
|
|
|
|
// SQL tab
|
|
|
|
|
|
m_tableSql = new SqlCodePreview(this);
|
|
|
|
|
|
m_detailsTabs->addTab(m_tableSql, "");
|
|
|
|
|
|
|
|
|
|
|
|
// Force focus on columns tab by default
|
|
|
|
|
|
m_detailsTabs->setCurrentIndex(0);
|
|
|
|
|
|
|
|
|
|
|
|
// Signals
|
|
|
|
|
|
connect(m_tableView->selectionModel(), &QItemSelectionModel::currentRowChanged, this,
|
|
|
|
|
|
&CatalogTablesPage::tableListTable_currentRowChanged);
|
|
|
|
|
|
|
|
|
|
|
|
connect(m_tablesModel, &QAbstractItemModel::layoutChanged,
|
|
|
|
|
|
this, &CatalogTablesPage::tableListTable_layoutChanged);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
set_tabtext(m_propertiesPage, QApplication::translate("TablesPage", "Properties", nullptr));
|
|
|
|
|
|
set_tabtext(m_triggerPage, QApplication::translate("TablesPage", "Triggers", nullptr));
|
|
|
|
|
|
set_tabtext(m_tableSql, QApplication::translate("TablesPage", "SQL", nullptr));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CatalogTablesPage::setCatalog(std::shared_ptr<PgDatabaseCatalog> cat)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_catalog = cat;
|
|
|
|
|
|
m_tablesModel->setCatalog(cat);
|
|
|
|
|
|
m_tableView->resizeColumnsToContents();
|
|
|
|
|
|
|
|
|
|
|
|
m_constraintPage->setCatalog(cat);
|
|
|
|
|
|
m_indexPage->setCatalog(cat);
|
|
|
|
|
|
m_triggerPage->setCatalog(cat);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-02-09 20:37:34 +01:00
|
|
|
|
void CatalogTablesPage::setNamespaceFilter(NamespaceFilter filter)
|
2019-02-09 09:49:27 +01:00
|
|
|
|
{
|
|
|
|
|
|
m_tablesModel->setNamespaceFilter(filter);
|
2019-07-07 14:05:30 +02:00
|
|
|
|
m_tableView->resizeColumnsToContents();
|
2019-02-09 09:49:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CatalogTablesPage::tableListTable_currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (current.row() != previous.row()) {
|
|
|
|
|
|
if (current.isValid()) {
|
2019-02-09 17:44:05 +01:00
|
|
|
|
auto row = m_tablesSortFilter->mapToSource(current).row();
|
|
|
|
|
|
PgClass table = m_tablesModel->getTable(row);
|
2019-02-09 09:49:27 +01:00
|
|
|
|
selectedTableChanged(table);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
selectedTableChanged({});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CatalogTablesPage::tableListTable_layoutChanged(const QList<QPersistentModelIndex> &, QAbstractItemModel::LayoutChangeHint )
|
|
|
|
|
|
{
|
|
|
|
|
|
auto&& index = m_tableView->selectionModel()->currentIndex();
|
|
|
|
|
|
if (index.isValid()) {
|
2019-02-09 17:44:05 +01:00
|
|
|
|
auto row = m_tablesSortFilter->mapToSource(index).row();
|
|
|
|
|
|
PgClass table = m_tablesModel->getTable(row);
|
2019-02-09 09:49:27 +01:00
|
|
|
|
selectedTableChanged(table);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
selectedTableChanged({});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CatalogTablesPage::on_tableListTable_doubleClicked(const QModelIndex &index)
|
|
|
|
|
|
{
|
2019-02-09 17:44:05 +01:00
|
|
|
|
|
|
|
|
|
|
auto row = m_tablesSortFilter->mapToSource(index).row();
|
|
|
|
|
|
PgClass table = m_tablesModel->getTable(row);
|
2019-02-09 09:49:27 +01:00
|
|
|
|
if (table.oid() != InvalidOid) {
|
|
|
|
|
|
// context()->moduleAction("pglab.crudpage", "open", {
|
|
|
|
|
|
// { "oid", table.oid() }
|
|
|
|
|
|
// });
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
updateSqlTab(table);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CatalogTablesPage::updateSqlTab(const std::optional<PgClass> &table)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!table.has_value()) {
|
|
|
|
|
|
m_tableSql->clear();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString drop_sql;
|
|
|
|
|
|
QString create_sql;
|
|
|
|
|
|
// create table
|
|
|
|
|
|
create_sql += table->createSql();
|
|
|
|
|
|
// - columns
|
|
|
|
|
|
// - constraints
|
|
|
|
|
|
// table details (inherits etc)
|
|
|
|
|
|
|
|
|
|
|
|
// Indexes
|
|
|
|
|
|
drop_sql += "-- drop Indexes\n";
|
|
|
|
|
|
create_sql += "-- create Indexes\n";
|
|
|
|
|
|
auto && indexes = m_catalog->indexes()->getIndexesForTable(table->oid());
|
|
|
|
|
|
for (auto && index : indexes) {
|
|
|
|
|
|
drop_sql += index.dropSql() % "\n";
|
|
|
|
|
|
create_sql += index.createSql() % "\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Triggers
|
|
|
|
|
|
drop_sql += "-- drop Triggers\n";
|
|
|
|
|
|
create_sql += "-- create Triggers\n";
|
|
|
|
|
|
auto && triggers = m_catalog->triggers()->getTriggersForRelation(table->oid());
|
|
|
|
|
|
for (auto && trg : triggers) {
|
|
|
|
|
|
drop_sql += trg.dropSql() % "\n";
|
|
|
|
|
|
create_sql += trg.createSql() % "\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Privileges
|
|
|
|
|
|
create_sql += "-- set Privileges\n";
|
|
|
|
|
|
create_sql += table->grantSql() % "\n";
|
|
|
|
|
|
|
|
|
|
|
|
// Comments
|
|
|
|
|
|
create_sql += "-- set Comments table + columns\n";
|
|
|
|
|
|
//
|
|
|
|
|
|
m_tableSql->setPlainText(drop_sql % "\n\n" % create_sql);
|
|
|
|
|
|
}
|