56 lines
1.6 KiB
C++
56 lines
1.6 KiB
C++
#include "PgAttribute.h"
|
|
#include "QStringBuilder"
|
|
#include "SqlFormattingUtils.h"
|
|
#include "PgClass.h"
|
|
#include "PgDatabaseCatalog.h"
|
|
#include "PgTypeContainer.h"
|
|
#include "PgCollation.h"
|
|
#include "PgCollationContainer.h"
|
|
|
|
QString PgAttribute::columnDefinition(const PgDatabaseCatalog &cat) const
|
|
{
|
|
// create: column_name data_type [ COLLATE collation ] [ column_constraint [ ... ]
|
|
// alter: column_name data_type [ COLLATE collation ] [ column_constraint [ ... ]
|
|
// constraints NULL/NOT NULL, DEFAULT, GENERATED other constraints will be ignored here a
|
|
auto&& type = cat.types()->getByKey(typid);
|
|
|
|
QString sql = quoteIdent(name) % " " % type->objectName();
|
|
if (collation != InvalidOid) {
|
|
auto&& col = cat.collations()->getByKey(collation);
|
|
QString oname = col->objectName();
|
|
if (oname != "default")
|
|
sql += " COLLATE " % quoteIdent(oname);
|
|
}
|
|
|
|
if (notnull)
|
|
sql += " NOT NULL";
|
|
|
|
if (hasdef)
|
|
sql += " DEFAULT " % defaultValue;
|
|
|
|
if (identity != ' ') {
|
|
sql += " GENERATED ";
|
|
if (identity == 'a') sql += "ALWAYS";
|
|
else if (identity == 'd') sql += "BY DEFAULT";
|
|
sql += " AS IDENTITY";
|
|
}
|
|
// TODO sequence options might be missing
|
|
|
|
return sql;
|
|
}
|
|
|
|
QString PgAttribute::alterTableAddColumn(const PgDatabaseCatalog &cat, const PgClass &table) const
|
|
{
|
|
QString sql = "ALTER TABLE " % table.fullyQualifiedQuotedObjectName()
|
|
% " ADD COLUMN " % columnDefinition(cat) % ";";
|
|
return sql;
|
|
}
|
|
|
|
QString PgAttribute::alterTableDropColumn(const PgDatabaseCatalog &cat, const PgClass &table) const
|
|
{
|
|
QString sql = "ALTER TABLE " % table.fullyQualifiedQuotedObjectName()
|
|
% " DROP COLUMN " % quoteIdent(name) % ";";
|
|
return sql;
|
|
}
|
|
|
|
|