diff --git a/jOOQ/src/main/java/org/jooq/impl/CreateTableImpl.java b/jOOQ/src/main/java/org/jooq/impl/CreateTableImpl.java index 7c9009410b..6fea0ec726 100644 --- a/jOOQ/src/main/java/org/jooq/impl/CreateTableImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/CreateTableImpl.java @@ -268,6 +268,21 @@ final class CreateTableImpl 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); diff --git a/jOOQ/src/main/java/org/jooq/impl/Tools.java b/jOOQ/src/main/java/org/jooq/impl/Tools.java index b2f5be730b..150f8e2b76 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Tools.java +++ b/jOOQ/src/main/java/org/jooq/impl/Tools.java @@ -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; + } + } } // -------------------------------------------------------------------------