Search in sources :

Example 16 with Table

use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Schema.Table in project jaxdb by jaxdb.

the class JSqlTest method loadEntitiesJaxSB.

@SuppressWarnings("unchecked")
static int loadEntitiesJaxSB(final Connection connection, final String name) throws ClassNotFoundException, IOException, SAXException, SQLException, TransformerException {
    Database.threadLocal((Class<? extends Schema>) Class.forName(Entities.class.getPackage().getName() + "." + name)).connectPrepared(() -> connection);
    final URL sqlx = ClassLoader.getSystemClassLoader().getResource("jaxdb/" + name + ".sqlx");
    assertNotNull(sqlx);
    final $Database database = ($Database) Bindings.parse(sqlx);
    final DDLx ddlx = new DDLx(ClassLoader.getSystemClassLoader().getResource(name + ".ddlx"));
    Schemas.truncate(connection, ddlx.getMergedSchema().getTable());
    final Batch batch = new Batch();
    final int expectedCount = DBVendor.valueOf(connection.getMetaData()) == DBVendor.ORACLE ? 0 : 1;
    for (final data.Table<?> table : Entities.toEntities(database)) batch.addStatement(INSERT(table), (s, e, c) -> assertEquals(expectedCount, c));
    return batch.execute();
}
Also used : Bindings(org.jaxsb.runtime.Bindings) Connection(java.sql.Connection) DDLx(org.jaxdb.ddlx.DDLx) InMemoryCompiler(org.libj.jci.InMemoryCompiler) TransformerException(javax.xml.transform.TransformerException) Files(java.nio.file.Files) URL(java.net.URL) org.jaxdb.www.sqlx_0_5.xLygluGCXAA.$Database(org.jaxdb.www.sqlx_0_5.xLygluGCXAA.$Database) IOException(java.io.IOException) File(java.io.File) SQLException(java.sql.SQLException) GeneratorExecutionException(org.jaxdb.ddlx.GeneratorExecutionException) DML(org.jaxdb.jsql.DML) Schemas(org.jaxdb.ddlx.Schemas) Generator(org.jaxdb.jsql.generator.Generator) SAXException(org.xml.sax.SAXException) DBVendor(org.jaxdb.vendor.DBVendor) Throwing(org.libj.util.function.Throwing) CompilationException(org.libj.jci.CompilationException) Assert(org.junit.Assert) Path(java.nio.file.Path) Assertions.assertNotNull(org.libj.lang.Assertions.assertNotNull) DDLx(org.jaxdb.ddlx.DDLx) org.jaxdb.www.sqlx_0_5.xLygluGCXAA.$Database(org.jaxdb.www.sqlx_0_5.xLygluGCXAA.$Database) URL(java.net.URL)

Example 17 with Table

use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Schema.Table in project jaxdb by jaxdb.

the class Compiler method createColumn.

private CreateStatement createColumn(final LinkedHashSet<CreateStatement> alterStatements, final $Table table, final $Column column, final Map<String, Map<String, String>> tableNameToEnumToOwner) {
    final StringBuilder builder = new StringBuilder();
    builder.append(q(column.getName$().text())).append(' ');
    // FIXME: Passing null to compile*() methods will throw a NPE
    if (column instanceof $Char) {
        final $Char type = ($Char) column;
        builder.append(getDialect().compileChar(type.getVarying$() != null && type.getVarying$().text(), type.getLength$() == null ? null : type.getLength$().text()));
    } else if (column instanceof $Binary) {
        final $Binary type = ($Binary) column;
        builder.append(getDialect().compileBinary(type.getVarying$() != null && type.getVarying$().text(), type.getLength$() == null ? null : type.getLength$().text()));
    } else if (column instanceof $Blob) {
        final $Blob type = ($Blob) column;
        builder.append(getDialect().compileBlob(type.getLength$() == null ? null : type.getLength$().text()));
    } else if (column instanceof $Clob) {
        final $Clob type = ($Clob) column;
        builder.append(getDialect().compileClob(type.getLength$() == null ? null : type.getLength$().text()));
    } else if (column instanceof $Integer) {
        builder.append(createIntegerColumn(($Integer) column));
    } else if (column instanceof $Float) {
        final $Float type = ($Float) column;
        builder.append(getDialect().declareFloat(type.getMin$() == null ? null : type.getMin$().text()));
    } else if (column instanceof $Double) {
        final $Double type = ($Double) column;
        builder.append(getDialect().declareDouble(type.getMin$() == null ? null : type.getMin$().text()));
    } else if (column instanceof $Decimal) {
        final $Decimal type = ($Decimal) column;
        builder.append(getDialect().declareDecimal(type.getPrecision$() == null ? null : type.getPrecision$().text(), type.getScale$() == null ? null : type.getScale$().text(), type.getMin$() == null ? null : type.getMin$().text()));
    } else if (column instanceof $Date) {
        builder.append(getDialect().declareDate());
    } else if (column instanceof $Time) {
        final $Time type = ($Time) column;
        builder.append(getDialect().declareTime(type.getPrecision$() == null ? null : type.getPrecision$().text()));
    } else if (column instanceof $Datetime) {
        final $Datetime type = ($Datetime) column;
        builder.append(getDialect().declareDateTime(type.getPrecision$() == null ? null : type.getPrecision$().text()));
    } else if (column instanceof $Boolean) {
        builder.append(getDialect().declareBoolean());
    } else if (column instanceof $Enum) {
        builder.append(getDialect().declareEnum(($Enum) column, tableNameToEnumToOwner));
    }
    final String autoIncrementFragment = column instanceof $Integer ? $autoIncrement(alterStatements, table, ($Integer) column) : null;
    if (autoIncrementFragment == null || autoIncrementFragment.length() == 0) {
        final String defaultFragment = $default(column);
        if (defaultFragment != null && defaultFragment.length() > 0)
            builder.append(" DEFAULT ").append(defaultFragment);
    }
    final String nullFragment = $null(table, column);
    if (nullFragment != null && nullFragment.length() > 0)
        builder.append(' ').append(nullFragment);
    if (autoIncrementFragment != null && autoIncrementFragment.length() > 0)
        builder.append(' ').append(autoIncrementFragment);
    return new CreateStatement(builder.toString());
}
Also used : org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Char(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Char) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Integer(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Integer) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Decimal(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Decimal) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Float(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Float) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Double(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Double) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Date(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Date) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Time(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Time) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Enum(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Enum) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Boolean(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Boolean) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Blob(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Blob) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Clob(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Clob) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Datetime(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Datetime) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Binary(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Binary)

