pgLab/pglab/TablesPage.cpp
eelke f1020ac56e Index DROP and CREATE statements are now shown.
For now the create is still single line. Either complex query is required
to get details for custom generator or we need to format after the fact.

Close #12
2018-08-05 11:27:05 +02:00

163 lines
5.4 KiB
C++

#include "TablesPage.h"
#include "ui_TablesPage.h"
#include "PgAttribute.h"
#include "PgDatabaseCatalog.h"
#include "TablesTableModel.h"
#include "ResultTableModelUtil.h"
#include "ColumnTableModel.h"
#include "ConstraintModel.h"
//#include "NamespaceFilterWidget.h"
#include "IconColumnDelegate.h"
#include "IndexModel.h"
#include "SqlFormattingUtils.h"
#include "SqlSyntaxHighlighter.h"
#include <QStringBuilder>
#include <boost/container/flat_set.hpp>
#include "MainWindow.h"
#include "PgLabItemDelegate.h"
TablesPage::TablesPage(MainWindow *parent)
: QWidget(parent)
, ui(new Ui::TablesPage)
, m_window(parent)
{
ui->setupUi(this);
SetTableViewDefault(ui->tableListTable);
m_tablesModel = new TablesTableModel(this);
ui->tableListTable->setModel(m_tablesModel);
ui->tableListTable->setSortingEnabled(true);
ui->tableListTable->sortByColumn(0, Qt::AscendingOrder);
SetTableViewDefault(ui->columnsTable);
m_columnsModel = new ColumnTableModel(this);
ui->columnsTable->setModel(m_columnsModel);
SetTableViewDefault(ui->constraintsTable);
m_constraintModel = new ConstraintModel(this);
auto delegate = new IconColumnDelegate(this);
ui->constraintsTable->setModel(m_constraintModel);
ui->constraintsTable->setItemDelegateForColumn(0, delegate);
QFont font;
font.setFamily("Source Code Pro");
font.setFixedPitch(true);
font.setPointSize(10);
ui->constraintSqlEdit->setFont(font);
ui->indexSqlEdit->setFont(font);
SetTableViewDefault(ui->indexesTable);
m_indexModel = new IndexModel(this);
ui->indexesTable->setModel(m_indexModel);
ui->indexesTable->setItemDelegate(new PgLabItemDelegate(ui->indexesTable));
ui->indexesTable->setItemDelegateForColumn(0, delegate);
//m_namespaceFilterWidget = new NamespaceFilterWidget(this);
//ui->verticalLayoutTableView->addWidget(m_namespaceFilterWidget);
connect(ui->tableListTable->selectionModel(), &QItemSelectionModel::currentRowChanged, this,
&TablesPage::tableListTable_currentRowChanged);
// connect(ui->constraintsTable->selectionModel(), &QItemSelectionModel::currentRowChanged, this,
// &TablesPage::constraintsTable_currentRowChanged);
connect(ui->constraintsTable->selectionModel(), &QItemSelectionModel::selectionChanged, this,
&TablesPage::constraintsTable_selectionChanged);
connect(ui->indexesTable->selectionModel(), &QItemSelectionModel::selectionChanged, this,
&TablesPage::indexesTable_selectionChanged);
}
TablesPage::~TablesPage()
{
delete ui;
}
void TablesPage::setCatalog(std::shared_ptr<PgDatabaseCatalog> cat)
{
m_catalog = cat;
m_tablesModel->setCatalog(cat);
ui->tableListTable->resizeColumnsToContents();
// 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 &current, const QModelIndex &previous)
{
if (current.row() != previous.row()) {
PgClass table = m_tablesModel->getTable(current.row());
m_columnsModel->setData(m_catalog, table);
ui->columnsTable->resizeColumnsToContents();
m_constraintModel->setData(m_catalog, table);
ui->constraintsTable->resizeColumnsToContents();
ui->constraintsTable->selectionModel()->reset();
m_indexModel->setData(m_catalog, table);
ui->indexesTable->resizeColumnsToContents();
}
}
//void TablesPage::constraintsTable_currentRowChanged(const QModelIndex &current, 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);
// }
//}
void TablesPage::constraintsTable_selectionChanged(const QItemSelection &/*selected*/, const QItemSelection &/*deselected*/)
{
const auto indexes = ui->constraintsTable->selectionModel()->selectedIndexes();
boost::container::flat_set<int> rijen;
for (const auto e : indexes)
rijen.insert(e.row());
QString drops;
QString creates;
for (auto rij : rijen) {
const PgConstraint constraint = m_constraintModel->constraint(rij);
drops += getDropConstraintDefinition(*m_catalog, constraint) % "\n";
creates += getConstraintDefinition(*m_catalog, constraint) % "\n";
}
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<int> 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());
if (table.oid != InvalidOid) {
m_window->newCrudPage(table);
}
}