From 3c80bdb8f7353e2bcd84b40c9f069a19db9c55f3 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Wed, 8 Jan 2025 09:17:44 +0100 Subject: [PATCH] [jOOQ/jOOQ#17803] Avoid CAST expression in DDL DEFAULT where unsupported --- jOOQ/src/main/java/org/jooq/impl/Tools.java | 40 +++++++++++++-------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/impl/Tools.java b/jOOQ/src/main/java/org/jooq/impl/Tools.java index 770e75665d..cf260e8196 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Tools.java +++ b/jOOQ/src/main/java/org/jooq/impl/Tools.java @@ -1236,6 +1236,8 @@ final class Tools { + + // ------------------------------------------------------------------------ // XXX: Record constructors and related methods // ------------------------------------------------------------------------ @@ -5994,26 +5996,36 @@ final class Tools { Field v = type.defaultValue(); ctx.sql(' ').visit(K_DEFAULT).sql(' '); - // [#15943] Some dialects require parentheses around expressions. We can't use AbstractField::parenthesised - // as that just declares whether an expression requires additional parentheses in operator - // expressions, not if actual parentheses are rendered. - if (REQUIRES_PARENTHESISED_DEFAULT.contains(ctx.dialect())) - ctx.sql('(').visit(v).sql(')'); - // [#16853] MySQL LOB types can't have defaults. Except if we parenthesise them, then they can o_O - else if (REQUIRES_PARENTHESISED_DEFAULT_FOR_LOBS.contains(ctx.dialect()) && (type.isLob() || type.isJSON() || type.isSpatial())) - ctx.sql('(').visit(v).sql(')'); - // [#16498] Special cases where the standard datetime literal prefix needs to be omitted - // See: https://bugs.mysql.com/bug.php?id=114450 - else if (NO_SUPPORT_DEFAULT_DATETIME_LITERAL_PREFIX.contains(ctx.dialect()) && type.isDateTime()) - ctx.data(DATA_OMIT_DATETIME_LITERAL_PREFIX, true, c -> c.visit(v)); - else - ctx.visit(v); + + + + visitDefault(ctx, type, v); } } + private static final void visitDefault(Context ctx, DataType type, Field v) { + // [#15943] Some dialects require parentheses around expressions. We can't use AbstractField::parenthesised + // as that just declares whether an expression requires additional parentheses in operator + // expressions, not if actual parentheses are rendered. + if (REQUIRES_PARENTHESISED_DEFAULT.contains(ctx.dialect())) + ctx.sql('(').visit(v).sql(')'); + + // [#16853] MySQL LOB types can't have defaults. Except if we parenthesise them, then they can o_O + else if (REQUIRES_PARENTHESISED_DEFAULT_FOR_LOBS.contains(ctx.dialect()) && (type.isLob() || type.isJSON() || type.isSpatial())) + ctx.sql('(').visit(v).sql(')'); + + // [#16498] Special cases where the standard datetime literal prefix needs to be omitted + // See: https://bugs.mysql.com/bug.php?id=114450 + else if (NO_SUPPORT_DEFAULT_DATETIME_LITERAL_PREFIX.contains(ctx.dialect()) && type.isDateTime()) + ctx.data(DATA_OMIT_DATETIME_LITERAL_PREFIX, true, c -> c.visit(v)); + + else + ctx.visit(v); + } + static final void toSQLDDLTypeDeclaration(Context ctx, DataType type) { // [#10376] TODO: Move some of this logic into DataType