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> 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();
refTableNames.add(fktd.getSchemaName() + "." + 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(
fkNames, // foreign key names
cd.getSchemaDescriptor().getSchemaName(),
refTd.getName(), // table being modified
statementType, // INSERT|UPDATE|DELETE
type, // FOREIGN_KEY|REFERENCED_KEY
pkIndexId, // referenced backing index uuid
pkIndexConglom.getConglomerateNumber(),
// referenced backing index conglom
refcd.getUUID(),
refcd.deferrable(), // referenced constraint is
// deferrable?
uuids, // fk backing index uuids
conglomNumbers, // fk backing index congloms
isSelfReferencingFK, // is self ref array of bool
remapReferencedColumns(cd, rowMap),