use of org.jooq.meta.ColumnDefinition in project jOOQ by jOOQ.
the class FirebirdTableDefinition method getElements0.
@Override
protected List<ColumnDefinition> getElements0() throws SQLException {
List<ColumnDefinition> result = new ArrayList<>();
Rdb$relationFields r = RDB$RELATION_FIELDS.as("r");
Rdb$fields f = RDB$FIELDS.as("f");
// DatabaseMetaData implementation
for (Record record : create().select(trim(r.RDB$FIELD_NAME).as(r.RDB$FIELD_NAME), r.RDB$DESCRIPTION, r.RDB$DEFAULT_VALUE, DSL.bitOr(r.RDB$NULL_FLAG.nvl(inline((short) 0)), f.RDB$NULL_FLAG.nvl(inline((short) 0))).as(r.RDB$NULL_FLAG), r.RDB$DEFAULT_SOURCE, f.RDB$COMPUTED_SOURCE, r.RDB$FIELD_POSITION, // [#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"), trim(f.RDB$FIELD_NAME).as("DOMAIN_NAME"), r.RDB$DESCRIPTION, (((FirebirdDatabase) getDatabase()).is30() ? r.RDB$IDENTITY_TYPE : inline((short) 0)).as(r.RDB$IDENTITY_TYPE)).from(r).leftOuterJoin(f).on(r.RDB$FIELD_SOURCE.eq(f.RDB$FIELD_NAME)).where(r.RDB$RELATION_NAME.eq(getName())).orderBy(r.RDB$FIELD_POSITION)) {
// [#9411] Firebird reports the DEFAULT keyword in this column, which
// we do not want to reproduce in generated code.
String defaultValue = record.get(r.RDB$DEFAULT_SOURCE);
boolean computed = record.get(f.RDB$COMPUTED_SOURCE) != null;
if (defaultValue != null)
defaultValue = P_DEFAULT.matcher(defaultValue).replaceFirst("");
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(r.RDB$NULL_FLAG) == 0, computed ? null : defaultValue, record.get("DOMAIN_NAME") == null ? null : DSL.name(record.get("DOMAIN_NAME", String.class))).generatedAlwaysAs(computed ? record.get(f.RDB$COMPUTED_SOURCE) : null);
result.add(new DefaultColumnDefinition(getDatabase().getTable(getSchema(), getName()), record.get(r.RDB$FIELD_NAME), result.size() + 1, type, record.get(r.RDB$IDENTITY_TYPE, boolean.class), record.get(r.RDB$DESCRIPTION)));
}
return result;
}
use of org.jooq.meta.ColumnDefinition in project jOOQ by jOOQ.
the class DerbyTableDefinition method getElements0.
@Override
public List<ColumnDefinition> getElements0() throws SQLException {
List<ColumnDefinition> result = new ArrayList<>();
for (Record record : create().select(SYSCOLUMNS.COLUMNNAME, SYSCOLUMNS.COLUMNNUMBER, SYSCOLUMNS.COLUMNDATATYPE, when(SYSCOLUMNS.AUTOINCREMENTINC.isNull(), SYSCOLUMNS.COLUMNDEFAULT).as(SYSCOLUMNS.COLUMNDEFAULT), SYSCOLUMNS.AUTOINCREMENTINC).from(SYSCOLUMNS).where(SYSCOLUMNS.REFERENCEID.cast(VARCHAR(32672)).equal(inline(tableid))).orderBy(SYSCOLUMNS.COLUMNNUMBER)) {
String columnDataType = record.get(SYSCOLUMNS.COLUMNDATATYPE, String.class);
String typeName = parseTypeName(columnDataType);
// [#9945] Derby timestamps always have a precision of 9
Number precision = "TIMESTAMP".equalsIgnoreCase(typeName) ? 9 : parsePrecision(columnDataType);
Number scale = parseScale(columnDataType);
String defaultValue = record.get(SYSCOLUMNS.COLUMNDEFAULT);
boolean generated = defaultValue != null && defaultValue.toUpperCase().startsWith("GENERATED ALWAYS AS");
DataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), getSchema(), typeName, precision, precision, scale, !parseNotNull(columnDataType), generated ? null : defaultValue).generatedAlwaysAs(generated ? defaultValue.replaceAll("(?i:GENERATED\\s+ALWAYS\\s+AS\\s*\\(\\s*(.*?)\\s*\\)\\s*)", "$1") : null);
result.add(new DefaultColumnDefinition(getDatabase().getTable(getSchema(), getName()), record.get(SYSCOLUMNS.COLUMNNAME), result.size() + 1, type, null != record.get(SYSCOLUMNS.AUTOINCREMENTINC), null));
}
return result;
}
use of org.jooq.meta.ColumnDefinition in project jOOQ by jOOQ.
the class H2Database method getInlineEnums2_0.
private void getInlineEnums2_0(List<EnumDefinition> result) {
// TODO: Re-generate the H2 schema and use generated code to run this query
create().resultQuery(("" + "select c.TABLE_SCHEMA , c.TABLE_NAME , c.COLUMN_NAME , array_agg(ev.value_name order by ev.VALUE_ORDINAL)\n" + "from INFORMATION_SCHEMA.ENUM_VALUES ev\n" + "join information_schema.columns c\n" + "on ev.OBJECT_SCHEMA = c.TABLE_SCHEMA\n" + "and ev.OBJECT_NAME = c.TABLE_NAME\n" + "and ev.ENUM_IDENTIFIER = c.DTD_IDENTIFIER\n" + "where ev.OBJECT_TYPE = 'TABLE'\n" + "and c.DOMAIN_NAME IS NULL\n" + "group by c.TABLE_SCHEMA , c.TABLE_NAME , c.COLUMN_NAME\n" + "order by 1, 2, 3\n" + "")).coerce(COLUMNS.TABLE_SCHEMA, COLUMNS.TABLE_NAME, COLUMNS.COLUMN_NAME, arrayAgg(COLUMNS.COLUMN_NAME)).forEach(r -> {
SchemaDefinition schema = getSchema(r.value1());
if (schema == null)
return;
// [#1237] Don't generate enum classes for columns in MySQL tables
// that are excluded from code generation
TableDefinition tableDefinition = getTable(schema, r.value2());
if (tableDefinition != null) {
ColumnDefinition columnDefinition = tableDefinition.getColumn(r.value3());
if (columnDefinition != null) {
// are explicitly forced to another type
if (getConfiguredForcedType(columnDefinition, columnDefinition.getType()) == null) {
String name = r.value2() + "_" + r.value3();
DefaultEnumDefinition definition = new DefaultEnumDefinition(schema, name, "");
for (String string : r.value4()) definition.addLiteral(string);
result.add(definition);
}
}
}
});
}
use of org.jooq.meta.ColumnDefinition in project jOOQ by jOOQ.
the class H2TableDefinition method getElements2_0.
public List<ColumnDefinition> getElements2_0() {
List<ColumnDefinition> result = new ArrayList<>();
H2Database db = (H2Database) getDatabase();
for (Record record : create().select(COLUMNS.COLUMN_NAME, COLUMNS.ORDINAL_POSITION, // [#2230] [#11733] Translate INTERVAL_TYPE to supported types
nvl(concat(ELEMENT_TYPES.DATA_TYPE, inline(" ARRAY")), 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_(Tables.COLUMNS.DATA_TYPE)).as(COLUMNS.TYPE_NAME), nvl(ELEMENT_TYPES.CHARACTER_MAXIMUM_LENGTH, COLUMNS.CHARACTER_MAXIMUM_LENGTH).as(COLUMNS.CHARACTER_MAXIMUM_LENGTH), coalesce(ELEMENT_TYPES.DATETIME_PRECISION, ELEMENT_TYPES.NUMERIC_PRECISION, COLUMNS.DATETIME_PRECISION, COLUMNS.NUMERIC_PRECISION).as(COLUMNS.NUMERIC_PRECISION), nvl(ELEMENT_TYPES.NUMERIC_SCALE, COLUMNS.NUMERIC_SCALE).as(COLUMNS.NUMERIC_SCALE), COLUMNS.IS_NULLABLE, field(Tables.COLUMNS.IS_GENERATED.eq(inline("ALWAYS"))).as(COLUMNS.IS_COMPUTED), Tables.COLUMNS.GENERATION_EXPRESSION, COLUMNS.COLUMN_DEFAULT, COLUMNS.REMARKS, field(Tables.COLUMNS.IS_IDENTITY.eq(inline("YES"))).as(Tables.COLUMNS.IS_IDENTITY), COLUMNS.DOMAIN_SCHEMA, COLUMNS.DOMAIN_NAME).from(COLUMNS).leftJoin(ELEMENT_TYPES).on(Tables.COLUMNS.TABLE_SCHEMA.equal(ELEMENT_TYPES.OBJECT_SCHEMA)).and(Tables.COLUMNS.TABLE_NAME.equal(ELEMENT_TYPES.OBJECT_NAME)).and(Tables.COLUMNS.DTD_IDENTIFIER.equal(ELEMENT_TYPES.COLLECTION_TYPE_IDENTIFIER)).where(COLUMNS.TABLE_SCHEMA.equal(getSchema().getName())).and(COLUMNS.TABLE_NAME.equal(getName())).and(!getDatabase().getIncludeInvisibleColumns() ? condition(COLUMNS.IS_VISIBLE.coerce(BOOLEAN)) : 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(Tables.COLUMNS.IS_IDENTITY, boolean.class) || defaultString(record.get(COLUMNS.COLUMN_DEFAULT)).trim().toLowerCase().startsWith("nextval");
boolean isComputed = record.get(COLUMNS.IS_COMPUTED, boolean.class);
// [#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));
SchemaDefinition typeSchema = record.get(COLUMNS.DOMAIN_NAME) != null ? getDatabase().getSchema(record.get(COLUMNS.DOMAIN_SCHEMA)) : getSchema();
DataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), typeSchema == null ? getSchema() : typeSchema, record.get(COLUMNS.TYPE_NAME), record.get(COLUMNS.CHARACTER_MAXIMUM_LENGTH), record.get(COLUMNS.NUMERIC_PRECISION), 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(Tables.COLUMNS.GENERATION_EXPRESSION) : 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;
}
use of org.jooq.meta.ColumnDefinition in project jOOQ by jOOQ.
the class JavaGenerator method generatePojo0.
private final void generatePojo0(Definition tableUdtOrEmbeddable, JavaWriter out) {
final String className = getStrategy().getJavaClassName(tableUdtOrEmbeddable, Mode.POJO);
final String interfaceName = generateInterfaces() ? out.ref(getStrategy().getFullJavaClassName(tableUdtOrEmbeddable, Mode.INTERFACE)) : "";
final String superName = out.ref(getStrategy().getJavaClassExtends(tableUdtOrEmbeddable, Mode.POJO));
final List<String> interfaces = out.ref(getStrategy().getJavaClassImplements(tableUdtOrEmbeddable, Mode.POJO));
if (generateInterfaces())
interfaces.add(interfaceName);
final List<String> superTypes = list(superName, interfaces);
printPackage(out, tableUdtOrEmbeddable, Mode.POJO);
if (tableUdtOrEmbeddable instanceof TableDefinition)
generatePojoClassJavadoc((TableDefinition) tableUdtOrEmbeddable, out);
else if (tableUdtOrEmbeddable instanceof EmbeddableDefinition)
generateEmbeddableClassJavadoc((EmbeddableDefinition) tableUdtOrEmbeddable, out);
else
generateUDTPojoClassJavadoc((UDTDefinition) tableUdtOrEmbeddable, out);
printClassAnnotations(out, tableUdtOrEmbeddable, Mode.POJO);
if (tableUdtOrEmbeddable instanceof TableDefinition)
printTableJPAAnnotation(out, (TableDefinition) tableUdtOrEmbeddable);
int maxLength0 = 0;
for (TypedElementDefinition<?> column : getTypedElements(tableUdtOrEmbeddable)) maxLength0 = Math.max(maxLength0, out.ref(getJavaType(column.getType(resolver(out, Mode.POJO)), out, Mode.POJO)).length());
int maxLength = maxLength0;
if (scala) {
out.println("%s%sclass %s(", visibility(), (generatePojosAsScalaCaseClasses() ? "case " : ""), className);
forEach(getTypedElements(tableUdtOrEmbeddable), (column, separator) -> {
out.println("%s%s %s: %s%s", visibility(generateInterfaces()), generateImmutablePojos() ? "val" : "var", scalaWhitespaceSuffix(getStrategy().getJavaMemberName(column, Mode.POJO)), out.ref(getJavaType(column.getType(resolver(out, Mode.POJO)), out, Mode.POJO)), separator);
});
out.println(")[[before= extends ][%s]][[before= with ][separator= with ][%s]] {", first(superTypes), remaining(superTypes));
} else if (kotlin) {
out.println("%s%sclass %s(", visibility(), (generatePojosAsKotlinDataClasses() ? "data " : ""), className);
forEach(getTypedElements(tableUdtOrEmbeddable), (column, separator) -> {
final String member = getStrategy().getJavaMemberName(column, Mode.POJO);
if (column instanceof ColumnDefinition)
printColumnJPAAnnotation(out, (ColumnDefinition) column);
printValidationAnnotation(out, column);
if (!generateImmutablePojos())
printKotlinSetterAnnotation(out, column, Mode.POJO);
out.println("%s%s%s %s: %s? = null%s", visibility(generateInterfaces()), generateInterfaces() ? "override " : "", generateImmutablePojos() ? "val" : "var", member, out.ref(getJavaType(column.getType(resolver(out, Mode.POJO)), out, Mode.POJO)), separator);
});
out.println(")[[before=: ][%s]] {", superTypes);
} else {
if (generatePojosAsJavaRecordClasses()) {
out.println("%srecord %s(", visibility(), className);
forEach(getTypedElements(tableUdtOrEmbeddable), (column, separator) -> {
out.println("%s %s%s", StringUtils.rightPad(out.ref(getJavaType(column.getType(resolver(out, Mode.POJO)), out, Mode.POJO)), maxLength), getStrategy().getJavaMemberName(column, Mode.POJO), separator);
});
out.println(")[[before= implements ][%s]] {", interfaces);
} else {
out.println("%sclass %s[[before= extends ][%s]][[before= implements ][%s]] {", visibility(), className, list(superName), interfaces);
}
if (generateSerializablePojos() || generateSerializableInterfaces())
out.printSerial();
out.println();
if (!generatePojosAsJavaRecordClasses())
for (TypedElementDefinition<?> column : getTypedElements(tableUdtOrEmbeddable)) out.println("private %s%s %s;", generateImmutablePojos() ? "final " : "", StringUtils.rightPad(out.ref(getJavaType(column.getType(resolver(out, Mode.POJO)), out, Mode.POJO)), maxLength0), getStrategy().getJavaMemberName(column, Mode.POJO));
}
// Default constructor
if (!generateImmutablePojos() && !generatePojosAsJavaRecordClasses())
generatePojoDefaultConstructor(tableUdtOrEmbeddable, out);
if (!kotlin) {
if (!generatePojosAsJavaRecordClasses()) {
// [#1363] [#7055] copy constructor
generatePojoCopyConstructor(tableUdtOrEmbeddable, out);
// Multi-constructor
generatePojoMultiConstructor(tableUdtOrEmbeddable, out);
}
List<? extends TypedElementDefinition<?>> elements = getTypedElements(tableUdtOrEmbeddable);
for (int i = 0; i < elements.size(); i++) {
TypedElementDefinition<?> column = elements.get(i);
if (!generatePojosAsJavaRecordClasses() || generateInterfaces())
if (tableUdtOrEmbeddable instanceof TableDefinition)
generatePojoGetter(column, i, out);
else
generateUDTPojoGetter(column, i, out);
// Setter
if (!generateImmutablePojos())
if (tableUdtOrEmbeddable instanceof TableDefinition)
generatePojoSetter(column, i, out);
else
generateUDTPojoSetter(column, i, out);
}
}
if (tableUdtOrEmbeddable instanceof TableDefinition) {
List<EmbeddableDefinition> embeddables = ((TableDefinition) tableUdtOrEmbeddable).getReferencedEmbeddables();
for (int i = 0; i < embeddables.size(); i++) {
EmbeddableDefinition embeddable = embeddables.get(i);
if (!generateImmutablePojos())
generateEmbeddablePojoSetter(embeddable, i, out);
generateEmbeddablePojoGetter(embeddable, i, out);
}
}
if (generatePojosEqualsAndHashCode())
generatePojoEqualsAndHashCode(tableUdtOrEmbeddable, out);
if (generatePojosToString())
generatePojoToString(tableUdtOrEmbeddable, out);
if (generateInterfaces() && !generateImmutablePojos())
printFromAndInto(out, tableUdtOrEmbeddable, Mode.POJO);
if (tableUdtOrEmbeddable instanceof TableDefinition)
generatePojoClassFooter((TableDefinition) tableUdtOrEmbeddable, out);
else if (tableUdtOrEmbeddable instanceof EmbeddableDefinition)
generateEmbeddableClassFooter((EmbeddableDefinition) tableUdtOrEmbeddable, out);
else
generateUDTPojoClassFooter((UDTDefinition) tableUdtOrEmbeddable, out);
out.println("}");
closeJavaWriter(out);
}
Aggregations