Show SQL for database

Also improvements to the SQL for tables and views.
This commit is contained in:
eelke 2022-01-20 20:13:56 +01:00
parent b5a706a2a2
commit 3158a4364b
12 changed files with 172 additions and 58 deletions

View file

@ -13,7 +13,8 @@ void operator<<(RelPersistence &s, const Pgsql::Value &v)
{
//s = static_cast<T>(v);
const char *c = v.c_str();
switch (*c) {
switch (*c)
{
case 'p':
s = RelPersistence::Permanent;
break;
@ -30,7 +31,8 @@ void operator<<(RelKind &s, const Pgsql::Value &v)
{
//s = static_cast<T>(v);
const char *c = v.c_str();
switch (*c) {
switch (*c)
{
case 'r':
s = RelKind::Table;
break;
@ -58,26 +60,27 @@ void operator<<(RelKind &s, const Pgsql::Value &v)
}
}
//QString PgClass::objectName() const
//{
// return name;
//}
QString PgClass::createSql() const
{
if (createSqlCache.isEmpty()) {
if (kind == RelKind::Table)
createSqlCache = createTableSql();
else if (kind == RelKind::View)
createSqlCache = createViewSql();
if (createSqlCache.isEmpty())
{
switch (kind)
{
case RelKind::Table:
createSqlCache = createTableSql();
break;
case RelKind::View:
createSqlCache = createViewSql();
break;
}
}
return createSqlCache;
}
QString PgClass::typeName() const
{
switch (kind) {
switch (kind)
{
case RelKind::Table: return "TABLE";
case RelKind::Index: return "INDEX";
case RelKind::Sequence: return "SEQUENCE";
@ -92,7 +95,8 @@ QString PgClass::typeName() const
QString PgClass::aclAllPattern() const
{
switch (kind) {
switch (kind)
{
case RelKind::Table: return "arwdDxt";
default:
break;
@ -115,9 +119,12 @@ QString PgClass::createTableSql() const
auto && cols = catalog().attributes()->getColumnsForRelation(oid());
bool first = true;
for (auto && col : cols) {
if (col.num > 0 && !col.isdropped) {
if (first) {
for (auto && col : cols)
{
if (col.num > 0 && !col.isdropped)
{
if (first)
{
first = false;
}
else sql += ",\n ";
@ -129,8 +136,10 @@ QString PgClass::createTableSql() const
// ] )
}
auto && constraints = catalog().constraints()->getConstraintsForRelation(oid());
for (auto && constraint: constraints) {
if (first) {
for (auto && constraint: constraints)
{
if (first)
{
sql += "\n ";
first = false;
}
@ -142,10 +151,12 @@ QString PgClass::createTableSql() const
{
// [ INHERITS ( parent_table [, ... ] ) ]
auto parents = catalog().inherits()->getParentsOf(oid());
if (!parents.empty()) {
if (!parents.empty())
{
sql += "\nINHERITS (";
bool first = true;
for (auto parent_oid : parents) {
for (auto parent_oid : parents)
{
if (first) first = false;
else sql += ", ";
sql += catalog().classes()->getByKey(parent_oid)->fullyQualifiedQuotedObjectName();
@ -156,8 +167,12 @@ QString PgClass::createTableSql() const
// [ 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";
if (tablespace != 0)
{
auto ns = getTablespaceDisplayString(catalog(), tablespace);
sql += "\n TABLESPACE " % quoteIdent(ns);
}
sql += ";\n";
return sql;
}