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
|
|
@ -24,7 +24,7 @@ void ConstraintModel::setData(std::shared_ptr<const PgDatabaseCatalog> cat, cons
|
||||||
std::sort(m_constraints.begin(), m_constraints.end(),
|
std::sort(m_constraints.begin(), m_constraints.end(),
|
||||||
[] (auto &l, auto &r) {
|
[] (auto &l, auto &r) {
|
||||||
return l.type < r.type ||
|
return l.type < r.type ||
|
||||||
(l.type == r.type && l.name < r.name);
|
(l.type == r.type && l.objectName() < r.objectName());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -131,10 +131,10 @@ QVariant ConstraintModel::getData(const QModelIndex &index) const
|
||||||
s = IconForConstraintType(t.type);
|
s = IconForConstraintType(t.type);
|
||||||
break;
|
break;
|
||||||
case NameCol:
|
case NameCol:
|
||||||
s = t.name;
|
s = t.objectName();
|
||||||
break;
|
break;
|
||||||
case NsCol:
|
case NsCol:
|
||||||
s = getNamespaceDisplayString(*m_catalog, t.connamespace);
|
s = t.nsName();
|
||||||
break;
|
break;
|
||||||
case SupportingIndexCol:
|
case SupportingIndexCol:
|
||||||
s = getIndexDisplayString(*m_catalog, t.indid);
|
s = getIndexDisplayString(*m_catalog, t.indid);
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@
|
||||||
#include "IconColumnDelegate.h"
|
#include "IconColumnDelegate.h"
|
||||||
#include "IndexModel.h"
|
#include "IndexModel.h"
|
||||||
#include "MainWindow.h"
|
#include "MainWindow.h"
|
||||||
|
#include "PgIndexContainer.h"
|
||||||
#include "PgLabItemDelegate.h"
|
#include "PgLabItemDelegate.h"
|
||||||
#include "PropertiesPage.h"
|
#include "PropertiesPage.h"
|
||||||
#include "ResultTableModelUtil.h"
|
#include "ResultTableModelUtil.h"
|
||||||
|
|
@ -17,6 +18,7 @@
|
||||||
#include "TablesTableModel.h"
|
#include "TablesTableModel.h"
|
||||||
#include "TriggerPage.h"
|
#include "TriggerPage.h"
|
||||||
#include "UserConfiguration.h"
|
#include "UserConfiguration.h"
|
||||||
|
#include "SqlCodePreview.h"
|
||||||
#include <QStringBuilder>
|
#include <QStringBuilder>
|
||||||
#include <boost/container/flat_set.hpp>
|
#include <boost/container/flat_set.hpp>
|
||||||
|
|
||||||
|
|
@ -86,7 +88,6 @@ TablesPage::TablesPage(MainWindow *parent)
|
||||||
m_propertiesPage = new PropertiesPage(this);
|
m_propertiesPage = new PropertiesPage(this);
|
||||||
m_propertiesPage->setSourceModel(m_tablesModel);
|
m_propertiesPage->setSourceModel(m_tablesModel);
|
||||||
ui->twDetails->addTab(m_propertiesPage, "");
|
ui->twDetails->addTab(m_propertiesPage, "");
|
||||||
|
|
||||||
connect(ui->tableListTable->selectionModel(), &QItemSelectionModel::currentRowChanged,
|
connect(ui->tableListTable->selectionModel(), &QItemSelectionModel::currentRowChanged,
|
||||||
m_propertiesPage, &PropertiesPage::setActiveRow);
|
m_propertiesPage, &PropertiesPage::setActiveRow);
|
||||||
|
|
||||||
|
|
@ -94,6 +95,10 @@ TablesPage::TablesPage(MainWindow *parent)
|
||||||
m_triggerPage = new TriggerPage(this);
|
m_triggerPage = new TriggerPage(this);
|
||||||
ui->twDetails->addTab(m_triggerPage, "");
|
ui->twDetails->addTab(m_triggerPage, "");
|
||||||
|
|
||||||
|
// SQL tab
|
||||||
|
m_sqlCodePreview = new SqlCodePreview(this);
|
||||||
|
ui->twDetails->addTab(m_sqlCodePreview, "");
|
||||||
|
|
||||||
retranslateUi(false);
|
retranslateUi(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -103,9 +108,15 @@ void TablesPage::retranslateUi(bool all)
|
||||||
if (all)
|
if (all)
|
||||||
ui->retranslateUi(this);
|
ui->retranslateUi(this);
|
||||||
|
|
||||||
ui->twDetails->setTabText(ui->twDetails->indexOf(m_columnsPage), QApplication::translate("TablesPage", "Columns", nullptr));
|
auto set_tabtext = [this] (QWidget *widget, QString translation) {
|
||||||
ui->twDetails->setTabText(ui->twDetails->indexOf(m_propertiesPage), QApplication::translate("TablesPage", "Properties", nullptr));
|
ui->twDetails->setTabText(ui->twDetails->indexOf(widget), translation);
|
||||||
ui->twDetails->setTabText(ui->twDetails->indexOf(m_triggerPage), QApplication::translate("TablesPage", "Triggers", nullptr));
|
};
|
||||||
|
|
||||||
|
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()
|
TablesPage::~TablesPage()
|
||||||
|
|
@ -166,8 +177,9 @@ void TablesPage::selectedTableChanged(const std::optional<PgClass> &table)
|
||||||
ui->indexesTable->resizeColumnsToContents();
|
ui->indexesTable->resizeColumnsToContents();
|
||||||
|
|
||||||
m_triggerPage->setFilter(table);
|
m_triggerPage->setFilter(table);
|
||||||
}
|
|
||||||
|
|
||||||
|
updateSqlTab(table);
|
||||||
|
}
|
||||||
|
|
||||||
void TablesPage::constraintsTable_selectionChanged(const QItemSelection &/*selected*/, const QItemSelection &/*deselected*/)
|
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) {
|
for (auto rij : rijen) {
|
||||||
const PgConstraint constraint = m_constraintModel->constraint(rij);
|
const PgConstraint constraint = m_constraintModel->constraint(rij);
|
||||||
drops += getDropConstraintDefinition(*m_catalog, constraint) % "\n";
|
drops += getDropConstraintDefinition(*m_catalog, constraint) % "\n";
|
||||||
creates += getConstraintDefinition(*m_catalog, constraint) % "\n";
|
creates += getAlterTableConstraintDefinition(*m_catalog, constraint) % "\n";
|
||||||
}
|
}
|
||||||
ui->constraintSqlEdit->setPlainText(drops % "\n" % creates);
|
ui->constraintSqlEdit->setPlainText(drops % "\n" % creates);
|
||||||
}
|
}
|
||||||
|
|
@ -202,8 +214,8 @@ void TablesPage::indexesTable_selectionChanged(const QItemSelection &/*selected*
|
||||||
QString creates;
|
QString creates;
|
||||||
for (auto rij : rijen) {
|
for (auto rij : rijen) {
|
||||||
const PgIndex index = m_indexModel->getIndex(rij);
|
const PgIndex index = m_indexModel->getIndex(rij);
|
||||||
drops += getDropIndexDefinition(*m_catalog, index) % "\n";
|
drops += index.dropSql() % "\n";
|
||||||
creates += getIndexDefinition(*m_catalog, index) % "\n";
|
creates += index.createSql() % "\n";
|
||||||
}
|
}
|
||||||
ui->indexSqlEdit->setPlainText(drops % "\n" % creates);
|
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);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ class MainWindow;
|
||||||
class PropertiesPage;
|
class PropertiesPage;
|
||||||
class TriggerPage;
|
class TriggerPage;
|
||||||
class PgClass;
|
class PgClass;
|
||||||
|
class SqlCodePreview;
|
||||||
|
|
||||||
class TablesPage : public QWidget
|
class TablesPage : public QWidget
|
||||||
{
|
{
|
||||||
|
|
@ -40,6 +41,7 @@ private:
|
||||||
PropertiesPage *m_propertiesPage;
|
PropertiesPage *m_propertiesPage;
|
||||||
// QWidget *m_triggerTab;
|
// QWidget *m_triggerTab;
|
||||||
TriggerPage *m_triggerPage;
|
TriggerPage *m_triggerPage;
|
||||||
|
SqlCodePreview *m_sqlCodePreview;
|
||||||
std::shared_ptr<PgDatabaseCatalog> m_catalog;
|
std::shared_ptr<PgDatabaseCatalog> m_catalog;
|
||||||
TablesTableModel* m_tablesModel = nullptr;
|
TablesTableModel* m_tablesModel = nullptr;
|
||||||
ColumnTableModel* m_columnsModel = nullptr;
|
ColumnTableModel* m_columnsModel = nullptr;
|
||||||
|
|
@ -51,6 +53,7 @@ private:
|
||||||
// QWidget* addDetailTab(QWidget *contents, bool infront = false);
|
// QWidget* addDetailTab(QWidget *contents, bool infront = false);
|
||||||
|
|
||||||
void selectedTableChanged(const std::optional<PgClass> &table);
|
void selectedTableChanged(const std::optional<PgClass> &table);
|
||||||
|
void updateSqlTab(const std::optional<PgClass> &table);
|
||||||
private slots:
|
private slots:
|
||||||
|
|
||||||
void tableListTable_currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous);
|
void tableListTable_currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous);
|
||||||
|
|
|
||||||
|
|
@ -139,7 +139,7 @@ QVariant TablesTableModel::getData(const QModelIndex &index) const
|
||||||
const auto &t = m_tables[index.row()];
|
const auto &t = m_tables[index.row()];
|
||||||
switch (index.column()) {
|
switch (index.column()) {
|
||||||
case NameCol: return t.objectName();
|
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 OwnerCol: return t.ownerName();
|
||||||
case TablespaceCol: return getTablespaceDisplayString(*m_catalog, t.tablespace);
|
case TablespaceCol: return getTablespaceDisplayString(*m_catalog, t.tablespace);
|
||||||
case OptionsCol: break;
|
case OptionsCol: break;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,10 @@
|
||||||
#include "PgClass.h"
|
#include "PgClass.h"
|
||||||
|
#include "PgAttributeContainer.h"
|
||||||
|
#include "PgDatabaseCatalog.h"
|
||||||
|
#include "PgConstraintContainer.h"
|
||||||
|
#include <QStringBuilder>
|
||||||
|
#include "SqlFormattingUtils.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void operator<<(RelPersistence &s, const Pgsql::Value &v)
|
void operator<<(RelPersistence &s, const Pgsql::Value &v)
|
||||||
|
|
@ -54,3 +60,61 @@ void operator<<(RelKind &s, const Pgsql::Value &v)
|
||||||
//{
|
//{
|
||||||
// return name;
|
// return name;
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
QString PgClass::createSql() const
|
||||||
|
{
|
||||||
|
if (createSqlCache.isEmpty()) {
|
||||||
|
if (kind == RelKind::Table)
|
||||||
|
createSqlCache = createTableSql();
|
||||||
|
|
||||||
|
}
|
||||||
|
return createSqlCache;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString PgClass::createTableSql() const
|
||||||
|
{
|
||||||
|
QString sql;
|
||||||
|
// CREATE [ TEMP | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name ( [
|
||||||
|
sql += "CREATE ";
|
||||||
|
if (persistence == RelPersistence::Unlogged)
|
||||||
|
sql += "UNLOGGED ";
|
||||||
|
else if (persistence == RelPersistence::Temporary)
|
||||||
|
sql += "TEMP ";
|
||||||
|
sql += "TABLE ";
|
||||||
|
sql += fullyQualifiedQuotedObjectName();
|
||||||
|
sql += " (\n ";
|
||||||
|
|
||||||
|
auto && cols = catalog().attributes()->getColumnsForRelation(oid());
|
||||||
|
bool first = true;
|
||||||
|
for (auto && col : cols) {
|
||||||
|
if (col.num > 0 && !col.isdropped) {
|
||||||
|
if (first) {
|
||||||
|
sql += "\n ";
|
||||||
|
first = false;
|
||||||
|
}
|
||||||
|
else sql += ",\n ";
|
||||||
|
sql += col.columnDefinition(catalog());
|
||||||
|
}
|
||||||
|
// { column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ]
|
||||||
|
// | table_constraint
|
||||||
|
// ] )
|
||||||
|
}
|
||||||
|
auto && constraints = catalog().constraints()->getConstraintsForRelation(oid());
|
||||||
|
for (auto && constraint: constraints) {
|
||||||
|
if (first) {
|
||||||
|
sql += "\n ";
|
||||||
|
first = false;
|
||||||
|
}
|
||||||
|
else sql += ",\n ";
|
||||||
|
sql += getConstraintDefinition(catalog(), constraint);
|
||||||
|
}
|
||||||
|
|
||||||
|
sql += ")";
|
||||||
|
// [ INHERITS ( parent_table [, ... ] ) ]
|
||||||
|
// [ PARTITION BY { RANGE | LIST } ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [, ... ] ) ]
|
||||||
|
// [ WITH ( storage_parameter [= value] [, ... ] ) | WITH OIDS | WITHOUT OIDS ]
|
||||||
|
// [ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
|
||||||
|
// [ TABLESPACE tablespace_name ]
|
||||||
|
sql += ";\n";
|
||||||
|
return sql;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,12 @@ public:
|
||||||
// bool operator<(Oid _oid) const { return oid < _oid; }
|
// bool operator<(Oid _oid) const { return oid < _oid; }
|
||||||
// bool operator<(const PgClass &rhs) const { return oid < rhs.oid; }
|
// bool operator<(const PgClass &rhs) const { return oid < rhs.oid; }
|
||||||
|
|
||||||
|
QString createSql() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
mutable QString createSqlCache;
|
||||||
|
|
||||||
|
QString createTableSql() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // PGCLASS_H
|
#endif // PGCLASS_H
|
||||||
|
|
|
||||||
|
|
@ -163,7 +163,7 @@ QString ForeignKeyMatchToString(ForeignKeyMatch fkm)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
PgConstraint::PgConstraint()
|
//PgConstraint::PgConstraint()
|
||||||
{
|
//{
|
||||||
|
|
||||||
}
|
//}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#ifndef PGCONSTRAINT_H
|
#ifndef PGCONSTRAINT_H
|
||||||
#define PGCONSTRAINT_H
|
#define PGCONSTRAINT_H
|
||||||
|
|
||||||
|
#include "PgNamespaceObject.h"
|
||||||
#include "Pgsql_Value.h"
|
#include "Pgsql_Value.h"
|
||||||
#include "PgCatalogTypes.h"
|
#include "PgCatalogTypes.h"
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
@ -46,11 +47,8 @@ void operator<<(ForeignKeyMatch &s, const Pgsql::Value &v);
|
||||||
|
|
||||||
QString ForeignKeyMatchToString(ForeignKeyMatch fkm);
|
QString ForeignKeyMatchToString(ForeignKeyMatch fkm);
|
||||||
|
|
||||||
class PgConstraint {
|
class PgConstraint: public PgNamespaceObject {
|
||||||
public:
|
public:
|
||||||
Oid oid = InvalidOid;
|
|
||||||
QString name;
|
|
||||||
Oid connamespace = InvalidOid;
|
|
||||||
ConstraintType type;
|
ConstraintType type;
|
||||||
bool deferrable;
|
bool deferrable;
|
||||||
bool deferred;
|
bool deferred;
|
||||||
|
|
@ -76,11 +74,7 @@ public:
|
||||||
|
|
||||||
QString definition;
|
QString definition;
|
||||||
|
|
||||||
PgConstraint();
|
using PgNamespaceObject::PgNamespaceObject;
|
||||||
bool operator==(Oid _oid) const { return oid == _oid; }
|
|
||||||
bool operator==(const QString &n) const { return name == n; }
|
|
||||||
bool operator<(Oid _oid) const { return oid < _oid; }
|
|
||||||
bool operator<(const PgConstraint &rhs) const { return oid < rhs.oid; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,25 +20,17 @@ FROM pg_constraint)__";
|
||||||
return q;
|
return q;
|
||||||
}
|
}
|
||||||
|
|
||||||
//void operator<<(std::vector<int16_t> &s, const Pgsql::Value &v)
|
|
||||||
//{
|
|
||||||
|
|
||||||
//}
|
|
||||||
|
|
||||||
//void operator<<(std::vector<Oid> &s, const Pgsql::Value &v)
|
|
||||||
//{
|
|
||||||
|
|
||||||
//}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
PgConstraint PgConstraintContainer::loadElem(const Pgsql::Row &row)
|
PgConstraint PgConstraintContainer::loadElem(const Pgsql::Row &row)
|
||||||
{
|
{
|
||||||
using namespace Pgsql;
|
using namespace Pgsql;
|
||||||
Col col(row);
|
Col col(row);
|
||||||
PgConstraint v;
|
Oid oid = col.nextValue();
|
||||||
col >> v.oid >> v.name >> v.connamespace >> v.type >> v.deferrable
|
QString name = col.nextValue();
|
||||||
|
Oid ns_oid = col.nextValue();
|
||||||
|
PgConstraint v(m_catalog, oid, name, ns_oid);
|
||||||
|
col >> v.type >> v.deferrable
|
||||||
>> v.deferred >> v.validated >> v.relid >> v.typid >> v.indid
|
>> v.deferred >> v.validated >> v.relid >> v.typid >> v.indid
|
||||||
>> v.frelid >> v.fupdtype >> v.fdeltype >> v.fmatchtype
|
>> v.frelid >> v.fupdtype >> v.fdeltype >> v.fmatchtype
|
||||||
>> v.islocal >> v.inhcount >> v.noinherit
|
>> v.islocal >> v.inhcount >> v.noinherit
|
||||||
|
|
|
||||||
|
|
@ -42,16 +42,6 @@ QString getRoleDisplayString(const PgDatabaseCatalog &cat, Oid oid)
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString getNamespaceDisplayString(const PgDatabaseCatalog &cat, Oid oid)
|
|
||||||
{
|
|
||||||
QString result;
|
|
||||||
auto nss = cat.namespaces();
|
|
||||||
auto ns = nss->getByKey(oid);
|
|
||||||
if (ns)
|
|
||||||
result = ns->objectName(); //QString("ns %1").arg(oid);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString getClassDisplayString(const PgDatabaseCatalog &cat, Oid oid)
|
QString getClassDisplayString(const PgDatabaseCatalog &cat, Oid oid)
|
||||||
{
|
{
|
||||||
QString result;
|
QString result;
|
||||||
|
|
|
||||||
|
|
@ -112,7 +112,6 @@ private:
|
||||||
|
|
||||||
QString getRoleNameFromOid(const PgDatabaseCatalog &cat, Oid oid);
|
QString getRoleNameFromOid(const PgDatabaseCatalog &cat, Oid oid);
|
||||||
QString getRoleDisplayString(const PgDatabaseCatalog &cat, Oid oid);
|
QString getRoleDisplayString(const PgDatabaseCatalog &cat, Oid oid);
|
||||||
QString getNamespaceDisplayString(const PgDatabaseCatalog &cat, Oid oid);
|
|
||||||
QString getTablespaceDisplayString(const PgDatabaseCatalog &cat, Oid oid);
|
QString getTablespaceDisplayString(const PgDatabaseCatalog &cat, Oid oid);
|
||||||
QString getTypeDisplayString(const PgDatabaseCatalog &cat, Oid oid, int32_t typmod = -1);
|
QString getTypeDisplayString(const PgDatabaseCatalog &cat, Oid oid, int32_t typmod = -1);
|
||||||
QString getIndexDisplayString(const PgDatabaseCatalog &cat, Oid oid);
|
QString getIndexDisplayString(const PgDatabaseCatalog &cat, Oid oid);
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
#include "PgDatabaseCatalog.h"
|
#include "PgDatabaseCatalog.h"
|
||||||
#include "PgClassContainer.h"
|
#include "PgClassContainer.h"
|
||||||
#include "PgAmContainer.h"
|
#include "PgAmContainer.h"
|
||||||
|
#include <QStringBuilder>
|
||||||
|
|
||||||
QString PgIndex::getAm() const
|
QString PgIndex::getAm() const
|
||||||
{
|
{
|
||||||
|
|
@ -15,3 +16,62 @@ QString PgIndex::getAm() const
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString PgIndex::createSql() const
|
||||||
|
{
|
||||||
|
return definition + ";";
|
||||||
|
|
||||||
|
// const PgClass *table_class = catalog.classes()->getByKey(index.relid);
|
||||||
|
// const PgClass *index_class = catalog.classes()->getByKey(index.indexrelid);
|
||||||
|
|
||||||
|
// QString result;
|
||||||
|
// result = "CREATE ";
|
||||||
|
// if (index.isunique)
|
||||||
|
// result += "UNIQUE ";
|
||||||
|
// result += "INDEX "
|
||||||
|
//// % quoteIdent(getIndexDisplayString(catalog, index.indexrelid))
|
||||||
|
// % quoteIdent(index_class.name)
|
||||||
|
// % "\n ON " % genFQTableName(catalog, table_class);
|
||||||
|
//// % "\n USING " % index_class.am lookup in pg_am table
|
||||||
|
// return result;
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
+ wxT("\n USING ") + GetIndexType()
|
||||||
|
+ wxT("\n (");
|
||||||
|
if (GetProcName().IsNull())
|
||||||
|
str += GetQuotedColumns();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
str += GetQuotedSchemaPrefix(GetProcNamespace()) + qtIdent(GetProcName()) + wxT("(") + GetQuotedColumns() + wxT(")");
|
||||||
|
if (!this->GetOperatorClasses().IsNull())
|
||||||
|
str += wxT(" ") + GetOperatorClasses();
|
||||||
|
}
|
||||||
|
|
||||||
|
str += wxT(")");
|
||||||
|
|
||||||
|
if (GetConnection()->BackendMinimumVersion(8, 2) && GetFillFactor().Length() > 0)
|
||||||
|
str += wxT("\n WITH (FILLFACTOR=") + GetFillFactor() + wxT(")");
|
||||||
|
|
||||||
|
if (GetConnection()->BackendMinimumVersion(8, 0) && tablespace != GetDatabase()->GetDefaultTablespace())
|
||||||
|
str += wxT("\nTABLESPACE ") + qtIdent(tablespace);
|
||||||
|
|
||||||
|
AppendIfFilled(str, wxT("\n WHERE "), GetConstraint());
|
||||||
|
|
||||||
|
str += wxT(";\n");
|
||||||
|
|
||||||
|
if (GetConnection()->BackendMinimumVersion(7, 5))
|
||||||
|
if (GetIsClustered())
|
||||||
|
str += wxT("ALTER TABLE ") + GetQuotedSchemaPrefix(GetIdxSchema()) + qtIdent(GetIdxTable())
|
||||||
|
+ wxT(" CLUSTER ON ") + qtIdent(GetName())
|
||||||
|
+ wxT(";\n");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
QString PgIndex::dropSql() const
|
||||||
|
{
|
||||||
|
QString result;
|
||||||
|
result = "DROP INDEX "
|
||||||
|
% fullyQualifiedQuotedObjectName()
|
||||||
|
% ";";
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,9 @@ public:
|
||||||
// //bool operator==(const QString &n) const { return name == n; }
|
// //bool operator==(const QString &n) const { return name == n; }
|
||||||
// bool operator<(Oid _oid) const { return indexrelid < _oid; }
|
// bool operator<(Oid _oid) const { return indexrelid < _oid; }
|
||||||
// bool operator<(const PgIndex &rhs) const { return indexrelid < rhs.indexrelid; }
|
// bool operator<(const PgIndex &rhs) const { return indexrelid < rhs.indexrelid; }
|
||||||
|
|
||||||
|
QString createSql() const;
|
||||||
|
QString dropSql() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // PGINDEX_H
|
#endif // PGINDEX_H
|
||||||
|
|
|
||||||
|
|
@ -261,7 +261,7 @@ QString dollarQuoteString(const QString &value)
|
||||||
// return ns->quotedObjectName() % "." % cls.quotedObjectName();
|
// return ns->quotedObjectName() % "." % cls.quotedObjectName();
|
||||||
//}
|
//}
|
||||||
|
|
||||||
QString genAlterTable(const PgDatabaseCatalog &catalog, const PgClass &cls)
|
QString genAlterTable(const PgDatabaseCatalog &, const PgClass &cls)
|
||||||
{
|
{
|
||||||
return "ALTER TABLE " % cls.fullyQualifiedQuotedObjectName(); // genFQTableName(catalog, cls);
|
return "ALTER TABLE " % cls.fullyQualifiedQuotedObjectName(); // genFQTableName(catalog, cls);
|
||||||
}
|
}
|
||||||
|
|
@ -269,7 +269,7 @@ QString genAlterTable(const PgDatabaseCatalog &catalog, const PgClass &cls)
|
||||||
QString getDropConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint)
|
QString getDropConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint)
|
||||||
{
|
{
|
||||||
const PgClass *cls = catalog.classes()->getByKey(constraint.relid);
|
const PgClass *cls = catalog.classes()->getByKey(constraint.relid);
|
||||||
return genAlterTable(catalog, *cls) % " DROP CONSTRAINT " % quoteIdent(constraint.name) % ";";
|
return genAlterTable(catalog, *cls) % " DROP CONSTRAINT " % quoteIdent(constraint.objectName()) % ";";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -368,7 +368,7 @@ QString getForeignKeyConstraintReferencesShort(const PgDatabaseCatalog &catalog,
|
||||||
QString getPrimaryKeyConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint)
|
QString getPrimaryKeyConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint)
|
||||||
{
|
{
|
||||||
QString ddl = " PRIMARY KEY ("
|
QString ddl = " PRIMARY KEY ("
|
||||||
% getColumnNameList(catalog, constraint.relid, constraint.key) % ");";
|
% getColumnNameList(catalog, constraint.relid, constraint.key) % ")";
|
||||||
|
|
||||||
return ddl;
|
return ddl;
|
||||||
}
|
}
|
||||||
|
|
@ -376,18 +376,22 @@ QString getPrimaryKeyConstraintDefinition(const PgDatabaseCatalog &catalog, cons
|
||||||
QString getUniqueConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint)
|
QString getUniqueConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint)
|
||||||
{
|
{
|
||||||
QString ddl = " UNIQUE ("
|
QString ddl = " UNIQUE ("
|
||||||
% getColumnNameList(catalog, constraint.relid, constraint.key) % ");";
|
% getColumnNameList(catalog, constraint.relid, constraint.key) % ")";
|
||||||
|
|
||||||
return ddl;
|
return ddl;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString getConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint)
|
QString getAlterTableConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint)
|
||||||
{
|
{
|
||||||
const PgClass *cls = catalog.classes()->getByKey(constraint.relid);
|
const PgClass *cls = catalog.classes()->getByKey(constraint.relid);
|
||||||
// return genAlterTable(catalog, cls) % " ADD CONSTRAINT "
|
QString result = genAlterTable(catalog, *cls) % "\n ADD ";
|
||||||
// % quoteIdent(constraint.name) % " " % constraint.definition % ";";
|
result += getConstraintDefinition(catalog, constraint) % ";";
|
||||||
QString result = genAlterTable(catalog, *cls) % "\n ADD CONSTRAINT "
|
return result;
|
||||||
% quoteIdent(constraint.name);
|
}
|
||||||
|
|
||||||
|
QString getConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint)
|
||||||
|
{
|
||||||
|
QString result = "CONSTRAINT " % quoteIdent(constraint.objectName());
|
||||||
switch (constraint.type) {
|
switch (constraint.type) {
|
||||||
case ConstraintType::ForeignKey:
|
case ConstraintType::ForeignKey:
|
||||||
result += getForeignKeyConstraintDefinition(catalog, constraint);
|
result += getForeignKeyConstraintDefinition(catalog, constraint);
|
||||||
|
|
@ -406,66 +410,4 @@ QString getConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstr
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString getIndexDefinition(const PgDatabaseCatalog &, const PgIndex &index)
|
|
||||||
{
|
|
||||||
// const PgClass *table_class = catalog.classes()->getByKey(index.relid);
|
|
||||||
// const PgClass *index_class = catalog.classes()->getByKey(index.indexrelid);
|
|
||||||
|
|
||||||
|
|
||||||
return index.definition + ";";
|
|
||||||
|
|
||||||
// QString result;
|
|
||||||
// result = "CREATE ";
|
|
||||||
// if (index.isunique)
|
|
||||||
// result += "UNIQUE ";
|
|
||||||
// result += "INDEX "
|
|
||||||
//// % quoteIdent(getIndexDisplayString(catalog, index.indexrelid))
|
|
||||||
// % quoteIdent(index_class.name)
|
|
||||||
// % "\n ON " % genFQTableName(catalog, table_class);
|
|
||||||
//// % "\n USING " % index_class.am lookup in pg_am table
|
|
||||||
// return result;
|
|
||||||
|
|
||||||
#if 0
|
|
||||||
+ wxT("\n USING ") + GetIndexType()
|
|
||||||
+ wxT("\n (");
|
|
||||||
if (GetProcName().IsNull())
|
|
||||||
str += GetQuotedColumns();
|
|
||||||
else
|
|
||||||
{
|
|
||||||
str += GetQuotedSchemaPrefix(GetProcNamespace()) + qtIdent(GetProcName()) + wxT("(") + GetQuotedColumns() + wxT(")");
|
|
||||||
if (!this->GetOperatorClasses().IsNull())
|
|
||||||
str += wxT(" ") + GetOperatorClasses();
|
|
||||||
}
|
|
||||||
|
|
||||||
str += wxT(")");
|
|
||||||
|
|
||||||
if (GetConnection()->BackendMinimumVersion(8, 2) && GetFillFactor().Length() > 0)
|
|
||||||
str += wxT("\n WITH (FILLFACTOR=") + GetFillFactor() + wxT(")");
|
|
||||||
|
|
||||||
if (GetConnection()->BackendMinimumVersion(8, 0) && tablespace != GetDatabase()->GetDefaultTablespace())
|
|
||||||
str += wxT("\nTABLESPACE ") + qtIdent(tablespace);
|
|
||||||
|
|
||||||
AppendIfFilled(str, wxT("\n WHERE "), GetConstraint());
|
|
||||||
|
|
||||||
str += wxT(";\n");
|
|
||||||
|
|
||||||
if (GetConnection()->BackendMinimumVersion(7, 5))
|
|
||||||
if (GetIsClustered())
|
|
||||||
str += wxT("ALTER TABLE ") + GetQuotedSchemaPrefix(GetIdxSchema()) + qtIdent(GetIdxTable())
|
|
||||||
+ wxT(" CLUSTER ON ") + qtIdent(GetName())
|
|
||||||
+ wxT(";\n");
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
QString getDropIndexDefinition(const PgDatabaseCatalog &catalog, const PgIndex &index)
|
|
||||||
{
|
|
||||||
// const PgClass *table_class = catalog.classes()->getByKey(index.relid);
|
|
||||||
// const PgClass *index_class = catalog.classes()->getByKey(index.indexrelid);
|
|
||||||
|
|
||||||
QString result;
|
|
||||||
result = "DROP INDEX "
|
|
||||||
% index.fullyQualifiedQuotedObjectName() // quoteIdent(getIndexDisplayString(catalog, index.indexrelid))
|
|
||||||
% ";";
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,9 +20,8 @@ QString dollarQuoteString(const QString &value);
|
||||||
|
|
||||||
//QString genFQTableName(const PgDatabaseCatalog &catalog, const PgClass &cls);
|
//QString genFQTableName(const PgDatabaseCatalog &catalog, const PgClass &cls);
|
||||||
QString getDropConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint);
|
QString getDropConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint);
|
||||||
|
QString getAlterTableConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint);
|
||||||
QString getConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint);
|
QString getConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint);
|
||||||
QString getIndexDefinition(const PgDatabaseCatalog &catalog, const PgIndex &index);
|
|
||||||
QString getDropIndexDefinition(const PgDatabaseCatalog &catalog, const PgIndex &index);
|
|
||||||
/// Returns the foreignKey specific part of the constraint definition
|
/// Returns the foreignKey specific part of the constraint definition
|
||||||
QString getForeignKeyConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint);
|
QString getForeignKeyConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint);
|
||||||
/// Returns the REFERENCES construct as used directly after a column in the create table statement
|
/// Returns the REFERENCES construct as used directly after a column in the create table statement
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue