From 3d63aa82a0e8c7113b8ff46a85fbf60dd039cca2 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Fri, 21 Dec 2012 21:58:10 +0100 Subject: [PATCH] [#456] Add runtime support for PRECISION, SCALE, and LENGTH attributes - Added the precision/scale/length API --- jOOQ/src/main/java/org/jooq/DataType.java | 73 +++++++++++++++ .../java/org/jooq/impl/DefaultDataType.java | 92 ++++++++++++++++--- 2 files changed, 153 insertions(+), 12 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/DataType.java b/jOOQ/src/main/java/org/jooq/DataType.java index 9d07c62972..4376cceaa9 100644 --- a/jOOQ/src/main/java/org/jooq/DataType.java +++ b/jOOQ/src/main/java/org/jooq/DataType.java @@ -181,6 +181,79 @@ public interface DataType extends Serializable { */ List convert(Collection objects); + /** + * Return a new data type like this, with a new precision value + *

+ * This will have no effect if {@link #hasPrecision()} is false + * + * @param precision The new precision value + * @return The new data type + */ + DataType precision(int precision); + + /** + * Get the precision of this data type + * + * @return The precision of this data type + */ + int precision(); + + /** + * Whether this data type has a precision + * + * @return Whether this data type has a precision + */ + boolean hasPrecision(); + + /** + * Return a new data type like this, with a new scale value + *

+ * This will have no effect if {@link #hasScale()} is false + * + * @param scale The new scale value + * @return The new data type + */ + DataType scale(int scale); + + /** + * Get the scale of this data type + * + * @return The scale of this data type + */ + int scale(); + + /** + * Whether this data type has a scale + * + * @return Whether this data type has a scale + */ + boolean hasScale(); + + /** + * Return a new data type like this, with a new length value + *

+ * This will have no effect if {@link #hasLength()} is false + * + * @param length The new length value + * @return The new data type + */ + DataType length(int length); + + /** + * Get the length of this data type + * + * @return The length of this data type + */ + int length(); + + /** + * Whether this data type has a length + * + * @return Whether this data type has a length + */ + boolean hasLength(); + + /** * Whether this data type is any numeric data type. *

diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDataType.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDataType.java index 76e6d0283f..685d779404 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDataType.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDataType.java @@ -189,22 +189,22 @@ public class DefaultDataType implements DataType { } public DefaultDataType(SQLDialect dialect, DataType sqlDataType, String typeName) { - this(dialect, sqlDataType, sqlDataType.getType(), typeName, typeName); + this(dialect, sqlDataType, sqlDataType.getType(), typeName, typeName, 0, 0, 0); } public DefaultDataType(SQLDialect dialect, DataType sqlDataType, String typeName, String castTypeName) { - this(dialect, sqlDataType, sqlDataType.getType(), typeName, castTypeName); + this(dialect, sqlDataType, sqlDataType.getType(), typeName, castTypeName, 0, 0, 0); } public DefaultDataType(SQLDialect dialect, Class type, String typeName) { - this(dialect, null, type, typeName, typeName); + this(dialect, null, type, typeName, typeName, 0, 0, 0); } public DefaultDataType(SQLDialect dialect, Class type, String typeName, String castTypeName) { - this(dialect, null, type, typeName, castTypeName); + this(dialect, null, type, typeName, castTypeName, 0, 0, 0); } - private DefaultDataType(SQLDialect dialect, DataType sqlDataType, Class type, String typeName, String castTypeName) { + private DefaultDataType(SQLDialect dialect, DataType sqlDataType, Class type, String typeName, String castTypeName, int precision, int scale, int length) { this.dialect = dialect; // [#858] SQLDataTypes should reference themselves for more convenience @@ -214,9 +214,9 @@ public class DefaultDataType implements DataType { this.castTypeName = castTypeName; this.arrayType = (Class) Array.newInstance(type, 0).getClass(); - this.precision = 0; - this.scale = 0; - this.length = 0; + this.precision = precision; + this.scale = scale; + this.length = length; init(); } @@ -247,6 +247,74 @@ public class DefaultDataType implements DataType { } } + @Override + public final DataType precision(int p) { + if (hasPrecision()) { + return new DefaultDataType(dialect, sqlDataType, type, typeName, castTypeName, p, scale, length); + } + + return this; + } + + @Override + public final int precision() { + return precision; + } + + @Override + public final boolean hasPrecision() { + return sqlDataType == SQLDataType.DECIMAL + || sqlDataType == SQLDataType.DECIMAL_INTEGER + || sqlDataType == SQLDataType.NUMERIC; + } + + @Override + public final DataType scale(int s) { + if (hasScale()) { + return new DefaultDataType(dialect, sqlDataType, type, typeName, castTypeName, precision, s, length); + } + + return this; + } + + @Override + public final int scale() { + return scale; + } + + @Override + public final boolean hasScale() { + return hasPrecision(); + } + + @Override + public final DataType length(int l) { + if (hasLength()) { + return new DefaultDataType(dialect, sqlDataType, type, typeName, castTypeName, precision, scale, l); + } + + return this; + } + + @Override + public final int length() { + return length; + } + + @Override + public final boolean hasLength() { + return sqlDataType == SQLDataType.BINARY + || sqlDataType == SQLDataType.BIT + || sqlDataType == SQLDataType.CHAR + || sqlDataType == SQLDataType.LONGNVARCHAR + || sqlDataType == SQLDataType.LONGVARBINARY + || sqlDataType == SQLDataType.LONGVARCHAR + || sqlDataType == SQLDataType.NCHAR + || sqlDataType == SQLDataType.NVARCHAR + || sqlDataType == SQLDataType.VARBINARY + || sqlDataType == SQLDataType.VARCHAR; + } + @Override public final DataType getSQLDataType() { return sqlDataType; @@ -437,12 +505,12 @@ public class DefaultDataType implements DataType { @Override public final > DataType asArrayDataType(Class arrayDataType) { - return new DefaultDataType(dialect, null, arrayDataType, typeName, castTypeName); + return new DefaultDataType(dialect, arrayDataType, typeName, castTypeName); } @Override public final DataType asEnumDataType(Class enumDataType) { - return new DefaultDataType(dialect, null, enumDataType, typeName, castTypeName); + return new DefaultDataType(dialect, enumDataType, typeName, castTypeName); } @Override @@ -481,11 +549,11 @@ public class DefaultDataType implements DataType { } public static DataType getDefaultDataType(String typeName) { - return new DefaultDataType(SQLDialect.SQL99, null, Object.class, typeName, typeName); + return new DefaultDataType(SQLDialect.SQL99, Object.class, typeName, typeName); } public static DataType getDefaultDataType(SQLDialect dialect, String typeName) { - return new DefaultDataType(dialect, null, Object.class, typeName, typeName); + return new DefaultDataType(dialect, Object.class, typeName, typeName); } public static DataType getDataType(SQLDialect dialect, String typeName) {