Example 18 with Table

use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Schema.Table in project jaxdb by jaxdb.

the class Compiler method createColumns.

private String createColumns(final LinkedHashSet<CreateStatement> alterStatements, final $Table table, final Map<String, Map<String, String>> tableNameToEnumToOwner) {
    final StringBuilder builder = new StringBuilder();
    final Iterator<$Column> iterator = table.getColumn().iterator();
    $Column column = null;
    for (int i = 0; iterator.hasNext(); ++i) {
        if (i > 0) {
            builder.append(',');
            if (column != null && column.getDocumentation() != null)
                builder.append(" -- ").append(column.getDocumentation().text().replace('\n', ' '));
            builder.append('\n');
        }
        builder.append("  ").append(createColumn(alterStatements, table, column = iterator.next(), tableNameToEnumToOwner));
    }
    return builder.toString();
}
Also used : org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column) 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)

Example 19 with Table

use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Schema.Table in project jaxdb by jaxdb.

the class Compiler method blockPrimaryKey.

String blockPrimaryKey(final $Table table, final $Constraints constraints, final Map<String, ColumnRef> columnNameToColumn) throws GeneratorExecutionException {
    if (constraints.getPrimaryKey() == null)
        return "";
    final StringBuilder builder = new StringBuilder();
    final List<$Named> columns = constraints.getPrimaryKey().getColumn();
    final PrimaryKey.Using$ using = constraints.getPrimaryKey().getUsing$();
    final int[] columnIndexes = new int[columns.size()];
    final Iterator<$Named> iterator = columns.iterator();
    for (int i = 0; iterator.hasNext(); ++i) {
        final $Named primaryColumn = iterator.next();
        final String primaryKeyColumn = primaryColumn.getName$().text();
        final ColumnRef ref = columnNameToColumn.get(primaryKeyColumn);
        if (ref == null)
            throw new GeneratorExecutionException("PRIMARY KEY column " + table.getName$().text() + "." + primaryKeyColumn + " is not defined");
        if (ref.column.getNull$() == null || ref.column.getNull$().text())
            throw new GeneratorExecutionException("Column " + ref.column.getName$() + " must be NOT NULL to be a PRIMARY KEY");
        if (i > 0)
            builder.append(", ");
        builder.append(q(primaryKeyColumn));
        columnIndexes[i] = ref.index;
    }
    return ",\n  " + primaryKey(table, columnIndexes, using) + " (" + builder + ")";
}
Also used : org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Named(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Named) PrimaryKey(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Constraints.PrimaryKey) ColumnRef(org.jaxdb.ddlx.Generator.ColumnRef) 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)

Example 20 with Table

use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Schema.Table in project jaxdb by jaxdb.

the class DDLx method consolidateEnums.

private static void consolidateEnums(final Schema schema) {
    final Map<String, String> enumToValues = new HashMap<>();
    final List<$Column> templates = schema.getTemplate();
    if (templates != null) {
        for (final $Column template : templates) {
            if (!(template instanceof $Enum))
                throw new IllegalStateException("Input schema is not normalized");
            enumToValues.put(template.getName$().text(), (($Enum) template).getValues$().text());
        }
    }
    final Map<String, $Table> tableNameToTable = new HashMap<>();
    for (final $Table table : schema.getTable()) {
        tableNameToTable.put(table.getName$().text(), table);
        if (table.getColumn() != null) {
            for (final $Column column : table.getColumn()) {
                if (column instanceof $Enum && column.getTemplate$() != null) {
                    final $Enum type = ($Enum) column;
                    final String values = enumToValues.get(column.getTemplate$().text());
                    type.setValues$(new $Enum.Values$(Objects.requireNonNull(values)));
                }
            }
        }
    }
}
Also used : org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table) HashMap(java.util.HashMap) 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