use of org.obeonetwork.dsl.database.AbstractTable in project InformationSystem by ObeoNetwork.
the class PostGresDataBaseBuilder method buildSequences.
private void buildSequences(TableContainer owner) {
ResultSet rs = null;
PreparedStatement pstmt = null;
try {
PreparedStatement psmt = metaData.getConnection().prepareStatement("SELECT s.SEQUENCE_NAME, s.INCREMENT, s.MINIMUM_VALUE, s.MAXIMUM_VALUE, s.START_VALUE, s.CYCLE_OPTION , pg_catalog.obj_description(c.oid) " + "FROM INFORMATION_SCHEMA.SEQUENCES s " + "LEFT JOIN PG_CATALOG.pg_class c " + "ON c.relname = s.SEQUENCE_NAME " + "AND c.relkind = 'S' " + "WHERE s.SEQUENCE_SCHEMA = '" + schemaName + "'");
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 = "YES".equals(cycleAsString);
String comment = rs.getString(7);
// Retrieve CACHE value
BigInteger cacheValue = null;
PreparedStatement psmtCache = metaData.getConnection().prepareStatement("SELECT CACHE_VALUE FROM " + schemaName + "." + name);
ResultSet rsCache = psmtCache.executeQuery();
if (rsCache.next()) {
cacheValue = getBigIntValueForColumn(rsCache, 1);
}
Sequence sequence = CreationUtils.createSequence(owner, name, increment, minValue, maxValue, start, cycle, cacheValue);
sequence.setComments(comment);
// 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 SQLServerDataBaseBuilder method buildSequences.
private void buildSequences(DatabaseMetaData metaData, TableContainer owner) {
ResultSet rs = null;
PreparedStatement pstmt = null;
try {
String query = "SELECT CAST(seq.name AS NVARCHAR(128)), " + " CAST(seq.increment AS NVARCHAR(128)), " + " CAST(seq.minimum_value AS NVARCHAR(128)), " + " CAST(seq.maximum_value AS NVARCHAR(128)), " + " CAST(seq.start_value AS NVARCHAR(128)), " + " CAST(seq.is_cycling AS NVARCHAR(128)), " + " CAST(seq.cache_size AS NVARCHAR(128)) " + "FROM sys.sequences AS seq " + "INNER JOIN sys.schemas AS sch " + "ON seq.schema_id = sch.schema_id " + "WHERE sch.name = ?";
pstmt = metaData.getConnection().prepareStatement(query);
pstmt.setString(1, schemaName);
rs = pstmt.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);
boolean cycle = rs.getBoolean(6);
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 MpdToMldBidiRules method createOrUpdateSequences.
/**
* Create needed new sequences or update existing ones
* @param sourceTableContainer
* @return list of sequences processed so that all other sequences (now useless) could be removed
*/
/**
* @param sourceTableContainer
* @return
*/
private Collection<Sequence> createOrUpdateSequences(TableContainer sourceTableContainer) {
Collection<Sequence> sequences = new ArrayList<Sequence>();
// for each non-composite PK we create a sequence and associate it with the PK column
for (AbstractTable sourceAbstractTable : sourceTableContainer.getTables()) {
if (sourceAbstractTable instanceof Table) {
Table sourceTable = (Table) sourceAbstractTable;
// Get associated table
Table targetTable = getFromOutputTraceabilityMap(sourceTable, DatabasePackage.Literals.TABLE);
if (targetTable != null) {
PrimaryKey pk = targetTable.getPrimaryKey();
// Only for non-composite PK
if (pk != null && pk.getColumns().size() == 1) {
Column targetColumn = pk.getColumns().get(0);
// Retrieve the potentially existing sequence
Sequence existingSequence = targetColumn.getSequence();
String sequenceName = targetTable.getName() + "_SEQ";
if (existingSequence != null) {
// Update name
existingSequence.setName(sequenceName);
// Ensure the sequence is in the right container
if (!targetTable.getOwner().getSequences().contains(existingSequence)) {
targetTable.getOwner().getSequences().add(existingSequence);
}
sequences.add(existingSequence);
} else {
// Create a new sequence
Sequence newSequence = DatabaseFactory.eINSTANCE.createSequence();
newSequence.setName(sequenceName);
newSequence.setIncrement(new BigInteger("1"));
newSequence.setStart(new BigInteger("1"));
newSequence.setComments(String.format(SEQUENCE_INITIAL_COMMENTS, targetTable.getName()));
targetTable.getOwner().getSequences().add(newSequence);
// Retrieve the associated column and associate it with the sequence
targetColumn.setSequence(newSequence);
sequences.add(newSequence);
}
}
}
}
}
if (sourceTableContainer instanceof DataBase) {
DataBase sourceDataBase = (DataBase) sourceTableContainer;
for (Schema sourceSchema : sourceDataBase.getSchemas()) {
sequences.addAll(createOrUpdateSequences(sourceSchema));
}
}
return sequences;
}
use of org.obeonetwork.dsl.database.AbstractTable in project InformationSystem by ObeoNetwork.
the class MpdToMldBidiRules method createForeignKeys.
private void createForeignKeys(TableContainer sourceTableContainer) {
// create all foreignkeys
for (AbstractTable sourceAbstractTable : sourceTableContainer.getTables()) {
if (sourceAbstractTable instanceof Table) {
Table sourceTable = (Table) sourceAbstractTable;
Table targetTable = getFromOutputTraceabilityMap(sourceTable, DatabasePackage.Literals.TABLE);
for (ForeignKey foreignKey : sourceTable.getForeignKeys()) {
createFK(foreignKey, targetTable);
}
}
}
if (sourceTableContainer instanceof DataBase) {
DataBase sourceDataBase = (DataBase) sourceTableContainer;
for (Schema sourceSchema : sourceDataBase.getSchemas()) {
createForeignKeys(sourceSchema);
}
}
}
use of org.obeonetwork.dsl.database.AbstractTable in project InformationSystem by ObeoNetwork.
the class DatabaseServices method getViews.
/**
* Get all views.
* @param tableContainer
* @return list of views or empty list.
*/
public List<View> getViews(TableContainer tableContainer) {
List<AbstractTable> tables = tableContainer.getTables();
List<View> result = new ArrayList<View>();
for (AbstractTable table : tables) {
if (table instanceof View) {
initializeViewContent((View) table);
result.add((View) table);
}
}
return result;
}
Aggregations