Search in sources :

Example 1 with AbstractTable

use of org.obeonetwork.dsl.database.AbstractTable in project InformationSystem by ObeoNetwork.

the class DatabaseServices method allReferencedTables.

/**
 * Retrieve all referenced Tables.
 * @param context the Table Container
 * @return list of Tables
 */
public List<Table> allReferencedTables(TableContainer context) {
    // Retrieve all tables
    List<AbstractTable> abstractTables = context.getTables();
    List<Table> tables = new ArrayList<Table>();
    List<Table> tablesFKtarget = new ArrayList<Table>();
    // Retrieve all tables instance of Table
    for (AbstractTable abstractTable : abstractTables) {
        if (abstractTable instanceof Table) {
            tables.add((Table) abstractTable);
        }
    }
    // Retrieve all foreigns keys for the tables
    for (Table table : tables) {
        for (ForeignKey fk : table.getForeignKeys()) {
            if (!tablesFKtarget.contains(fk.getTarget())) {
                // retrieve all table from target of foreigns keys
                tablesFKtarget.add(fk.getTarget());
            }
        }
    }
    Set<Table> allReferencedTables = new HashSet<Table>();
    // Remove duplicates
    allReferencedTables.addAll(tablesFKtarget);
    return new ArrayList<Table>(allReferencedTables);
}
Also used : AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Table(org.obeonetwork.dsl.database.Table) AbstractTable(org.obeonetwork.dsl.database.AbstractTable) ArrayList(java.util.ArrayList) ForeignKey(org.obeonetwork.dsl.database.ForeignKey) HashSet(java.util.HashSet)

Example 2 with AbstractTable

use of org.obeonetwork.dsl.database.AbstractTable in project InformationSystem by ObeoNetwork.

the class DatabaseServices method getForeignKeys.

/**
 * Return ForeignKeys from tables.
 * @param tableContainer
 * @return foreignKeys
 */
public List<ForeignKey> getForeignKeys(TableContainer tableContainer, DSemanticDiagram diagram) {
    List<ForeignKey> foreignKeys = new ArrayList<ForeignKey>();
    List<AbstractTable> tables = tableContainer.getTables();
    for (AbstractTable abstractTable : tables) {
        if (abstractTable instanceof Table) {
            foreignKeys.addAll(((Table) abstractTable).getForeignKeys());
        }
    }
    // Foreign keys from external tables on diagram
    for (DDiagramElement diagramElement : diagram.getDiagramElements()) {
        EObject semanticElt = diagramElement.getTarget();
        if (semanticElt instanceof Table) {
            foreignKeys.addAll(((Table) semanticElt).getForeignKeys());
        }
    }
    return foreignKeys;
}
Also used : AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Table(org.obeonetwork.dsl.database.Table) AbstractTable(org.obeonetwork.dsl.database.AbstractTable) EObject(org.eclipse.emf.ecore.EObject) ArrayList(java.util.ArrayList) ForeignKey(org.obeonetwork.dsl.database.ForeignKey) DDiagramElement(org.eclipse.sirius.diagram.DDiagramElement)

Example 3 with AbstractTable

use of org.obeonetwork.dsl.database.AbstractTable in project InformationSystem by ObeoNetwork.

the class DefaultDataBaseBuilder method buildForeignKeys.

@Override
public void buildForeignKeys() {
    ArrayList<AbstractTable> tables = new ArrayList<AbstractTable>();
    tables.addAll(tableContainer.getTables());
    for (AbstractTable table : tables) {
        if (table instanceof Table) {
            buildForeignKeys((Table) table);
        }
    }
}
Also used : AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Table(org.obeonetwork.dsl.database.Table) AbstractTable(org.obeonetwork.dsl.database.AbstractTable) ArrayList(java.util.ArrayList)

Example 4 with AbstractTable

use of org.obeonetwork.dsl.database.AbstractTable in project InformationSystem by ObeoNetwork.

the class H2DataBaseBuilder method buildSequences.

private void buildSequences(DatabaseMetaData metaData, TableContainer owner) {
    ResultSet rs = null;
    PreparedStatement pstmt = null;
    try {
        // TODO récupérer cycle avec IS_CYCLE
        // TODO récupérer Cache
        PreparedStatement psmt = metaData.getConnection().prepareStatement("SELECT SEQUENCE_NAME, INCREMENT, MIN_VALUE, MAX_VALUE, CURRENT_VALUE " + "FROM INFORMATION_SCHEMA.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);
            Sequence sequence = CreationUtils.createSequence(owner, name, increment, minValue, maxValue, start, false, null);
            // 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 5 with AbstractTable

use of org.obeonetwork.dsl.database.AbstractTable in project InformationSystem by ObeoNetwork.

the class MySQLDataBaseBuilder method removeIndexForFK.

private void removeIndexForFK(TableContainer owner) {
    HashSet<String> fkNames = new HashSet<String>();
    for (AbstractTable table : owner.getTables()) {
        if (table instanceof Table) {
            for (ForeignKey foreignKey : ((Table) table).getForeignKeys()) {
                fkNames.add(foreignKey.getName());
            }
        }
    }
    for (AbstractTable t : owner.getTables()) {
        if (t instanceof Table) {
            Table table = (Table) t;
            ArrayList<Index> indices = new ArrayList<Index>();
            if (table.getIndexes().size() > 0) {
                for (Index index : table.getIndexes()) {
                    if (!fkNames.contains(index.getName())) {
                        indices.add(index);
                    } else {
                        for (IndexElement indexElt : index.getElements()) {
                            indexElt.getColumn().getIndexElements().remove(indexElt);
                        }
                    }
                }
                table.getIndexes().clear();
                table.getIndexes().addAll(indices);
            }
        }
    }
}
Also used : AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Table(org.obeonetwork.dsl.database.Table) AbstractTable(org.obeonetwork.dsl.database.AbstractTable) ArrayList(java.util.ArrayList) Index(org.obeonetwork.dsl.database.Index) ForeignKey(org.obeonetwork.dsl.database.ForeignKey) HashSet(java.util.HashSet) IndexElement(org.obeonetwork.dsl.database.IndexElement)

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