use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Integer in project jaxdb by jaxdb.
the class SqlJaxSBLoader method loadRow.
@SuppressWarnings("unchecked")
private static void loadRow(final ArrayList<Row> rows, final $Row row, final Dialect dialect, final Compiler compiler, final TableToColumnToIncrement tableToColumnToIncrement) {
try {
final int i = row.id().lastIndexOf('-');
final String tableName = row.id().substring(0, i);
final int weight = Integer.parseInt(row.id().substring(i + 1));
final StringBuilder columns = new StringBuilder();
final StringBuilder values = new StringBuilder();
boolean hasValues = false;
final Method[] methods = Classes.getDeclaredMethodsDeep(row.getClass());
for (final Method method : methods) {
if (!method.getName().startsWith("get") || !Attribute.class.isAssignableFrom(method.getReturnType()))
continue;
final Id id = method.getReturnType().getAnnotation(Id.class);
if (id == null)
continue;
final String idValue = id.value();
final int d1 = idValue.indexOf('-');
final int d2 = idValue.indexOf('-', d1 + 1);
final String columnName;
final String generateOnInsert;
final boolean isAutoIncremented;
if (d2 != -1) {
columnName = idValue.substring(d1 + 1, d2);
generateOnInsert = idValue.substring(d2 + 1);
isAutoIncremented = "AUTO_INCREMENT".equals(generateOnInsert);
} else {
columnName = idValue.substring(d1 + 1);
generateOnInsert = null;
isAutoIncremented = false;
}
final $AnySimpleType<?> attribute = ($AnySimpleType<?>) method.invoke(row);
String value = getValue(compiler, attribute);
if (value == null) {
if (generateOnInsert == null || isAutoIncremented)
continue;
value = generateValue(dialect, compiler, (Class<? extends $AnySimpleType<?>>) method.getReturnType(), generateOnInsert);
} else if (isAutoIncremented) {
final Map<String, Integer> columnToIncrement = tableToColumnToIncrement.get(tableName);
final Integer increment = columnToIncrement.get(columnName);
final Integer intValue = Integer.valueOf(value);
if (increment == null || increment < intValue)
columnToIncrement.put(columnName, intValue);
}
if (hasValues) {
columns.append(", ");
values.append(", ");
}
columns.append(dialect.quoteIdentifier(columnName));
values.append(value);
hasValues = true;
}
if (columns.length() == 0)
throw new IllegalStateException();
rows.add(new Row(weight, compiler.insert(tableName, columns, values)));
} catch (final IllegalAccessException e) {
throw new RuntimeException(e);
} catch (final InvocationTargetException e) {
if (e.getCause() instanceof RuntimeException)
throw (RuntimeException) e.getCause();
throw new RuntimeException(e.getCause());
}
}
use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Integer in project jaxdb by jaxdb.
the class SqlJaxSBLoader method INSERT.
int[] INSERT(final RowIterator iterator) throws IOException, SQLException {
final DBVendor vendor = DBVendor.valueOf(connection.getMetaData());
if (!iterator.hasNext())
return new int[0];
final int[] counts;
final Compiler compiler = Compiler.getCompiler(vendor);
final ArrayList<Row> rows = new ArrayList<>();
final TableToColumnToIncrement tableToColumnToIncrement = new TableToColumnToIncrement();
while (iterator.hasNext()) {
loadRow(rows, iterator.next(), getDialect(), compiler, tableToColumnToIncrement);
}
rows.sort(null);
try (final Statement statement = connection.createStatement()) {
for (final Row row : rows) statement.addBatch(row.toString());
counts = statement.executeBatch();
}
if (tableToColumnToIncrement.size() > 0)
for (final Map.Entry<String, Map<String, Integer>> entry : tableToColumnToIncrement.entrySet()) for (final Map.Entry<String, Integer> columnToIncrement : entry.getValue().entrySet()) compiler.sequenceReset(connection, null, entry.getKey(), columnToIncrement.getKey(), columnToIncrement.getValue() + 1);
return counts;
}
use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Integer 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.$Integer in project jaxdb by jaxdb.
the class Decompiler method createDDL.
public static Schema createDDL(final Connection connection) throws SQLException {
final DBVendor vendor = DBVendor.valueOf(connection.getMetaData());
final Decompiler decompiler = Decompiler.getDecompiler(vendor);
final DatabaseMetaData metaData = connection.getMetaData();
try (final ResultSet tableRows = metaData.getTables(null, null, null, new String[] { "TABLE" })) {
final Schema schema = new Schema();
final Map<String, List<$CheckReference>> tableNameToChecks = decompiler.getCheckConstraints(connection);
final Map<String, List<$Table.Constraints.Unique>> tableNameToUniques = decompiler.getUniqueConstraints(connection);
final Map<String, $Table.Indexes> tableNameToIndexes = decompiler.getIndexes(connection);
final Map<String, Map<String, $ForeignKeyUnary>> tableNameToForeignKeys = decompiler.getForeignKeys(connection);
final Map<String, $Column> columnNameToColumn = new HashMap<>();
final Map<Integer, $Column> columnNumberToColumn = new TreeMap<>();
final Map<String, TreeMap<Short, String>> indexNameToIndex = new HashMap<>();
final Map<String, String> indexNameToType = new HashMap<>();
final Map<String, Boolean> indexNameToUnique = new HashMap<>();
while (tableRows.next()) {
final String tableName = tableRows.getString(3);
final $Table table = new Schema.Table();
table.setName$(new $Named.Name$(tableName.toLowerCase()));
schema.addTable(table);
try (final ResultSet columnRows = metaData.getColumns(null, null, tableName, null)) {
while (columnRows.next()) {
final String columnName = columnRows.getString("COLUMN_NAME").toLowerCase();
final String typeName = columnRows.getString("TYPE_NAME");
final int columnSize = columnRows.getInt("COLUMN_SIZE");
final String _default = columnRows.getString("COLUMN_DEF");
final int index = columnRows.getInt("ORDINAL_POSITION");
final String nullable = columnRows.getString("IS_NULLABLE");
final String autoIncrement = columnRows.getString("IS_AUTOINCREMENT");
final int decimalDigits = columnRows.getInt("DECIMAL_DIGITS");
final $Column column = decompiler.makeColumn(columnName.toLowerCase(), typeName, columnSize, decimalDigits, _default, nullable.length() == 0 ? null : "YES".equals(nullable), autoIncrement.length() == 0 ? null : "YES".equals(autoIncrement));
columnNameToColumn.put(columnName, column);
columnNumberToColumn.put(index, column);
}
columnNumberToColumn.values().forEach(table::addColumn);
try (final ResultSet primaryKeyRows = metaData.getPrimaryKeys(null, null, tableName)) {
while (primaryKeyRows.next()) {
final String columnName = primaryKeyRows.getString("COLUMN_NAME").toLowerCase();
if (table.getConstraints() == null)
table.setConstraints(new $Table.Constraints());
if (table.getConstraints().getPrimaryKey() == null)
table.getConstraints().setPrimaryKey(new $Table.Constraints.PrimaryKey());
final $Table.Constraints.PrimaryKey.Column column = new $Table.Constraints.PrimaryKey.Column();
column.setName$(new $Table.Constraints.PrimaryKey.Column.Name$(columnName));
table.getConstraints().getPrimaryKey().addColumn(column);
}
}
final List<$Table.Constraints.Unique> uniques = tableNameToUniques == null ? null : tableNameToUniques.get(tableName);
if (uniques != null && uniques.size() > 0) {
if (table.getConstraints() == null)
table.setConstraints(new $Table.Constraints());
for (final $Table.Constraints.Unique unique : uniques) table.getConstraints().addUnique(unique);
}
try (final ResultSet indexRows = metaData.getIndexInfo(null, null, tableName, false, true)) {
while (indexRows.next()) {
final String columnName = indexRows.getString("COLUMN_NAME").toLowerCase();
if (columnName == null)
continue;
final String indexName = indexRows.getString("INDEX_NAME").toLowerCase();
TreeMap<Short, String> indexes = indexNameToIndex.get(indexName);
if (indexes == null)
indexNameToIndex.put(indexName, indexes = new TreeMap<>());
final short ordinalPosition = indexRows.getShort("ORDINAL_POSITION");
indexes.put(ordinalPosition, columnName);
final String type = getType(indexRows.getShort("TYPE"));
final String currentType = indexNameToType.get(indexName);
if (currentType == null)
indexNameToType.put(indexName, type);
else if (!type.equals(currentType))
throw new IllegalStateException("Expected " + type + " = " + currentType);
final boolean unique = !indexRows.getBoolean("NON_UNIQUE");
final Boolean currentUnique = indexNameToUnique.get(indexName);
if (currentUnique == null)
indexNameToUnique.put(indexName, unique);
else if (unique != currentUnique)
throw new IllegalStateException("Expected " + unique + " = " + currentType);
}
}
final $Table.Indexes indexes = tableNameToIndexes == null ? null : tableNameToIndexes.get(tableName);
if (indexes != null)
table.setIndexes(indexes);
final List<$CheckReference> checks = tableNameToChecks == null ? null : tableNameToChecks.get(tableName);
if (checks != null)
for (final $CheckReference check : checks) addCheck(columnNameToColumn.get(check.getColumn$().text()), check);
final Map<String, $ForeignKeyUnary> foreignKeys = tableNameToForeignKeys == null ? null : tableNameToForeignKeys.get(tableName);
if (foreignKeys != null)
for (final Map.Entry<String, $ForeignKeyUnary> entry : foreignKeys.entrySet()) columnNameToColumn.get(entry.getKey().toLowerCase()).setForeignKey(entry.getValue());
}
columnNameToColumn.clear();
columnNumberToColumn.clear();
indexNameToIndex.clear();
indexNameToType.clear();
}
return schema;
}
}
use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Integer in project jaxdb by jaxdb.
the class OracleCompiler method dropTypes.
@Override
LinkedHashSet<DropStatement> dropTypes(final $Table table, final Map<String, Map<String, String>> tableNameToEnumToOwner) {
final LinkedHashSet<DropStatement> statements = super.dropTypes(table, tableNameToEnumToOwner);
if (table.getColumn() != null) {
for (final $Column column : table.getColumn()) {
if (column instanceof $Integer) {
final $Integer type = ($Integer) column;
if (Generator.isAuto(type)) {
statements.add(new DropStatement("BEGIN EXECUTE IMMEDIATE 'DROP SEQUENCE " + q(getSequenceName(table, type)) + "'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -2289 THEN RAISE; END IF; END;"));
statements.add(new DropStatement("BEGIN EXECUTE IMMEDIATE 'DROP TRIGGER " + q(getTriggerName(table, type)) + "'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -4080 THEN RAISE; END IF; END;"));
}
}
}
}
return statements;
}
Aggregations