use of org.jooq.Sequence in project jOOQ by jOOQ.
the class ParserImpl method parseRename.
private static final DDLQuery parseRename(ParserContext ctx) {
parseKeyword(ctx, "RENAME");
parseWhitespaceIf(ctx);
switch(ctx.character()) {
case 'c':
case 'C':
if (parseKeywordIf(ctx, "COLUMN")) {
TableField<?, ?> oldName = parseFieldName(ctx);
parseKeyword(ctx, "TO");
Field<?> newName = parseFieldName(ctx);
return ctx.dsl.alterTable(oldName.getTable().getName()).renameColumn(oldName).to(newName);
}
break;
case 'i':
case 'I':
if (parseKeywordIf(ctx, "INDEX")) {
Name oldName = parseIndexName(ctx);
parseKeyword(ctx, "TO");
Name newName = parseIndexName(ctx);
return ctx.dsl.alterIndex(oldName).renameTo(newName);
}
break;
case 's':
case 'S':
if (parseKeywordIf(ctx, "SCHEMA")) {
Schema oldName = parseSchemaName(ctx);
parseKeyword(ctx, "TO");
Schema newName = parseSchemaName(ctx);
return ctx.dsl.alterSchema(oldName).renameTo(newName);
} else if (parseKeywordIf(ctx, "SEQUENCE")) {
Sequence<?> oldName = parseSequenceName(ctx);
parseKeyword(ctx, "TO");
Sequence<?> newName = parseSequenceName(ctx);
return ctx.dsl.alterSequence(oldName).renameTo(newName);
}
break;
case 'v':
case 'V':
if (parseKeywordIf(ctx, "VIEW")) {
Table<?> oldName = parseTableName(ctx);
parseKeyword(ctx, "TO");
Table<?> newName = parseTableName(ctx);
return ctx.dsl.alterView(oldName).renameTo(newName);
}
break;
}
// If all of the above fails, we can assume we're renaming a table.
parseKeywordIf(ctx, "TABLE");
Table<?> oldName = parseTableName(ctx);
parseKeyword(ctx, "TO");
Table<?> newName = parseTableName(ctx);
return ctx.dsl.alterTable(oldName).renameTo(newName);
}
use of org.jooq.Sequence in project jOOQ by jOOQ.
the class JavaGenerator method generateSequences.
protected void generateSequences(SchemaDefinition schema) {
log.info("Generating sequences");
JavaWriter out = newJavaWriter(new File(getFile(schema).getParentFile(), "Sequences.java"));
printPackage(out, schema);
printClassJavadoc(out, "Convenience access to all sequences in " + schema.getOutputName());
printClassAnnotations(out, schema);
if (scala)
out.println("object Sequences {");
else
out.println("public class Sequences {");
for (SequenceDefinition sequence : database.getSequences(schema)) {
final String seqType = out.ref(getJavaType(sequence.getType()));
final String seqId = getStrategy().getJavaIdentifier(sequence);
final String seqName = sequence.getOutputName();
final String schemaId = out.ref(getStrategy().getFullJavaIdentifier(schema), 2);
final String typeRef = getJavaTypeReference(sequence.getDatabase(), sequence.getType());
out.tab(1).javadoc("The sequence <code>%s</code>", sequence.getQualifiedOutputName());
if (scala)
out.tab(1).println("val %s : %s[%s] = new %s[%s](\"%s\", %s, %s)", seqId, Sequence.class, seqType, SequenceImpl.class, seqType, seqName, schemaId, typeRef);
else
out.tab(1).println("public static final %s<%s> %s = new %s<%s>(\"%s\", %s, %s);", Sequence.class, seqType, seqId, SequenceImpl.class, seqType, seqName, schemaId, typeRef);
}
out.println("}");
closeJavaWriter(out);
watch.splitInfo("Sequences generated");
}
use of org.jooq.Sequence in project jOOQ by jOOQ.
the class JavaGenerator method generateSchema.
protected void generateSchema(SchemaDefinition schema, JavaWriter out) {
final String catalogId = out.ref(getStrategy().getFullJavaIdentifier(schema.getCatalog()), 2);
final String schemaName = schema.getQualifiedOutputName();
final String schemaId = getStrategy().getJavaIdentifier(schema);
final String className = getStrategy().getJavaClassName(schema);
final List<String> interfaces = out.ref(getStrategy().getJavaClassImplements(schema, Mode.DEFAULT));
printPackage(out, schema);
if (scala) {
out.println("object %s {", className);
out.tab(1).javadoc("The reference instance of <code>%s</code>", schemaName);
out.tab(1).println("val %s = new %s", schemaId, className);
out.println("}");
out.println();
}
generateSchemaClassJavadoc(schema, out);
printClassAnnotations(out, schema);
if (scala) {
out.println("class %s extends %s(\"%s\", %s)[[before= with ][separator= with ][%s]] {", className, SchemaImpl.class, schema.getOutputName(), catalogId, interfaces);
} else {
out.println("public class %s extends %s[[before= implements ][%s]] {", className, SchemaImpl.class, interfaces);
out.printSerial();
out.tab(1).javadoc("The reference instance of <code>%s</code>", schemaName);
out.tab(1).println("public static final %s %s = new %s();", className, schemaId, className);
if (generateGlobalTableReferences()) {
for (TableDefinition table : schema.getTables()) {
final String tableClassName = out.ref(getStrategy().getFullJavaClassName(table));
final String tableId = getStrategy().getJavaIdentifier(table);
final String tableFullId = getStrategy().getFullJavaIdentifier(table);
final String tableComment = !StringUtils.isBlank(table.getComment()) ? escapeEntities(table.getComment()) : "The table <code>" + table.getQualifiedOutputName() + "</code>.";
out.tab(1).javadoc(tableComment);
if (scala)
out.tab(1).println("val %s = %s", tableId, tableFullId);
else
out.tab(1).println("public final %s %s = %s;", tableClassName, tableId, tableFullId);
// globalObjectReferences
if (table.isTableValuedFunction())
printTableValuedFunction(out, table, getStrategy().getJavaIdentifier(table));
}
}
out.tab(1).javadoc(NO_FURTHER_INSTANCES_ALLOWED);
out.tab(1).println("private %s() {", className);
out.tab(2).println("super(\"%s\", null);", schema.getOutputName());
out.tab(1).println("}");
}
out.println();
if (scala) {
out.tab(1).println("override def getCatalog : %s = %s", Catalog.class, catalogId);
} else {
out.tab(1).overrideInherit();
out.tab(1).println("public %s getCatalog() {", Catalog.class);
out.tab(2).println("return %s;", catalogId);
out.tab(1).println("}");
}
// [#2255] Avoid referencing sequence literals, if they're not generated
if (generateGlobalSequenceReferences())
printReferences(out, database.getSequences(schema), Sequence.class, true);
if (generateGlobalTableReferences())
printReferences(out, database.getTables(schema), Table.class, true);
if (generateGlobalUDTReferences())
printReferences(out, database.getUDTs(schema), UDT.class, true);
generateSchemaClassFooter(schema, out);
out.println("}");
}
use of org.jooq.Sequence in project jOOQ by jOOQ.
the class InformationSchemaMetaImpl method init.
private final void init(InformationSchema meta) {
List<String> errors = new ArrayList<String>();
// -------------------------------------------------------------------------------------------------------------
for (org.jooq.util.xml.jaxb.Schema xs : meta.getSchemata()) {
InformationSchemaCatalog catalog = new InformationSchemaCatalog(xs.getCatalogName());
if (!catalogs.contains(catalog))
catalogs.add(catalog);
InformationSchemaSchema is = new InformationSchemaSchema(xs.getSchemaName(), catalog);
schemas.add(is);
schemasByName.put(name(xs.getCatalogName(), xs.getSchemaName()), is);
}
// -------------------------------------------------------------------------------------------------------------
tableLoop: for (org.jooq.util.xml.jaxb.Table xt : meta.getTables()) {
Name schemaName = name(xt.getTableCatalog(), xt.getTableSchema());
Schema schema = schemasByName.get(schemaName);
if (schema == null) {
errors.add(String.format("Schema " + schemaName + " not defined for table " + xt.getTableName()));
continue tableLoop;
}
InformationSchemaTable it = new InformationSchemaTable(xt.getTableName(), schema);
tables.add(it);
tablesByName.put(name(xt.getTableCatalog(), xt.getTableSchema(), xt.getTableName()), it);
}
// Columns
// -------------------------------------------------------------------------------------------------------------
List<Column> columns = new ArrayList<Column>(meta.getColumns());
Collections.sort(columns, new Comparator<Column>() {
@Override
public int compare(Column o1, Column o2) {
Integer p1 = o1.getOrdinalPosition();
Integer p2 = o2.getOrdinalPosition();
if (p1 == p2)
return 0;
if (p1 == null)
return -1;
if (p2 == null)
return 1;
return p1.compareTo(p2);
}
});
columnLoop: for (Column xc : columns) {
String typeName = xc.getDataType();
int length = xc.getCharacterMaximumLength() == null ? 0 : xc.getCharacterMaximumLength();
int precision = xc.getNumericPrecision() == null ? 0 : xc.getNumericPrecision();
int scale = xc.getNumericScale() == null ? 0 : xc.getNumericScale();
boolean nullable = xc.isIsNullable() == null ? true : xc.isIsNullable();
// TODO: Exception handling should be moved inside SQLDataType
Name tableName = name(xc.getTableCatalog(), xc.getTableSchema(), xc.getTableName());
InformationSchemaTable table = tablesByName.get(tableName);
if (table == null) {
errors.add(String.format("Table " + tableName + " not defined for column " + xc.getColumnName()));
continue columnLoop;
}
AbstractTable.createField(xc.getColumnName(), type(typeName, length, precision, scale, nullable), table);
}
// Constraints
// -------------------------------------------------------------------------------------------------------------
Map<Name, List<TableField<Record, ?>>> columnsByConstraint = new HashMap<Name, List<TableField<Record, ?>>>();
List<KeyColumnUsage> keyColumnUsages = new ArrayList<KeyColumnUsage>(meta.getKeyColumnUsages());
Collections.sort(keyColumnUsages, new Comparator<KeyColumnUsage>() {
@Override
public int compare(KeyColumnUsage o1, KeyColumnUsage o2) {
int p1 = o1.getOrdinalPosition();
int p2 = o2.getOrdinalPosition();
return (p1 < p2) ? -1 : ((p1 == p2) ? 0 : 1);
}
});
keyColumnLoop: for (KeyColumnUsage xc : keyColumnUsages) {
Name constraintName = name(xc.getConstraintCatalog(), xc.getConstraintSchema(), xc.getConstraintName());
List<TableField<Record, ?>> fields = columnsByConstraint.get(constraintName);
if (fields == null) {
fields = new ArrayList<TableField<Record, ?>>();
columnsByConstraint.put(constraintName, fields);
}
Name tableName = name(xc.getTableCatalog(), xc.getTableSchema(), xc.getTableName());
InformationSchemaTable table = tablesByName.get(tableName);
if (table == null) {
errors.add(String.format("Table " + tableName + " not defined for constraint " + constraintName));
continue keyColumnLoop;
}
TableField<Record, ?> field = (TableField<Record, ?>) table.field(xc.getColumnName());
if (field == null) {
errors.add(String.format("Column " + xc.getColumnName() + " not defined for table " + tableName));
continue keyColumnLoop;
}
fields.add(field);
}
tableConstraintLoop: for (TableConstraint xc : meta.getTableConstraints()) {
switch(xc.getConstraintType()) {
case PRIMARY_KEY:
case UNIQUE:
{
Name tableName = name(xc.getTableCatalog(), xc.getTableSchema(), xc.getTableName());
Name constraintName = name(xc.getConstraintCatalog(), xc.getConstraintSchema(), xc.getConstraintName());
InformationSchemaTable table = tablesByName.get(tableName);
if (table == null) {
errors.add(String.format("Table " + tableName + " not defined for constraint " + constraintName));
continue tableConstraintLoop;
}
List<TableField<Record, ?>> c = columnsByConstraint.get(constraintName);
if (c == null || c.isEmpty()) {
errors.add(String.format("No columns defined for constraint " + constraintName));
continue tableConstraintLoop;
}
UniqueKeyImpl<Record> key = (UniqueKeyImpl<Record>) AbstractKeys.createUniqueKey(table, xc.getConstraintName(), c.toArray(new TableField[0]));
if (xc.getConstraintType() == PRIMARY_KEY) {
table.primaryKey = key;
primaryKeys.add(key);
}
table.uniqueKeys.add(key);
uniqueKeysByName.put(constraintName, key);
break;
}
}
}
for (ReferentialConstraint xr : meta.getReferentialConstraints()) {
referentialKeys.put(name(xr.getConstraintCatalog(), xr.getConstraintSchema(), xr.getConstraintName()), name(xr.getUniqueConstraintCatalog(), xr.getUniqueConstraintSchema(), xr.getUniqueConstraintName()));
}
tableConstraintLoop: for (TableConstraint xc : meta.getTableConstraints()) {
switch(xc.getConstraintType()) {
case FOREIGN_KEY:
{
Name tableName = name(xc.getTableCatalog(), xc.getTableSchema(), xc.getTableName());
Name constraintName = name(xc.getConstraintCatalog(), xc.getConstraintSchema(), xc.getConstraintName());
InformationSchemaTable table = tablesByName.get(tableName);
if (table == null) {
errors.add(String.format("Table " + tableName + " not defined for constraint " + constraintName));
continue tableConstraintLoop;
}
List<TableField<Record, ?>> c = columnsByConstraint.get(constraintName);
if (c == null || c.isEmpty()) {
errors.add(String.format("No columns defined for constraint " + constraintName));
continue tableConstraintLoop;
}
UniqueKeyImpl<Record> uniqueKey = uniqueKeysByName.get(referentialKeys.get(constraintName));
if (uniqueKey == null) {
errors.add(String.format("No unique key defined for foreign key " + constraintName));
continue tableConstraintLoop;
}
ForeignKey<Record, Record> key = AbstractKeys.createForeignKey(uniqueKey, table, xc.getConstraintName(), c.toArray(new TableField[0]));
table.foreignKeys.add(key);
break;
}
}
}
// -------------------------------------------------------------------------------------------------------------
sequenceLoop: for (org.jooq.util.xml.jaxb.Sequence xs : meta.getSequences()) {
Name schemaName = name(xs.getSequenceCatalog(), xs.getSequenceSchema());
Schema schema = schemasByName.get(schemaName);
if (schema == null) {
errors.add(String.format("Schema " + schemaName + " not defined for sequence " + xs.getSequenceName()));
continue sequenceLoop;
}
String typeName = xs.getDataType();
int length = xs.getCharacterMaximumLength() == null ? 0 : xs.getCharacterMaximumLength();
int precision = xs.getNumericPrecision() == null ? 0 : xs.getNumericPrecision();
int scale = xs.getNumericScale() == null ? 0 : xs.getNumericScale();
boolean nullable = true;
@SuppressWarnings({ "rawtypes", "unchecked" }) InformationSchemaSequence is = new InformationSchemaSequence(xs.getSequenceName(), schema, type(typeName, length, precision, scale, nullable));
sequences.add(is);
}
// -------------------------------------------------------------------------------------------------------------
for (Schema s : schemas) {
Catalog c = s.getCatalog();
List<Schema> list = schemasPerCatalog.get(c);
if (list == null) {
list = new ArrayList<Schema>();
schemasPerCatalog.put(c, list);
}
list.add(s);
}
for (InformationSchemaTable t : tables) {
Schema s = t.getSchema();
List<InformationSchemaTable> list = tablesPerSchema.get(s);
if (list == null) {
list = new ArrayList<InformationSchemaTable>();
tablesPerSchema.put(s, list);
}
list.add(t);
}
for (Sequence<?> q : sequences) {
Schema s = q.getSchema();
List<Sequence<?>> list = sequencesPerSchema.get(s);
if (list == null) {
list = new ArrayList<Sequence<?>>();
sequencesPerSchema.put(s, list);
}
list.add(q);
}
if (!errors.isEmpty())
throw new IllegalArgumentException(errors.toString());
}
Aggregations