Search in sources :

Example 1 with FKInfo

use of org.apache.derby.impl.sql.execute.FKInfo in project derby by apache.

the class DMLModStatementNode method generateFKInfo.

/**
 * Generate the FKInfo structures used during code generation.
 * For each constraint that isn't a check constraint, add another
 * one of these FKInfo structures and then package them up into
 * a single array.
 *
 * @param cdl				The constraint descriptor list
 * @param dd				The DataDictionary
 * @param td				The TableDescriptor
 * @param readColsBitSet 	columns read
 *
 * @exception StandardException		Thrown on failure
 */
private void generateFKInfo(ConstraintDescriptorList cdl, DataDictionary dd, TableDescriptor td, FormatableBitSet readColsBitSet) throws StandardException {
    ArrayList<FKInfo> fkList = new ArrayList<FKInfo>();
    int type;
    UUID[] uuids;
    long[] conglomNumbers;
    String[] fkNames;
    ConstraintDescriptorList fkcdl;
    ReferencedKeyConstraintDescriptor refcd;
    boolean[] isSelfReferencingFK;
    ConstraintDescriptorList activeList = dd.getActiveConstraintDescriptors(cdl);
    int[] rowMap = getRowMap(readColsBitSet, td);
    int[] raRules;
    boolean[] deferrable;
    UUID[] fkIds;
    ArrayList<String> refSchemaNames = new ArrayList<String>(1);
    ArrayList<String> refTableNames = new ArrayList<String>(1);
    ArrayList<Long> refIndexConglomNum = new ArrayList<Long>(1);
    ArrayList<Integer> refActions = new ArrayList<Integer>(1);
    ArrayList<ColumnDescriptorList> refColDescriptors = new ArrayList<ColumnDescriptorList>(1);
    ArrayList<int[]> fkColMap = new ArrayList<int[]>(1);
    int activeSize = activeList.size();
    for (int index = 0; index < activeSize; index++) {
        ConstraintDescriptor cd = activeList.elementAt(index);
        if (cd instanceof ForeignKeyConstraintDescriptor) {
            /*
				** We are saving information for checking the
				** primary/unique key that is referenced by this
				** foreign key, so type is FOREIGN KEY.
				*/
            type = FKInfo.FOREIGN_KEY;
            refcd = ((ForeignKeyConstraintDescriptor) cd).getReferencedConstraint();
            uuids = new UUID[1];
            deferrable = new boolean[1];
            fkIds = new UUID[1];
            conglomNumbers = new long[1];
            fkNames = new String[1];
            isSelfReferencingFK = new boolean[1];
            raRules = new int[1];
            fkSetupArrays(dd, (ForeignKeyConstraintDescriptor) cd, 0, uuids, conglomNumbers, fkNames, isSelfReferencingFK, raRules, deferrable, fkIds);
            // oops, get the right constraint name -- for error
            // handling we want the FK name, not refcd name
            fkNames[0] = cd.getConstraintName();
        } else if (cd instanceof ReferencedKeyConstraintDescriptor) {
            refcd = (ReferencedKeyConstraintDescriptor) cd;
            /*
				** We are saving information for checking the
				** foreign key(s) that is dependent on this referenced
				** key, so type is REFERENCED KEY.
				*/
            type = FKInfo.REFERENCED_KEY;
            fkcdl = dd.getActiveConstraintDescriptors(((ReferencedKeyConstraintDescriptor) cd).getForeignKeyConstraints(ConstraintDescriptor.ENABLED));
            int size = fkcdl.size();
            if (size == 0) {
                continue;
            }
            uuids = new UUID[size];
            deferrable = new boolean[size];
            fkIds = new UUID[size];
            fkNames = new String[size];
            conglomNumbers = new long[size];
            isSelfReferencingFK = new boolean[size];
            raRules = new int[size];
            TableDescriptor fktd;
            ColumnDescriptorList coldl;
            int[] refColumns;
            ColumnDescriptor cold;
            int[] colArray = remapReferencedColumns(cd, rowMap);
            for (int inner = 0; inner < size; inner++) {
                ForeignKeyConstraintDescriptor fkcd = (ForeignKeyConstraintDescriptor) fkcdl.elementAt(inner);
                fkSetupArrays(dd, fkcd, inner, uuids, conglomNumbers, fkNames, isSelfReferencingFK, raRules, deferrable, fkIds);
                if ((raRules[inner] == StatementType.RA_CASCADE) || (raRules[inner] == StatementType.RA_SETNULL)) {
                    // find  the referencing  table Name
                    fktd = fkcd.getTableDescriptor();
                    refSchemaNames.add(fktd.getSchemaName());
                    refTableNames.add(fktd.getName());
                    refActions.add(Integer.valueOf(raRules[inner]));
                    // find the referencing column name required for update null.
                    refColumns = fkcd.getReferencedColumns();
                    coldl = fktd.getColumnDescriptorList();
                    ColumnDescriptorList releventColDes = new ColumnDescriptorList();
                    for (int i = 0; i < refColumns.length; i++) {
                        cold = coldl.elementAt(refColumns[i] - 1);
                        releventColDes.add(cold);
                    }
                    refColDescriptors.add(releventColDes);
                    refIndexConglomNum.add(Long.valueOf(conglomNumbers[inner]));
                    fkColMap.add(colArray);
                }
            }
        } else {
            continue;
        }
        final TableDescriptor pktd = refcd.getTableDescriptor();
        final UUID pkIndexId = refcd.getIndexId();
        final ConglomerateDescriptor pkIndexConglom = pktd.getConglomerateDescriptor(pkIndexId);
        final TableDescriptor refTd = cd.getTableDescriptor();
        fkList.add(new FKInfo(// foreign key names
        fkNames, cd.getSchemaDescriptor().getSchemaName(), // table being modified
        refTd.getName(), // INSERT|UPDATE|DELETE
        statementType, // FOREIGN_KEY|REFERENCED_KEY
        type, // referenced backing index uuid
        pkIndexId, pkIndexConglom.getConglomerateNumber(), // referenced backing index conglom
        refcd.getUUID(), // referenced constraint is
        refcd.deferrable(), // fk backing index uuids
        uuids, // fk backing index congloms
        conglomNumbers, // is self ref array of bool
        isSelfReferencingFK, remapReferencedColumns(cd, rowMap), // columns referenced by key
        dd.getRowLocationTemplate(getLanguageConnectionContext(), refTd), // referential action rules
        raRules, // deferrable flags
        deferrable, // UUID of fks
        fkIds));
    }
    // Now convert the list into an array.
    if (!fkList.isEmpty()) {
        fkInfo = fkList.toArray(new FKInfo[fkList.size()]);
    }
    // Convert the ref action info lists to arrays.
    int size = refActions.size();
    if (size > 0) {
        fkTableNames = new String[size];
        fkSchemaNames = new String[size];
        fkRefActions = new int[size];
        fkColDescriptors = new ColumnDescriptorList[size];
        fkIndexConglomNumbers = new long[size];
        fkColArrays = new int[size][];
        for (int i = 0; i < size; i++) {
            fkTableNames[i] = refTableNames.get(i);
            fkSchemaNames[i] = refSchemaNames.get(i);
            fkRefActions[i] = (refActions.get(i)).intValue();
            fkColDescriptors[i] = refColDescriptors.get(i);
            fkIndexConglomNumbers[i] = (refIndexConglomNum.get(i)).longValue();
            fkColArrays[i] = (fkColMap.get(i));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ColumnDescriptorList(org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList) UUID(org.apache.derby.catalog.UUID) FKInfo(org.apache.derby.impl.sql.execute.FKInfo) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) ConstraintDescriptorList(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList) ForeignKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor) ConglomerateDescriptor(org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor) TableDescriptor(org.apache.derby.iapi.sql.dictionary.TableDescriptor) ReferencedKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor) ForeignKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor) ConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor) ReferencedKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor)

Aggregations

ArrayList (java.util.ArrayList)1 UUID (org.apache.derby.catalog.UUID)1 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)1 ColumnDescriptorList (org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList)1 ConglomerateDescriptor (org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)1 ConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor)1 ConstraintDescriptorList (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList)1 ForeignKeyConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor)1 ReferencedKeyConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor)1 TableDescriptor (org.apache.derby.iapi.sql.dictionary.TableDescriptor)1 FKInfo (org.apache.derby.impl.sql.execute.FKInfo)1