Search in sources :

Example 1 with PrimaryKey

use of orgomg.cwm.resource.relational.PrimaryKey in project tdq-studio-se by Talend.

the class AddTdRelationalSwitch method casePrimaryKey.

@Override
public Boolean casePrimaryKey(PrimaryKey object) {
    ColumnSet columnSet = null;
    TdColumn tdColumn = null;
    if (leftElement instanceof TdColumn) {
        tdColumn = (TdColumn) leftElement;
        columnSet = ColumnHelper.getColumnOwnerAsColumnSet(tdColumn);
    }
    if (columnSet == null) {
        return Boolean.FALSE;
    }
    String pkName = object.getName();
    PrimaryKey primaryKey = orgomg.cwm.resource.relational.RelationalFactory.eINSTANCE.createPrimaryKey();
    primaryKey.setName(pkName);
    if (columnSet instanceof Table) {
        primaryKey = TableHelper.addPrimaryKey((TdTable) columnSet, primaryKey);
        if (tdColumn != null) {
            tdColumn.getUniqueKey().add(primaryKey);
        }
    }
    return Boolean.TRUE;
}
Also used : TdColumn(org.talend.cwm.relational.TdColumn) Table(orgomg.cwm.resource.relational.Table) TdTable(org.talend.cwm.relational.TdTable) TdTable(org.talend.cwm.relational.TdTable) PrimaryKey(orgomg.cwm.resource.relational.PrimaryKey) ColumnSet(orgomg.cwm.resource.relational.ColumnSet)

Example 2 with PrimaryKey

use of orgomg.cwm.resource.relational.PrimaryKey in project tdq-studio-se by Talend.

the class UniquePkAndFkTask method checkPkAndFk.

/**
 * DOC zshen Comment method "checkPkAndFk".
 *
 * @param element
 * @return merge pks or fks if their name is same each other
 */
private Boolean checkPkAndFk(TdTable element) {
    boolean needToSave = false;
    // case pk
    if (TableHelper.getPrimaryKeys(element).size() > 1) {
        List<PrimaryKey> primaryKeyList = TableHelper.getPrimaryKeys(element);
        for (int i = 1; i < primaryKeyList.size(); i++) {
            PrimaryKey pk = primaryKeyList.get(i);
            TdColumn column = (TdColumn) pk.getFeature().get(pk.getFeature().size() - 1);
            // remove old pk from column
            ColumnHelper.removePrimaryKey(column);
            // add new pk to column
            column.getUniqueKey().add(primaryKeyList.get(0));
            // add column to new pk
            primaryKeyList.get(0).getFeature().add(column);
            // remove old pk from table
            TableHelper.removeTableKey(element, pk);
        }
        needToSave = true;
    }
    // case fk
    if (TableHelper.getForeignKeys(element).size() > 1) {
        Map<String, ForeignKey> foreignKeySet = new HashMap<String, ForeignKey>();
        List<ForeignKey> foreignKeyList = TableHelper.getForeignKeys(element);
        for (int i = 0; i < foreignKeyList.size(); i++) {
            ForeignKey fk = foreignKeyList.get(i);
            if (foreignKeySet.get(fk.getName()) != null) {
                foreignKeySet.get(fk.getName()).getFeature().addAll(fk.getFeature());
                StructuralFeature[] structFeatureArray = fk.getFeature().toArray(new StructuralFeature[fk.getFeature().size()]);
                for (StructuralFeature fkFeature : structFeatureArray) {
                    TdColumn fkColumn = (TdColumn) fkFeature;
                    fkColumn.getKeyRelationship().remove(fk);
                    fkColumn.getKeyRelationship().add(foreignKeySet.get(fk.getName()));
                }
                // remove old fk from table
                TableHelper.removeForeignKey(element, fk);
                needToSave = true;
            } else {
                foreignKeySet.put(fk.getName(), fk);
            }
        }
    }
    return Boolean.valueOf(needToSave);
}
Also used : TdColumn(org.talend.cwm.relational.TdColumn) StructuralFeature(orgomg.cwm.objectmodel.core.StructuralFeature) HashMap(java.util.HashMap) PrimaryKey(orgomg.cwm.resource.relational.PrimaryKey) ForeignKey(orgomg.cwm.resource.relational.ForeignKey)

Aggregations

TdColumn (org.talend.cwm.relational.TdColumn)2 PrimaryKey (orgomg.cwm.resource.relational.PrimaryKey)2 HashMap (java.util.HashMap)1 TdTable (org.talend.cwm.relational.TdTable)1 StructuralFeature (orgomg.cwm.objectmodel.core.StructuralFeature)1 ColumnSet (orgomg.cwm.resource.relational.ColumnSet)1 ForeignKey (orgomg.cwm.resource.relational.ForeignKey)1 Table (orgomg.cwm.resource.relational.Table)1