[#5062] Add support for IDENTITY columns in CREATE TABLE statements

This commit is contained in:
lukaseder 2016-12-16 16:45:08 +01:00
parent 78d23a3111
commit 7f5f4ddfee
2 changed files with 48 additions and 8 deletions

View File

@ -268,6 +268,21 @@ final class CreateTableImpl<R extends Record> extends AbstractQuery implements
ctx.sql(' ').keyword("not null");
}
if (type.identity()) {
// [#5062] H2's (and others') AUTO_INCREMENT flag is syntactically located *after* NULL flags.
switch (ctx.family()) {
case H2:
case MARIADB:
case MYSQL: ctx.sql(' ').keyword("auto_increment"); break;
}
}
if (!asList(HSQLDB).contains(ctx.family()))
acceptDefault(ctx, type);

View File

@ -3207,6 +3207,18 @@ final class Tools {
static final void toSQLDDLTypeDeclaration(Context<?> ctx, DataType<?> type) {
String typeName = type.getTypeName(ctx.configuration());
// In some databases, identity is a type, not a flag.
if (type.identity()) {
switch (ctx.family()) {
case POSTGRES: ctx.keyword(type.getType() == Long.class ? "serial8" : "serial"); return;
}
}
if (type.hasLength()) {
if (type.length() > 0) {
ctx.keyword(typeName).sql('(').sql(type.length()).sql(')');
@ -3215,25 +3227,38 @@ final class Tools {
// Some databases don't allow for length-less VARCHAR, VARBINARY types
else {
String castTypeName = type.getCastTypeName(ctx.configuration());
if (!typeName.equals(castTypeName)) {
if (!typeName.equals(castTypeName))
ctx.keyword(castTypeName);
}
else {
else
ctx.keyword(typeName);
}
}
}
else if (type.hasPrecision() && type.precision() > 0) {
if (type.hasScale()) {
if (type.hasScale())
ctx.keyword(typeName).sql('(').sql(type.precision()).sql(", ").sql(type.scale()).sql(')');
}
else {
else
ctx.keyword(typeName).sql('(').sql(type.precision()).sql(')');
}
}
else {
ctx.keyword(typeName);
}
if (type.identity()) {
switch (ctx.family()) {
case CUBRID: ctx.sql(' ').keyword("auto_increment"); break;
case DERBY: ctx.sql(' ').keyword("generated by default as identity"); break;
case HSQLDB: ctx.sql(' ').keyword("generated by default as identity").sql('(').keyword("start with").sql(" 1)"); break;
}
}
}
// -------------------------------------------------------------------------