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

@ -1,4 +1,10 @@
#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)
@ -54,3 +60,61 @@ void operator<<(RelKind &s, const Pgsql::Value &v)
//{
// 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;
}