use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Int 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.$Int 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.$Int in project jaxdb by jaxdb.
the class DerbyDecompiler method getUniqueConstraints.
@Override
@SuppressWarnings("null")
Map<String, List<$Table.Constraints.Unique>> getUniqueConstraints(final Connection connection) throws SQLException {
final Map<String, List<String>> tableNameToColumns = getTables(connection);
final PreparedStatement statement = connection.prepareStatement(constraintsSql);
final ResultSet rows = statement.executeQuery();
final Map<String, List<$Table.Constraints.Unique>> tableNameToUniques = new HashMap<>();
String lastTable = null;
List<$Table.Constraints.Unique> uniques = null;
while (rows.next()) {
final String tableName = rows.getString(2);
if (!tableName.equals(lastTable)) {
lastTable = tableName;
tableNameToUniques.put(tableName, uniques = new ArrayList<>());
}
final List<String> columns = tableNameToColumns.get(tableName);
final String descriptor = rows.getString(3);
final int close = descriptor.lastIndexOf(')');
final int open = descriptor.lastIndexOf('(', close - 1);
final String[] colRefs = descriptor.substring(open + 1, close).split(",");
final $Table.Constraints.Unique unique = new $Table.Constraints.Unique();
uniques.add(unique);
for (int i = 0; i < colRefs.length; ++i) {
colRefs[i] = columns.get(Integer.valueOf(colRefs[i].trim()) - 1);
final $Table.Constraints.Unique.Column column = new $Table.Constraints.Unique.Column();
column.setName$(new $Table.Constraints.Unique.Column.Name$(colRefs[i].toLowerCase()));
unique.addColumn(column);
}
}
return tableNameToUniques;
}
use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Int in project jaxdb by jaxdb.
the class Generator method registerColumns.
private static void registerColumns(final $Table table, final Set<? super String> tableNames, final Map<? super String, ColumnRef> columnNameToColumn) throws GeneratorExecutionException {
final String tableName = table.getName$().text();
final List<String> violations = new ArrayList<>();
String nameViolation = checkNameViolation(tableName);
if (nameViolation != null)
violations.add(nameViolation);
if (tableNames.contains(tableName))
throw new GeneratorExecutionException("Circular table dependency detected: " + tableName);
tableNames.add(tableName);
final List<$Column> columns = table.getColumn();
if (columns != null) {
for (int c = 0, len = columns.size(); c < len; ++c) {
final $Column column = columns.get(c);
final String columnName = column.getName$().text();
nameViolation = checkNameViolation(columnName);
if (nameViolation != null)
violations.add(nameViolation);
final ColumnRef existing = columnNameToColumn.get(columnName);
if (existing != null)
throw new GeneratorExecutionException("Duplicate column definition: " + tableName + "." + columnName);
columnNameToColumn.put(columnName, new ColumnRef(column, c));
}
}
if (violations.size() > 0)
violations.forEach(logger::warn);
}
use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Int in project jaxdb by jaxdb.
the class SQLiteDecompiler 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 (typeName.startsWith("BIGINT")) {
final $Bigint type = newColumn($Bigint.class);
if (size != 2000000000)
type.setPrecision$(new $Bigint.Precision$((byte) size));
if (_default != null)
type.setDefault$(new $Bigint.Default$(Long.valueOf(_default)));
if (autoIncrement != null && autoIncrement)
type.setGenerateOnInsert$(new $Bigint.GenerateOnInsert$($Integer.GenerateOnInsert$.AUTO_5FINCREMENT));
column = type;
} else if (typeName.startsWith("BINARY")) {
final $Binary type = newColumn($Binary.class);
final Long length = getLength(typeName);
if (length != null)
type.setLength$(new $Binary.Length$(length));
column = type;
} else if (typeName.startsWith("BLOB")) {
final $Blob type = newColumn($Blob.class);
final Long length = getLength(typeName);
if (length != null)
type.setLength$(new $Blob.Length$(length));
column = type;
} else if ("BOOLEAN".equals(typeName)) {
final $Boolean type = newColumn($Boolean.class);
if (_default != null)
type.setDefault$(new $Boolean.Default$(Boolean.parseBoolean(_default)));
column = type;
} else if (typeName.startsWith("VARCHAR") || typeName.startsWith("CHARACTER")) {
final $Char type = newColumn($Char.class);
if (typeName.startsWith("VARCHAR"))
type.setVarying$(new $Char.Varying$(true));
final Long length = getLength(typeName);
if (length != null)
type.setLength$(new $Char.Length$(length));
if (_default != null)
type.setDefault$(new $Char.Default$(_default.substring(1, _default.length() - 1)));
column = type;
} else if (typeName.startsWith("TEXT")) {
final $Clob type = newColumn($Clob.class);
final Long length = getLength(typeName);
if (length != null)
type.setLength$(new $Clob.Length$(length));
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 ("DATETIME".equals(typeName)) {
final $Datetime type = newColumn($Datetime.class);
if (size != 2000000000)
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 (typeName.startsWith("DECIMAL")) {
final $Decimal type = newColumn($Decimal.class);
if (!"DECIMAL(15,0)".equals(typeName)) {
final int open = typeName.indexOf('(');
if (open > 0) {
final int comma = typeName.indexOf(',', open + 1);
if (comma > open) {
final int close = typeName.indexOf(')', comma + 1);
if (close > comma) {
type.setPrecision$(new $Decimal.Precision$(Integer.valueOf(typeName.substring(open + 1, comma).trim())));
type.setScale$(new $Decimal.Scale$(Integer.valueOf(typeName.substring(comma + 1, close).trim())));
}
}
}
}
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 (typeName.startsWith("INT") || typeName.startsWith("MEDIUMINT")) {
final $Int type = newColumn($Int.class);
if (size != 2000000000)
type.setPrecision$(new $Int.Precision$((byte) size));
if (_default != null)
type.setDefault$(new $Int.Default$(Integer.valueOf(_default)));
if ("INTEGER".equals(typeName))
type.setGenerateOnInsert$(new $Int.GenerateOnInsert$($Integer.GenerateOnInsert$.AUTO_5FINCREMENT));
column = type;
} else if ("SMALLINT".equals(typeName)) {
final $Smallint type = newColumn($Smallint.class);
if (size != 2000000000)
type.setPrecision$(new $Smallint.Precision$((byte) size));
if (_default != null)
type.setDefault$(new $Smallint.Default$(Short.valueOf(_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);
if (size != 2000000000)
type.setPrecision$(new $Time.Precision$((byte) size));
if (_default != null)
type.setDefault$(new $Time.Default$(_default.substring(1, _default.length() - 1)));
column = type;
} else if ("TINYINT".equals(typeName)) {
final $Tinyint type = newColumn($Tinyint.class);
if (size != 2000000000)
type.setPrecision$(new $Tinyint.Precision$((byte) size));
if (_default != null)
type.setDefault$(new $Tinyint.Default$(Byte.valueOf(_default)));
if (autoIncrement != null && autoIncrement)
type.setGenerateOnInsert$(new $Tinyint.GenerateOnInsert$($Integer.GenerateOnInsert$.AUTO_5FINCREMENT));
column = type;
} else {
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