From 0dc4c6b11b97ed4db735e14c5ced2e032e65d6ce Mon Sep 17 00:00:00 2001 From: Roland Praml Date: Wed, 14 Jun 2023 17:18:06 +0200 Subject: [PATCH] NEW: DbPlatformType supports fallback, when length exceeded (removed SqlServer workaround) --- .../config/dbplatform/DbPlatformType.java | 87 +++++++++++++++---- 1 file changed, 68 insertions(+), 19 deletions(-) diff --git a/ebean-api/src/main/java/io/ebean/config/dbplatform/DbPlatformType.java b/ebean-api/src/main/java/io/ebean/config/dbplatform/DbPlatformType.java index 13c0d1fcb..9ac1cf09d 100644 --- a/ebean-api/src/main/java/io/ebean/config/dbplatform/DbPlatformType.java +++ b/ebean-api/src/main/java/io/ebean/config/dbplatform/DbPlatformType.java @@ -28,6 +28,16 @@ public class DbPlatformType implements ExtraDbTypes { */ private final boolean canHaveLength; + /** + * The maximum length supported by this platform type. If length is too big, fallback is used. + */ + private final int maxLength; + + /** + * Use this platform type if length exceedes + */ + private final DbPlatformType fallback; + /** * Parse a type definition into a DbPlatformType. *

@@ -53,6 +63,41 @@ public class DbPlatformType implements ExtraDbTypes { this(name, defaultLength, 0); } + /** + * Construct without length, but with a max length limit and a fallback type, that is used if maxLength is exceeded. + * This can be used to use

+ */ + public DbPlatformType(String name, int maxLength, DbPlatformType fallback) { + this.name = name; + this.defaultLength = 0; + this.defaultScale = 0; + this.canHaveLength = false; + this.maxLength = maxLength; + this.fallback = fallback; + } + + /** + * Construct with a given default length, a max length limit and a fallback type, that is used if maxLength is exceeded. + * This can be used to use + */ + public DbPlatformType(String name, int defaultPrecision, int maxLength, DbPlatformType fallback) { + this.name = name; + this.defaultLength = defaultPrecision; + this.defaultScale = 0; + this.canHaveLength = true; + this.maxLength = maxLength; + this.fallback = fallback; + } + /** * Construct for Decimal with precision and scale. */ @@ -61,6 +106,8 @@ public class DbPlatformType implements ExtraDbTypes { this.defaultLength = defaultPrecision; this.defaultScale = defaultScale; this.canHaveLength = true; + this.maxLength = Integer.MAX_VALUE; + this.fallback = null; } /** @@ -74,6 +121,8 @@ public class DbPlatformType implements ExtraDbTypes { this.defaultLength = 0; this.defaultScale = 0; this.canHaveLength = canHaveLength; + this.maxLength = Integer.MAX_VALUE; + this.fallback = null; } /** @@ -124,33 +173,33 @@ public class DbPlatformType implements ExtraDbTypes { */ public String renderType(int deployLength, int deployScale, boolean strict) { - StringBuilder sb = new StringBuilder(); - sb.append(name); - if (canHaveLength || !strict) { - renderLengthScale(deployLength, deployScale, sb); - } + int len = deployLength != 0 ? deployLength : defaultLength; + if (len > maxLength) { + return fallback.renderType(deployLength, deployScale, strict); + } else { + StringBuilder sb = new StringBuilder(); + sb.append(name); + if ((canHaveLength || !strict) && len > 0) { + renderLengthScale(len, deployScale, sb); + } - return sb.toString(); + return sb.toString(); + } } /** * Render the length and scale part of the column definition. */ - protected void renderLengthScale(int deployLength, int deployScale, StringBuilder sb) { + protected void renderLengthScale(int len, int deployScale, StringBuilder sb) { // see if there is a precision/scale to add (or not) - int len = deployLength != 0 ? deployLength : defaultLength; - if (len == Integer.MAX_VALUE) { - sb.append("(max)"); // TODO: this is sqlserver specific - } else if (len > 0) { - sb.append("("); - sb.append(len); - int scale = deployScale != 0 ? deployScale : defaultScale; - if (scale > 0) { - sb.append(","); - sb.append(scale); - } - sb.append(")"); + sb.append('('); + sb.append(len); + int scale = deployScale != 0 ? deployScale : defaultScale; + if (scale > 0) { + sb.append(','); + sb.append(scale); } + sb.append(')'); } /**