Show SQL for database
Also improvements to the SQL for tables and views.
This commit is contained in:
parent
b5a706a2a2
commit
3158a4364b
12 changed files with 172 additions and 58 deletions
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -32,7 +32,6 @@ class PgClass: public PgNamespaceObject {
|
|||
public:
|
||||
Oid type = InvalidOid;
|
||||
Oid oftype = InvalidOid;
|
||||
//Oid owner = InvalidOid;
|
||||
Oid am = InvalidOid;
|
||||
Oid filenode = InvalidOid;
|
||||
Oid tablespace = InvalidOid;
|
||||
|
|
@ -49,8 +48,6 @@ public:
|
|||
std::vector<QString> options;
|
||||
QString viewdef;
|
||||
|
||||
QString description; // from pg_description
|
||||
|
||||
using PgNamespaceObject::PgNamespaceObject;
|
||||
|
||||
QString kindString() const;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,51 @@
|
|||
#include "PgDatabase.h"
|
||||
#include "PgDatabaseCatalog.h"
|
||||
#include "SqlFormattingUtils.h"
|
||||
#include <QStringBuilder>
|
||||
|
||||
QString PgDatabase::typeName() const
|
||||
{
|
||||
return "DATABASE";
|
||||
return "DATABASE";
|
||||
}
|
||||
|
||||
QString PgDatabase::aclAllPattern() const
|
||||
{
|
||||
// Create
|
||||
// Connect
|
||||
// Temporary
|
||||
return "CcT";
|
||||
}
|
||||
|
||||
QString PgDatabase::dropSql() const
|
||||
{
|
||||
return "DROP DATABASE " % quotedObjectName() % ";";
|
||||
}
|
||||
|
||||
QString PgDatabase::createSql() const
|
||||
{
|
||||
QString s = "CREATE DATABASE " % quotedObjectName()
|
||||
// TEMPLATE is missing as this is not stored in the catalog
|
||||
% "\n OWNER " % quoteIdent(ownerName())
|
||||
% "\n ENCODING " % escapeLiteral(encodingString)
|
||||
% "\n LC_COLLATE " % escapeLiteral(collate)
|
||||
% "\n LC_TYPE " % escapeLiteral(ctype);
|
||||
auto ns = getTablespaceDisplayString(catalog(), tablespace);
|
||||
if (ns != "pg_default")
|
||||
{
|
||||
s += "\n TABLESPACE " % quoteIdent(ns);
|
||||
}
|
||||
if (!allowConn)
|
||||
{
|
||||
s += "\n ALLOW_CONNECTIONS FALSE";
|
||||
}
|
||||
if (connLimit >= 0)
|
||||
{
|
||||
s += "\n CONNECTION LIMIT " % QString::number(connLimit);
|
||||
}
|
||||
if (isTemplate)
|
||||
{
|
||||
s += "\n IS_TEMPLATE TRUE";
|
||||
}
|
||||
s += ";";
|
||||
return s;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@
|
|||
class PgDatabase: public PgServerObject {
|
||||
public:
|
||||
|
||||
Oid dba; // owner?
|
||||
int encoding;
|
||||
QString encodingString;
|
||||
QString collate;
|
||||
|
|
@ -17,13 +16,15 @@ public:
|
|||
bool allowConn;
|
||||
int connLimit;
|
||||
Oid tablespace;
|
||||
QString description;
|
||||
|
||||
using PgServerObject::PgServerObject;
|
||||
|
||||
bool isValid() const { return oid() != InvalidOid; }
|
||||
|
||||
QString typeName() const override;
|
||||
virtual QString aclAllPattern() const override;
|
||||
virtual QString dropSql() const override;
|
||||
virtual QString createSql() const override;
|
||||
};
|
||||
|
||||
#endif // PGDATABASE_H
|
||||
|
|
|
|||
|
|
@ -18,9 +18,10 @@ PgDatabase PgDatabaseContainer::loadElem(const Pgsql::Row &row)
|
|||
Oid oid = col.nextValue();
|
||||
QString name = col.nextValue();
|
||||
PgDatabase v(m_catalog, oid, name);
|
||||
col >> v.dba >> v.encoding >> v.encodingString >> v.collate >> v.ctype >> v.isTemplate
|
||||
Oid owner;
|
||||
col >> owner >> v.encoding >> v.encodingString >> v.collate >> v.ctype >> v.isTemplate
|
||||
>> v.allowConn >> v.connLimit >> v.tablespace >> v.description;
|
||||
|
||||
v.setOwnerOid(owner);
|
||||
AclList acl_list;
|
||||
col >> acl_list;
|
||||
v.setAcls(std::move(acl_list));
|
||||
|
|
|
|||
|
|
@ -94,10 +94,20 @@ QString PgServerObject::aclAllPattern() const
|
|||
|
||||
QString PgServerObject::dropSql() const
|
||||
{
|
||||
return {};
|
||||
return "DROP " % typeName() % " " % fullyQualifiedQuotedObjectName() % ";";
|
||||
}
|
||||
|
||||
QString PgServerObject::createSql() const
|
||||
{
|
||||
return {};
|
||||
return {};
|
||||
}
|
||||
|
||||
QString PgServerObject::commentSql() const
|
||||
{
|
||||
if (description.isEmpty())
|
||||
return {};
|
||||
|
||||
return "COMMENT ON " + typeName() + " " + fullyQualifiedQuotedObjectName()
|
||||
+ " IS " + escapeLiteral(description) + ";";
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@ class PgServerObject: public PgObject {
|
|||
public:
|
||||
using PgObject::PgObject;
|
||||
|
||||
|
||||
void setOwnerOid(Oid oid);
|
||||
Oid ownerOid() const;
|
||||
bool hasOwner() const;
|
||||
|
|
@ -21,6 +20,8 @@ public:
|
|||
const PgAuthId* owner() const;
|
||||
QString alterOwnerSql(const QString& ident) const;
|
||||
|
||||
QString description;
|
||||
|
||||
/**
|
||||
* @brief setAcls
|
||||
* @param acls Important: pass empty optional when acl IS NULL, pass empty list for empty array
|
||||
|
|
@ -44,6 +45,7 @@ public:
|
|||
|
||||
virtual QString dropSql() const;
|
||||
virtual QString createSql() const;
|
||||
QString commentSql() const;
|
||||
private:
|
||||
Oid m_ownerOid = InvalidOid;
|
||||
const PgAuthId * m_owner;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue