use of org.jooq.meta.SchemaDefinition in project jOOQ by jOOQ.
the class JavaGenerator method printClassAnnotations.
protected void printClassAnnotations(JavaWriter out, Definition definition, Mode mode) {
if (generateGeneratedAnnotation()) {
SchemaDefinition schema = definition.getSchema();
CatalogDefinition catalog = definition.getCatalog();
// [#7581] The concrete annotation type depends on the JDK, with
// javax.annotation.Generated being deprecated in JDK 9
GeneratedAnnotationType type = generateGeneratedAnnotationType();
if (type == null)
type = GeneratedAnnotationType.DETECT_FROM_JDK;
String generated;
switch(type) {
case DETECT_FROM_JDK:
try {
// Seems more reliable than tampering with java.version
Reflect.onClass("java.util.Optional").call("of", new Object()).call("stream");
generated = "javax.annotation.processing.Generated";
} catch (ReflectException e) {
generated = "javax.annotation.Generated";
}
break;
case JAVAX_ANNOTATION_GENERATED:
generated = "javax.annotation.Generated";
break;
case JAVAX_ANNOTATION_PROCESSING_GENERATED:
generated = "javax.annotation.processing.Generated";
break;
default:
throw new IllegalStateException("Unsupported type: " + type);
}
out.println("@%s(", out.ref(generated));
if (useSchemaVersionProvider() || useCatalogVersionProvider()) {
boolean hasCatalogVersion = !StringUtils.isBlank(catalogVersions.get(catalog));
boolean hasSchemaVersion = !StringUtils.isBlank(schemaVersions.get(schema));
if (scala)
out.println("value = %s(", out.ref("scala.Array"));
else if (kotlin)
out.println("value = [");
else
out.println("value = {");
out.println("\"https://www.jooq.org\",");
out.println("\"jOOQ version:%s\"%s", Constants.VERSION, (hasCatalogVersion || hasSchemaVersion ? "," : ""));
if (hasCatalogVersion)
out.println("\"catalog version:%s\"%s", escapeString(catalogVersions.get(catalog)), (hasSchemaVersion ? "," : ""));
if (hasSchemaVersion)
out.println("\"schema version:%s\"", escapeString(schemaVersions.get(schema)));
if (scala)
out.println("),");
else if (kotlin)
out.println("],");
else
out.println("},");
if (generateGeneratedAnnotationDate())
out.println("date = \"" + isoDate + "\",");
out.println("comments = \"This class is generated by jOOQ\"");
} else {
if (scala)
out.println("value = %s(", out.ref("scala.Array"));
else if (kotlin)
out.println("value = [");
else
out.println("value = {");
out.println("\"https://www.jooq.org\",");
out.println("\"jOOQ version:%s\"", Constants.VERSION);
if (scala)
out.println("),");
else if (kotlin)
out.println("],");
else
out.println("},");
out.println("comments = \"This class is generated by jOOQ\"");
}
out.println(")");
}
if (scala) {
} else if (kotlin)
out.println("@Suppress(\"UNCHECKED_CAST\")");
else
out.println("@%s({ \"all\", \"unchecked\", \"rawtypes\" })", out.ref("java.lang.SuppressWarnings"));
}
use of org.jooq.meta.SchemaDefinition in project jOOQ by jOOQ.
the class JavaGenerator method generateCatalog.
protected void generateCatalog(CatalogDefinition catalog, JavaWriter out) {
final String catalogId = getStrategy().getJavaIdentifier(catalog);
final String catalogName = !catalog.getQualifiedOutputName().isEmpty() ? catalog.getQualifiedOutputName() : catalogId;
final String className = getStrategy().getJavaClassName(catalog);
final List<String> interfaces = out.ref(getStrategy().getJavaClassImplements(catalog, Mode.DEFAULT));
printPackage(out, catalog);
if (scala) {
out.println("object %s {", className);
out.javadoc("The reference instance of <code>%s</code>", catalogName);
out.println("val %s = new %s", catalogId, className);
out.println("}");
out.println();
}
generateCatalogClassJavadoc(catalog, out);
printClassAnnotations(out, catalog, Mode.DEFAULT);
if (scala) {
out.println("%sclass %s extends %s(\"%s\")[[before= with ][separator= with ][%s]] {", visibility(), className, CatalogImpl.class, catalog.getOutputName(), interfaces);
} else if (kotlin) {
out.println("%sopen class %s : %s(\"%s\")[[before=, ][%s]] {", visibility(), className, CatalogImpl.class, catalog.getOutputName(), interfaces);
out.println("%scompanion object {", visibility());
out.javadoc("The reference instance of <code>%s</code>", catalogName);
out.println("public val %s: %s = %s()", catalogId, className, className);
out.println("}");
} else {
out.println("%sclass %s extends %s[[before= implements ][%s]] {", visibility(), className, CatalogImpl.class, interfaces);
out.printSerial();
out.javadoc("The reference instance of <code>%s</code>", catalogName);
out.println("%sstatic final %s %s = new %s();", visibility(), className, catalogId, className);
}
List<SchemaDefinition> schemas = new ArrayList<>();
if (generateGlobalSchemaReferences()) {
Set<String> fieldNames = new HashSet<>();
fieldNames.add(catalogId);
for (SchemaDefinition schema : catalog.getSchemata()) if (generateSchemaIfEmpty(schema))
fieldNames.add(getStrategy().getJavaIdentifier(schema));
for (SchemaDefinition schema : catalog.getSchemata()) {
if (generateSchemaIfEmpty(schema)) {
schemas.add(schema);
final String schemaClassName = out.ref(getStrategy().getFullJavaClassName(schema));
final String schemaId = getStrategy().getJavaIdentifier(schema);
final String schemaFullId = getStrategy().getFullJavaIdentifier(schema);
String schemaShortId = out.ref(getStrategy().getFullJavaIdentifier(schema), 2);
if (fieldNames.contains(schemaShortId.substring(0, schemaShortId.indexOf('.'))))
schemaShortId = schemaFullId;
final String schemaComment = escapeEntities(comment(schema));
out.javadoc(isBlank(schemaComment) ? ("The schema <code>" + (!schema.getQualifiedOutputName().isEmpty() ? schema.getQualifiedOutputName() : schemaId) + "</code>.") : schemaComment);
if (scala)
out.println("%sdef %s = %s", visibility(), schemaId, schemaShortId);
else if (kotlin)
out.println("%sval %s: %s get(): %s = %s", visibility(), schemaId, schemaClassName, schemaClassName, schemaShortId);
else
out.println("%sfinal %s %s = %s;", visibility(), schemaClassName, schemaId, schemaShortId);
}
}
}
if (scala || kotlin)
;
else {
out.javadoc(NO_FURTHER_INSTANCES_ALLOWED);
out.println("private %s() {", className);
out.println("super(\"%s\");", catalog.getOutputName());
out.println("}");
}
printReferences(out, schemas, Schema.class, false);
if (generateJooqVersionReference()) {
String version = org.jooq.codegen.Constants.MINOR_VERSION.replace(".", "_");
out.javadoc("A reference to the " + org.jooq.codegen.Constants.MINOR_VERSION + " minor release of the code generator. " + "If this doesn't compile, it's because the runtime library uses an older minor release, namely: " + org.jooq.Constants.MINOR_VERSION + ". " + "You can turn off the generation of this reference by specifying /configuration/generator/generate/jooqVersionReference");
if (scala)
out.println("private val REQUIRE_RUNTIME_JOOQ_VERSION = %s.VERSION_%s", org.jooq.Constants.class, version);
else if (kotlin)
out.println("private val REQUIRE_RUNTIME_JOOQ_VERSION = %s.VERSION_%s", org.jooq.Constants.class, version);
else
out.println("private static final String REQUIRE_RUNTIME_JOOQ_VERSION = %s.VERSION_%s;", org.jooq.Constants.class, version);
}
generateCatalogClassFooter(catalog, out);
out.println("}");
}
use of org.jooq.meta.SchemaDefinition in project jOOQ by jOOQ.
the class JavaGenerator method printTableJPAAnnotation.
protected void printTableJPAAnnotation(JavaWriter out, TableDefinition table) {
SchemaDefinition schema = table.getSchema();
int indent = out.indent();
if (generateJPAAnnotations()) {
// Since JPA 1.0
out.println("@%s", out.ref("jakarta.persistence.Entity"));
// Since JPA 1.0
out.println("@%s(", out.ref("jakarta.persistence.Table"));
out.print("name = \"", out.ref("jakarta.persistence.Table"));
out.print(escapeString(table.getName()));
out.print("\"");
if (!schema.isDefaultSchema()) {
out.println(",");
out.print("schema = \"");
out.print(escapeString(schema.getOutputName()));
out.print("\"");
}
List<UniqueKeyDefinition> keys = table.getUniqueKeys();
if (!keys.isEmpty()) {
out.println(",");
out.print("uniqueConstraints = ");
out.println(scala ? "Array(" : kotlin ? "[" : "{");
for (int i = 0; i < keys.size(); i++) {
UniqueKeyDefinition uk = keys.get(i);
out.print(scala ? "new " : kotlin ? "" : "@").print(out.ref("jakarta.persistence.UniqueConstraint")).print("(");
if (!StringUtils.isBlank(uk.getOutputName()))
out.print("name = \"" + escapeString(uk.getOutputName()) + "\", ");
out.print("columnNames = ").print(scala ? "Array(" : kotlin ? "[ " : "{ ");
List<ColumnDefinition> columns = uk.getKeyColumns();
for (int j = 0; j < columns.size(); j++) {
out.print(j > 0 ? ", " : "");
out.print("\"");
out.print(escapeString(columns.get(j).getName()));
out.print("\"");
}
out.print(scala ? ")" : kotlin ? " ]" : " }").print(")").println(i < keys.size() - 1 ? "," : "");
}
out.print(scala ? ")" : kotlin ? "]" : "}");
}
if (StringUtils.isBlank(generateJPAVersion()) || "2.1".compareTo(generateJPAVersion()) <= 0) {
List<IndexDefinition> indexes = table.getIndexes();
if (!indexes.isEmpty()) {
out.println(",");
out.print("indexes = ").println(scala ? "Array(" : kotlin ? "[" : "{");
for (int i = 0; i < indexes.size(); i++) {
IndexDefinition index = indexes.get(i);
out.print(scala ? "new " : kotlin ? "" : "@");
out.print(out.ref("jakarta.persistence.Index"));
out.print("(name = \"").print(escapeString(index.getOutputName())).print("\"");
if (index.isUnique())
out.print(", unique = true");
out.print(", columnList = \"");
List<IndexColumnDefinition> columns = index.getIndexColumns();
for (int j = 0; j < columns.size(); j++) {
IndexColumnDefinition column = columns.get(j);
if (j > 0)
out.print(", ");
out.print(escapeString(column.getOutputName()));
if (column.getSortOrder() == SortOrder.ASC)
out.print(" ASC");
else if (column.getSortOrder() == SortOrder.DESC)
out.print(" DESC");
}
out.print("\")").println(i < indexes.size() - 1 ? "," : "");
}
out.print(scala ? ")" : kotlin ? "]" : "}");
}
}
out.println();
out.println(")");
}
// [#10196] The above logic triggers an indent level of -1, incorrectly
out.indent(indent);
}
use of org.jooq.meta.SchemaDefinition in project jOOQ by jOOQ.
the class JavaGenerator method generateTable.
protected void generateTable(TableDefinition table, JavaWriter out) {
final SchemaDefinition schema = table.getSchema();
final UniqueKeyDefinition primaryKey = table.getPrimaryKey();
final String className = getStrategy().getJavaClassName(table);
final String tableId = scala ? out.ref(getStrategy().getFullJavaIdentifier(table), 2) : getStrategy().getJavaIdentifier(table);
final String recordType = out.ref(getStrategy().getFullJavaClassName(table, Mode.RECORD));
final List<String> interfaces = out.ref(getStrategy().getJavaClassImplements(table, Mode.DEFAULT));
final String schemaId = out.ref(getStrategy().getFullJavaIdentifier(schema), 2);
final String tableType = table.isTemporary() ? "temporaryTable" : table.isView() ? "view" : table.isMaterializedView() ? "materializedView" : table.isTableValuedFunction() ? "function" : "table";
final List<ParameterDefinition> parameters = table.getParameters();
printPackage(out, table);
if (scala) {
out.println("object %s {", className);
printSingletonInstance(out, table);
out.println("}");
out.println();
}
generateTableClassJavadoc(table, out);
printClassAnnotations(out, table, Mode.DEFAULT);
if (scala) {
out.println("%sclass %s(", visibility(), className);
out.println("alias: %s,", Name.class);
out.println("child: %s[_ <: %s],", Table.class, Record.class);
out.println("path: %s[_ <: %s, %s],", ForeignKey.class, Record.class, recordType);
out.println("aliased: %s[%s],", Table.class, recordType);
out.println("parameters: %s[ %s[_] ]", out.ref("scala.Array"), Field.class);
out.println(")");
out.println("extends %s[%s](", TableImpl.class, recordType);
out.println("alias,");
out.println("%s,", schemaId);
out.println("child,");
out.println("path,");
out.println("aliased,");
out.println("parameters,");
out.println("%s.comment(\"%s\"),", DSL.class, escapeString(comment(table)));
if ((generateSourcesOnViews() || table.isSynthetic()) && table.isView() && table.getSource() != null)
out.println("%s.%s(\"%s\")", TableOptions.class, tableType, escapeString(table.getSource()));
else if (table.isSynthetic() && table.isTableValuedFunction() && table.getSource() != null)
out.println("%s.%s(\"%s\")", TableOptions.class, tableType, escapeString(table.getSource()));
else
out.println("%s.%s", TableOptions.class, tableType);
out.println(")[[before= with ][separator= with ][%s]] {", interfaces);
} else if (kotlin) {
out.println("%sopen class %s(", visibility(), className);
out.println("alias: %s,", Name.class);
out.println("child: %s<out %s>?,", Table.class, Record.class);
out.println("path: %s<out %s, %s>?,", ForeignKey.class, Record.class, recordType);
out.println("aliased: %s<%s>?,", Table.class, recordType);
out.println("parameters: Array<%s<*>?>?", Field.class);
out.println("): %s<%s>(", TableImpl.class, recordType);
out.println("alias,");
out.println("%s,", schemaId);
out.println("child,");
out.println("path,");
out.println("aliased,");
out.println("parameters,");
out.println("%s.comment(\"%s\"),", DSL.class, escapeString(comment(table)));
if ((generateSourcesOnViews() || table.isSynthetic()) && table.isView() && table.getSource() != null)
out.println("%s.%s(\"%s\")", TableOptions.class, tableType, escapeString(table.getSource()));
else if (table.isSynthetic() && table.isTableValuedFunction() && table.getSource() != null)
out.println("%s.%s(\"%s\")", TableOptions.class, tableType, escapeString(table.getSource()));
else
out.println("%s.%s()", TableOptions.class, tableType);
out.println(")[[before=, ][%s]] {", interfaces);
out.println("%scompanion object {", visibility());
printSingletonInstance(out, table);
out.println("}");
} else {
out.println("%sclass %s extends %s<%s>[[before= implements ][%s]] {", visibility(), className, TableImpl.class, recordType, interfaces);
out.printSerial();
printSingletonInstance(out, table);
}
printRecordTypeMethod(out, table);
if (table.isSynthetic()) {
if (scala) {
out.println();
out.println("protected override def isSynthetic(): Boolean = true");
} else if (kotlin) {
out.println();
out.println("protected override fun isSynthetic(): Boolean = true");
} else {
out.println();
out.override();
out.println("protected boolean isSynthetic() {");
out.println("return true;");
out.println("}");
}
}
for (ColumnDefinition column : table.getColumns()) {
final String columnTypeFull = getJavaType(column.getType(resolver(out)), out);
final String columnType = out.ref(columnTypeFull);
final String columnTypeRef = getJavaTypeReference(column.getDatabase(), column.getType(resolver(out)), out);
final String columnId = out.ref(getStrategy().getJavaIdentifier(column), colRefSegments(column));
final String columnName = column.getName();
final List<String> converter = out.ref(list(column.getType(resolver(out)).getConverter()));
final List<String> binding = out.ref(list(column.getType(resolver(out)).getBinding()));
final String columnVisibility = visibility();
if (!printDeprecationIfUnknownType(out, columnTypeFull))
out.javadoc("The column <code>%s</code>.[[before= ][%s]]", column.getQualifiedOutputName(), list(escapeEntities(comment(column))));
if (scala) {
out.println("%sval %s: %s[%s, %s] = createField(%s.name(\"%s\"), %s, \"%s\"" + converterTemplate(converter) + converterTemplate(binding) + ")", columnVisibility, scalaWhitespaceSuffix(columnId), TableField.class, recordType, columnType, DSL.class, columnName, columnTypeRef, escapeString(comment(column)), converter, binding);
} else if (kotlin) {
out.println("%sval %s: %s<%s, %s?> = createField(%s.name(\"%s\"), %s, this, \"%s\"" + converterTemplate(converter) + converterTemplate(binding) + ")", columnVisibility, columnId, TableField.class, recordType, columnType, DSL.class, columnName, columnTypeRef, escapeString(comment(column)), converter, binding);
} else {
String isStatic = generateInstanceFields() ? "" : "static ";
String tableRef = generateInstanceFields() ? "this" : out.ref(getStrategy().getJavaIdentifier(table), 2);
out.println("%s%sfinal %s<%s, %s> %s = createField(%s.name(\"%s\"), %s, %s, \"%s\"" + converterTemplate(converter) + converterTemplate(binding) + ");", columnVisibility, isStatic, TableField.class, recordType, columnType, columnId, DSL.class, columnName, columnTypeRef, tableRef, escapeString(comment(column)), converter, binding);
}
}
// [#2530] Embeddable types
for (EmbeddableDefinition embeddable : table.getReferencedEmbeddables()) {
final String columnId = out.ref(getStrategy().getJavaIdentifier(embeddable), colRefSegments(null));
final String columnType = out.ref(getStrategy().getFullJavaClassName(embeddable, Mode.RECORD));
final List<String> columnIds = new ArrayList<>();
for (EmbeddableColumnDefinition column : embeddable.getColumns()) columnIds.add(out.ref(getStrategy().getJavaIdentifier(column.getReferencingColumn()), colRefSegments(column.getReferencingColumn())));
out.javadoc("The embeddable type <code>%s</code>.[[before= ][%s]]", embeddable.getOutputName(), list(escapeEntities(referencingComment(embeddable))));
if (scala)
out.println("%sval %s: %s[%s, %s] = %s.createEmbeddable(%s.name(\"%s\"), classOf[%s], %s, this, [[%s]])", visibility(), scalaWhitespaceSuffix(columnId), TableField.class, recordType, columnType, Internal.class, DSL.class, escapeString(embeddable.getName()), columnType, embeddable.replacesFields(), columnIds);
else if (kotlin)
out.println("%sval %s: %s<%s, %s> = %s.createEmbeddable(%s.name(\"%s\"), %s::class.java, %s, this, [[%s]])", visibility(), columnId, TableField.class, recordType, columnType, Internal.class, DSL.class, escapeString(embeddable.getName()), columnType, embeddable.replacesFields(), columnIds);
else
out.println("%sfinal %s<%s, %s> %s = %s.createEmbeddable(%s.name(\"%s\"), %s.class, %s, this, [[%s]]);", visibility(), TableField.class, recordType, columnType, columnId, Internal.class, DSL.class, escapeString(embeddable.getName()), columnType, embeddable.replacesFields(), columnIds);
}
out.println();
// "called constructor's definition must precede calling constructor's definition"
if (scala) {
if (table.isTableValuedFunction()) {
out.println("private def this(alias: %s, aliased: %s[%s]) = this(alias, null, null, aliased, %s(", Name.class, Table.class, recordType, out.ref("scala.Array"));
forEach(parameters, (parameter, separator) -> {
final String paramTypeRef = getJavaTypeReference(parameter.getDatabase(), parameter.getType(resolver(out)), out);
final List<String> converter = out.ref(list(parameter.getType(resolver(out)).getConverter()));
final List<String> binding = out.ref(list(parameter.getType(resolver(out)).getBinding()));
out.println("%s.value(null, %s" + converterTemplateForTableValuedFunction(converter) + converterTemplateForTableValuedFunction(binding) + ")%s", DSL.class, paramTypeRef, converter, binding, separator);
});
out.println("))");
} else
out.println("private def this(alias: %s, aliased: %s[%s]) = this(alias, null, null, aliased, null)", Name.class, Table.class, recordType);
} else if (kotlin) {
if (table.isTableValuedFunction()) {
out.println("private constructor(alias: %s, aliased: %s<%s>?): this(alias, null, null, aliased, arrayOf(", Name.class, Table.class, recordType, Field.class, parameters.size());
forEach(parameters, (parameter, separator) -> {
final String paramTypeRef = getJavaTypeReference(parameter.getDatabase(), parameter.getType(resolver(out)), out);
final List<String> converter = out.ref(list(parameter.getType(resolver(out)).getConverter()));
final List<String> binding = out.ref(list(parameter.getType(resolver(out)).getBinding()));
out.println("%s.value(null, %s" + converterTemplateForTableValuedFunction(converter) + converterTemplateForTableValuedFunction(binding) + ")%s", DSL.class, paramTypeRef, converter, binding, separator);
});
out.println("))");
} else
out.println("private constructor(alias: %s, aliased: %s<%s>?): this(alias, null, null, aliased, null)", Name.class, Table.class, recordType);
out.println("private constructor(alias: %s, aliased: %s<%s>?, parameters: Array<%s<*>?>?): this(alias, null, null, aliased, parameters)", Name.class, Table.class, recordType, Field.class);
} else {
out.println("private %s(%s alias, %s<%s> aliased) {", className, Name.class, Table.class, recordType);
if (table.isTableValuedFunction()) {
out.println("this(alias, aliased, new %s[] {", Field.class);
forEach(parameters, (parameter, separator) -> {
final String paramTypeRef = getJavaTypeReference(parameter.getDatabase(), parameter.getType(resolver(out)), out);
final List<String> converter = out.ref(list(parameter.getType(resolver(out)).getConverter()));
final List<String> binding = out.ref(list(parameter.getType(resolver(out)).getBinding()));
out.println("%s.val(null, %s" + converterTemplateForTableValuedFunction(converter) + converterTemplateForTableValuedFunction(binding) + ")%s", DSL.class, paramTypeRef, converter, binding, separator);
});
out.println("});");
} else
out.println("this(alias, aliased, null);");
out.println("}");
out.println();
out.println("private %s(%s alias, %s<%s> aliased, %s<?>[] parameters) {", className, Name.class, Table.class, recordType, Field.class);
if ((generateSourcesOnViews() || table.isSynthetic()) && table.isView() && table.getSource() != null)
out.println("super(alias, null, aliased, parameters, %s.comment(\"%s\"), %s.%s(\"%s\"));", DSL.class, escapeString(comment(table)), TableOptions.class, tableType, escapeString(table.getSource()));
else if (table.isSynthetic() && table.isTableValuedFunction() && table.getSource() != null)
out.println("super(alias, null, aliased, parameters, %s.comment(\"%s\"), %s.%s(\"%s\"));", DSL.class, escapeString(comment(table)), TableOptions.class, tableType, escapeString(table.getSource()));
else
out.println("super(alias, null, aliased, parameters, %s.comment(\"%s\"), %s.%s());", DSL.class, escapeString(comment(table)), TableOptions.class, tableType);
out.println("}");
}
if (scala) {
out.javadoc("Create an aliased <code>%s</code> table reference", table.getQualifiedOutputName());
out.println("%sdef this(alias: %s) = this(%s.name(alias), %s)", visibility(), String.class, DSL.class, tableId);
out.javadoc("Create an aliased <code>%s</code> table reference", table.getQualifiedOutputName());
out.println("%sdef this(alias: %s) = this(alias, %s)", visibility(), Name.class, tableId);
} else if (kotlin) {
out.javadoc("Create an aliased <code>%s</code> table reference", table.getQualifiedOutputName());
out.println("%sconstructor(alias: %s): this(%s.name(alias))", visibility(), String.class, DSL.class);
out.javadoc("Create an aliased <code>%s</code> table reference", table.getQualifiedOutputName());
out.println("%sconstructor(alias: %s): this(alias, null)", visibility(), Name.class, tableId);
} else // be public, as tables are no longer singletons
if (generateInstanceFields()) {
out.javadoc("Create an aliased <code>%s</code> table reference", table.getQualifiedOutputName());
out.println("%s%s(%s alias) {", visibility(), className, String.class);
out.println("this(%s.name(alias), %s);", DSL.class, tableId);
out.println("}");
out.javadoc("Create an aliased <code>%s</code> table reference", table.getQualifiedOutputName());
out.println("%s%s(%s alias) {", visibility(), className, Name.class);
out.println("this(alias, %s);", tableId);
out.println("}");
}
if (scala) {
out.javadoc("Create a <code>%s</code> table reference", table.getQualifiedOutputName());
out.println("%sdef this() = this(%s.name(\"%s\"), null)", visibility(), DSL.class, escapeString(table.getOutputName()));
} else if (kotlin) {
out.javadoc("Create a <code>%s</code> table reference", table.getQualifiedOutputName());
out.println("%sconstructor(): this(%s.name(\"%s\"), null)", visibility(), DSL.class, escapeString(table.getOutputName()));
} else {
// be public, as tables are no longer singletons
if (generateInstanceFields()) {
out.javadoc("Create a <code>%s</code> table reference", table.getQualifiedOutputName());
out.println("%s%s() {", visibility(), className);
} else {
out.javadoc(NO_FURTHER_INSTANCES_ALLOWED);
out.println("private %s() {", className);
}
out.println("this(%s.name(\"%s\"), null);", DSL.class, escapeString(table.getOutputName()));
out.println("}");
}
if (generateImplicitJoinPathsToOne() && generateGlobalKeyReferences() && !table.isTableValuedFunction()) {
out.println();
if (scala) {
out.println("%sdef this(child: %s[_ <: %s], key: %s[_ <: %s, %s]) = this(%s.createPathAlias(child, key), child, key, %s, null)", visibility(), Table.class, Record.class, ForeignKey.class, Record.class, recordType, Internal.class, tableId);
} else if (kotlin) {
out.println("%sconstructor(child: %s<out %s>, key: %s<out %s, %s>): this(%s.createPathAlias(child, key), child, key, %s, null)", visibility(), Table.class, Record.class, ForeignKey.class, Record.class, recordType, Internal.class, tableId);
} else {
out.println("%s<O extends %s> %s(%s<O> child, %s<O, %s> key) {", visibility(), Record.class, className, Table.class, ForeignKey.class, recordType);
out.println("super(child, key, %s);", tableId);
out.println("}");
}
}
if (scala) {
out.println();
out.println("%soverride def getSchema: %s = if (aliased()) null else %s", visibilityPublic(), Schema.class, schemaId);
} else if (kotlin) {
out.println("%soverride fun getSchema(): %s? = if (aliased()) null else %s", visibilityPublic(), Schema.class, schemaId);
} else {
out.overrideInherit();
printNonnullAnnotation(out);
out.println("%s%s getSchema() {", visibilityPublic(), Schema.class);
out.println("return aliased() ? null : %s;", schemaId);
out.println("}");
}
// Add index information
if (generateIndexes()) {
List<IndexDefinition> indexes = table.getIndexes();
if (!indexes.isEmpty()) {
if (generateGlobalIndexReferences()) {
final List<String> indexFullIds = kotlin ? out.ref(getStrategy().getFullJavaIdentifiers(indexes)) : out.ref(getStrategy().getFullJavaIdentifiers(indexes), 2);
if (scala) {
out.println();
out.println("%soverride def getIndexes: %s[%s] = %s.asList[ %s ]([[%s]])", visibilityPublic(), List.class, Index.class, Arrays.class, Index.class, indexFullIds);
} else if (kotlin) {
out.println("%soverride fun getIndexes(): %s<%s> = listOf([[%s]])", visibilityPublic(), out.ref(KLIST), Index.class, indexFullIds);
} else {
out.overrideInherit();
printNonnullAnnotation(out);
out.println("%s%s<%s> getIndexes() {", visibilityPublic(), List.class, Index.class);
out.println("return %s.asList([[%s]]);", Arrays.class, indexFullIds);
out.println("}");
}
} else {
if (scala) {
out.println();
out.println("%soverride def getIndexes: %s[%s] = %s.asList[%s](", visibilityPublic(), List.class, Index.class, Arrays.class, Index.class);
forEach(indexes, "", ", ", (index, separator) -> {
printCreateIndex(out, index);
out.println("%s", separator);
});
out.println(")");
} else if (kotlin) {
out.println("%soverride fun getIndexes(): %s<%s> = listOf(", visibilityPublic(), out.ref(KLIST), Index.class);
forEach(indexes, "", ", ", (index, separator) -> {
printCreateIndex(out, index);
out.println("%s", separator);
});
out.println(")");
} else {
out.overrideInherit();
printNonnullAnnotation(out);
out.println("%s%s<%s> getIndexes() {", visibilityPublic(), List.class, Index.class);
out.println("return %s.asList(", Arrays.class);
forEach(indexes, "", ", ", (index, separator) -> {
printCreateIndex(out, index);
out.println("%s", separator);
});
out.println(");");
out.println("}");
}
}
}
}
// Add primary / unique / foreign key information
if (generateRelations()) {
IdentityDefinition identity = table.getIdentity();
// The identity column
if (identity != null) {
final String identityTypeFull = getJavaType(identity.getColumn().getType(resolver(out)), out);
final String identityType = out.ref(identityTypeFull);
if (scala) {
out.println();
printDeprecationIfUnknownType(out, identityTypeFull);
out.println("%soverride def getIdentity: %s[%s, %s] = super.getIdentity.asInstanceOf[ %s[%s, %s] ]", visibilityPublic(), Identity.class, recordType, identityType, Identity.class, recordType, identityType);
} else if (kotlin) {
printDeprecationIfUnknownType(out, identityTypeFull);
out.println("%soverride fun getIdentity(): %s<%s, %s?> = super.getIdentity() as %s<%s, %s?>", visibilityPublic(), Identity.class, recordType, identityType, Identity.class, recordType, identityType);
} else {
if (printDeprecationIfUnknownType(out, identityTypeFull))
out.override();
else
out.overrideInherit();
printNonnullAnnotation(out);
out.println("%s%s<%s, %s> getIdentity() {", visibilityPublic(), Identity.class, recordType, identityType);
out.println("return (%s<%s, %s>) super.getIdentity();", Identity.class, recordType, identityType);
out.println("}");
}
}
// The primary / main unique key
if (primaryKey != null) {
final String keyFullId = generateGlobalKeyReferences() ? kotlin ? out.ref(getStrategy().getFullJavaIdentifier(primaryKey)) : out.ref(getStrategy().getFullJavaIdentifier(primaryKey), 2) : null;
if (scala) {
out.println();
out.print("%soverride def getPrimaryKey: %s[%s] = ", visibilityPublic(), UniqueKey.class, recordType);
if (keyFullId != null)
out.print("%s", keyFullId);
else
printCreateUniqueKey(out, primaryKey);
out.println();
} else if (kotlin) {
out.print("%soverride fun getPrimaryKey(): %s<%s> = ", visibilityPublic(), UniqueKey.class, recordType);
if (keyFullId != null)
out.print("%s", keyFullId);
else
printCreateUniqueKey(out, primaryKey);
out.println();
} else {
out.overrideInherit();
printNonnullAnnotation(out);
out.println("%s%s<%s> getPrimaryKey() {", visibilityPublic(), UniqueKey.class, recordType);
out.print("return ");
if (keyFullId != null)
out.print("%s", keyFullId);
else
printCreateUniqueKey(out, primaryKey);
out.println(";");
out.println("}");
}
}
// The remaining unique keys
List<UniqueKeyDefinition> uniqueKeys = table.getUniqueKeys();
if (uniqueKeys.size() > 0) {
if (generateGlobalKeyReferences()) {
final List<String> keyFullIds = kotlin ? out.ref(getStrategy().getFullJavaIdentifiers(uniqueKeys)) : out.ref(getStrategy().getFullJavaIdentifiers(uniqueKeys), 2);
if (scala) {
out.println();
out.println("%soverride def getUniqueKeys: %s[ %s[%s] ] = %s.asList[ %s[%s] ]([[%s]])", visibilityPublic(), List.class, UniqueKey.class, recordType, Arrays.class, UniqueKey.class, recordType, keyFullIds);
} else if (kotlin) {
out.println("%soverride fun getUniqueKeys(): %s<%s<%s>> = listOf([[%s]])", visibilityPublic(), out.ref(KLIST), UniqueKey.class, recordType, keyFullIds);
} else {
out.overrideInherit();
printNonnullAnnotation(out);
out.println("%s%s<%s<%s>> getUniqueKeys() {", visibilityPublic(), List.class, UniqueKey.class, recordType);
out.println("return %s.asList([[%s]]);", Arrays.class, keyFullIds);
out.println("}");
}
} else {
if (scala) {
out.println();
out.println("%soverride def getUniqueKeys: %s[ %s[%s] ] = %s.asList[ %s[%s] ](", visibilityPublic(), List.class, UniqueKey.class, recordType, Arrays.class, UniqueKey.class, recordType);
forEach(uniqueKeys, "", ", ", (uniqueKey, separator) -> {
printCreateUniqueKey(out, uniqueKey);
out.println("%s", separator);
});
out.println(")");
} else if (kotlin) {
out.println("%soverride fun getUniqueKeys(): %s<%s<%s>> = listOf(", visibilityPublic(), out.ref(KLIST), UniqueKey.class, recordType);
forEach(uniqueKeys, "", ", ", (uniqueKey, separator) -> {
printCreateUniqueKey(out, uniqueKey);
out.println("%s", separator);
});
out.println(")");
} else {
out.overrideInherit();
printNonnullAnnotation(out);
out.println("%s%s<%s<%s>> getUniqueKeys() {", visibilityPublic(), List.class, UniqueKey.class, recordType);
out.println("return %s.asList(", Arrays.class);
forEach(uniqueKeys, "", ", ", (uniqueKey, separator) -> {
printCreateUniqueKey(out, uniqueKey);
out.println("%s", separator);
});
out.println(");");
out.println("}");
}
}
}
// Foreign keys
List<ForeignKeyDefinition> foreignKeys = table.getForeignKeys();
// [#7554] [#8028] Not yet supported with global key references turned off
if (foreignKeys.size() > 0 && generateGlobalKeyReferences()) {
final List<String> keyFullIds = kotlin ? out.ref(getStrategy().getFullJavaIdentifiers(foreignKeys)) : out.ref(getStrategy().getFullJavaIdentifiers(foreignKeys), 2);
if (scala) {
out.println();
out.println("%soverride def getReferences: %s[ %s[%s, _] ] = %s.asList[ %s[%s, _] ]([[%s]])", visibilityPublic(), List.class, ForeignKey.class, recordType, Arrays.class, ForeignKey.class, recordType, keyFullIds);
} else if (kotlin) {
out.println("%soverride fun getReferences(): %s<%s<%s, *>> = listOf([[%s]])", visibilityPublic(), out.ref(KLIST), ForeignKey.class, recordType, keyFullIds);
} else {
out.overrideInherit();
printNonnullAnnotation(out);
out.println("%s%s<%s<%s, ?>> getReferences() {", visibilityPublic(), List.class, ForeignKey.class, recordType);
out.println("return %s.asList([[%s]]);", Arrays.class, keyFullIds);
out.println("}");
}
// Outbound (to-one) implicit join paths
if (generateImplicitJoinPathsToOne()) {
if (scala) {
} else {
out.println();
// [#8762] Cache these calls for much improved runtime performance!
for (ForeignKeyDefinition foreignKey : foreignKeys) {
final String referencedTableClassName = out.ref(getStrategy().getFullJavaClassName(foreignKey.getReferencedTable()));
final String keyMethodName = out.ref(getStrategy().getJavaMethodName(foreignKey));
// [#13008] Prevent conflicts with the below leading underscore
final String unquotedKeyMethodName = keyMethodName.replace("`", "");
if (kotlin)
out.println("private lateinit var _%s: %s", unquotedKeyMethodName, referencedTableClassName);
else
out.println("private transient %s _%s;", referencedTableClassName, keyMethodName);
}
}
Map<TableDefinition, Long> pathCounts = foreignKeys.stream().collect(groupingBy(ForeignKeyDefinition::getReferencedTable, counting()));
for (ForeignKeyDefinition foreignKey : foreignKeys) {
final String keyFullId = kotlin ? out.ref(getStrategy().getFullJavaIdentifier(foreignKey)) : out.ref(getStrategy().getFullJavaIdentifier(foreignKey), 2);
final String referencedTableClassName = out.ref(getStrategy().getFullJavaClassName(foreignKey.getReferencedTable()));
final String keyMethodName = out.ref(getStrategy().getJavaMethodName(foreignKey));
final String unquotedKeyMethodName = keyMethodName.replace("`", "");
out.javadoc("Get the implicit join path to the <code>" + foreignKey.getReferencedTable().getQualifiedName() + "</code> table" + (pathCounts.get(foreignKey.getReferencedTable()) > 1 ? ", via the <code>" + foreignKey.getInputName() + "</code> key" : "") + ".");
if (scala) {
out.println("%slazy val %s: %s = { new %s(this, %s) }", visibility(), scalaWhitespaceSuffix(keyMethodName), referencedTableClassName, referencedTableClassName, keyFullId);
} else if (kotlin) {
out.println("%sfun %s(): %s {", visibility(), keyMethodName, referencedTableClassName);
out.println("if (!this::_%s.isInitialized)", unquotedKeyMethodName);
out.println("_%s = %s(this, %s)", unquotedKeyMethodName, referencedTableClassName, keyFullId);
out.println();
out.println("return _%s;", unquotedKeyMethodName);
out.println("}");
} else {
out.println("%s%s %s() {", visibility(), referencedTableClassName, keyMethodName);
out.println("if (_%s == null)", keyMethodName);
out.println("_%s = new %s(this, %s);", keyMethodName, referencedTableClassName, keyFullId);
out.println();
out.println("return _%s;", keyMethodName);
out.println("}");
}
}
}
}
}
List<CheckConstraintDefinition> cc = table.getCheckConstraints();
if (!cc.isEmpty()) {
if (scala) {
out.println("%soverride def getChecks: %s[ %s[%s] ] = %s.asList[ %s[%s] ](", visibilityPublic(), List.class, Check.class, recordType, Arrays.class, Check.class, recordType);
} else if (kotlin) {
out.println("%soverride fun getChecks(): %s<%s<%s>> = listOf(", visibilityPublic(), out.ref(KLIST), Check.class, recordType);
} else {
out.overrideInherit();
printNonnullAnnotation(out);
out.println("%s%s<%s<%s>> getChecks() {", visibilityPublic(), List.class, Check.class, recordType);
out.println("return %s.asList(", Arrays.class);
}
forEach(cc, (c, separator) -> {
out.println("%s.createCheck(this, %s.name(\"%s\"), \"%s\", %s)%s", Internal.class, DSL.class, escapeString(c.getName()), escapeString(c.getCheckClause()), c.enforced(), separator);
});
if (scala || kotlin) {
out.println(")");
} else {
out.println(");");
out.println("}");
}
}
// can still be generated
versionLoop: for (String pattern : database.getRecordVersionFields()) {
Pattern p = Pattern.compile(pattern, Pattern.COMMENTS);
for (ColumnDefinition column : table.getColumns()) {
if ((p.matcher(column.getName()).matches() || p.matcher(column.getQualifiedName()).matches())) {
final String columnTypeFull = getJavaType(column.getType(resolver(out)), out);
final String columnType = out.ref(columnTypeFull);
final String columnId = getStrategy().getJavaIdentifier(column);
if (scala) {
printDeprecationIfUnknownType(out, columnTypeFull);
out.println("%soverride def getRecordVersion: %s[%s, %s] = %s", visibilityPublic(), TableField.class, recordType, columnType, columnId);
} else if (kotlin) {
printDeprecationIfUnknownType(out, columnTypeFull);
out.println("%soverride fun getRecordVersion(): %s<%s, %s?> = %s", visibilityPublic(), TableField.class, recordType, columnType, columnId);
} else {
if (printDeprecationIfUnknownType(out, columnTypeFull))
out.override();
else
out.overrideInherit();
printNonnullAnnotation(out);
out.println("%s%s<%s, %s> getRecordVersion() {", visibilityPublic(), TableField.class, recordType, columnType);
out.println("return %s;", columnId);
out.println("}");
}
// Avoid generating this method twice
break versionLoop;
}
}
}
timestampLoop: for (String pattern : database.getRecordTimestampFields()) {
Pattern p = Pattern.compile(pattern, Pattern.COMMENTS);
for (ColumnDefinition column : table.getColumns()) {
if ((p.matcher(column.getName()).matches() || p.matcher(column.getQualifiedName()).matches())) {
final String columnTypeFull = getJavaType(column.getType(resolver(out)), out);
final String columnType = out.ref(columnTypeFull);
final String columnId = getStrategy().getJavaIdentifier(column);
if (scala) {
printDeprecationIfUnknownType(out, columnTypeFull);
out.println("%soverride def getRecordTimestamp: %s[%s, %s] = %s", visibilityPublic(), TableField.class, recordType, columnType, columnId);
} else if (kotlin) {
printDeprecationIfUnknownType(out, columnTypeFull);
out.println("%soverride fun getRecordTimestamp(): %s<%s, %s?> = %s", visibilityPublic(), TableField.class, recordType, columnType, columnId);
} else {
if (printDeprecationIfUnknownType(out, columnTypeFull))
out.override();
else
out.overrideInherit();
printNonnullAnnotation(out);
out.println("%s%s<%s, %s> getRecordTimestamp() {", visibilityPublic(), TableField.class, recordType, columnType);
out.println("return %s;", columnId);
out.println("}");
}
// Avoid generating this method twice
break timestampLoop;
}
}
}
if (scala) {
out.print("%soverride def as(alias: %s): %s = ", visibilityPublic(), String.class, className);
if (table.isTableValuedFunction())
out.println("new %s(%s.name(alias), null, null, this, parameters)", className, DSL.class);
else
out.println("new %s(%s.name(alias), this)", className, DSL.class);
out.print("%soverride def as(alias: %s): %s = ", visibilityPublic(), Name.class, className);
if (table.isTableValuedFunction())
out.println("new %s(alias, null, null, this, parameters)", className);
else
out.println("new %s(alias, this)", className);
} else if (kotlin) {
out.print("%soverride fun `as`(alias: %s): %s = ", visibilityPublic(), String.class, className);
if (table.isTableValuedFunction())
out.println("%s(%s.name(alias), this, parameters)", className, DSL.class);
else
out.println("%s(%s.name(alias), this)", className, DSL.class);
out.print("%soverride fun `as`(alias: %s): %s = ", visibilityPublic(), Name.class, className);
if (table.isTableValuedFunction())
out.println("%s(alias, this, parameters)", className);
else
out.println("%s(alias, this)", className);
} else // type-safe table alias
if (generateInstanceFields()) {
out.overrideInherit();
printNonnullAnnotation(out);
out.println("%s%s as(%s alias) {", visibilityPublic(), className, String.class);
if (table.isTableValuedFunction())
out.println("return new %s(%s.name(alias), this, parameters);", className, DSL.class);
else
out.println("return new %s(%s.name(alias), this);", className, DSL.class);
out.println("}");
out.overrideInherit();
printNonnullAnnotation(out);
out.println("%s%s as(%s alias) {", visibilityPublic(), className, Name.class);
if (table.isTableValuedFunction())
out.println("return new %s(alias, this, parameters);", className);
else
out.println("return new %s(alias, this);", className);
out.println("}");
}
if (scala) {
out.javadoc("Rename this table");
out.print("%soverride def rename(name: %s): %s = ", visibilityPublic(), String.class, className);
if (table.isTableValuedFunction())
out.println("new %s(%s.name(name), null, null, null, parameters)", className, DSL.class);
else
out.println("new %s(%s.name(name), null)", className, DSL.class);
out.javadoc("Rename this table");
out.print("%soverride def rename(name: %s): %s = ", visibilityPublic(), Name.class, className);
if (table.isTableValuedFunction())
out.println("new %s(name, null, null, null, parameters)", className);
else
out.println("new %s(name, null)", className);
} else if (kotlin) {
out.javadoc("Rename this table");
out.print("%soverride fun rename(name: %s): %s = ", visibilityPublic(), String.class, className);
if (table.isTableValuedFunction())
out.println("%s(%s.name(name), null, parameters)", className, DSL.class);
else
out.println("%s(%s.name(name), null)", className, DSL.class);
out.javadoc("Rename this table");
out.print("%soverride fun rename(name: %s): %s = ", visibilityPublic(), Name.class, className);
if (table.isTableValuedFunction())
out.println("%s(name, null, parameters)", className);
else
out.println("%s(name, null)", className);
} else // [#2921] With instance fields, tables can be renamed.
if (generateInstanceFields()) {
out.javadoc("Rename this table");
out.override();
printNonnullAnnotation(out);
out.println("%s%s rename(%s name) {", visibilityPublic(), className, String.class);
if (table.isTableValuedFunction())
out.println("return new %s(%s.name(name), null, parameters);", className, DSL.class);
else
out.println("return new %s(%s.name(name), null);", className, DSL.class);
out.println("}");
out.javadoc("Rename this table");
out.override();
printNonnullAnnotation(out);
out.println("%s%s rename(%s name) {", visibilityPublic(), className, Name.class);
if (table.isTableValuedFunction())
out.println("return new %s(name, null, parameters);", className);
else
out.println("return new %s(name, null);", className);
out.println("}");
}
// [#7809] fieldsRow()
// [#10481] Use the types from replaced embeddables if applicable
List<Definition> replacingEmbeddablesAndUnreplacedColumns = replacingEmbeddablesAndUnreplacedColumns(table);
int degree = replacingEmbeddablesAndUnreplacedColumns.size();
String rowType = refRowType(out, replacingEmbeddablesAndUnreplacedColumns);
String rowTypeContravariantJava = refRowType(out, replacingEmbeddablesAndUnreplacedColumns, s -> "? super " + s);
if (generateRecordsImplementingRecordN() && degree > 0 && degree <= Constants.MAX_ROW_DEGREE) {
final String rowNType = out.ref(Row.class.getName() + degree);
out.header("Row%s type methods", degree);
if (scala) {
out.println("%soverride def fieldsRow: %s[%s] = super.fieldsRow.asInstanceOf[ %s[%s] ]", visibilityPublic(), rowNType, rowType, rowNType, rowType);
} else if (kotlin) {
out.println("%soverride fun fieldsRow(): %s<%s> = super.fieldsRow() as %s<%s>", visibilityPublic(), rowNType, rowType, rowNType, rowType);
} else {
out.overrideInherit();
printNonnullAnnotation(out);
out.println("%s%s<%s> fieldsRow() {", visibilityPublic(), rowNType, rowType);
out.println("return (%s) super.fieldsRow();", rowNType);
out.println("}");
}
}
// [#1070] Table-valued functions should generate an additional set of call() methods
if (table.isTableValuedFunction()) {
for (boolean parametersAsField : new boolean[] { false, true }) {
// Don't overload no-args call() methods
if (parametersAsField && parameters.size() == 0)
break;
out.javadoc("Call this table-valued function");
if (scala) {
out.print("%sdef call(", visibility()).printlnIf(!parameters.isEmpty());
printParameterDeclarations(out, parameters, parametersAsField, " ");
out.print("): %s = ", className);
out.print("Option(new %s(%s.name(\"%s\"), null, null, null, %s(", className, DSL.class, escapeString(table.getOutputName()), out.ref("scala.Array")).printlnIf(!parameters.isEmpty());
forEach(parameters, (parameter, separator) -> {
final String paramArgName = getStrategy().getJavaMemberName(parameter);
final String paramTypeRef = getJavaTypeReference(parameter.getDatabase(), parameter.getType(resolver(out)), out);
final List<String> converter = out.ref(list(parameter.getType(resolver(out)).getConverter()));
final List<String> binding = out.ref(list(parameter.getType(resolver(out)).getBinding()));
if (parametersAsField)
out.println("%s%s", paramArgName, separator);
else
out.println("%s.value(%s, %s" + converterTemplateForTableValuedFunction(converter) + converterTemplateForTableValuedFunction(binding) + ")%s", DSL.class, paramArgName, paramTypeRef, converter, binding, separator);
});
out.println("))).map(r => if (aliased()) r.as(getUnqualifiedName) else r).get");
} else if (kotlin) {
out.print("%sfun call(", visibility()).printlnIf(!parameters.isEmpty());
printParameterDeclarations(out, parameters, parametersAsField, " ");
out.print("): %s = %s(%s.name(\"%s\"), null, arrayOf(", className, className, DSL.class, escapeString(table.getOutputName()), Field.class).printlnIf(!parameters.isEmpty());
forEach(parameters, (parameter, separator) -> {
final String paramArgName = getStrategy().getJavaMemberName(parameter);
final String paramTypeRef = getJavaTypeReference(parameter.getDatabase(), parameter.getType(resolver(out)), out);
final List<String> converter = out.ref(list(parameter.getType(resolver(out)).getConverter()));
final List<String> binding = out.ref(list(parameter.getType(resolver(out)).getBinding()));
if (parametersAsField)
out.println("%s%s", paramArgName, separator);
else
out.println("%s.value(%s, %s" + converterTemplateForTableValuedFunction(converter) + converterTemplateForTableValuedFunction(binding) + ")%s", DSL.class, paramArgName, paramTypeRef, converter, binding, separator);
});
out.println(")).let { if (aliased()) it.`as`(unqualifiedName) else it }");
} else {
out.print("%s%s call(", visibility(), className).printlnIf(!parameters.isEmpty());
printParameterDeclarations(out, parameters, parametersAsField, " ");
out.println(") {");
out.print("%s result = new %s(%s.name(\"%s\"), null, new %s[] {", className, className, DSL.class, escapeString(table.getOutputName()), Field.class).printlnIf(!parameters.isEmpty());
forEach(parameters, (parameter, separator) -> {
final String paramArgName = getStrategy().getJavaMemberName(parameter);
final String paramTypeRef = getJavaTypeReference(parameter.getDatabase(), parameter.getType(resolver(out)), out);
final List<String> converter = out.ref(list(parameter.getType(resolver(out)).getConverter()));
final List<String> binding = out.ref(list(parameter.getType(resolver(out)).getBinding()));
if (parametersAsField)
out.println("%s%s", paramArgName, separator);
else
out.println("%s.val(%s, %s" + converterTemplateForTableValuedFunction(converter) + converterTemplateForTableValuedFunction(binding) + ")%s", DSL.class, paramArgName, paramTypeRef, converter, binding, separator);
});
out.println("});");
out.println();
out.println("return aliased() ? result.as(getUnqualifiedName()) : result;");
out.println("}");
}
}
}
if (generateRecordsImplementingRecordN() && degree > 0 && degree <= Constants.MAX_ROW_DEGREE) {
out.javadoc("Convenience mapping calling {@link #convertFrom(%s)}.", Function.class);
if (scala) {
out.println("%sdef mapping[U](from: (" + rowType + ") => U): %s[U] = convertFrom(r => from.apply(" + rangeClosed(1, degree).mapToObj(i -> "r.value" + i + "()").collect(joining(", ")) + "))", visibility(), SelectField.class);
} else if (kotlin) {
out.println("%sfun <U> mapping(from: (" + rowType + ") -> U): %s<U> = convertFrom(%s.mapping(from))", visibility(), SelectField.class, Records.class);
} else {
out.println("%s<U> %s<U> mapping(%s<" + rowTypeContravariantJava + ", ? extends U> from) {", visibility(), SelectField.class, out.ref("org.jooq.Function" + degree));
out.println("return convertFrom(%s.mapping(from));", Records.class);
out.println("}");
}
out.javadoc("Convenience mapping calling {@link #convertFrom(%s, %s)}.", Class.class, Function.class);
if (scala) {
out.println("%sdef mapping[U](toType: %s[U], from: (" + rowType + ") => U): %s[U] = convertFrom(toType,r => from.apply(" + rangeClosed(1, degree).mapToObj(i -> "r.value" + i + "()").collect(joining(", ")) + "))", visibility(), Class.class, SelectField.class);
} else if (kotlin) {
out.println("%sfun <U> mapping(toType: %s<U>, from: (" + rowType + ") -> U): %s<U> = convertFrom(toType, %s.mapping(from))", visibility(), Class.class, SelectField.class, Records.class);
} else {
out.println("%s<U> %s<U> mapping(%s<U> toType, %s<" + rowTypeContravariantJava + ", ? extends U> from) {", visibility(), SelectField.class, Class.class, out.ref("org.jooq.Function" + degree));
out.println("return convertFrom(toType, %s.mapping(from));", Records.class);
out.println("}");
}
}
generateTableClassFooter(table, out);
out.println("}");
closeJavaWriter(out);
}
use of org.jooq.meta.SchemaDefinition in project jOOQ by jOOQ.
the class JavaGenerator method generateRoutine.
protected void generateRoutine(RoutineDefinition routine, JavaWriter out) {
final SchemaDefinition schema = routine.getSchema();
final String className = getStrategy().getJavaClassName(routine);
final String returnTypeFull = (routine.getReturnValue() == null) ? Void.class.getName() : getJavaType(routine.getReturnType(resolver(out)), out);
final String returnType = (routine.getReturnValue() == null) ? Void.class.getName() : out.ref(returnTypeFull);
final List<String> returnTypeRef = list((routine.getReturnValue() != null) ? getJavaTypeReference(database, routine.getReturnType(resolver(out)), out) : null);
final List<String> returnConverter = out.ref(list((routine.getReturnValue() != null) ? routine.getReturnType(resolver(out)).getConverter() : null));
final List<String> returnBinding = out.ref(list((routine.getReturnValue() != null) ? routine.getReturnType(resolver(out)).getBinding() : null));
final List<String> interfaces = out.ref(getStrategy().getJavaClassImplements(routine, Mode.DEFAULT));
final String schemaId = out.ref(getStrategy().getFullJavaIdentifier(schema), 2);
final List<String> packageId = out.ref(getStrategy().getFullJavaIdentifiers(routine.getPackage()), 2);
printPackage(out, routine);
if (scala) {
out.println("object %s {", className);
for (ParameterDefinition parameter : routine.getAllParameters()) {
final String paramTypeFull = getJavaType(parameter.getType(resolver(out)), out);
final String paramType = out.ref(paramTypeFull);
final String paramTypeRef = getJavaTypeReference(parameter.getDatabase(), parameter.getType(resolver(out)), out);
final String paramId = out.ref(getStrategy().getJavaIdentifier(parameter), 2);
final String paramName = parameter.getName();
final String isDefaulted = parameter.isDefaulted() ? "true" : "false";
final String isUnnamed = parameter.isUnnamed() ? "true" : "false";
final List<String> converter = out.ref(list(parameter.getType(resolver(out)).getConverter()));
final List<String> binding = out.ref(list(parameter.getType(resolver(out)).getBinding()));
if (!printDeprecationIfUnknownType(out, paramTypeFull))
out.javadoc("The parameter <code>%s</code>.[[before= ][%s]]", parameter.getQualifiedOutputName(), list(escapeEntities(comment(parameter))));
out.println("val %s: %s[%s] = %s.createParameter(\"%s\", %s, %s, %s" + converterTemplate(converter) + converterTemplate(binding) + ")", scalaWhitespaceSuffix(paramId), Parameter.class, paramType, Internal.class, escapeString(paramName), paramTypeRef, isDefaulted, isUnnamed, converter, binding);
}
out.println("}");
out.println();
}
if (!printDeprecationIfUnknownType(out, returnTypeFull))
generateRoutineClassJavadoc(routine, out);
printClassAnnotations(out, routine, Mode.DEFAULT);
if (scala) {
out.println("%sclass %s extends %s[%s](\"%s\", %s[[before=, ][%s]][[before=, ][%s]]" + converterTemplate(returnConverter) + converterTemplate(returnBinding) + ")[[before= with ][separator= with ][%s]] {", visibility(), className, AbstractRoutine.class, returnType, escapeString(routine.getName()), schemaId, packageId, returnTypeRef, returnConverter, returnBinding, interfaces);
} else {
if (kotlin) {
out.println("%sopen class %s : %s<%s>(\"%s\", %s[[before=, ][%s]][[before=, ][%s]]" + converterTemplate(returnConverter) + converterTemplate(returnBinding) + ")[[before=, ][%s]] {", visibility(), className, AbstractRoutine.class, returnType, escapeString(routine.getName()), schemaId, packageId, returnTypeRef, returnConverter, returnBinding, interfaces);
} else {
out.println("%sclass %s extends %s<%s>[[before= implements ][%s]] {", visibility(), className, AbstractRoutine.class, returnType, interfaces);
out.printSerial();
}
if (kotlin)
out.println("%scompanion object {", visibility());
for (ParameterDefinition parameter : routine.getAllParameters()) {
final String paramTypeFull = getJavaType(parameter.getType(resolver(out)), out);
final String paramType = out.ref(paramTypeFull);
final String paramTypeRef = getJavaTypeReference(parameter.getDatabase(), parameter.getType(resolver(out)), out);
final String paramId = getStrategy().getJavaIdentifier(parameter);
final String paramName = parameter.getName();
final String isDefaulted = parameter.isDefaulted() ? "true" : "false";
final String isUnnamed = parameter.isUnnamed() ? "true" : "false";
final List<String> converter = out.ref(list(parameter.getType(resolver(out)).getConverter()));
final List<String> binding = out.ref(list(parameter.getType(resolver(out)).getBinding()));
if (!printDeprecationIfUnknownType(out, paramTypeFull))
out.javadoc("The parameter <code>%s</code>.[[before= ][%s]]", parameter.getQualifiedOutputName(), list(escapeEntities(comment(parameter))));
if (kotlin)
out.println("%sval %s: %s<%s?> = %s.createParameter(\"%s\", %s, %s, %s" + converterTemplate(converter) + converterTemplate(binding) + ")", visibility(), scalaWhitespaceSuffix(paramId), Parameter.class, paramType, Internal.class, escapeString(paramName), paramTypeRef, isDefaulted, isUnnamed, converter, binding);
else
out.println("%sstatic final %s<%s> %s = %s.createParameter(\"%s\", %s, %s, %s" + converterTemplate(converter) + converterTemplate(binding) + ");", visibility(), Parameter.class, paramType, paramId, Internal.class, escapeString(paramName), paramTypeRef, isDefaulted, isUnnamed, converter, binding);
}
if (kotlin)
out.println("}").println();
}
if (scala) {
out.println("{");
} else if (kotlin) {
out.println("init {");
} else {
out.javadoc("Create a new routine call instance");
out.println("%s%s() {", visibility(), className);
out.println("super(\"%s\", %s[[before=, ][%s]][[before=, ][%s]]" + converterTemplate(returnConverter) + converterTemplate(returnBinding) + ");", routine.getName(), schemaId, packageId, returnTypeRef, returnConverter, returnBinding);
if (routine.getAllParameters().size() > 0)
out.println();
}
for (ParameterDefinition parameter : routine.getAllParameters()) {
final String paramId = getStrategy().getJavaIdentifier(parameter);
if (parameter.equals(routine.getReturnValue())) {
if (parameter.isSynthetic()) {
if (scala)
out.println("setSyntheticReturnParameter(%s.%s)", className, paramId);
else if (kotlin)
out.println("syntheticReturnParameter = %s", paramId);
else
out.println("setSyntheticReturnParameter(%s);", paramId);
} else {
if (scala)
out.println("setReturnParameter(%s.%s)", className, paramId);
else if (kotlin)
out.println("returnParameter = %s", paramId);
else
out.println("setReturnParameter(%s);", paramId);
}
} else if (routine.getInParameters().contains(parameter)) {
if (routine.getOutParameters().contains(parameter)) {
if (scala)
out.println("addInOutParameter(%s.%s)", className, paramId);
else if (kotlin)
out.println("addInOutParameter(%s)", paramId);
else
out.println("addInOutParameter(%s);", paramId);
} else {
if (scala)
out.println("addInParameter(%s.%s)", className, paramId);
else if (kotlin)
out.println("addInParameter(%s)", paramId);
else
out.println("addInParameter(%s);", paramId);
}
} else {
if (scala)
out.println("addOutParameter(%s.%s)", className, paramId);
else if (kotlin)
out.println("addOutParameter(%s)", paramId);
else
out.println("addOutParameter(%s);", paramId);
}
}
if (routine.getOverload() != null)
out.println("setOverloaded(true)%s", semicolon);
if (routine instanceof PostgresRoutineDefinition)
if (((PostgresRoutineDefinition) routine).isProcedure())
out.println("setSQLUsable(false)%s", semicolon);
out.println("}");
for (ParameterDefinition parameter : routine.getInParameters()) {
final String setterReturnType = generateFluentSetters() ? className : tokenVoid;
final String setter = getStrategy().getJavaSetterName(parameter, Mode.DEFAULT);
final String numberValue = parameter.getType(resolver(out)).isGenericNumberType() ? "Number" : "Value";
final String numberField = parameter.getType(resolver(out)).isGenericNumberType() ? "Number" : "Field";
final String paramId = getStrategy().getJavaIdentifier(parameter);
final String paramName = "value".equals(paramId) ? "value_" : "value";
out.javadoc("Set the <code>%s</code> parameter IN value to the routine", parameter.getOutputName());
if (scala) {
out.println("%sdef %s(%s: %s) : Unit = set%s(%s.%s, %s)", visibility(), setter, scalaWhitespaceSuffix(paramName), refNumberType(out, parameter.getType(resolver(out))), numberValue, className, paramId, paramName);
} else if (kotlin) {
out.println("%sfun %s(%s: %s?): Unit = set%s(%s, %s)", visibility(), setter, paramName, refNumberType(out, parameter.getType(resolver(out))), numberValue, paramId, paramName);
} else {
out.println("%svoid %s(%s %s) {", visibility(), setter, varargsIfArray(refNumberType(out, parameter.getType(resolver(out)))), paramName);
out.println("set%s(%s, %s);", numberValue, paramId, paramName);
out.println("}");
}
if (routine.isSQLUsable()) {
out.javadoc("Set the <code>%s</code> parameter to the function to be used with a {@link org.jooq.Select} statement", parameter.getOutputName());
if (scala) {
out.println("%sdef %s(field: %s[%s]): %s = {", visibility(), setter, Field.class, refExtendsNumberType(out, parameter.getType(resolver(out))), setterReturnType);
out.println("set%s(%s.%s, field)", numberField, className, paramId);
if (generateFluentSetters())
out.println("this");
out.println("}");
} else if (kotlin) {
out.println("%sfun %s(field: %s<%s?>): %s {", visibility(), setter, Field.class, refExtendsNumberType(out, parameter.getType(resolver(out))), setterReturnType);
out.println("set%s(%s, field)", numberField, paramId);
if (generateFluentSetters())
out.println("return this");
out.println("}");
} else {
out.println("%s%s %s(%s<%s> field) {", visibility(), setterReturnType, setter, Field.class, refExtendsNumberType(out, parameter.getType(resolver(out))));
out.println("set%s(%s, field);", numberField, paramId);
if (generateFluentSetters())
out.println("return this;");
out.println("}");
}
}
}
for (ParameterDefinition parameter : routine.getAllParameters()) {
boolean isReturnValue = parameter.equals(routine.getReturnValue());
boolean isOutParameter = routine.getOutParameters().contains(parameter);
if (isOutParameter && !isReturnValue) {
final String paramName = parameter.getOutputName();
final String paramTypeFull = getJavaType(parameter.getType(resolver(out)), out);
final String paramType = out.ref(paramTypeFull);
final String paramGetter = getStrategy().getJavaGetterName(parameter, Mode.DEFAULT);
final String paramId = getStrategy().getJavaIdentifier(parameter);
if (!printDeprecationIfUnknownType(out, paramTypeFull))
out.javadoc("Get the <code>%s</code> parameter OUT value from the routine", paramName);
if (scala) {
out.println("%sdef %s: %s = get(%s.%s)", visibility(), scalaWhitespaceSuffix(paramGetter), paramType, className, paramId);
} else if (kotlin) {
out.println("%sfun %s(): %s? = get(%s)", visibility(), paramGetter, paramType, paramId);
} else {
out.println("%s%s %s() {", visibility(), paramType, paramGetter);
out.println("return get(%s);", paramId);
out.println("}");
}
}
}
generateRoutineClassFooter(routine, out);
out.println("}");
}
Aggregations