use of org.obeonetwork.dsl.database.AbstractTable in project InformationSystem by ObeoNetwork.
the class DefaultDataBaseBuilder method buildTable.
protected void buildTable(ProgressListener progressListener, DatabaseMetaData metaData, TableContainer owner, NativeTypesLibrary nativeTypesLibrary, ResultSet rs) throws SQLException {
String tableName = rs.getString(3);
String message = "Discovering table: " + owner.getName() + "." + tableName;
progressListener.progressTo(1, message);
String tableType = rs.getString(4);
AbstractTable table = null;
if ("TABLE".equals(tableType)) {
table = CreationUtils.createTable(owner, tableName);
} else if ("VIEW".equals(tableType)) {
table = CreationUtils.createView(owner, tableName);
((View) table).setQuery(getViewQuery(metaData, tableName));
}
String tableComments = getRealComments(getTableComments(metaData, rs, owner.getName(), tableName));
if (tableComments == null || tableComments.length() == 0) {
table.setComments(null);
} else {
table.setComments(tableComments);
}
if (table instanceof Table) {
buildColumns(metaData, owner, nativeTypesLibrary, table);
Table t = (Table) table;
buildPrimaryKeys(metaData, t);
buildIndexes(metaData, t);
}
}
use of org.obeonetwork.dsl.database.AbstractTable in project InformationSystem by ObeoNetwork.
the class DefaultDataBaseBuilder method buildForeignKeys.
protected void buildForeignKeys(Table table) {
Map foreignKeys = new HashMap();
ResultSet rs = null;
try {
rs = metaData.getImportedKeys(null, schemaName, table.getName());
while (rs.next()) {
String pkSchemaName = rs.getString(2);
String pkTableName = rs.getString(3);
String fkName = rs.getString(12);
String pkName = rs.getString(13);
String foreignKeyId = tableContainer.getName() + "::" + table.getName() + "::" + fkName;
ForeignKey foreignKey = (ForeignKey) foreignKeys.get(foreignKeyId);
if (foreignKey == null) {
foreignKey = CreationUtils.createForeignKey(table);
foreignKey.setName(fkName);
foreignKeys.put(foreignKeyId, foreignKey);
}
int keySeq = rs.getInt(9);
ForeignKeyElement foreignKeyElement = CreationUtils.createForeignKeyElement(keySeq, foreignKey);
foreignKey.getElements().add(foreignKeyElement);
String pkColumnName = rs.getString(4);
Column pkColumn = queries.getColumn(tableContainer, pkTableName, pkColumnName);
if (pkColumn != null) {
foreignKeyElement.setPkColumn(pkColumn);
} else {
AbstractTable pkTable = queries.getTable(pkTableName);
if (pkTable == null) {
pkTable = CreationUtils.createTable(tableContainer, pkTableName);
pkColumn = CreationUtils.createColumn(pkTable, pkColumnName);
} else {
pkColumn = queries.getColumn(pkTable, pkColumnName);
}
foreignKeyElement.setPkColumn(pkColumn);
}
String fkSchemaName = rs.getString(6);
String fkTableName = rs.getString(7);
String fkColumnName = rs.getString(8);
Column fkColumn = queries.getColumn(tableContainer, fkTableName, fkColumnName);
foreignKeyElement.setFkColumn(fkColumn);
// Set target table
if (foreignKey.getTarget() == null) {
Table targetTable = (Table) pkColumn.getOwner();
foreignKey.setTarget(targetTable);
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
JdbcUtils.closeResultSet(rs);
}
}
use of org.obeonetwork.dsl.database.AbstractTable in project InformationSystem by ObeoNetwork.
the class OracleDataBaseBuilder method buildColumnConstraint.
@Override
protected void buildColumnConstraint(DatabaseMetaData metaData, TableContainer owner, Column column) {
Schema schema = (Schema) owner;
String key = schema.getName() + column.getOwner().getName() + column.getName();
if (cacheConstraints == null) {
cacheConstraints = new HashMap<String, OracleConstraint>();
ResultSet rs = null;
PreparedStatement pstmt = null;
try {
PreparedStatement psmt = metaData.getConnection().prepareStatement(" SELECT dc.constraint_name, dc.search_condition, dcc.table_name, dcc.column_name " + " FROM all_constraints dc, all_cons_columns dcc " + " where dc.owner=?" + " AND dcc.owner=?" + " AND constraint_type='C'" + " AND substr(dc.constraint_name,1,3) <> 'SYS'" + " AND dc.owner=dcc.owner" + " AND dc.constraint_name=dcc.constraint_name" + " ORDER BY dc.constraint_name");
psmt.setString(1, schema.getName());
psmt.setString(2, schema.getName());
rs = psmt.executeQuery();
while (rs.next()) {
String name = rs.getString(1);
// do not reference recyclebin internal name
if (name.startsWith("BIN$")) {
name = "";
}
String expression = rs.getString(2);
String tableName = rs.getString(3);
String columnName = rs.getString(4);
if (!expression.endsWith("IS NOT NULL")) {
OracleConstraint oracleConstraint = new OracleConstraint(tableName, name, expression);
cacheConstraints.put(schema.getName() + tableName + columnName, oracleConstraint);
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
JdbcUtils.closeStatement(pstmt);
JdbcUtils.closeResultSet(rs);
}
}
if (cacheConstraints.containsKey(key)) {
OracleConstraint oracleConstraint = cacheConstraints.get(key);
AbstractTable table = queries.getTable(owner, oracleConstraint.tableName);
Constraint constraint = CreationUtils.createConstraint((Table) table, oracleConstraint.name);
constraint.setExpression(oracleConstraint.expression);
}
}
use of org.obeonetwork.dsl.database.AbstractTable in project InformationSystem by ObeoNetwork.
the class OracleDataBaseBuilder method buildSequences.
private void buildSequences(DatabaseMetaData metaData, TableContainer owner) {
ResultSet rs = null;
PreparedStatement pstmt = null;
try {
PreparedStatement psmt = metaData.getConnection().prepareStatement("SELECT SEQUENCE_NAME, INCREMENT_BY, MIN_VALUE, MAX_VALUE, LAST_NUMBER, CYCLE_FLAG, CACHE_SIZE " + "FROM user_sequences");
rs = psmt.executeQuery();
while (rs.next()) {
String name = rs.getString(1);
BigInteger increment = getBigIntValueForColumn(rs, 2);
BigInteger minValue = getBigIntValueForColumn(rs, 3);
BigInteger maxValue = getBigIntValueForColumn(rs, 4);
BigInteger start = getBigIntValueForColumn(rs, 5);
String cycleAsString = rs.getString(6);
boolean cycle = "Y".equals(cycleAsString);
BigInteger cacheSize = getBigIntValueForColumn(rs, 7);
Sequence sequence = CreationUtils.createSequence(owner, name, increment, minValue, maxValue, start, cycle, cacheSize);
// Look for a table that could correspond to the sequence
if (name.endsWith("_SEQ")) {
String tableName = name.substring(0, name.length() - "_SEQ".length());
AbstractTable abstractTable = queries.getTable(tableName);
if (abstractTable != null && abstractTable instanceof Table) {
Table table = (Table) abstractTable;
if (table.getPrimaryKey() != null && table.getPrimaryKey().getColumns().size() == 1) {
Column column = table.getPrimaryKey().getColumns().get(0);
column.setSequence(sequence);
}
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
JdbcUtils.closeStatement(pstmt);
JdbcUtils.closeResultSet(rs);
}
}
use of org.obeonetwork.dsl.database.AbstractTable in project InformationSystem by ObeoNetwork.
the class CreationUtils method createColumn.
public static Column createColumn(AbstractTable abstractTable, String name) {
if (abstractTable instanceof Table) {
Table table = (Table) abstractTable;
Column column = DatabaseFactory.eINSTANCE.createColumn();
column.setName(name);
column.setOwner(table);
table.getColumns().add(column);
return column;
}
return null;
}
Aggregations