Search in sources :

Example 6 with AbstractTable

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);
    }
}
Also used : AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Table(org.obeonetwork.dsl.database.Table) AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Column(org.obeonetwork.dsl.database.Column) ResultSet(java.sql.ResultSet) BigInteger(java.math.BigInteger) PreparedStatement(java.sql.PreparedStatement) Sequence(org.obeonetwork.dsl.database.Sequence) SQLException(java.sql.SQLException)

Example 7 with AbstractTable

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);
    }
}
Also used : AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Table(org.obeonetwork.dsl.database.Table) AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Column(org.obeonetwork.dsl.database.Column) ResultSet(java.sql.ResultSet) BigInteger(java.math.BigInteger) PreparedStatement(java.sql.PreparedStatement) Sequence(org.obeonetwork.dsl.database.Sequence) SQLException(java.sql.SQLException)

Example 8 with AbstractTable

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;
}
Also used : AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Table(org.obeonetwork.dsl.database.Table) AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Column(org.obeonetwork.dsl.database.Column) Schema(org.obeonetwork.dsl.database.Schema) ArrayList(java.util.ArrayList) PrimaryKey(org.obeonetwork.dsl.database.PrimaryKey) BigInteger(java.math.BigInteger) Sequence(org.obeonetwork.dsl.database.Sequence) DataBase(org.obeonetwork.dsl.database.DataBase)

Example 9 with AbstractTable

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);
        }
    }
}
Also used : AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Table(org.obeonetwork.dsl.database.Table) AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Schema(org.obeonetwork.dsl.database.Schema) ForeignKey(org.obeonetwork.dsl.database.ForeignKey) DataBase(org.obeonetwork.dsl.database.DataBase)

Example 10 with AbstractTable

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;
}
Also used : AbstractTable(org.obeonetwork.dsl.database.AbstractTable) ArrayList(java.util.ArrayList) View(org.obeonetwork.dsl.database.View)

Aggregations

AbstractTable (org.obeonetwork.dsl.database.AbstractTable)15 Table (org.obeonetwork.dsl.database.Table)13 Column (org.obeonetwork.dsl.database.Column)7 ResultSet (java.sql.ResultSet)6 SQLException (java.sql.SQLException)6 ArrayList (java.util.ArrayList)6 BigInteger (java.math.BigInteger)5 PreparedStatement (java.sql.PreparedStatement)5 ForeignKey (org.obeonetwork.dsl.database.ForeignKey)5 Sequence (org.obeonetwork.dsl.database.Sequence)5 Schema (org.obeonetwork.dsl.database.Schema)3 HashSet (java.util.HashSet)2 DataBase (org.obeonetwork.dsl.database.DataBase)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 EObject (org.eclipse.emf.ecore.EObject)1 DDiagramElement (org.eclipse.sirius.diagram.DDiagramElement)1 Constraint (org.obeonetwork.dsl.database.Constraint)1 ForeignKeyElement (org.obeonetwork.dsl.database.ForeignKeyElement)1 Index (org.obeonetwork.dsl.database.Index)1