use of org.jooq.meta.DefaultDataTypeDefinition in project jOOQ by jOOQ.
the class JavaGenerator method getTypeReference.
protected String getTypeReference(Database db, SchemaDefinition schema, JavaWriter out, String t, int p, int s, int l, boolean n, boolean i, boolean r, String g, GenerationOption go, String d, Name u) {
StringBuilder sb = new StringBuilder();
if (db.getArray(schema, u) != null) {
ArrayDefinition array = database.getArray(schema, u);
sb.append(getJavaTypeReference(db, array.getElementType(resolver(out)), out));
sb.append(array.getIndexType() != null ? ".asAssociativeArrayDataType(" : ".asArrayDataType(");
sb.append(classOf(getStrategy().getFullJavaClassName(array, Mode.RECORD)));
sb.append(")");
} else if (db.getDomain(schema, u) != null) {
sb.append(getStrategy().getFullJavaIdentifier(db.getDomain(schema, u)));
sb.append(".getDataType()");
} else if (db.getUDT(schema, u) != null) {
sb.append(getStrategy().getFullJavaIdentifier(db.getUDT(schema, u)));
sb.append(".getDataType()");
} else // [#5334] In MySQL, the user type is (ab)used for synthetic enum types. This can lead to accidental matches here
if (SUPPORT_TABLE_AS_UDT.contains(db.getDialect()) && db.getTable(schema, u) != null) {
sb.append(getStrategy().getFullJavaIdentifier(db.getTable(schema, u)));
sb.append(".getDataType()");
} else if (db.getEnum(schema, u) != null) {
sb.append(getJavaTypeReference(db, new DefaultDataTypeDefinition(db, schema, DefaultDataType.getDataType(db.getDialect(), String.class).getTypeName(), l, p, s, n, d, (Name) null), out));
sb.append(".asEnumDataType(");
sb.append(classOf(getStrategy().getFullJavaClassName(db.getEnum(schema, u), Mode.ENUM)));
sb.append(")");
} else {
DataType<?> dataType;
String sqlDataTypeRef;
try {
dataType = mapJavaTimeTypes(getDataType(db, t, p, s));
}// Mostly because of unsupported data types.
catch (SQLDialectNotSupportedException ignore) {
dataType = SQLDataType.OTHER.nullable(n).identity(i);
sb = new StringBuilder();
sb.append(DefaultDataType.class.getName());
sb.append(".getDefaultDataType(\"");
sb.append(escapeString(u != null ? u.toString() : t));
sb.append("\")");
}
dataType = dataType.nullable(n).identity(i);
if (d != null)
dataType = dataType.defaultValue((Field) DSL.field(d, dataType));
// specific DataType t, then reference that one.
if (dataType.getSQLDataType() != null && sb.length() == 0) {
DataType<?> sqlDataType = dataType.getSQLDataType();
String literal = SQLDATATYPE_LITERAL_LOOKUP.get(sqlDataType);
sqlDataTypeRef = out.ref(SQLDataType.class) + '.' + (literal == null ? "OTHER" : literal);
sb.append(sqlDataTypeRef);
if (dataType.hasPrecision() && (dataType.isTimestamp() || p > 0)) {
// [#6411] Call static method if available, rather than instance method
if (SQLDATATYPE_WITH_PRECISION.contains(literal))
sb.append('(').append(p);
else
sb.append(".precision(").append(p);
if (dataType.hasScale() && s > 0)
sb.append(", ").append(s);
sb.append(')');
}
if (dataType.hasLength() && l > 0)
// [#6411] Call static method if available, rather than instance method
if (SQLDATATYPE_WITH_LENGTH.contains(literal))
sb.append("(").append(l).append(")");
else
sb.append(".length(").append(l).append(")");
} else {
sqlDataTypeRef = SQLDataType.class.getCanonicalName() + ".OTHER";
if (sb.length() == 0)
sb.append(sqlDataTypeRef);
}
if (!dataType.nullable())
sb.append(".nullable(false)");
if (dataType.identity())
sb.append(".identity(true)");
// report actual values (e.g. MySQL).
if (dataType.defaulted()) {
sb.append(".defaultValue(");
if (asList(MYSQL).contains(db.getDialect().family()))
// a CURRENT_TIMESTAMP expression, inconsistently
if (d != null && d.toLowerCase(getStrategy().getTargetLocale()).startsWith("current_timestamp"))
sb.append(out.ref(DSL.class)).append(".field(\"").append(escapeString(d)).append("\"");
else
sb.append(out.ref(DSL.class)).append(".inline(\"").append(escapeString(d)).append("\"");
else
sb.append(out.ref(DSL.class)).append(".field(\"").append(escapeString(d)).append("\"");
sb.append(", ").append(sqlDataTypeRef).append(")").append(kotlin && dataType.getType() == Object.class ? " as Any?" : "").append(")");
}
}
return sb.toString();
}
use of org.jooq.meta.DefaultDataTypeDefinition in project jOOQ by jOOQ.
the class FirebirdRoutineDefinition method init0.
@Override
protected void init0() throws SQLException {
Rdb$procedureParameters p = RDB$PROCEDURE_PARAMETERS.as("p");
Rdb$functionArguments a = RDB$FUNCTION_ARGUMENTS.as("a");
Rdb$fields f = RDB$FIELDS.as("f");
int i = 0;
for (Record record : returnValue == null ? create().select(p.RDB$PARAMETER_NUMBER, p.RDB$PARAMETER_TYPE, p.RDB$PARAMETER_NAME.trim().as(p.RDB$PARAMETER_NAME), FIELD_TYPE(f).as("FIELD_TYPE"), CHARACTER_LENGTH(f).as("CHAR_LEN"), f.RDB$FIELD_PRECISION, FIELD_SCALE(f).as("FIELD_SCALE"), DSL.bitOr(p.RDB$NULL_FLAG.nvl((short) 0), f.RDB$NULL_FLAG.nvl((short) 0)).as(p.RDB$NULL_FLAG), p.RDB$DEFAULT_SOURCE).from(p).leftOuterJoin(f).on(p.RDB$FIELD_SOURCE.eq(f.RDB$FIELD_NAME)).where(p.RDB$PROCEDURE_NAME.eq(getName())).orderBy(p.RDB$PARAMETER_TYPE.desc(), p.RDB$PARAMETER_NUMBER.asc()) : create().select(a.RDB$ARGUMENT_POSITION.as(p.RDB$PARAMETER_NUMBER), inline(0).as(p.RDB$PARAMETER_TYPE), a.RDB$ARGUMENT_NAME.trim().as(p.RDB$PARAMETER_NAME), FIELD_TYPE(f).as("FIELD_TYPE"), CHARACTER_LENGTH(f).as("CHAR_LEN"), f.RDB$FIELD_PRECISION, FIELD_SCALE(f).as("FIELD_SCALE"), DSL.bitOr(a.RDB$NULL_FLAG.nvl((short) 0), f.RDB$NULL_FLAG.nvl((short) 0)).as(p.RDB$NULL_FLAG), a.RDB$DEFAULT_SOURCE).from(a).leftOuterJoin(f).on(a.RDB$FIELD_SOURCE.eq(f.RDB$FIELD_NAME)).where(a.RDB$FUNCTION_NAME.eq(getName())).and(a.RDB$ARGUMENT_POSITION.gt(inline((short) 0))).orderBy(a.RDB$ARGUMENT_POSITION)) {
DataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), getSchema(), record.get("FIELD_TYPE", String.class), record.get("CHAR_LEN", short.class), 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));
ParameterDefinition parameter = new DefaultParameterDefinition(this, record.get(p.RDB$PARAMETER_NAME), i++, type);
addParameter(record.get(p.RDB$PARAMETER_TYPE, int.class).equals(0) ? InOutDefinition.IN : InOutDefinition.OUT, parameter);
}
}
use of org.jooq.meta.DefaultDataTypeDefinition in project jOOQ by jOOQ.
the class FirebirdTableValuedFunction method getElements0.
@Override
protected List<ColumnDefinition> getElements0() throws SQLException {
List<ColumnDefinition> result = new ArrayList<>();
Rdb$procedureParameters p = RDB$PROCEDURE_PARAMETERS.as("p");
Rdb$fields f = RDB$FIELDS.as("f");
// DatabaseMetaData implementation
for (Record record : create().select(p.RDB$PARAMETER_NUMBER, p.RDB$PARAMETER_NAME.trim(), p.RDB$DESCRIPTION, p.RDB$DEFAULT_VALUE, DSL.bitOr(p.RDB$NULL_FLAG.nvl((short) 0), f.RDB$NULL_FLAG.nvl((short) 0)).as(p.RDB$NULL_FLAG), p.RDB$DEFAULT_SOURCE, // [#3342] FIELD_LENGTH should be ignored for LOBs
CHARACTER_LENGTH(f).as("CHAR_LEN"), f.RDB$FIELD_PRECISION, FIELD_SCALE(f).as("FIELD_SCALE"), FIELD_TYPE(f).as("FIELD_TYPE"), f.RDB$FIELD_SUB_TYPE).from(p).leftOuterJoin(f).on(p.RDB$FIELD_SOURCE.eq(f.RDB$FIELD_NAME)).where(p.RDB$PROCEDURE_NAME.eq(getName())).and(p.RDB$PARAMETER_TYPE.eq((short) 1)).orderBy(p.RDB$PARAMETER_NUMBER)) {
DefaultDataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), getSchema(), record.get("FIELD_TYPE", String.class), record.get("CHAR_LEN", short.class), 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));
result.add(new DefaultColumnDefinition(getDatabase().getTable(getSchema(), getName()), record.get(p.RDB$PARAMETER_NAME.trim()), result.size() + 1, type, false, null));
}
return result;
}
use of org.jooq.meta.DefaultDataTypeDefinition in project jOOQ by jOOQ.
the class H2Database method getSequences0.
@Override
protected List<SequenceDefinition> getSequences0() throws SQLException {
List<SequenceDefinition> result = new ArrayList<>();
for (Record record : sequences(getInputSchemata())) {
SchemaDefinition schema = getSchema(record.get(SEQUENCES.SEQUENCE_SCHEMA));
if (schema != null) {
String name = record.get(SEQUENCES.SEQUENCE_NAME);
DefaultDataTypeDefinition type = new DefaultDataTypeDefinition(this, schema, H2DataType.BIGINT.getTypeName());
result.add(new DefaultSequenceDefinition(schema, name, type, null, // H2 doesn't support Postgres-style START WITH
null, record.get(SEQUENCES.INCREMENT), record.get(SEQUENCES.MIN_VALUE), record.get(SEQUENCES.MAX_VALUE), record.get(SEQUENCES.IS_CYCLE), record.get(SEQUENCES.CACHE)));
}
}
return result;
}
use of org.jooq.meta.DefaultDataTypeDefinition in project jOOQ by jOOQ.
the class H2TableDefinition method getElements1_4.
public List<ColumnDefinition> getElements1_4() {
List<ColumnDefinition> result = new ArrayList<>();
// [#7206] H2 defaults to these precision/scale values when a DECIMAL/NUMERIC type
// does not have any precision/scale. What works in H2 works in almost no
// other database, which is relevant when using the DDLDatabase for instance,
// which is based on the H2Database
Param<Long> maxP = inline(65535L);
Param<Long> maxS = inline(32767L);
H2Database db = (H2Database) getDatabase();
for (Record record : create().select(COLUMNS.COLUMN_NAME, COLUMNS.ORDINAL_POSITION, // [#2230] [#11733] Translate INTERVAL_TYPE to supported types
(db.is1_4_198() ? (when(COLUMNS.INTERVAL_TYPE.like(any(inline("%YEAR%"), inline("%MONTH%"))), inline("INTERVAL YEAR TO MONTH")).when(COLUMNS.INTERVAL_TYPE.like(any(inline("%DAY%"), inline("%HOUR%"), inline("%MINUTE%"), inline("%SECOND%"))), inline("INTERVAL DAY TO SECOND")).else_(COLUMNS.TYPE_NAME)) : COLUMNS.TYPE_NAME).as(COLUMNS.TYPE_NAME), choose().when(COLUMNS.NUMERIC_PRECISION.eq(maxP).and(COLUMNS.NUMERIC_SCALE.eq(maxS)), inline(0L)).otherwise(COLUMNS.CHARACTER_MAXIMUM_LENGTH).as(COLUMNS.CHARACTER_MAXIMUM_LENGTH), COLUMNS.NUMERIC_PRECISION.decode(maxP, inline(0L), COLUMNS.NUMERIC_PRECISION).as(COLUMNS.NUMERIC_PRECISION), COLUMNS.NUMERIC_SCALE.decode(maxS, inline(0L), COLUMNS.NUMERIC_SCALE).as(COLUMNS.NUMERIC_SCALE), COLUMNS.IS_NULLABLE, COLUMNS.IS_COMPUTED.as(COLUMNS.IS_COMPUTED), COLUMNS.COLUMN_DEFAULT.as("GENERATION_EXPRESSION"), COLUMNS.COLUMN_DEFAULT, COLUMNS.REMARKS, field(COLUMNS.SEQUENCE_NAME.isNotNull()).as("IS_IDENTITY"), db.is1_4_198() ? COLUMNS.DOMAIN_SCHEMA : inline("").as(COLUMNS.DOMAIN_SCHEMA), db.is1_4_198() ? COLUMNS.DOMAIN_NAME : inline("").as(COLUMNS.DOMAIN_NAME)).from(COLUMNS).where(COLUMNS.TABLE_SCHEMA.equal(getSchema().getName())).and(COLUMNS.TABLE_NAME.equal(getName())).and(!getDatabase().getIncludeInvisibleColumns() ? db.is1_4_198() ? COLUMNS.IS_VISIBLE.eq(inline("TRUE")) : COLUMNS.COLUMN_TYPE.notLike(inline("%INVISIBLE%")) : noCondition()).orderBy(COLUMNS.ORDINAL_POSITION)) {
// [#5331] AUTO_INCREMENT (MySQL style)
// [#5331] DEFAULT nextval('sequence') (PostgreSQL style)
// [#6332] [#6339] system-generated defaults shouldn't produce a default clause
boolean isIdentity = record.get("IS_IDENTITY", boolean.class) || defaultString(record.get(COLUMNS.COLUMN_DEFAULT)).trim().toLowerCase().startsWith("nextval");
boolean isComputed = record.get(COLUMNS.IS_COMPUTED, boolean.class);
// [#7644] H2 1.4.200 puts DATETIME_PRECISION in NUMERIC_SCALE column
boolean isTimestamp = record.get(COLUMNS.TYPE_NAME).trim().toLowerCase().startsWith("timestamp");
// [#681] Domain name if available
Name userType = record.get(COLUMNS.DOMAIN_NAME) != null ? name(record.get(COLUMNS.DOMAIN_SCHEMA), record.get(COLUMNS.DOMAIN_NAME)) : name(getSchema().getName(), getName() + "_" + record.get(COLUMNS.COLUMN_NAME));
DataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), getSchema(), record.get(COLUMNS.TYPE_NAME), record.get(COLUMNS.CHARACTER_MAXIMUM_LENGTH), isTimestamp ? record.get(COLUMNS.NUMERIC_SCALE) : record.get(COLUMNS.NUMERIC_PRECISION), isTimestamp ? 0 : record.get(COLUMNS.NUMERIC_SCALE), record.get(COLUMNS.IS_NULLABLE, boolean.class), isIdentity || isComputed ? null : record.get(COLUMNS.COLUMN_DEFAULT), userType).generatedAlwaysAs(isComputed ? record.get("GENERATION_EXPRESSION", String.class) : null);
result.add(new DefaultColumnDefinition(getDatabase().getTable(getSchema(), getName()), record.get(COLUMNS.COLUMN_NAME), result.size() + 1, type, isIdentity, record.get(COLUMNS.REMARKS)));
}
return result;
}
Aggregations