Search in sources :

Example 11 with PrimaryKey

use of org.obeonetwork.dsl.database.PrimaryKey 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 12 with PrimaryKey

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

the class DefaultDataBaseBuilder method buildPrimaryKeys.

protected void buildPrimaryKeys(DatabaseMetaData metaData, Table table) {
    PrimaryKey primaryKey = null;
    ResultSet rs = null;
    try {
        rs = metaData.getPrimaryKeys(null, schemaName, table.getName());
        while (rs.next()) {
            if (primaryKey == null) {
                String primaryKeyName = rs.getString(6);
                primaryKey = CreationUtils.createPrimaryKey(table, primaryKeyName);
            }
            buildPrimaryKey(table, rs, primaryKey);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        JdbcUtils.closeResultSet(rs);
    }
}
Also used : ResultSet(java.sql.ResultSet) PrimaryKey(org.obeonetwork.dsl.database.PrimaryKey) SQLException(java.sql.SQLException)

Example 13 with PrimaryKey

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

the class CreationUtils method createPrimaryKey.

public static PrimaryKey createPrimaryKey(Table table, String name) {
    PrimaryKey primaryKey = DatabaseFactory.eINSTANCE.createPrimaryKey();
    primaryKey.setName(name);
    primaryKey.setOwner(table);
    table.setPrimaryKey(primaryKey);
    return primaryKey;
}
Also used : PrimaryKey(org.obeonetwork.dsl.database.PrimaryKey)

Example 14 with PrimaryKey

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

the class PrimaryKeyChangeItemProvider method getText.

/**
 * This returns the label text for the adapted class.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated NOT
 */
@Override
public String getText(Object object) {
    PrimaryKeyChange primaryKeyChange = (PrimaryKeyChange) object;
    PrimaryKey primaryKey = primaryKeyChange.getPrimaryKey();
    return getString("_UI_PrimaryKeyChange_type") + " " + primaryKey.getName();
}
Also used : PrimaryKeyChange(org.obeonetwork.dsl.database.dbevolution.PrimaryKeyChange) PrimaryKey(org.obeonetwork.dsl.database.PrimaryKey)

Example 15 with PrimaryKey

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

the class RemovePrimaryKeyItemProvider method getText.

/**
 * This returns the label text for the adapted class.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated NOT
 */
@Override
public String getText(Object object) {
    RemovePrimaryKey removePrimaryKey = (RemovePrimaryKey) object;
    PrimaryKey primaryKey = removePrimaryKey.getPrimaryKey();
    return getString("_UI_RemovePrimaryKey_type") + " " + primaryKey.getName();
}
Also used : RemovePrimaryKey(org.obeonetwork.dsl.database.dbevolution.RemovePrimaryKey) RemovePrimaryKey(org.obeonetwork.dsl.database.dbevolution.RemovePrimaryKey) PrimaryKey(org.obeonetwork.dsl.database.PrimaryKey)

Aggregations

PrimaryKey (org.obeonetwork.dsl.database.PrimaryKey)15 Table (org.obeonetwork.dsl.database.Table)3 UpdatePrimaryKey (org.obeonetwork.dsl.database.dbevolution.UpdatePrimaryKey)3 ArrayList (java.util.ArrayList)2 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)2 Column (org.obeonetwork.dsl.database.Column)2 AddPrimaryKey (org.obeonetwork.dsl.database.dbevolution.AddPrimaryKey)2 RemovePrimaryKey (org.obeonetwork.dsl.database.dbevolution.RemovePrimaryKey)2 BigInteger (java.math.BigInteger)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 EObject (org.eclipse.emf.ecore.EObject)1 EObjectStrictFilter (org.eclipse.emf.eef.runtime.impl.filters.EObjectStrictFilter)1 ReferencesTableSettings (org.eclipse.emf.eef.runtime.ui.widgets.referencestable.ReferencesTableSettings)1 Viewer (org.eclipse.jface.viewers.Viewer)1 ViewerFilter (org.eclipse.jface.viewers.ViewerFilter)1 AbstractTable (org.obeonetwork.dsl.database.AbstractTable)1 DataBase (org.obeonetwork.dsl.database.DataBase)1 Schema (org.obeonetwork.dsl.database.Schema)1 Sequence (org.obeonetwork.dsl.database.Sequence)1