[#4841] Add code generation support for column DEFAULT values

This commit is contained in:
lukaseder 2016-03-11 15:22:27 +01:00
parent 57dfdbc51d
commit 93ddd14d1f
28 changed files with 151 additions and 91 deletions

View File

@ -4864,7 +4864,7 @@ public class JavaGenerator extends AbstractGenerator {
0,
0,
true,
false,
null,
baseType
) + ".getArrayDataType()";
}
@ -4877,7 +4877,7 @@ public class JavaGenerator extends AbstractGenerator {
type.getScale(),
type.getLength(),
type.isNullable(),
type.isDefaulted(),
type.getDefaultValue(),
type.getUserType()
);
}
@ -4998,7 +4998,7 @@ public class JavaGenerator extends AbstractGenerator {
return type;
}
protected String getTypeReference(Database db, SchemaDefinition schema, String t, int p, int s, int l, boolean n, boolean d, String u) {
protected String getTypeReference(Database db, SchemaDefinition schema, String t, int p, int s, int l, boolean n, String d, String u) {
StringBuilder sb = new StringBuilder();
if (db.getArray(schema, u) != null) {
ArrayDefinition array = database.getArray(schema, u);
@ -5032,7 +5032,10 @@ public class JavaGenerator extends AbstractGenerator {
DataType<?> dataType = null;
try {
dataType = DefaultDataType.getDataType(db.getDialect(), t, p, s).nullable(n).defaulted(d);
dataType = DefaultDataType.getDataType(db.getDialect(), t, p, s).nullable(n);
if (d != null)
dataType = dataType.defaultValue((Field) DSL.field(d, dataType));
}
// Mostly because of unsupported data types. Will be handled later.
@ -5043,32 +5046,34 @@ public class JavaGenerator extends AbstractGenerator {
// specific DataType t, then reference that one.
if (dataType != null && dataType.getSQLDataType() != null) {
DataType<?> sqlDataType = dataType.getSQLDataType();
String sqlDataTypeRef =
SQLDataType.class.getCanonicalName()
+ '.'
+ DefaultDataType.normalise(sqlDataType.getTypeName());
sb.append(SQLDataType.class.getCanonicalName());
sb.append(".");
sb.append(DefaultDataType.normalise(sqlDataType.getTypeName()));
sb.append(sqlDataTypeRef);
if (dataType.hasPrecision() && p > 0) {
sb.append(".precision(").append(p);
if (dataType.hasScale() && s > 0) {
if (dataType.hasScale() && s > 0)
sb.append(", ").append(s);
}
sb.append(")");
}
if (dataType.hasLength() && l > 0) {
if (dataType.hasLength() && l > 0)
sb.append(".length(").append(l).append(")");
}
if (!dataType.nullable()) {
if (!dataType.nullable())
sb.append(".nullable(false)");
}
if (dataType.defaulted()) {
sb.append(".defaulted(true)");
}
if (dataType.defaulted())
sb.append(".defaultValue(org.jooq.impl.DSL.field(\"")
.append(escapeString(d))
.append("\", ")
.append(sqlDataTypeRef)
.append("))");
}
// Otherwise, reference the dialect-specific DataType itself.

View File

@ -1483,19 +1483,19 @@ public abstract class AbstractDatabase implements Database {
DataTypeDefinition type;
if (BigInteger.valueOf(Byte.MAX_VALUE).compareTo(value) >= 0) {
type = new DefaultDataTypeDefinition(this, schema, SQLDataType.NUMERIC.getTypeName(), 0, 2, 0, false, false);
type = new DefaultDataTypeDefinition(this, schema, SQLDataType.NUMERIC.getTypeName(), 0, 2, 0, false, (String) null);
}
else if (BigInteger.valueOf(Short.MAX_VALUE).compareTo(value) >= 0) {
type = new DefaultDataTypeDefinition(this, schema, SQLDataType.NUMERIC.getTypeName(), 0, 4, 0, false, false);
type = new DefaultDataTypeDefinition(this, schema, SQLDataType.NUMERIC.getTypeName(), 0, 4, 0, false, (String) null);
}
else if (BigInteger.valueOf(Integer.MAX_VALUE).compareTo(value) >= 0) {
type = new DefaultDataTypeDefinition(this, schema, SQLDataType.NUMERIC.getTypeName(), 0, 9, 0, false, false);
type = new DefaultDataTypeDefinition(this, schema, SQLDataType.NUMERIC.getTypeName(), 0, 9, 0, false, (String) null);
}
else if (BigInteger.valueOf(Long.MAX_VALUE).compareTo(value) >= 0) {
type = new DefaultDataTypeDefinition(this, schema, SQLDataType.NUMERIC.getTypeName(), 0, 18, 0, false, false);
type = new DefaultDataTypeDefinition(this, schema, SQLDataType.NUMERIC.getTypeName(), 0, 18, 0, false, (String) null);
}
else {
type = new DefaultDataTypeDefinition(this, schema, SQLDataType.NUMERIC.getTypeName(), 0, 38, 0, false, false);
type = new DefaultDataTypeDefinition(this, schema, SQLDataType.NUMERIC.getTypeName(), 0, 38, 0, false, (String) null);
}
return type;

View File

@ -134,7 +134,7 @@ abstract class AbstractTypedElementDefinition<T extends Definition>
if (dataType != null) {
if (dataType.getSQLType() == Types.DATE) {
DataType<?> forcedDataType = DefaultDataType.getDataType(db.getDialect(), SQLDataType.TIMESTAMP.getTypeName(), 0, 0);
result = new DefaultDataTypeDefinition(db, child.getSchema(), forcedDataType.getTypeName(), 0, 0, 0, result.isNullable(), result.isDefaulted(), null, null, DateAsTimestampBinding.class.getName());
result = new DefaultDataTypeDefinition(db, child.getSchema(), forcedDataType.getTypeName(), 0, 0, 0, result.isNullable(), result.getDefaultValue(), null, null, DateAsTimestampBinding.class.getName());
}
}
}
@ -164,7 +164,7 @@ abstract class AbstractTypedElementDefinition<T extends Definition>
DataType<?> forcedDataType = null;
boolean n = result.isNullable();
boolean d = result.isDefaulted();
String d = result.getDefaultValue();
int l = 0;
int p = 0;

View File

@ -100,6 +100,11 @@ public interface DataTypeDefinition {
*/
boolean isDefaulted();
/**
* The default value expression.
*/
String getDefaultValue();
/**
* Whether this data type represents a udt.
*/

View File

@ -60,32 +60,76 @@ public class DefaultDataTypeDefinition implements DataTypeDefinition {
private final String converter;
private final String binding;
private final boolean nullable;
private final boolean defaulted;
private final String defaultValue;
private final int length;
private final int precision;
private final int scale;
public DefaultDataTypeDefinition(Database database, SchemaDefinition schema, String typeName) {
this(database, schema, typeName, null, null, null, null, null, null);
private static final String defaultValue(Boolean defaultable) {
return defaultable != null && defaultable ? "NULL" : null;
}
public DefaultDataTypeDefinition(Database database, SchemaDefinition schema, String typeName) {
this(database, schema, typeName, null, null, null, null, (String) null, null);
}
/**
* @deprecated - [#4841] - 3.8.0 - Use {@link #DefaultDataTypeDefinition(Database, SchemaDefinition, String, Number, Number, Number, Boolean, String)} instead.
*/
@Deprecated
public DefaultDataTypeDefinition(Database database, SchemaDefinition schema, String typeName, Number length, Number precision, Number scale, Boolean nullable, Boolean defaultable) {
this(database, schema, typeName, length, precision, scale, nullable, defaultable, typeName, null);
}
public DefaultDataTypeDefinition(Database database, SchemaDefinition schema, String typeName, Number length, Number precision, Number scale, Boolean nullable, String defaultValue) {
this(database, schema, typeName, length, precision, scale, nullable, defaultValue, typeName, null);
}
/**
* @deprecated - [#4841] - 3.8.0 - Use {@link #DefaultDataTypeDefinition(Database, SchemaDefinition, String, Number, Number, Number, Boolean, String, String)} instead.
*/
@Deprecated
public DefaultDataTypeDefinition(Database database, SchemaDefinition schema, String typeName, Number length, Number precision, Number scale, Boolean nullable, Boolean defaultable, String userType) {
this(database, schema, typeName, length, precision, scale, nullable, defaultable, userType, null);
}
public DefaultDataTypeDefinition(Database database, SchemaDefinition schema, String typeName, Number length, Number precision, Number scale, Boolean nullable, String defaultValue, String userType) {
this(database, schema, typeName, length, precision, scale, nullable, defaultValue, userType, null);
}
/**
* @deprecated - [#4841] - 3.8.0 - Use {@link #DefaultDataTypeDefinition(Database, SchemaDefinition, String, Number, Number, Number, Boolean, String, String, String)} instead.
*/
@Deprecated
public DefaultDataTypeDefinition(Database database, SchemaDefinition schema, String typeName, Number length, Number precision, Number scale, Boolean nullable, Boolean defaultable, String userType, String converter) {
this(database, schema, typeName, length, precision, scale, nullable, defaultable, userType, converter, null);
}
public DefaultDataTypeDefinition(Database database, SchemaDefinition schema, String typeName, Number length, Number precision, Number scale, Boolean nullable, String defaultValue, String userType, String converter) {
this(database, schema, typeName, length, precision, scale, nullable, defaultValue, userType, converter, null);
}
/**
* @deprecated - [#4841] - 3.8.0 - Use {@link #DefaultDataTypeDefinition(Database, SchemaDefinition, String, Number, Number, Number, Boolean, String, String, String, String)} instead.
*/
@Deprecated
public DefaultDataTypeDefinition(Database database, SchemaDefinition schema, String typeName, Number length, Number precision, Number scale, Boolean nullable, Boolean defaultable, String userType, String converter, String binding) {
this(database, schema, typeName, length, precision, scale, nullable, defaultable, userType, converter, binding, null);
}
public DefaultDataTypeDefinition(Database database, SchemaDefinition schema, String typeName, Number length, Number precision, Number scale, Boolean nullable, String defaultValue, String userType, String converter, String binding) {
this(database, schema, typeName, length, precision, scale, nullable, defaultValue, userType, converter, binding, null);
}
/**
* @deprecated - [#4841] - 3.8.0 - Use {@link #DefaultDataTypeDefinition(Database, SchemaDefinition, String, Number, Number, Number, Boolean, String, String, String, String, String)} instead.
*/
@Deprecated
public DefaultDataTypeDefinition(Database database, SchemaDefinition schema, String typeName, Number length, Number precision, Number scale, Boolean nullable, Boolean defaultable, String userType, String converter, String binding, String javaType) {
this(database, schema, typeName, length, precision, scale, nullable, defaultValue(defaultable), userType, converter, binding, javaType);
}
public DefaultDataTypeDefinition(Database database, SchemaDefinition schema, String typeName, Number length, Number precision, Number scale, Boolean nullable, String defaultValue, String userType, String converter, String binding, String javaType) {
this.database = database;
this.schema = schema;
@ -114,7 +158,7 @@ public class DefaultDataTypeDefinition implements DataTypeDefinition {
this.precision = precision == null ? 0 : precision.intValue();
this.scale = scale == null ? 0 : scale.intValue();
this.nullable = nullable == null ? true : nullable.booleanValue();
this.defaulted = defaultable == null ? false : defaultable.booleanValue();
this.defaultValue = defaultValue;
}
@Override
@ -138,7 +182,12 @@ public class DefaultDataTypeDefinition implements DataTypeDefinition {
@Override
public final boolean isDefaulted() {
return defaulted;
return getDefaultValue() != null;
}
@Override
public final String getDefaultValue() {
return defaultValue;
}
@Override

View File

@ -42,7 +42,8 @@
package org.jooq.util.cubrid;
import static org.jooq.impl.DSL.concat;
import static org.jooq.impl.DSL.fieldByName;
import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.name;
import static org.jooq.impl.DSL.val;
import static org.jooq.tools.StringUtils.defaultIfNull;
import static org.jooq.util.cubrid.dba.Tables.DB_CLASS;
@ -242,7 +243,7 @@ public class CUBRIDDatabase extends AbstractDatabase {
List<EnumDefinition> result = new ArrayList<EnumDefinition>();
for (TableDefinition tableDefinition : getTables(getSchemata().get(0))) {
for (Record record : create().fetch("SHOW COLUMNS FROM {0} WHERE TYPE LIKE 'ENUM(%)'", fieldByName(tableDefinition.getInputName()))) {
for (Record record : create().fetch("SHOW COLUMNS FROM {0} WHERE TYPE LIKE 'ENUM(%)'", field(name(tableDefinition.getInputName())))) {
String table = tableDefinition.getInputName();
String column = record.get("Field", String.class);
String columnType = record.get("Type", String.class);

View File

@ -97,7 +97,7 @@ public class CUBRIDTableDefinition extends AbstractTableDefinition {
record.get(DB_ATTRIBUTE.PREC),
record.get(DB_ATTRIBUTE.SCALE),
record.get(DB_ATTRIBUTE.IS_NULLABLE, boolean.class),
record.get(DB_ATTRIBUTE.DEFAULT_VALUE) != null,
record.get(DB_ATTRIBUTE.DEFAULT_VALUE),
getName() + "_" + record.get(DB_ATTRIBUTE.ATTR_NAME)
);

View File

@ -98,7 +98,7 @@ public class DerbyTableDefinition extends AbstractTableDefinition {
precision,
scale,
!parseNotNull(typeName),
record.get(Syscolumns.COLUMNDEFAULT) != null
record.get(Syscolumns.COLUMNDEFAULT)
);
ColumnDefinition column = new DefaultColumnDefinition(

View File

@ -101,7 +101,7 @@ public class FirebirdRoutineDefinition extends AbstractRoutineDefinition {
record.get(f.RDB$FIELD_PRECISION),
record.get("FIELD_SCALE", Integer.class),
record.get(p.RDB$NULL_FLAG) == 0,
record.get(p.RDB$DEFAULT_SOURCE) != null
record.get(p.RDB$DEFAULT_SOURCE)
);
ParameterDefinition parameter = new DefaultParameterDefinition(

View File

@ -107,7 +107,7 @@ public class FirebirdTableDefinition extends AbstractTableDefinition {
record.get(f.RDB$FIELD_PRECISION),
record.get("FIELD_SCALE", Integer.class),
record.get(r.RDB$NULL_FLAG) == 0,
record.get(r.RDB$DEFAULT_SOURCE) != null
record.get(r.RDB$DEFAULT_SOURCE)
);
ColumnDefinition column = new DefaultColumnDefinition(

View File

@ -112,7 +112,7 @@ public class FirebirdTableValuedFunction extends AbstractTableDefinition {
record.get(f.RDB$FIELD_PRECISION),
record.get("FIELD_SCALE", Integer.class),
record.get(p.RDB$NULL_FLAG) == 0,
record.get(p.RDB$DEFAULT_SOURCE) != null
record.get(p.RDB$DEFAULT_SOURCE)
);
ColumnDefinition column = new DefaultColumnDefinition(

View File

@ -76,7 +76,7 @@ public class H2RoutineDefinition extends AbstractRoutineDefinition {
precision,
scale,
null,
null
(String) null
);
this.returnValue = new DefaultParameterDefinition(this, "RETURN_VALUE", -1, type);
@ -93,7 +93,7 @@ public class H2RoutineDefinition extends AbstractRoutineDefinition {
FunctionColumns.SCALE,
FunctionColumns.POS,
FunctionColumns.NULLABLE,
FunctionColumns.COLUMN_DEFAULT.nvl2(true, false).as("default"))
FunctionColumns.COLUMN_DEFAULT)
.from(FUNCTION_COLUMNS)
.where(FunctionColumns.ALIAS_SCHEMA.equal(getSchema().getName()))
.and(FunctionColumns.ALIAS_NAME.equal(getName()))
@ -109,7 +109,7 @@ public class H2RoutineDefinition extends AbstractRoutineDefinition {
Short scale = record.get(FunctionColumns.SCALE);
int position = record.get(FunctionColumns.POS);
boolean nullable = record.get(FunctionColumns.NULLABLE, boolean.class);
boolean defaulted = record.get("default", boolean.class);
String defaultValue = record.get(FunctionColumns.COLUMN_DEFAULT);
// VERY special case for H2 alias/function parameters. The first parameter
// may be a java.sql.Connection object and in such cases it should NEVER be used.
@ -125,7 +125,7 @@ public class H2RoutineDefinition extends AbstractRoutineDefinition {
precision,
scale,
nullable,
defaulted
defaultValue
);
ParameterDefinition parameter = new DefaultParameterDefinition(this, paramName, position, type);

View File

@ -79,7 +79,7 @@ public class H2TableDefinition extends AbstractTableDefinition {
Columns.NUMERIC_PRECISION,
Columns.NUMERIC_SCALE,
Columns.IS_NULLABLE,
Columns.COLUMN_DEFAULT.nvl2(true, false).as("default"),
Columns.COLUMN_DEFAULT,
Columns.REMARKS,
Columns.SEQUENCE_NAME)
.from(COLUMNS)
@ -96,7 +96,7 @@ public class H2TableDefinition extends AbstractTableDefinition {
record.get(Columns.NUMERIC_PRECISION),
record.get(Columns.NUMERIC_SCALE),
record.get(Columns.IS_NULLABLE, boolean.class),
record.get("default", boolean.class));
record.get(Columns.COLUMN_DEFAULT));
ColumnDefinition column = new DefaultColumnDefinition(
getDatabase().getTable(getSchema(), getName()),

View File

@ -42,7 +42,7 @@
package org.jooq.util.hsqldb;
import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.fieldByName;
import static org.jooq.impl.DSL.name;
import static org.jooq.impl.DSL.nvl;
import static org.jooq.util.hsqldb.information_schema.Tables.CHECK_CONSTRAINTS;
import static org.jooq.util.hsqldb.information_schema.Tables.ELEMENT_TYPES;
@ -195,7 +195,7 @@ public class HSQLDBDatabase extends AbstractDatabase {
CheckConstraints cc = CHECK_CONSTRAINTS.as("cc");
// [#2808] [#3019] Workaround for bad handling of JOIN .. USING
Field<String> constraintName = fieldByName(String.class, cc.CONSTRAINT_NAME.getName());
Field<String> constraintName = field(name(cc.CONSTRAINT_NAME.getName()), String.class);
for (Record record : create()
.select(

View File

@ -86,7 +86,7 @@ public class HSQLDBRoutineDefinition extends AbstractRoutineDefinition {
precision,
scale,
null,
null
(String) null
);
this.returnValue = new DefaultParameterDefinition(this, "RETURN_VALUE", -1, type);
@ -132,7 +132,7 @@ public class HSQLDBRoutineDefinition extends AbstractRoutineDefinition {
record.get(PARAMETERS.NUMERIC_PRECISION),
record.get(PARAMETERS.NUMERIC_SCALE),
null,
null
(String) null
);
ParameterDefinition parameter = new DefaultParameterDefinition(

View File

@ -105,7 +105,7 @@ public class HSQLDBTableDefinition extends AbstractTableDefinition {
record.get(COLUMNS.NUMERIC_PRECISION),
record.get(COLUMNS.NUMERIC_SCALE),
record.get(COLUMNS.IS_NULLABLE, boolean.class),
record.get(COLUMNS.COLUMN_DEFAULT) != null,
record.get(COLUMNS.COLUMN_DEFAULT),
record.get(COLUMNS.UDT_NAME)
);

View File

@ -81,7 +81,7 @@ public class JDBCTableDefinition extends AbstractTableDefinition {
field.getDataType().precision(),
field.getDataType().scale(),
field.getDataType().nullable(),
field.getDataType().defaulted(),
create().renderInlined(field.getDataType().defaultValue()),
null
);

View File

@ -125,7 +125,7 @@ public class MySQLRoutineDefinition extends AbstractRoutineDefinition {
record.get(Parameters.NUMERIC_PRECISION),
record.get(Parameters.NUMERIC_SCALE),
null,
null
(String) null
);
if (inOut == null) {
@ -196,7 +196,7 @@ public class MySQLRoutineDefinition extends AbstractRoutineDefinition {
precision,
scale,
null,
null
(String) null
);
return new DefaultParameterDefinition(this, paramName, columnIndex, type);

View File

@ -88,8 +88,7 @@ public class MySQLTableDefinition extends AbstractTableDefinition {
.from(COLUMNS)
.where(TABLE_SCHEMA.equal(getSchema().getName()))
.and(TABLE_NAME.equal(getName()))
.orderBy(ORDINAL_POSITION)
.fetch()) {
.orderBy(ORDINAL_POSITION)) {
String dataType = record.get(Columns.DATA_TYPE);
@ -110,7 +109,7 @@ public class MySQLTableDefinition extends AbstractTableDefinition {
record.get(Columns.NUMERIC_PRECISION),
record.get(Columns.NUMERIC_SCALE),
record.get(Columns.IS_NULLABLE, boolean.class),
record.get(Columns.COLUMN_DEFAULT) != null,
record.get(Columns.COLUMN_DEFAULT),
getName() + "_" + record.get(Columns.COLUMN_NAME)
);

View File

@ -63,6 +63,7 @@ import static org.jooq.util.postgres.PostgresDSL.oid;
import static org.jooq.util.postgres.information_schema.Tables.ATTRIBUTES;
import static org.jooq.util.postgres.information_schema.Tables.CHECK_CONSTRAINTS;
import static org.jooq.util.postgres.information_schema.Tables.KEY_COLUMN_USAGE;
import static org.jooq.util.postgres.information_schema.Tables.PARAMETERS;
import static org.jooq.util.postgres.information_schema.Tables.ROUTINES;
import static org.jooq.util.postgres.information_schema.Tables.SEQUENCES;
import static org.jooq.util.postgres.information_schema.Tables.TABLES;
@ -137,6 +138,7 @@ public class PostgresDatabase extends AbstractDatabase {
private static final JooqLogger log = JooqLogger.getLogger(PostgresDatabase.class);
private static Boolean is84;
private static Boolean is94;
private static Boolean canCastToEnumType;
@Override
@ -462,7 +464,7 @@ public class PostgresDatabase extends AbstractDatabase {
record.get(SEQUENCES.NUMERIC_PRECISION),
record.get(SEQUENCES.NUMERIC_SCALE),
false,
false
(String) null
);
result.add(new DefaultSequenceDefinition(schema, record.get(SEQUENCES.SEQUENCE_NAME), type));
@ -594,7 +596,7 @@ public class PostgresDatabase extends AbstractDatabase {
record.get(b.TYPLEN),
0, // ?
!record.get(d.TYPNOTNULL, boolean.class),
record.get(d.TYPDEFAULT) != null,
record.get(d.TYPDEFAULT),
record.get(b.TYPNAME, String.class)
);
@ -706,7 +708,7 @@ public class PostgresDatabase extends AbstractDatabase {
return DSL.using(getConnection(), SQLDialect.POSTGRES);
}
private boolean is84() {
boolean is84() {
if (is84 == null) {
// [#2916] Window functions were introduced with PostgreSQL 9.0
@ -725,6 +727,28 @@ public class PostgresDatabase extends AbstractDatabase {
return is84;
}
boolean is94() {
if (is94 == null) {
// [#4254] INFORMATION_SCHEMA.PARAMETERS.PARAMETER_DEFAULT was added
// in PostgreSQL 9.4 only
try {
create(true)
.select(PARAMETERS.PARAMETER_DEFAULT)
.from(PARAMETERS)
.where(falseCondition())
.fetch();
is94 = true;
}
catch (DataAccessException e) {
is94 = false;
}
}
return is94;
}
private List<String> enumLabels(String nspname, String typname) {
Field<Object> cast = field("{0}::{1}", PG_ENUM.ENUMLABEL, name(nspname, typname));

View File

@ -174,7 +174,7 @@ public class PostgresMaterializedViewDefinition extends AbstractTableDefinition
record.get(COLUMNS.NUMERIC_PRECISION),
record.get(COLUMNS.NUMERIC_SCALE),
record.get(COLUMNS.IS_NULLABLE, boolean.class),
record.get(COLUMNS.COLUMN_DEFAULT) != null,
record.get(COLUMNS.COLUMN_DEFAULT),
record.get(COLUMNS.UDT_NAME)
);

View File

@ -41,7 +41,6 @@
package org.jooq.util.postgres;
import static org.jooq.impl.DSL.falseCondition;
import static org.jooq.impl.DSL.inline;
import static org.jooq.util.postgres.information_schema.Tables.PARAMETERS;
import static org.jooq.util.postgres.information_schema.Tables.ROUTINES;
@ -51,7 +50,6 @@ import java.sql.SQLException;
import java.util.Arrays;
import org.jooq.Record;
import org.jooq.exception.DataAccessException;
import org.jooq.tools.StringUtils;
import org.jooq.util.AbstractRoutineDefinition;
import org.jooq.util.DataTypeDefinition;
@ -69,7 +67,6 @@ import org.jooq.util.SchemaDefinition;
*/
public class PostgresRoutineDefinition extends AbstractRoutineDefinition {
private static Boolean is94;
private final String specificName;
public PostgresRoutineDefinition(Database database, Record record) {
@ -97,7 +94,7 @@ public class PostgresRoutineDefinition extends AbstractRoutineDefinition {
record.get(ROUTINES.NUMERIC_PRECISION),
record.get(ROUTINES.NUMERIC_SCALE),
null,
null,
(String) null,
record.get(ROUTINES.TYPE_UDT_NAME)
);
@ -125,7 +122,7 @@ public class PostgresRoutineDefinition extends AbstractRoutineDefinition {
PARAMETERS.UDT_NAME,
PARAMETERS.ORDINAL_POSITION,
PARAMETERS.PARAMETER_MODE,
is94()
((PostgresDatabase) getDatabase()).is94()
? PARAMETERS.PARAMETER_DEFAULT
: inline((String) null).as(PARAMETERS.PARAMETER_DEFAULT))
.from(PARAMETERS)
@ -144,7 +141,7 @@ public class PostgresRoutineDefinition extends AbstractRoutineDefinition {
record.get(PARAMETERS.NUMERIC_PRECISION),
record.get(PARAMETERS.NUMERIC_SCALE),
null,
record.get(PARAMETERS.PARAMETER_DEFAULT) != null,
record.get(PARAMETERS.PARAMETER_DEFAULT),
record.get(PARAMETERS.UDT_NAME)
);
@ -160,26 +157,4 @@ public class PostgresRoutineDefinition extends AbstractRoutineDefinition {
addParameter(InOutDefinition.getFromString(inOut), parameter);
}
}
private boolean is94() {
if (is94 == null) {
// [#4254] INFORMATION_SCHEMA.PARAMETERS.PARAMETER_DEFAULT was added
// in PostgreSQL 9.4 only
try {
create(true)
.select(PARAMETERS.PARAMETER_DEFAULT)
.from(PARAMETERS)
.where(falseCondition())
.fetch();
is94 = true;
}
catch (DataAccessException e) {
is94 = false;
}
}
return is94;
}
}

View File

@ -113,7 +113,7 @@ public class PostgresTableDefinition extends AbstractTableDefinition {
record.get(COLUMNS.NUMERIC_PRECISION),
record.get(COLUMNS.NUMERIC_SCALE),
record.get(COLUMNS.IS_NULLABLE, boolean.class),
record.get(COLUMNS.COLUMN_DEFAULT) != null,
record.get(COLUMNS.COLUMN_DEFAULT),
record.get(COLUMNS.UDT_NAME)
);

View File

@ -112,7 +112,9 @@ public class PostgresTableValuedFunction extends AbstractTableDefinition {
p.NUMERIC_PRECISION,
p.NUMERIC_SCALE,
inline("true").as(c.IS_NULLABLE),
inline(null, String.class).as(c.COLUMN_DEFAULT),
(((PostgresDatabase) getDatabase()).is94()
? PARAMETERS.PARAMETER_DEFAULT
: inline((String) null)).as(c.COLUMN_DEFAULT),
p.UDT_SCHEMA,
p.UDT_NAME
)
@ -183,7 +185,7 @@ public class PostgresTableValuedFunction extends AbstractTableDefinition {
record.get(p.NUMERIC_PRECISION),
record.get(p.NUMERIC_SCALE),
record.get(c.IS_NULLABLE, boolean.class),
record.get(c.COLUMN_DEFAULT) != null,
record.get(c.COLUMN_DEFAULT),
record.get(p.UDT_NAME)
);

View File

@ -97,7 +97,7 @@ public class PostgresUDTDefinition extends AbstractUDTDefinition {
record.get(ATTRIBUTES.NUMERIC_PRECISION),
record.get(ATTRIBUTES.NUMERIC_SCALE),
record.get(ATTRIBUTES.IS_NULLABLE, boolean.class),
record.get(ATTRIBUTES.ATTRIBUTE_DEFAULT) != null,
record.get(ATTRIBUTES.ATTRIBUTE_DEFAULT),
record.get(ATTRIBUTES.ATTRIBUTE_UDT_NAME)
);

View File

@ -96,7 +96,7 @@ public class SQLiteTableDefinition extends AbstractTableDefinition {
precision,
scale,
!record.get("notnull", boolean.class),
record.get("dflt_value") != null
record.get("dflt_value", String.class)
);
ColumnDefinition column = new DefaultColumnDefinition(

View File

@ -332,7 +332,7 @@ public class XMLDatabase extends AbstractDatabase {
sequence.getNumericPrecision(),
sequence.getNumericScale(),
false,
false
(String) null
);
result.add(new DefaultSequenceDefinition(schema, sequence.getSequenceName(), type));

View File

@ -91,7 +91,7 @@ public class XMLTableDefinition extends AbstractTableDefinition {
unbox(column.getNumericPrecision()),
unbox(column.getNumericScale()),
column.isIsNullable(),
column.getColumnDefault() != null
column.getColumnDefault()
);
result.add(new DefaultColumnDefinition(