WIP: SQL for creating table and related objects

This commit is contained in:
eelke 2018-11-30 18:41:38 +01:00
parent 57217974f4
commit 498233d58c
15 changed files with 221 additions and 121 deletions

View file

@ -24,7 +24,7 @@ void ConstraintModel::setData(std::shared_ptr<const PgDatabaseCatalog> cat, cons
std::sort(m_constraints.begin(), m_constraints.end(),
[] (auto &l, auto &r) {
return l.type < r.type ||
(l.type == r.type && l.name < r.name);
(l.type == r.type && l.objectName() < r.objectName());
});
}
else
@ -131,10 +131,10 @@ QVariant ConstraintModel::getData(const QModelIndex &index) const
s = IconForConstraintType(t.type);
break;
case NameCol:
s = t.name;
s = t.objectName();
break;
case NsCol:
s = getNamespaceDisplayString(*m_catalog, t.connamespace);
s = t.nsName();
break;
case SupportingIndexCol:
s = getIndexDisplayString(*m_catalog, t.indid);

View file

@ -9,6 +9,7 @@
#include "IconColumnDelegate.h"
#include "IndexModel.h"
#include "MainWindow.h"
#include "PgIndexContainer.h"
#include "PgLabItemDelegate.h"
#include "PropertiesPage.h"
#include "ResultTableModelUtil.h"
@ -17,6 +18,7 @@
#include "TablesTableModel.h"
#include "TriggerPage.h"
#include "UserConfiguration.h"
#include "SqlCodePreview.h"
#include <QStringBuilder>
#include <boost/container/flat_set.hpp>
@ -86,7 +88,6 @@ TablesPage::TablesPage(MainWindow *parent)
m_propertiesPage = new PropertiesPage(this);
m_propertiesPage->setSourceModel(m_tablesModel);
ui->twDetails->addTab(m_propertiesPage, "");
connect(ui->tableListTable->selectionModel(), &QItemSelectionModel::currentRowChanged,
m_propertiesPage, &PropertiesPage::setActiveRow);
@ -94,6 +95,10 @@ TablesPage::TablesPage(MainWindow *parent)
m_triggerPage = new TriggerPage(this);
ui->twDetails->addTab(m_triggerPage, "");
// SQL tab
m_sqlCodePreview = new SqlCodePreview(this);
ui->twDetails->addTab(m_sqlCodePreview, "");
retranslateUi(false);
}
@ -103,9 +108,15 @@ void TablesPage::retranslateUi(bool all)
if (all)
ui->retranslateUi(this);
ui->twDetails->setTabText(ui->twDetails->indexOf(m_columnsPage), QApplication::translate("TablesPage", "Columns", nullptr));
ui->twDetails->setTabText(ui->twDetails->indexOf(m_propertiesPage), QApplication::translate("TablesPage", "Properties", nullptr));
ui->twDetails->setTabText(ui->twDetails->indexOf(m_triggerPage), QApplication::translate("TablesPage", "Triggers", nullptr));
auto set_tabtext = [this] (QWidget *widget, QString translation) {
ui->twDetails->setTabText(ui->twDetails->indexOf(widget), translation);
};
set_tabtext(m_columnsPage, QApplication::translate("TablesPage", "Columns", nullptr));
set_tabtext(m_propertiesPage, QApplication::translate("TablesPage", "Properties", nullptr));
set_tabtext(m_triggerPage, QApplication::translate("TablesPage", "Triggers", nullptr));
set_tabtext(m_sqlCodePreview, QApplication::translate("TablesPage", "SQL", nullptr));
}
TablesPage::~TablesPage()
@ -166,8 +177,9 @@ void TablesPage::selectedTableChanged(const std::optional<PgClass> &table)
ui->indexesTable->resizeColumnsToContents();
m_triggerPage->setFilter(table);
}
updateSqlTab(table);
}
void TablesPage::constraintsTable_selectionChanged(const QItemSelection &/*selected*/, const QItemSelection &/*deselected*/)
{
@ -181,7 +193,7 @@ void TablesPage::constraintsTable_selectionChanged(const QItemSelection &/*selec
for (auto rij : rijen) {
const PgConstraint constraint = m_constraintModel->constraint(rij);
drops += getDropConstraintDefinition(*m_catalog, constraint) % "\n";
creates += getConstraintDefinition(*m_catalog, constraint) % "\n";
creates += getAlterTableConstraintDefinition(*m_catalog, constraint) % "\n";
}
ui->constraintSqlEdit->setPlainText(drops % "\n" % creates);
}
@ -202,8 +214,8 @@ void TablesPage::indexesTable_selectionChanged(const QItemSelection &/*selected*
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";
drops += index.dropSql() % "\n";
creates += index.createSql() % "\n";
}
ui->indexSqlEdit->setPlainText(drops % "\n" % creates);
@ -222,3 +234,40 @@ void TablesPage::on_tableListTable_doubleClicked(const QModelIndex &index)
}
}
void TablesPage::updateSqlTab(const std::optional<PgClass> &table)
{
if (!table.has_value()) {
m_sqlCodePreview->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";
// Privileges
create_sql += "-- set Privileges\n";
// Comments
create_sql += "-- set Comments table + columns\n";
//
m_sqlCodePreview->setPlainText(drop_sql % "\n\n" % create_sql);
}

View file

@ -21,6 +21,7 @@ class MainWindow;
class PropertiesPage;
class TriggerPage;
class PgClass;
class SqlCodePreview;
class TablesPage : public QWidget
{
@ -40,6 +41,7 @@ private:
PropertiesPage *m_propertiesPage;
// QWidget *m_triggerTab;
TriggerPage *m_triggerPage;
SqlCodePreview *m_sqlCodePreview;
std::shared_ptr<PgDatabaseCatalog> m_catalog;
TablesTableModel* m_tablesModel = nullptr;
ColumnTableModel* m_columnsModel = nullptr;
@ -51,6 +53,7 @@ private:
// QWidget* addDetailTab(QWidget *contents, bool infront = false);
void selectedTableChanged(const std::optional<PgClass> &table);
void updateSqlTab(const std::optional<PgClass> &table);
private slots:
void tableListTable_currentRowChanged(const QModelIndex &current, const QModelIndex &previous);

View file

@ -139,7 +139,7 @@ QVariant TablesTableModel::getData(const QModelIndex &index) const
const auto &t = m_tables[index.row()];
switch (index.column()) {
case NameCol: return t.objectName();
case NamespaceCol: return t.nsName(); //getNamespaceDisplayString(*m_catalog, t.relnamespace);
case NamespaceCol: return t.nsName();
case OwnerCol: return t.ownerName();
case TablespaceCol: return getTablespaceDisplayString(*m_catalog, t.tablespace);
case OptionsCol: break;