use of org.jooq.meta.PackageDefinition in project jOOQ by jOOQ.
the class JavaGenerator method generateUDT.
protected void generateUDT(UDTDefinition udt, JavaWriter out) {
final SchemaDefinition schema = udt.getSchema();
final PackageDefinition pkg = udt.getPackage();
final boolean synthetic = udt.isSynthetic();
final String className = getStrategy().getJavaClassName(udt);
final String recordType = out.ref(getStrategy().getFullJavaClassName(udt, Mode.RECORD));
final List<String> interfaces = out.ref(getStrategy().getJavaClassImplements(udt, Mode.DEFAULT));
final String schemaId = out.ref(getStrategy().getFullJavaIdentifier(schema), 2);
final String packageId = pkg == null ? null : out.ref(getStrategy().getFullJavaIdentifier(pkg), 2);
final String udtId = out.ref(getStrategy().getJavaIdentifier(udt), 2);
printPackage(out, udt);
if (scala) {
out.println("object %s {", className);
printSingletonInstance(out, udt);
for (AttributeDefinition attribute : udt.getAttributes()) {
final String attrId = out.ref(getStrategy().getJavaIdentifier(attribute), 2);
out.javadoc("The attribute <code>%s</code>.[[before= ][%s]]", attribute.getQualifiedOutputName(), list(escapeEntities(comment(attribute))));
out.println("val %s = %s.%s", attrId, udtId, attrId);
}
out.println("}");
out.println();
}
generateUDTClassJavadoc(udt, out);
printClassAnnotations(out, udt, Mode.DEFAULT);
if (scala) {
out.println("%sclass %s extends %s[%s](\"%s\", null, %s, %s)[[before= with ][separator= with ][%s]] {", visibility(), className, UDTImpl.class, recordType, escapeString(udt.getOutputName()), packageId, synthetic, interfaces);
} else if (kotlin) {
out.println("%sopen class %s : %s<%s>(\"%s\", null, %s, %s)[[before=, ][%s]] {", visibility(), className, UDTImpl.class, recordType, escapeString(udt.getOutputName()), packageId, synthetic, interfaces);
out.println();
out.println("public companion object {");
out.javadoc("The reference instance of <code>%s</code>", udt.getQualifiedOutputName());
out.println("public val %s: %s = %s()", getStrategy().getJavaIdentifier(udt), className, className);
out.println("}");
} else {
out.println("%sclass %s extends %s<%s>[[before= implements ][%s]] {", visibility(), className, UDTImpl.class, recordType, interfaces);
out.printSerial();
printSingletonInstance(out, udt);
}
printRecordTypeMethod(out, udt);
for (AttributeDefinition attribute : udt.getAttributes()) {
final String attrTypeFull = getJavaType(attribute.getType(resolver(out)), out);
final String attrType = out.ref(attrTypeFull);
final String attrTypeRef = getJavaTypeReference(attribute.getDatabase(), attribute.getType(resolver(out)), out);
final String attrId = out.ref(getStrategy().getJavaIdentifier(attribute), 2);
final String attrName = attribute.getName();
final List<String> converter = out.ref(list(attribute.getType(resolver(out)).getConverter()));
final List<String> binding = out.ref(list(attribute.getType(resolver(out)).getBinding()));
if (!printDeprecationIfUnknownType(out, attrTypeFull))
out.javadoc("The attribute <code>%s</code>.[[before= ][%s]]", attribute.getQualifiedOutputName(), list(escapeEntities(comment(attribute))));
if (scala)
out.println("private val %s: %s[%s, %s] = %s.createField(%s.name(\"%s\"), %s, this, \"%s\"" + converterTemplate(converter) + converterTemplate(binding) + ")", scalaWhitespaceSuffix(attrId), UDTField.class, recordType, attrType, UDTImpl.class, DSL.class, escapeString(attrName), attrTypeRef, escapeString(""), converter, binding);
else if (kotlin)
out.println("%sval %s: %s<%s, %s> = %s.createField(%s.name(\"%s\"), %s, this, \"%s\"" + converterTemplate(converter) + converterTemplate(binding) + ")", visibility(), attrId, UDTField.class, recordType, attrType, UDTImpl.class, DSL.class, escapeString(attrName), attrTypeRef, escapeString(""), converter, binding);
else
out.println("%sstatic final %s<%s, %s> %s = createField(%s.name(\"%s\"), %s, %s, \"%s\"" + converterTemplate(converter) + converterTemplate(binding) + ");", visibility(), UDTField.class, recordType, attrType, attrId, DSL.class, escapeString(attrName), attrTypeRef, udtId, escapeString(""), converter, binding);
}
// [#799] Oracle UDT's can have member procedures
for (RoutineDefinition routine : udt.getRoutines()) {
try {
if (!routine.isSQLUsable()) {
// Static execute() convenience method
printConvenienceMethodProcedure(out, routine, false);
} else {
// Static execute() convenience method
if (!routine.isAggregate())
printConvenienceMethodFunction(out, routine, false);
// Static asField() convenience method
printConvenienceMethodFunctionAsField(out, routine, false);
printConvenienceMethodFunctionAsField(out, routine, true);
}
} catch (Exception e) {
log.error("Error while generating routine " + routine, e);
}
}
if (scala || kotlin) {
} else {
out.javadoc(NO_FURTHER_INSTANCES_ALLOWED);
out.println("private %s() {", className);
out.println("super(\"%s\", null, %s, %s);", udt.getOutputName(), packageId, synthetic);
out.println("}");
}
if (scala) {
out.println();
out.println("%soverride def getSchema: %s = %s", visibilityPublic(), Schema.class, schemaId);
} else if (kotlin) {
out.println();
out.println("%s override fun getSchema(): %s = %s", visibilityPublic(), Schema.class, schemaId);
} else {
out.overrideInherit();
out.println("%s%s getSchema() {", visibilityPublic(), Schema.class);
out.println("return %s != null ? %s : new %s(%s.name(\"%s\"));", schemaId, schemaId, SchemaImpl.class, DSL.class, schema.getOutputName());
out.println("}");
}
generateUDTClassFooter(udt, out);
out.println("}");
closeJavaWriter(out);
}
use of org.jooq.meta.PackageDefinition in project jOOQ by jOOQ.
the class XMLDatabase method getPackages0.
@Override
protected List<PackageDefinition> getPackages0() {
List<PackageDefinition> result = new ArrayList<>();
Set<String> packages = new HashSet<>();
for (Routine routine : info().getRoutines()) {
String schemaName = defaultIfBlank(routine.getSpecificSchema(), routine.getRoutineSchema());
if (getInputSchemata().contains(schemaName)) {
SchemaDefinition schema = getSchema(schemaName);
String packageName = defaultIfBlank(routine.getSpecificPackage(), routine.getRoutinePackage());
if (!isBlank(packageName) && packages.add(packageName)) {
result.add(new XMLPackageDefinition(schema, info(), packageName));
}
}
}
return result;
}
use of org.jooq.meta.PackageDefinition in project jOOQ by jOOQ.
the class JavaGenerator method generateUDTReferences.
/**
* Generating central static udt access
*/
protected void generateUDTReferences(Definition schemaOrPackage) {
String logSuffix = schemaOrPackage instanceof SchemaDefinition ? "" : (" for package " + schemaOrPackage.getOutputName());
log.info("Generating UDT references" + logSuffix);
JavaWriter out = newJavaWriter(getStrategy().getGlobalReferencesFile(schemaOrPackage, UDTDefinition.class));
printGlobalReferencesPackage(out, schemaOrPackage, UDTDefinition.class);
if (!kotlin) {
printClassJavadoc(out, "Convenience access to all UDTs in " + schemaNameOrDefault(schemaOrPackage) + ".");
printClassAnnotations(out, schemaOrPackage, Mode.DEFAULT);
}
final String referencesClassName = getStrategy().getGlobalReferencesJavaClassName(schemaOrPackage, UDTDefinition.class);
if (scala)
out.println("%sobject %s {", visibility(), referencesClassName);
else if (kotlin) {
} else
out.println("%sclass %s {", visibility(), referencesClassName);
List<UDTDefinition> udts = new ArrayList<>();
if (schemaOrPackage instanceof SchemaDefinition) {
for (UDTDefinition udt : database.getUDTs((SchemaDefinition) schemaOrPackage)) if (udt.getPackage() == null)
udts.add(udt);
} else
udts.addAll(database.getUDTs((PackageDefinition) schemaOrPackage));
for (UDTDefinition udt : udts) {
final String className = out.ref(getStrategy().getFullJavaClassName(udt));
final String id = getStrategy().getJavaIdentifier(udt);
final String fullId = getStrategy().getFullJavaIdentifier(udt);
out.javadoc("The type <code>%s</code>", udt.getQualifiedOutputName());
if (scala)
out.println("%sdef %s = %s", visibility(), id, fullId);
else if (kotlin)
out.println("%sval %s: %s = %s", visibility(), id, className, fullId);
else
out.println("%sstatic final %s %s = %s;", visibility(), className, id, fullId);
}
generateUDTReferencesClassFooter(schemaOrPackage, out);
if (!kotlin)
out.println("}");
closeJavaWriter(out);
watch.splitInfo("UDT references generated" + logSuffix);
if (schemaOrPackage instanceof SchemaDefinition)
for (PackageDefinition pkg : database.getPackages((SchemaDefinition) schemaOrPackage)) if (!pkg.getUDTs().isEmpty())
generateUDTReferences(pkg);
}
use of org.jooq.meta.PackageDefinition in project jOOQ by jOOQ.
the class XMLGenerator method generate0.
@Override
public void generate0(Database db) {
logDatabaseParameters(db);
log.info("");
logGenerationRemarks(db);
log.info("");
log.info("----------------------------------------------------------");
TextWriter out = new TextWriter(getStrategy().getFile("information_schema.xml"), targetEncoding);
log.info("");
log.info("Generating XML", out.file().getName());
log.info("==========================================================");
InformationSchema is = new InformationSchema();
boolean hasNonDefaultCatalogs = false;
for (CatalogDefinition c : db.getCatalogs()) {
if (!StringUtils.isBlank(c.getName())) {
hasNonDefaultCatalogs = true;
break;
}
}
for (CatalogDefinition c : db.getCatalogs()) {
String catalogName = c.getOutputName();
if (hasNonDefaultCatalogs)
is.getCatalogs().add(new Catalog().withCatalogName(catalogName).withComment(generateCommentsOnCatalogs() ? c.getComment() : null));
for (SchemaDefinition s : c.getSchemata()) {
String schemaName = s.getOutputName();
Schema schema = new Schema();
schema.setCatalogName(catalogName);
schema.setSchemaName(schemaName);
if (generateCommentsOnSchemas())
schema.setComment(s.getComment());
is.getSchemata().add(schema);
for (TableDefinition t : s.getTables()) {
String tableName = t.getOutputName();
Table table = new Table();
table.setTableCatalog(catalogName);
table.setTableSchema(schemaName);
table.setTableName(tableName);
table.setTableType(t.isView() ? TableType.VIEW : t.isTemporary() ? TableType.GLOBAL_TEMPORARY : TableType.BASE_TABLE);
if (generateCommentsOnTables())
table.setComment(t.getComment());
is.getTables().add(table);
if (t.isView()) {
View view = new View();
view.setTableCatalog(catalogName);
view.setTableSchema(schemaName);
view.setTableName(tableName);
if (generateSourcesOnViews())
view.setViewDefinition(t.getSource());
is.getViews().add(view);
}
for (ColumnDefinition co : t.getColumns()) {
String columnName = co.getOutputName();
DataTypeDefinition type = co.getType();
Column column = new Column();
column.setTableCatalog(catalogName);
column.setTableSchema(schemaName);
column.setTableName(tableName);
column.setColumnName(columnName);
if (generateCommentsOnColumns())
column.setComment(co.getComment());
column.setCharacterMaximumLength(type.getLength());
column.setColumnDefault(type.getDefaultValue());
column.setDataType(type.getType());
if (co.isIdentity())
column.setIdentityGeneration("YES");
column.setIsNullable(type.isNullable());
column.setNumericPrecision(type.getPrecision());
column.setNumericScale(type.getScale());
column.setOrdinalPosition(co.getPosition());
column.setReadonly(co.isReadonly());
if (type.isComputed()) {
column.setIsGenerated(type.isComputed());
column.setGenerationExpression(type.getGeneratedAlwaysAs());
column.setGenerationOption(type.getGenerationOption() == VIRTUAL ? "VIRTUAL" : type.getGenerationOption() == STORED ? "STORED" : null);
}
is.getColumns().add(column);
}
}
for (IndexDefinition i : db.getIndexes(s)) {
String indexName = i.getOutputName();
TableDefinition table = i.getTable();
List<IndexColumnDefinition> columns = i.getIndexColumns();
Index index = new Index();
index.setIndexCatalog(catalogName);
index.setIndexSchema(schemaName);
index.setIndexName(indexName);
if (generateCommentsOnKeys())
index.setComment(i.getComment());
index.setTableCatalog(table.getCatalog().getOutputName());
index.setTableSchema(table.getSchema().getOutputName());
index.setTableName(table.getOutputName());
index.setIsUnique(i.isUnique());
is.getIndexes().add(index);
for (int j = 0; j < columns.size(); j++) {
IndexColumnDefinition indexColumn = columns.get(j);
ColumnDefinition column = indexColumn.getColumn();
IndexColumnUsage ic = new IndexColumnUsage();
ic.setIndexCatalog(catalogName);
ic.setIndexSchema(schemaName);
ic.setIndexName(indexName);
ic.setColumnName(column.getOutputName());
ic.setOrdinalPosition(j + 1);
ic.setIsDescending(indexColumn.getSortOrder() == SortOrder.DESC);
ic.setTableCatalog(table.getCatalog().getOutputName());
ic.setTableSchema(table.getSchema().getOutputName());
ic.setTableName(table.getOutputName());
is.getIndexColumnUsages().add(ic);
}
}
for (UniqueKeyDefinition u : db.getKeys(s)) {
String constraintName = u.getOutputName();
TableDefinition table = u.getTable();
List<ColumnDefinition> columns = u.getKeyColumns();
TableConstraint tc = new TableConstraint();
tc.setConstraintCatalog(catalogName);
tc.setConstraintSchema(schemaName);
tc.setConstraintName(constraintName);
tc.setConstraintType(u.isPrimaryKey() ? PRIMARY_KEY : UNIQUE);
if (generateCommentsOnKeys())
tc.setComment(u.getComment());
tc.setTableCatalog(table.getCatalog().getOutputName());
tc.setTableSchema(table.getSchema().getOutputName());
tc.setTableName(table.getOutputName());
tc.setEnforced(u.enforced());
is.getTableConstraints().add(tc);
for (int i = 0; i < columns.size(); i++) {
ColumnDefinition column = columns.get(i);
KeyColumnUsage kc = new KeyColumnUsage();
kc.setConstraintCatalog(catalogName);
kc.setConstraintSchema(schemaName);
kc.setConstraintName(constraintName);
kc.setColumnName(column.getOutputName());
kc.setOrdinalPosition(i + 1);
kc.setTableCatalog(table.getCatalog().getOutputName());
kc.setTableSchema(table.getSchema().getOutputName());
kc.setTableName(table.getOutputName());
is.getKeyColumnUsages().add(kc);
}
}
for (ForeignKeyDefinition f : db.getForeignKeys(s)) {
String constraintName = f.getOutputName();
UniqueKeyDefinition referenced = f.getReferencedKey();
TableDefinition table = f.getKeyTable();
List<ColumnDefinition> columns = f.getKeyColumns();
TableConstraint tc = new TableConstraint();
tc.setConstraintCatalog(catalogName);
tc.setConstraintSchema(schemaName);
tc.setConstraintName(constraintName);
tc.setConstraintType(FOREIGN_KEY);
if (generateCommentsOnKeys())
tc.setComment(f.getComment());
tc.setTableCatalog(table.getCatalog().getOutputName());
tc.setTableSchema(table.getSchema().getOutputName());
tc.setTableName(table.getOutputName());
tc.setEnforced(f.enforced());
ReferentialConstraint rc = new ReferentialConstraint();
rc.setConstraintCatalog(catalogName);
rc.setConstraintSchema(schemaName);
rc.setConstraintName(constraintName);
rc.setUniqueConstraintCatalog(referenced.getCatalog().getOutputName());
rc.setUniqueConstraintSchema(referenced.getSchema().getOutputName());
rc.setUniqueConstraintName(referenced.getOutputName());
is.getTableConstraints().add(tc);
is.getReferentialConstraints().add(rc);
for (int i = 0; i < columns.size(); i++) {
ColumnDefinition column = columns.get(i);
KeyColumnUsage kc = new KeyColumnUsage();
kc.setConstraintCatalog(catalogName);
kc.setConstraintSchema(schemaName);
kc.setConstraintName(constraintName);
kc.setColumnName(column.getOutputName());
kc.setOrdinalPosition(i + 1);
kc.setTableCatalog(table.getCatalog().getOutputName());
kc.setTableSchema(table.getSchema().getOutputName());
kc.setTableName(table.getOutputName());
is.getKeyColumnUsages().add(kc);
}
}
for (CheckConstraintDefinition ch : db.getCheckConstraints(s)) {
String constraintName = ch.getOutputName();
TableDefinition table = ch.getTable();
TableConstraint tc = new TableConstraint();
tc.setConstraintCatalog(catalogName);
tc.setConstraintSchema(schemaName);
tc.setConstraintName(constraintName);
tc.setConstraintType(CHECK);
if (generateCommentsOnKeys())
tc.setComment(ch.getComment());
tc.setTableCatalog(table.getCatalog().getOutputName());
tc.setTableSchema(table.getSchema().getOutputName());
tc.setTableName(table.getOutputName());
tc.setEnforced(ch.enforced());
is.getTableConstraints().add(tc);
CheckConstraint cc = new CheckConstraint();
cc.setConstraintCatalog(catalogName);
cc.setConstraintSchema(schemaName);
cc.setConstraintName(constraintName);
cc.setCheckClause(ch.getCheckClause());
is.getCheckConstraints().add(cc);
}
for (SequenceDefinition se : db.getSequences(s)) {
String sequenceName = se.getOutputName();
DataTypeDefinition type = se.getType();
Sequence sequence = new Sequence();
sequence.setSequenceCatalog(catalogName);
sequence.setSequenceSchema(schemaName);
sequence.setSequenceName(sequenceName);
if (generateCommentsOnSequences())
sequence.setComment(se.getComment());
sequence.setCharacterMaximumLength(type.getLength());
sequence.setDataType(type.getType());
sequence.setNumericPrecision(type.getPrecision());
sequence.setNumericScale(type.getScale());
sequence.setStartValue(Convert.convert(se.getStartWith(), BigInteger.class));
sequence.setIncrement(Convert.convert(se.getIncrementBy(), BigInteger.class));
sequence.setMinimumValue(Convert.convert(se.getMinvalue(), BigInteger.class));
sequence.setMaximumValue(Convert.convert(se.getMaxvalue(), BigInteger.class));
sequence.setCycleOption(se.getCycle());
sequence.setCache(Convert.convert(se.getCache(), BigInteger.class));
is.getSequences().add(sequence);
}
for (PackageDefinition pkg : db.getPackages(s)) for (RoutineDefinition r : pkg.getRoutines()) exportRoutine(is, r, catalogName, schemaName);
for (RoutineDefinition r : db.getRoutines(s)) exportRoutine(is, r, catalogName, schemaName);
}
}
StringWriter writer = new StringWriter();
MiniJAXB.marshal(is, writer);
out.print(writer.toString());
out.close();
}
Aggregations