1 package com.daffodilwoods.daffodildb.server.datadictionarysystem; 2 3 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator._Iterator; 4 5 import java.util.*; 6 import com.daffodilwoods.database.resource.*; 7 import com.daffodilwoods.database.general.*; 8 import com.daffodilwoods.daffodildb.server.serversystem._ServerSession; 9 import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*; 10 import com.daffodilwoods.daffodildb.server.sql99.common.SqlKeywords; 11 import com.daffodilwoods.daffodildb.server.sql99._Executer; 12 import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*; 13 import com.daffodilwoods.database.utility.P; 14 import com.daffodilwoods.daffodildb.server.sql99.utils._Reference; 15 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*; 16 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator._SelectQueryIterator; 17 18 public class ReferencingConstraintCharacteristics implements _ReferencingConstraintCharacteristics { 19 20 private PreparedStatementGetter preparedStatementGetter; 21 TreeMap constraints; 22 transient HashMap constraint_column_mapping; 23 private Object [] parameters; 24 private _DataDictionary dataDictionary; 25 private boolean initialized; 26 private boolean isDefered; 27 private boolean isSystemTable; 28 29 public ReferencingConstraintCharacteristics(QualifiedIdentifier tableName, PreparedStatementGetter preparedStatementGetter0, boolean deferable, _DataDictionary dataDictionary) throws DException { 30 isSystemTable = SystemTables.isSystemTable(tableName.getIdentifier()); 31 preparedStatementGetter = preparedStatementGetter0; 32 isDefered = deferable; 33 parameters = new Object [] {tableName.catalog, tableName.schema, tableName.name}; 34 this.dataDictionary = dataDictionary; 35 } 36 37 private void initializeConstraintDescriptor() throws DException { 38 if (initialized) { 39 return; 40 } 41 synchronized (this) { 42 if (constraints != null) 43 return; 44 constraints = new TreeMap(String.CASE_INSENSITIVE_ORDER); 45 constraint_column_mapping = new HashMap(4); 46 if (isSystemTable) { 47 if (!isDefered && dataDictionary.getSystemTableConstraint()) 48 SystemConstraintsFactory.getReferencingConstraints(constraints, parameters[2].toString(), dataDictionary); 49 } else { 50 _SelectQueryIterator constraintsIterator = isDefered 51 ? getIterator(preparedStatementGetter.getReferencingConstraintsJoinExecuter(), parameters) 52 : getIterator(preparedStatementGetter.getReferencingConstraintsJoinExecuterNonDef(), parameters); 53 if (constraintsIterator.first()) { 54 Object [] record = null; 55 do { 56 ReferentialConstraintDescriptor constraint = new ReferentialConstraintDescriptor(); 57 TableConstraintDescriptor tableConstraintDescriptor = new TableConstraintDescriptor(); 58 record = (Object []) constraintsIterator.getObject(); 59 constraint.tableConstraintDescriptor = tableConstraintDescriptor; 60 tableConstraintDescriptor.constraintDescriptor = constraint; 61 constraint.load(preparedStatementGetter, record, dataDictionary); 62 constraints.put(constraint.constraint_name, constraint); 63 } while (constraintsIterator.next()); 64 } 65 } 66 initialized = true; 67 } 68 } 69 70 public _ReferentialConstraint[] getReferencingConstraints(int[] columns) throws DException { 71 return getInsertConstraints(columns); 72 } 73 74 private ReferentialConstraintDescriptor[] getInsertConstraints(int[] columns) throws DException { 75 initializeConstraintDescriptor(); 76 ArrayList constraintsToBeReturned = new ArrayList(4); 77 for (int i = 0; i < columns.length; i++) { 78 ReferentialConstraintDescriptor[] effectedConstraints = getConstraintsUsingColumn(columns[i]); 79 if (effectedConstraints != null) 80 for (int j = 0; j < effectedConstraints.length; j++) { 81 int size = constraintsToBeReturned.size(); 82 boolean contains = false; 83 for (int k = 0; k < size; k++) { 84 ReferentialConstraintDescriptor rcd = (ReferentialConstraintDescriptor) constraintsToBeReturned.get(k); 85 if (effectedConstraints[j].constraint_name.equalsIgnoreCase(rcd.constraint_name)) { 86 contains = true; 87 break; 88 } 89 } 90 if (!contains) 91 constraintsToBeReturned.add(effectedConstraints[j]); 92 } 93 } 94 return constraintsToBeReturned.size() == 0 ? null : (ReferentialConstraintDescriptor[]) constraintsToBeReturned.toArray(new ReferentialConstraintDescriptor[0]); 95 } 96 97 private ReferentialConstraintDescriptor[] getConstraintsUsingColumn(int columnIndex) throws DException { 98 Integer key = new Integer (columnIndex); 99 if (constraint_column_mapping.containsKey(key)) 100 return (ReferentialConstraintDescriptor[]) constraint_column_mapping.get(key); 101 102 ArrayList effectedConstraints = new ArrayList(3); 103 Iterator iterator = constraints.values().iterator(); 104 while (iterator.hasNext()) { 105 ReferentialConstraintDescriptor constraint = (ReferentialConstraintDescriptor) iterator.next(); 106 if (constraint.isConstraintColumn(columnIndex)) { 107 effectedConstraints.add(constraint); 108 } 109 } 110 111 ReferentialConstraintDescriptor[] constraintsArray = null; 112 if (effectedConstraints.size() != 0) { 113 constraintsArray = new ReferentialConstraintDescriptor[effectedConstraints.size()]; 114 effectedConstraints.toArray(constraintsArray); 115 } 116 constraint_column_mapping.put(key, constraintsArray); 117 return constraintsArray; 118 } 119 120 121 public _ReferentialConstraint[] getReferencingConstraints() throws DException { 122 initializeConstraintDescriptor(); 123 int constraintsCount = constraints.size(); 124 ReferentialConstraintDescriptor[] deleteConstraints = constraintsCount == 0 ? null : 125 new ReferentialConstraintDescriptor[constraintsCount]; 126 if (constraintsCount > 0) 127 constraints.values().toArray(deleteConstraints); 128 return deleteConstraints; 129 130 } 131 132 133 private _SelectQueryIterator getIterator(_Executer executer, Object [] parameters) throws DException { 134 return (_SelectQueryIterator) executer.executeForFresh(parameters); 135 } 136 } 137 | Popular Tags |