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();
}
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());
}
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();
}
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 + ")";
}
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)));
}
}
}
}
}
Aggregations