WIP: SQL for creating table and related objects
This commit is contained in:
parent
57217974f4
commit
498233d58c
15 changed files with 221 additions and 121 deletions
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue