Search in sources :

Example 11 with AbstractTable

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

Example 12 with AbstractTable

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);
    }
}
Also used : ForeignKeyElement(org.obeonetwork.dsl.database.ForeignKeyElement) AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Table(org.obeonetwork.dsl.database.Table) AbstractTable(org.obeonetwork.dsl.database.AbstractTable) HashMap(java.util.HashMap) Column(org.obeonetwork.dsl.database.Column) ResultSet(java.sql.ResultSet) ForeignKey(org.obeonetwork.dsl.database.ForeignKey) HashMap(java.util.HashMap) Map(java.util.Map) SQLException(java.sql.SQLException)

Example 13 with AbstractTable

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);
    }
}
Also used : AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Constraint(org.obeonetwork.dsl.database.Constraint) Schema(org.obeonetwork.dsl.database.Schema) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException)

Example 14 with AbstractTable

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);
    }
}
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 15 with AbstractTable

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

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