Search in sources :

Example 6 with Table

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;
}
Also used : org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Named(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Named) ArrayList(java.util.ArrayList) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Tinyint(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Tinyint) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Bigint(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Bigint) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Smallint(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Smallint) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column)

Example 7 with Table

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;
}
Also used : org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table) RefDigraph(org.libj.util.RefDigraph) ArrayList(java.util.ArrayList) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKeyComposite(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKeyComposite) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column)

Example 8 with Table

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;
}
Also used : org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Integer(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Integer) ArrayList(java.util.ArrayList) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column)

Example 9 with Table

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;
}
Also used : org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Integer(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Integer) ArrayList(java.util.ArrayList) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column)

Example 10 with Table

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;
}
Also used : org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Integer(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Integer) ArrayList(java.util.ArrayList) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Enum(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Enum) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column)

Aggregations

org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column)17 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Bigint (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Bigint)10 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Smallint (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Smallint)10 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Tinyint (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Tinyint)10 ArrayList (java.util.ArrayList)9 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Integer (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Integer)8 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Enum (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Enum)7 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Named (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Named)7 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table)6 HashMap (java.util.HashMap)4 org.jaxdb.jsql.data (org.jaxdb.jsql.data)4 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Boolean (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Boolean)4 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Decimal (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Decimal)4 Schema (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.Schema)4 Map (java.util.Map)3 GeneratorExecutionException (org.jaxdb.ddlx.GeneratorExecutionException)3 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Char (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Char)3 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Double (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Double)3 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Float (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Float)3 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Int (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Int)3