Resolve "Improve GENERATED support"

This commit is contained in:
Eelke Klein 2022-09-06 11:17:18 +00:00
parent 54e39ccdb3
commit 9277731c4e
13 changed files with 749 additions and 246 deletions

View file

@ -3,18 +3,43 @@
#include "SqlFormattingUtils.h"
#include "PgClass.h"
#include "PgDatabaseCatalog.h"
#include "PgSequenceContainer.h"
#include "PgTypeContainer.h"
#include "PgCollation.h"
#include "PgCollationContainer.h"
#include "Pgsql_oids.h"
PgAttribute::Identity PgAttribute::getIdentity() const
{
switch (identity)
{
case '\0': return Identity::None;
case 'a': return Identity::Always;
case 'd': return Identity::ByDefault;
}
assert(false); // we shouldn't get here
return {};
}
PgAttribute::Generated PgAttribute::getGenerated() const
{
switch (generated)
{
case '\0': return Generated::None;
case 's': return Generated::Stored;
}
assert(false); // we shouldn't get here
return {};
}
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
QString type_str;
if (isSerial()) {
if (isSerial() && identity == '\0') {
if (typid == Pgsql::int4_oid)
type_str = "SERIAL";
else if (typid == Pgsql::int8_oid)
@ -35,16 +60,31 @@ QString PgAttribute::columnDefinition(const PgDatabaseCatalog &cat) const
if (notnull)
sql += " NOT NULL";
if (hasdef && !isSerial())
sql += " DEFAULT " % defaultValue;
if (identity != '\0') {
auto identity = getIdentity();
auto generated = getGenerated();
if (identity != Identity::None) {
sql += " GENERATED ";
if (identity == 'a') sql += "ALWAYS";
else if (identity == 'd') sql += "BY DEFAULT";
if (identity == Identity::Always)
sql += "ALWAYS";
else if (identity == Identity::ByDefault)
sql += "BY DEFAULT";
sql += " AS IDENTITY";
auto sequence = cat.sequences()->getByObjectNsAndName(serschema, sername);
auto options_string = sequence->nonDefaultOptionsString();
if (!options_string.isEmpty())
sql += "(" % options_string % ")";
}
// TODO sequence options might be missing
else if (generated != Generated::None)
{
if (generated == Generated::Stored)
sql += " GENERATED ALWAYS AS " % defaultValue % " STORED";
}
else
{
if (hasdef && !isSerial())
sql += " DEFAULT " % defaultValue;
}
return sql;
}