1 package com.daffodilwoods.daffodildb.server.datadictionarysystem; 2 3 import java.util.*; 4 5 import com.daffodilwoods.daffodildb.server.sql99.*; 6 import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*; 7 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 8 import com.daffodilwoods.database.general.*; 9 import com.daffodilwoods.database.resource.*; 10 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*; 11 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator._SelectQueryIterator; 12 13 public class CheckConstraintCharacteristics implements _CheckConstraintCharacteristics { 14 15 16 private PreparedStatementGetter preparedStatementGetter; 17 private TreeMap constraints; 18 transient HashMap constraint_column_mapping; 19 private Object [] parameters; 20 private _ColumnCharacteristics columnCharacteristics; 21 private DataDictionary dataDictionary; 22 private boolean isSystemTable; 23 private boolean initialized; 24 private boolean isDefered; 25 26 public CheckConstraintCharacteristics(DataDictionary dataDictionary, 27 QualifiedIdentifier tableName, 28 _ColumnCharacteristics 29 columnCharacteristics, 30 PreparedStatementGetter 31 preparedStatementGetter0, 32 boolean deferable) throws DException { 33 isSystemTable = SystemTables.isSystemTable(tableName.getIdentifier()); 34 preparedStatementGetter = preparedStatementGetter0; 35 isDefered = deferable; 36 parameters = new Object [] { 37 tableName.catalog, tableName.schema, tableName.name}; 38 this.columnCharacteristics = columnCharacteristics; 39 this.dataDictionary = dataDictionary; 40 } 41 42 private void initializeConstraintDescriptor() throws DException { 43 if (initialized) 44 return; 45 synchronized (this) { 46 if (constraints != null) 47 return; 48 constraints = new TreeMap(String.CASE_INSENSITIVE_ORDER); 49 constraint_column_mapping = new HashMap(4); 50 if (isSystemTable) { 51 if (!isDefered && dataDictionary.getSystemTableConstraint()) 52 SystemConstraintsFactory.getCheckConstraints(dataDictionary, parameters[2].toString(), constraints); 53 } else { 54 _SelectQueryIterator constraintsIterator = isDefered 55 ? 56 getIterator(preparedStatementGetter. 57 getTableDescriptorExecuterForCheckConstraints(), 58 parameters) 59 : 60 getIterator(preparedStatementGetter. 61 getTableDescriptorExecuterForCheckConstraintsNonDef(), 62 parameters); 63 if (constraintsIterator.first()) { 64 Object [] record = null; 65 do { 66 TableConstraintDescriptor tableConstraintDescriptor = new TableConstraintDescriptor(); 67 CheckConstraintDescriptor constraint = new CheckConstraintDescriptor(); 68 record = (Object []) constraintsIterator.getObject(); 69 tableConstraintDescriptor.loadDataFromRecord(record); 70 constraint.constraint_catalog = tableConstraintDescriptor.constraint_catalog; 71 constraint.constraint_schema = tableConstraintDescriptor.constraint_schema; 72 constraint.constraint_name = tableConstraintDescriptor.constraint_name; 73 constraint.tableConstraintDescriptor = tableConstraintDescriptor; 74 tableConstraintDescriptor.constraintDescriptor = constraint; 75 constraint.load(preparedStatementGetter, columnCharacteristics); 76 constraints.put(constraint.constraint_name, constraint); 77 } while (constraintsIterator.next()); 78 } 79 initializeConstraintDescriptorForDomains(); 80 } 81 initialized = true; 82 } 83 } 84 85 private void initializeConstraintDescriptorForDomains() throws DException { 86 ColumnCharacteristics columnCharacteristics = (ColumnCharacteristics)this. 87 columnCharacteristics; 88 int[] domainColumnIndexes = columnCharacteristics.getDomainColumnIndexes(); 89 if (domainColumnIndexes == null) 90 return; 91 for (int i = 0; i < domainColumnIndexes.length; i++) { 92 QualifiedIdentifier identifier = new QualifiedIdentifier( 93 columnCharacteristics.getDomainCatalog(domainColumnIndexes[i]), 94 columnCharacteristics.getDomainSchema(domainColumnIndexes[i]), 95 columnCharacteristics.getDomainName(domainColumnIndexes[i])); 96 CheckConstraintDescriptor[] domainConstraints = dataDictionary. 97 getCheckConstraintForDomain(identifier); 98 if (domainConstraints != null) { 99 for (int j = 0; j < domainConstraints.length; j++) { 100 CheckConstraintDescriptor constraint = null; if (domainConstraints[j].domainConstraintDescriptor.isDeffered() == 102 isDefered) { 103 try { 104 constraints.put(domainConstraints[j].constraint_name, 105 constraint = (CheckConstraintDescriptor) 106 domainConstraints[j].clone()); 107 } catch (CloneNotSupportedException ex) { 108 } 109 constraint.addColumnIndex(domainColumnIndexes[i], 110 new String [] { (String ) parameters[0], 111 (String ) parameters[1], 112 (String ) parameters[2], 113 columnCharacteristics.getColumnName( 114 domainColumnIndexes[i])}); 115 } 116 } 117 } 118 } 119 } 120 121 122 public _CheckConstraint[] getCheckConstraints() throws DException { 123 return getCheckConstraintsForDelete(); } 125 126 127 public _CheckConstraint[] getCheckConstraintsForDelete() throws DException { 128 129 if (dataDictionary.getServerSession().getDbVersion() < 3.4) { 130 if (isSystemTable) 131 return null; 132 } 133 initializeConstraintDescriptor(); 134 int constraintsCount = constraints.size(); 135 CheckConstraintDescriptor[] deleteConstraints = constraintsCount == 0 ? null : 136 new CheckConstraintDescriptor[constraintsCount]; 137 if (constraintsCount > 0) 138 constraints.values().toArray(deleteConstraints); 139 return deleteConstraints; 140 } 141 142 private _SelectQueryIterator getIterator(_Executer executer, Object [] parameters) throws 143 DException { 144 return (_SelectQueryIterator) executer.executeForFresh(parameters); 145 } 146 } 147 | Popular Tags |