2017-12-12 20:13:53 +01:00
|
|
|
|
#include "PgAttribute.h"
|
2018-11-29 20:21:36 +01:00
|
|
|
|
#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);
|
|
|
|
|
|
|
2018-12-16 09:24:27 +01:00
|
|
|
|
QString sql = quoteIdent(name) % " " % type->objectName();
|
2018-11-29 20:21:36 +01:00
|
|
|
|
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;
|
|
|
|
|
|
|
2019-08-19 19:44:07 +02:00
|
|
|
|
if (identity != '\0') {
|
2018-11-29 20:21:36 +01:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-12 20:13:53 +01:00
|
|
|
|
|