use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Enum in project jaxdb by jaxdb.
the class Generator method declareEnumClass.
private static String declareEnumClass(final String containerClassName, final $Enum column, final int spaces) {
final String classSimpleName = Identifiers.toClassCase(column.getName$().text());
final String className = containerClassName + "." + classSimpleName;
final List<String> names = Dialect.parseEnum(column.getValues$().text());
final StringBuilder out = new StringBuilder();
final String s = Strings.repeat(' ', spaces);
out.append('\n').append(s).append('@').append(EntityEnum.Type.class.getCanonicalName()).append("(\"").append(Dialect.getTypeName(column, null)).append("\")");
out.append('\n').append(s).append("public static final class ").append(classSimpleName).append(" implements ").append(EntityEnum.class.getName()).append(" {");
out.append('\n').append(s).append(" private static byte index = 0;");
out.append('\n').append(s).append(" public static final ").append(className);
for (int i = 0, len = names.size(); i < len; ++i) {
out.append(' ').append(enumStringToEnum(names.get(i))).append(',');
}
out.setCharAt(out.length() - 1, ';');
out.append('\n').append(s).append(" private static final ").append(className).append("[] values = {");
for (int i = 0, len = names.size(); i < len; ++i) {
final String name = names.get(i);
out.append(enumStringToEnum(name)).append(" = new ").append(className).append("(\"").append(name).append("\"), ");
}
out.setCharAt(out.length() - 2, '}');
out.setCharAt(out.length() - 1, ';');
out.append("\n\n").append(s).append(" public static ").append(className).append("[] values() {");
out.append('\n').append(s).append(" return values;");
out.append('\n').append(s).append(" }\n");
out.append('\n').append(s).append(" public static ").append(className).append(" valueOf(final ").append(String.class.getName()).append(" string) {");
out.append('\n').append(s).append(" if (string == null)");
out.append('\n').append(s).append(" return null;\n");
out.append('\n').append(s).append(" for (final ").append(className).append(" value : values())");
out.append('\n').append(s).append(" if (string.equals(value.name))");
out.append('\n').append(s).append(" return value;\n");
out.append('\n').append(s).append(" return null;");
out.append('\n').append(s).append(" }\n");
out.append('\n').append(s).append(" private final byte ordinal;");
out.append('\n').append(s).append(" private final ").append(String.class.getName()).append(" name;\n");
out.append('\n').append(s).append(" private ").append(classSimpleName).append("(final ").append(String.class.getName()).append(" name) {");
out.append('\n').append(s).append(" this.ordinal = index++;");
out.append('\n').append(s).append(" this.name = name;");
out.append('\n').append(s).append(" }\n");
out.append('\n').append(s).append(" public byte ordinal() {");
out.append('\n').append(s).append(" return ordinal;");
out.append('\n').append(s).append(" }\n");
out.append('\n').append(s).append(" @").append(Override.class.getName()).append('\n').append(s).append(" public ").append(String.class.getName()).append(" toString() {\n").append(s).append(" return name;\n").append(s).append(" }\n").append(s).append("}");
return out.toString();
}
use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Enum 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.$Enum 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)));
}
}
}
}
}
use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Enum in project jaxdb by jaxdb.
the class Generator method parse.
public LinkedHashSet<Statement> parse(final DBVendor vendor) throws GeneratorExecutionException {
final Map<String, LinkedHashSet<DropStatement>> dropTableStatements = new HashMap<>();
final Map<String, LinkedHashSet<DropStatement>> dropTypeStatements = new HashMap<>();
final Map<String, LinkedHashSet<CreateStatement>> createTableStatements = new HashMap<>();
final Schema normalized = ddlx.getNormalizedSchema();
// The following code resolves a problem with ENUM types. The DDLx is generated from merged schema, whereby the original owner
// of the ENUM type is lost. The jSQL, however, is generated from the normalized schema, where the owner of the ENUM type is
// present. The `tableNameToEnumToOwner` variable is a map for each table linking each table's ENUMs to their original owners.
final Map<String, $Table> tableNameToTable = new HashMap<>();
final Map<String, Map<String, String>> tableNameToEnumToOwner = new HashMap<String, Map<String, String>>() {
@Override
public Map<String, String> get(final Object key) {
final String tableName = (String) key;
Map<String, String> map = super.get(key);
if (map == null)
put(tableName, map = new HashMap<>());
return map;
}
};
for (final $Table table : normalized.getTable()) tableNameToTable.put(table.getName$().text(), table);
for ($Table table : normalized.getTable()) {
if (table.getAbstract$().text())
continue;
final Map<String, String> colNameToOwnerTable = tableNameToEnumToOwner.get(table.getName$().text());
do {
for (final $Column column : table.getColumn()) if (column instanceof $Enum)
colNameToOwnerTable.put(column.getName$().text(), table.getName$().text());
table = table.getExtends$() != null ? tableNameToTable.get(table.getExtends$().text()) : null;
} while (table != null);
}
final Set<String> skipTables = new HashSet<>();
final Schema merged = ddlx.getMergedSchema();
final List<$Table> tables = merged.getTable();
for (final $Table table : tables) {
if (table.getSkip$().text()) {
skipTables.add(table.getName$().text());
} else if (!table.getAbstract$().text()) {
dropTableStatements.put(table.getName$().text(), Compiler.getCompiler(vendor).dropTable(table));
dropTypeStatements.put(table.getName$().text(), Compiler.getCompiler(vendor).dropTypes(table, tableNameToEnumToOwner));
}
}
final Set<String> tableNames = new HashSet<>();
for (final $Table table : tables) if (!table.getAbstract$().text())
createTableStatements.put(table.getName$().text(), parseTable(vendor, table, tableNames, tableNameToEnumToOwner));
final LinkedHashSet<Statement> statements = new LinkedHashSet<>();
final CreateStatement createSchema = Compiler.getCompiler(vendor).createSchemaIfNotExists(merged);
if (createSchema != null)
statements.add(createSchema);
final ListIterator<$Table> listIterator = tables.listIterator(tables.size());
while (listIterator.hasPrevious()) {
final $Table table = listIterator.previous();
final String tableName = table.getName$().text();
if (!skipTables.contains(tableName) && !table.getAbstract$().text())
statements.addAll(dropTableStatements.get(tableName));
}
for (final $Table table : tables) {
final String tableName = table.getName$().text();
if (!skipTables.contains(tableName) && !table.getAbstract$().text())
statements.addAll(dropTypeStatements.get(tableName));
}
for (final $Table table : tables) {
final String tableName = table.getName$().text();
if (!skipTables.contains(tableName) && !table.getAbstract$().text())
statements.addAll(createTableStatements.get(tableName));
}
return statements;
}
Aggregations