use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Schema.Table in project jaxdb by jaxdb.
the class Compiler method indexes.
List<CreateStatement> indexes(final $Table table, final Map<String, ColumnRef> columnNameToColumn) {
final List<CreateStatement> statements = new ArrayList<>();
if (table.getIndexes() != null) {
for (final $Table.Indexes.Index index : table.getIndexes().getIndex()) {
final List<$Named> columns = index.getColumn();
final int[] columnIndexes = new int[columns.size()];
for (int c = 0, len = columns.size(); c < len; ++c) {
final $Named column = columns.get(c);
columnIndexes[c] = columnNameToColumn.get(column.getName$().text()).index;
}
final CreateStatement createIndex = createIndex(index.getUnique$() != null && index.getUnique$().text(), getIndexName(table, index, columnIndexes), index.getType$(), table.getName$().text(), index.getColumn().toArray(new $Named[index.getColumn().size()]));
if (createIndex != null)
statements.add(createIndex);
}
}
if (table.getColumn() != null) {
final List<$Column> columns = table.getColumn();
for (int c = 0, len = columns.size(); c < len; ++c) {
final $Column column = columns.get(c);
if (column.getIndex() != null) {
final CreateStatement createIndex = createIndex(column.getIndex().getUnique$() != null && column.getIndex().getUnique$().text(), getIndexName(table, column.getIndex(), c), column.getIndex().getType$(), table.getName$().text(), column);
if (createIndex != null)
statements.add(createIndex);
}
}
}
return statements;
}
use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Schema.Table in project jaxdb by jaxdb.
the class DDLx method topologicalSort.
private static Schema topologicalSort(final Schema schema) {
final List<$Table> tables = new ArrayList<>(schema.getTable());
schema.getTable().clear();
tables.sort(tableNameComparator);
final RefDigraph<$Table, String> digraph = new RefDigraph<>(table -> table.getName$().text().toLowerCase());
for (final $Table table : tables) {
digraph.add(table);
if (table.getColumn() != null)
for (final $Column column : table.getColumn()) if (column.getForeignKey() != null)
digraph.add(table, column.getForeignKey().getReferences$().text().toLowerCase());
if (table.getConstraints() != null && table.getConstraints().getForeignKey() != null)
for (final $ForeignKeyComposite foreignKey : table.getConstraints().getForeignKey()) digraph.add(table, foreignKey.getReferences$().text().toLowerCase());
}
if (digraph.hasCycle())
throw new IllegalStateException("Cycle exists in relational model: " + CollectionUtil.toString(digraph.getCycle(), " -> "));
final List<$Table> topologialOrder = digraph.getTopologicalOrder();
final ListIterator<$Table> topological = topologialOrder.listIterator(digraph.size());
while (topological.hasPrevious()) schema.getTable().add(topological.previous());
return schema;
}
use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Schema.Table in project jaxdb by jaxdb.
the class OracleCompiler method triggers.
@Override
List<CreateStatement> triggers(final $Table table) {
final List<CreateStatement> statements = new ArrayList<>();
if (table.getColumn() != null) {
for (final $Column column : table.getColumn()) {
if (column instanceof $Integer) {
final $Integer type = ($Integer) column;
if (Generator.isAuto(type)) {
final String sequenceName = getSequenceName(table, type);
final String columnName = q(column.getName$().text());
statements.add(0, new CreateStatement("CREATE TRIGGER " + q(getTriggerName(table, type)) + " BEFORE INSERT ON " + q(table.getName$().text()) + " FOR EACH ROW WHEN (new." + columnName + " IS NULL) BEGIN SELECT " + q(sequenceName) + ".NEXTVAL INTO " + ":new." + columnName + " FROM dual; END;"));
}
}
}
}
statements.addAll(super.triggers(table));
return statements;
}
use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Schema.Table in project jaxdb by jaxdb.
the class OracleCompiler method types.
@Override
List<CreateStatement> types(final $Table table, final Map<String, Map<String, String>> tableNameToEnumToOwner) {
final List<CreateStatement> statements = new ArrayList<>();
if (table.getColumn() != null) {
for (final $Column column : table.getColumn()) {
if (column instanceof $Integer) {
final $Integer integer = ($Integer) column;
if (Generator.isAuto(integer)) {
final StringBuilder builder = new StringBuilder();
builder.append("CREATE SEQUENCE ").append(q(getSequenceName(table, integer)));
builder.append(" INCREMENT BY 1");
final String startWith = getAttr("default", integer);
if (startWith != null)
builder.append(" START WITH ").append(startWith);
final String max = getAttr("max", integer);
final Byte precision;
builder.append(" MAXVALUE ");
builder.append(max != null ? max : (precision = getPrecision(integer)) != null ? FastMath.longE10[precision] : Long.MAX_VALUE);
String min = getAttr("min", integer);
if (min == null)
min = startWith;
if (min != null)
builder.append(" MINVALUE ").append(min);
else
builder.append(" NOMINVALUE ");
builder.append(" CYCLE NOCACHE");
statements.add(0, new CreateStatement(builder.toString()));
}
}
}
}
statements.addAll(super.types(table, tableNameToEnumToOwner));
return statements;
}
use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Schema.Table in project jaxdb by jaxdb.
the class PostgreSQLCompiler method types.
@Override
List<CreateStatement> types(final $Table table, final Map<String, Map<String, String>> tableNameToEnumToOwner) {
final List<CreateStatement> statements = new ArrayList<>();
if (table.getColumn() != null) {
StringBuilder sql = null;
for (final $Column column : table.getColumn()) {
if (column instanceof $Enum) {
final $Enum type = ($Enum) column;
if (sql == null)
sql = new StringBuilder();
else
sql.setLength(0);
sql.append("CREATE TYPE ").append(q(Dialect.getTypeName(type, tableNameToEnumToOwner))).append(" AS ENUM (");
if (type.getValues$() != null) {
final List<String> enums = Dialect.parseEnum(type.getValues$().text());
final Iterator<String> iterator = enums.iterator();
for (int i = 0; iterator.hasNext(); ++i) {
if (i > 0)
sql.append(", ");
sql.append('\'').append(iterator.next()).append('\'');
}
}
statements.add(0, new CreateStatement(sql.append(')').toString()));
} else if (column instanceof $Integer) {
final $Integer integer = ($Integer) column;
if (Generator.isAuto(integer)) {
final StringBuilder builder = new StringBuilder("CREATE SEQUENCE " + q(getSequenceName(table, integer)));
final String min = getAttr("min", integer);
if (min != null)
builder.append(" MINVALUE ").append(min);
final String max = getAttr("max", integer);
if (max != null)
builder.append(" MAXVALUE ").append(max);
final String _default = getAttr("default", integer);
if (_default != null)
builder.append(" START ").append(_default);
builder.append(" CYCLE");
statements.add(0, new CreateStatement(builder.toString()));
}
}
}
}
statements.addAll(super.types(table, tableNameToEnumToOwner));
return statements;
}
Aggregations