use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKeyUnary.Column$ 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;
}
use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKeyUnary.Column$ 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;
}
use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKeyUnary.Column$ in project jaxdb by jaxdb.
the class Decompiler method getForeignKeys.
@SuppressWarnings("null")
// FIXME: This does not support composite foreign keys
Map<String, Map<String, $ForeignKeyUnary>> getForeignKeys(final Connection connection) throws SQLException {
final DatabaseMetaData metaData = connection.getMetaData();
try (final ResultSet foreignKeyRows = metaData.getImportedKeys(null, null, null)) {
final Map<String, Map<String, $ForeignKeyUnary>> tableNameToForeignKeys = new HashMap<>();
String lastTable = null;
Map<String, $ForeignKeyUnary> columnNameToForeignKey = null;
while (foreignKeyRows.next()) {
final String tableName = foreignKeyRows.getString("FKTABLE_NAME").toLowerCase();
if (!tableName.equals(lastTable)) {
lastTable = tableName;
tableNameToForeignKeys.put(tableName, columnNameToForeignKey = new HashMap<>());
}
final String primaryTable = foreignKeyRows.getString("PKTABLE_NAME").toLowerCase();
final String primaryColumn = foreignKeyRows.getString("PKCOLUMN_NAME").toLowerCase();
final String columnName = foreignKeyRows.getString("FKCOLUMN_NAME").toLowerCase();
final short updateRule = foreignKeyRows.getShort("UPDATE_RULE");
final short deleteRule = foreignKeyRows.getShort("DELETE_RULE");
final $ForeignKeyUnary foreignKey = new $Column.ForeignKey();
foreignKey.setReferences$(new References$(primaryTable));
foreignKey.setColumn$(new Column$(primaryColumn));
final $ChangeRule.Enum onUpdate = toBinding(updateRule);
if (onUpdate != null)
foreignKey.setOnUpdate$(new OnUpdate$(onUpdate));
final $ChangeRule.Enum onDelete = toBinding(deleteRule);
if (onDelete != null)
foreignKey.setOnDelete$(new OnDelete$(onDelete));
columnNameToForeignKey.put(columnName, foreignKey);
}
return tableNameToForeignKeys;
}
}
use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKeyUnary.Column$ in project jaxdb by jaxdb.
the class DerbyDecompiler method getCheckConstraints.
@Override
@SuppressWarnings("null")
Map<String, List<$CheckReference>> getCheckConstraints(final Connection connection) throws SQLException {
final PreparedStatement statement = connection.prepareStatement(checkSql);
final ResultSet rows = statement.executeQuery();
final Map<String, List<$CheckReference>> tableNameToChecks = new HashMap<>();
String lastTable = null;
List<$CheckReference> checks = null;
while (rows.next()) {
final String tableName = rows.getString(2);
if (!tableName.equals(lastTable)) {
lastTable = tableName;
tableNameToChecks.put(tableName, checks = new ArrayList<>());
}
final String checkDefinition = rows.getString(3);
final String referencedColumns = rows.getString(4);
if (referencedColumns.contains(","))
throw new UnsupportedOperationException("Only support single-column check constraints");
checks.add(makeCheck(checkDefinition));
}
return tableNameToChecks;
}
use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKeyUnary.Column$ in project jaxdb by jaxdb.
the class DerbyDecompiler method makeColumn.
@Override
$Column makeColumn(final String columnName, final String typeName, final long size, final int decimalDigits, final String _default, final Boolean nullable, final Boolean autoIncrement) {
final $Column column;
if ("BIGINT".equals(typeName)) {
final $Bigint type = newColumn($Bigint.class);
// type.setPrecision$(new $Bigint.Precision$((byte)size));
if (_default != null && !"GENERATED_BY_DEFAULT".equals(_default))
type.setDefault$(new $Bigint.Default$(Long.valueOf(getDefault(_default))));
if (autoIncrement != null && autoIncrement)
type.setGenerateOnInsert$(new $Bigint.GenerateOnInsert$($Integer.GenerateOnInsert$.AUTO_5FINCREMENT));
column = type;
} else if ("CHAR () FOR BIT DATA".equals(typeName) || "VARCHAR () FOR BIT DATA".equals(typeName)) {
final $Binary type = newColumn($Binary.class);
if (typeName.startsWith("VARCHAR"))
type.setVarying$(new $Binary.Varying$(true));
type.setLength$(new $Binary.Length$(size));
column = type;
} else if ("BLOB".equals(typeName)) {
final $Blob type = newColumn($Blob.class);
type.setLength$(new $Blob.Length$(size));
column = type;
} else if ("BOOLEAN".equals(typeName)) {
final $Boolean type = newColumn($Boolean.class);
if (_default != null)
type.setDefault$(new $Boolean.Default$(Boolean.valueOf(_default)));
column = type;
} else if ("VARCHAR".equals(typeName) || "CHAR".equals(typeName)) {
final $Char type = newColumn($Char.class);
if ("VARCHAR".equals(typeName))
type.setVarying$(new $Char.Varying$(true));
type.setLength$(new $Char.Length$(size));
if (_default != null)
type.setDefault$(new $Char.Default$(_default.substring(1, _default.length() - 1)));
column = type;
} else if ("CLOB".equals(typeName)) {
final $Clob type = newColumn($Clob.class);
type.setLength$(new $Clob.Length$(size));
column = type;
} else if ("DATE".equals(typeName)) {
final $Date type = newColumn($Date.class);
if (_default != null)
type.setDefault$(new $Date.Default$(_default.substring(1, _default.length() - 1)));
column = type;
} else if ("TIMESTAMP".equals(typeName)) {
final $Datetime type = newColumn($Datetime.class);
// type.setPrecision$(new $Datetime.Precision$((byte)size));
if (_default != null)
type.setDefault$(new $Datetime.Default$(_default.substring(1, _default.length() - 1)));
column = type;
} else if ("DECIMAL".equals(typeName)) {
final int precision = (int) size;
final $Decimal type = newColumn($Decimal.class);
type.setPrecision$(new $Decimal.Precision$(precision));
type.setScale$(new $Decimal.Scale$(decimalDigits));
if (_default != null)
type.setDefault$(new $Decimal.Default$(new BigDecimal(_default)));
column = type;
} else if ("DOUBLE".equals(typeName)) {
final $Double type = newColumn($Double.class);
if (_default != null)
type.setDefault$(new $Double.Default$(Double.valueOf(_default)));
column = type;
} else // }
if ("FLOAT".equals(typeName)) {
final $Float type = newColumn($Float.class);
if (_default != null)
type.setDefault$(new $Float.Default$(Float.valueOf(_default)));
column = type;
} else if ("INTEGER".equals(typeName)) {
final $Int type = newColumn($Int.class);
type.setPrecision$(new $Int.Precision$((byte) size));
if (_default != null && !"GENERATED_BY_DEFAULT".equals(_default))
type.setDefault$(new $Int.Default$(Integer.valueOf(getDefault(_default))));
if (autoIncrement != null && autoIncrement)
type.setGenerateOnInsert$(new $Int.GenerateOnInsert$($Integer.GenerateOnInsert$.AUTO_5FINCREMENT));
column = type;
} else if ("SMALLINT".equals(typeName)) {
final $Smallint type = newColumn($Smallint.class);
type.setPrecision$(new $Smallint.Precision$((byte) size));
if (_default != null && !"GENERATED_BY_DEFAULT".equals(_default))
type.setDefault$(new $Smallint.Default$(Short.valueOf(getDefault(_default))));
if (autoIncrement != null && autoIncrement)
type.setGenerateOnInsert$(new $Smallint.GenerateOnInsert$($Integer.GenerateOnInsert$.AUTO_5FINCREMENT));
column = type;
} else if ("TIME".equals(typeName)) {
final $Time type = newColumn($Time.class);
type.setPrecision$(new $Time.Precision$((byte) size));
if (_default != null)
type.setDefault$(new $Time.Default$(_default.substring(1, _default.length() - 1)));
column = type;
} else // else if ("TINYINT".equals(typeName)) {
// final $Tinyint type = newColumn($Tinyint.class);
// type.setPrecision$(new $Tinyint.Precision$((byte)size));
// if (_default != null && !"GENERATED_BY_DEFAULT".equals(_default))
// type.setDefault$(new $Tinyint.setDefault$(new BigInteger(getDefault(_default))));
//
// if (autoIncrement != null && autoIncrement)
// type.GenerateOnInsert$(new $Integer.GenerateOnInsert$($Integer.GenerateOnInsert$.AUTO_5FINCREMENT));
//
// column = type;
// }
{
throw new UnsupportedOperationException("Unsupported column type: " + typeName);
}
column.setName$(new $Column.Name$(columnName));
if (nullable != null && !nullable)
column.setNull$(new $Column.Null$(false));
return column;
}
Aggregations