diff --git a/jOOQ/src/main/java/org/jooq/DataType.java b/jOOQ/src/main/java/org/jooq/DataType.java index bec4d87e49..151fa87bb9 100644 --- a/jOOQ/src/main/java/org/jooq/DataType.java +++ b/jOOQ/src/main/java/org/jooq/DataType.java @@ -205,12 +205,49 @@ public interface DataType extends Serializable { */ boolean nullable(); + /** + * Specify an expression to be applied as the DEFAULT value for + * this data type. + * + * @see #defaultValue(Field) + */ + DataType defaultValue(T defaultValue); + + /** + * Specify an expression to be applied as the DEFAULT value for + * this data type. + *

+ * A default value of a data type applies to DDL statements, such as + *

+ *

+ * The distinct types of possible DEFAULT expressions is + * defined by the underlying database. Please refer to your database manual + * to learn what expressions are possible. + */ + DataType defaultValue(Field defaultValue); + + /** + * The expression to be applied as the DEFAULT value for this + * data type. + * + * @return The default value if present, or null if no default + * value is specified for this data type. + * @see #defaultValue(Field) + */ + Field defaultValue(); + /** * Return a new data type like this, with a new defaultability. * * @param defaulted The new defaultability * @return The new data type + * + * @deprecated - [#3852] - 3.8.0 - Use {@link #defaultValue(Field)} instead. */ + @Deprecated DataType defaulted(boolean defaulted); /** diff --git a/jOOQ/src/main/java/org/jooq/impl/CreateTableImpl.java b/jOOQ/src/main/java/org/jooq/impl/CreateTableImpl.java index b358ed4636..4e0e617414 100644 --- a/jOOQ/src/main/java/org/jooq/impl/CreateTableImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/CreateTableImpl.java @@ -263,6 +263,9 @@ final class CreateTableImpl extends AbstractQuery implements ctx.sql(' ').keyword("not null"); } + if (type.defaulted()) + ctx.sql(' ').keyword("default").sql(' ').visit(type.defaultValue()); + if (i < columnFields.size() - 1) ctx.sql(',').formatSeparator(); } diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDataType.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDataType.java index 1caa5245ea..ea6d795f8f 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDataType.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDataType.java @@ -68,6 +68,7 @@ import org.jooq.Converter; import org.jooq.Converters; import org.jooq.DataType; import org.jooq.EnumType; +import org.jooq.Field; import org.jooq.Result; import org.jooq.SQLDialect; import org.jooq.UDTRecord; @@ -204,7 +205,7 @@ public class DefaultDataType implements DataType { private final String typeName; private final boolean nullable; - private final boolean defaulted; + private final Field defaultValue; private final int precision; private final int scale; private final int length; @@ -230,34 +231,34 @@ public class DefaultDataType implements DataType { } public DefaultDataType(SQLDialect dialect, DataType sqlDataType, String typeName) { - this(dialect, sqlDataType, sqlDataType.getType(), typeName, typeName, sqlDataType.precision(), sqlDataType.scale(), sqlDataType.length(), sqlDataType.nullable(), sqlDataType.defaulted()); + this(dialect, sqlDataType, sqlDataType.getType(), typeName, typeName, sqlDataType.precision(), sqlDataType.scale(), sqlDataType.length(), sqlDataType.nullable(), sqlDataType.defaultValue()); } public DefaultDataType(SQLDialect dialect, DataType sqlDataType, String typeName, String castTypeName) { - this(dialect, sqlDataType, sqlDataType.getType(), typeName, castTypeName, sqlDataType.precision(), sqlDataType.scale(), sqlDataType.length(), sqlDataType.nullable(), sqlDataType.defaulted()); + this(dialect, sqlDataType, sqlDataType.getType(), typeName, castTypeName, sqlDataType.precision(), sqlDataType.scale(), sqlDataType.length(), sqlDataType.nullable(), sqlDataType.defaultValue()); } public DefaultDataType(SQLDialect dialect, Class type, String typeName) { - this(dialect, null, type, typeName, typeName, 0, 0, 0, true, false); + this(dialect, null, type, typeName, typeName, 0, 0, 0, true, null); } public DefaultDataType(SQLDialect dialect, Class type, String typeName, String castTypeName) { - this(dialect, null, type, typeName, castTypeName, 0, 0, 0, true, false); + this(dialect, null, type, typeName, castTypeName, 0, 0, 0, true, null); } - DefaultDataType(SQLDialect dialect, Class type, String typeName, String castTypeName, int precision, int scale, int length, boolean nullable, boolean defaulted) { - this(dialect, null, type, typeName, castTypeName, precision, scale, length, nullable, defaulted); + DefaultDataType(SQLDialect dialect, Class type, String typeName, String castTypeName, int precision, int scale, int length, boolean nullable, Field defaultValue) { + this(dialect, null, type, typeName, castTypeName, precision, scale, length, nullable, defaultValue); } - DefaultDataType(SQLDialect dialect, Class type, Binding binding, String typeName, String castTypeName, int precision, int scale, int length, boolean nullable, boolean defaulted) { - this(dialect, null, type, binding, typeName, castTypeName, precision, scale, length, nullable, defaulted); + DefaultDataType(SQLDialect dialect, Class type, Binding binding, String typeName, String castTypeName, int precision, int scale, int length, boolean nullable, Field defaultValue) { + this(dialect, null, type, binding, typeName, castTypeName, precision, scale, length, nullable, defaultValue); } - DefaultDataType(SQLDialect dialect, DataType sqlDataType, Class type, String typeName, String castTypeName, int precision, int scale, int length, boolean nullable, boolean defaulted) { - this(dialect, sqlDataType, type, null, typeName, castTypeName, precision, scale, length, nullable, defaulted); + DefaultDataType(SQLDialect dialect, DataType sqlDataType, Class type, String typeName, String castTypeName, int precision, int scale, int length, boolean nullable, Field defaultValue) { + this(dialect, sqlDataType, type, null, typeName, castTypeName, precision, scale, length, nullable, defaultValue); } - DefaultDataType(SQLDialect dialect, DataType sqlDataType, Class type, Binding binding, String typeName, String castTypeName, int precision, int scale, int length, boolean nullable, boolean defaulted) { + DefaultDataType(SQLDialect dialect, DataType sqlDataType, Class type, Binding binding, String typeName, String castTypeName, int precision, int scale, int length, boolean nullable, Field defaultValue) { // Initialise final instance members // --------------------------------- @@ -273,7 +274,7 @@ public class DefaultDataType implements DataType { this.arrayType = (Class) Array.newInstance(type, 0).getClass(); this.nullable = nullable; - this.defaulted = defaulted; + this.defaultValue = defaultValue; this.precision = precision0(type, precision); this.scale = scale; this.length = length; @@ -316,7 +317,7 @@ public class DefaultDataType implements DataType { /** * [#3225] Performant constructor for creating derived types. */ - private DefaultDataType(DefaultDataType t, int precision, int scale, int length, boolean nullable, boolean defaulted) { + private DefaultDataType(DefaultDataType t, int precision, int scale, int length, boolean nullable, Field defaultValue) { this.dialect = t.dialect; this.sqlDataType = t.sqlDataType; this.type = t.type; @@ -326,7 +327,7 @@ public class DefaultDataType implements DataType { this.arrayType = t.arrayType; this.nullable = nullable; - this.defaulted = defaulted; + this.defaultValue = defaultValue; this.precision = precision0(type, precision); this.scale = scale; this.length = length; @@ -355,7 +356,7 @@ public class DefaultDataType implements DataType { @Override public final DataType nullable(boolean n) { - return new DefaultDataType(this, precision, scale, length, n, defaulted); + return new DefaultDataType(this, precision, scale, length, n, defaultValue); } @Override @@ -364,13 +365,29 @@ public class DefaultDataType implements DataType { } @Override - public final DataType defaulted(boolean d) { + public final DataType defaultValue(T d) { + return defaultValue(Tools.field(d, this)); + } + + @Override + public final DataType defaultValue(Field d) { return new DefaultDataType(this, precision, scale, length, nullable, d); } + @Override + public final Field defaultValue() { + return defaultValue; + } + + @Override + @Deprecated + public final DataType defaulted(boolean d) { + return defaultValue(d ? Tools.field(null, this) : null); + } + @Override public final boolean defaulted() { - return defaulted; + return defaultValue != null; } @Override @@ -387,7 +404,7 @@ public class DefaultDataType implements DataType { else if (isLob()) return this; else - return new DefaultDataType(this, p, s, length, nullable, defaulted); + return new DefaultDataType(this, p, s, length, nullable, defaultValue); } @Override @@ -409,7 +426,7 @@ public class DefaultDataType implements DataType { if (isLob()) return this; else - return new DefaultDataType(this, precision, s, length, nullable, defaulted); + return new DefaultDataType(this, precision, s, length, nullable, defaultValue); } @Override @@ -431,7 +448,7 @@ public class DefaultDataType implements DataType { if (isLob()) return this; else - return new DefaultDataType(this, precision, scale, l, nullable, defaulted); + return new DefaultDataType(this, precision, scale, l, nullable, defaultValue); } @Override