1 package com.daffodilwoods.daffodildb.server.sql99.ddl.schemadefinition; 2 3 import java.util.*; 4 5 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*; 6 import com.daffodilwoods.daffodildb.server.serversystem.*; 7 import com.daffodilwoods.daffodildb.server.sql99.*; 8 import com.daffodilwoods.daffodildb.server.sql99.common.*; 9 import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*; 10 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 11 import com.daffodilwoods.daffodildb.server.sql99.dql.tableexpression.fromclause.*; 12 import com.daffodilwoods.database.resource.*; 13 import com.daffodilwoods.database.utility.*; 14 15 public class referencedtableandcolumns { 16 public parenreferencecolumnlist _Optparenreferencecolumnlist0; 17 public tablename _tablename1; 18 19 private ReferentialConstraintDescriptor referencialConsDes; 20 21 public void setReferentialConstraintDescriptor( 22 _Descriptor referencialConsDes0) { 23 referencialConsDes = (ReferentialConstraintDescriptor) 24 referencialConsDes0; 25 } 26 27 public Object run(Object object) throws DException { 28 _ServerSession currentSession = (_ServerSession) object; 29 TableDescriptor referencedTable = getReferencedTableDescriptor( 30 currentSession); 31 36 return getUniqueConstraintDescriptorAndOrinalPositions(referencedTable, 37 currentSession); 38 } 39 40 43 44 private TableDescriptor getReferencedTableDescriptor(_ServerSession 45 currentSession) throws DException { 46 TableDescriptor referencedTableDescriptor = new TableDescriptor(); 47 48 referencedTableDescriptor.table_catalog = _tablename1.getCatalogName(); 49 referencedTableDescriptor.table_schema = _tablename1.getSchemaName(); 50 referencedTableDescriptor.table_name = _tablename1.getTableName(); 51 52 55 56 if (SqlKeywords.MODULE.equalsIgnoreCase(referencedTableDescriptor. 57 table_schema)) { 58 referencedTableDescriptor.table_schema = currentSession.getModuleSchema(); 59 referencedTableDescriptor.table_catalog = currentSession.getModuleCatalog(); 60 } 61 if (referencedTableDescriptor.table_catalog == null) { 62 referencedTableDescriptor.table_catalog = referencialConsDes. 63 tableConstraintDescriptor.table_catalog; 64 65 } 66 if (referencedTableDescriptor.table_schema == null) { 67 referencedTableDescriptor.table_schema = referencialConsDes. 68 tableConstraintDescriptor.table_schema; 69 70 } 71 try { 72 referencedTableDescriptor.load(currentSession); 73 } catch (DException ex) { 74 throw new DException("DSE7003", 75 new Object [] {referencialConsDes. 76 getQualifiedIdentifier().getIdentifier(), 77 referencedTableDescriptor.getQualifiedTableName(). 78 getIdentifier()}); 79 } 80 return referencedTableDescriptor; 81 } 82 83 private Object [] getUniqueConstraintDescriptorAndOrinalPositions( 84 TableDescriptor referencedTable, _ServerSession currentSession) throws DException { 85 TableConstraintDescriptor referencedConstraint = new TableConstraintDescriptor(); 86 referencedConstraint.table_catalog = referencedTable.table_catalog; 87 referencedConstraint.table_schema = referencedTable.table_schema; 88 referencedConstraint.table_name = referencedTable.table_name; 89 int[] ordinalPositions = null; 90 String [] columns = null; 91 if (_Optparenreferencecolumnlist0 == null) { 92 setTableConstraintDesciptorForPrimaryKey(currentSession, 93 referencedConstraint, 94 ordinalPositions); 95 } else { 96 columns = (String []) 97 _Optparenreferencecolumnlist0.run(null); 98 int foreignConstraintsColumnCount = referencialConsDes. 99 tableConstraintDescriptor.getConstraintColumns().size(); 100 if (foreignConstraintsColumnCount != columns.length) { 101 throw new DException("DSE7002", 102 new Object [] {referencedTable.getQualifiedTableName(). 103 getIdentifier(), 104 referencedConstraint.getQualifiedTable(). 105 getIdentifier()}); 106 } 107 108 setTableCostraintDescriptorForPKorUK(currentSession, referencedConstraint, 109 columns); 110 } 111 ArrayList constraintColumns = referencedConstraint.getConstraintColumns(); 112 int constraintColumnCount = constraintColumns.size(); 113 if (columns != null) { 114 117 if (constraintColumnCount != columns.length) { 118 throw new DException("DSE1086", 119 new Object [] {referencedTable.getQualifiedTableName(). 120 getIdentifier(), 121 referencedConstraint.getQualifiedTable(). 122 getIdentifier()}); 123 } 124 ordinalPositions = getOrdinalPositions(columns, constraintColumns, 125 constraintColumnCount); 126 } else { 127 ordinalPositions = new int[constraintColumnCount]; 128 for (int i = 0; i < constraintColumnCount; i++) { 129 ordinalPositions[i] = i + 1; 130 } 131 } 132 referencedConstraint.tableDescriptor = referencedTable; 133 return new Object [] {referencedConstraint, ordinalPositions}; 134 } 135 136 private int[] getOrdinalPositions(String [] columns, 137 ArrayList constraintColumns, 138 int constraintColumnCount) { 139 int[] ordinalPositions = new int[constraintColumnCount]; 140 for (int i = 0; i < columns.length; i++) { 141 String columnName = (String ) columns[i]; 142 int column_ordinal_position = -1; 143 for (int j = 0; j < constraintColumnCount; j++) { 144 if (columnName.equalsIgnoreCase( (String ) constraintColumns.get(j))) { 145 column_ordinal_position = j + 1; 146 break; 147 } 148 } 149 ordinalPositions[i] = column_ordinal_position; 150 } 151 return ordinalPositions; 152 } 153 154 private void setTableCostraintDescriptorForPKorUK(_ServerSession 155 currentSession, TableConstraintDescriptor referencedConstraint, 156 String [] columns) throws DException { 157 findReferencedConstraint(currentSession, referencedConstraint, columns); 158 referencedConstraint.load(currentSession); 159 } 160 161 private void setTableConstraintDesciptorForPrimaryKey(_ServerSession 162 currentSession, TableConstraintDescriptor referencedConstraint, 163 int[] ordinalPositions) throws DException { 164 referencedConstraint.constraint_type = SqlSchemaConstants.PrimaryKey; 165 try { 166 referencedConstraint.loadPrimaryKeyConstraint(currentSession); 167 } catch (DException ex) { 168 if (ex.getDseCode().equalsIgnoreCase("DSE286")) { 169 throw new DException("DSE7001", 170 new Object [] {referencialConsDes. 171 getQualifiedIdentifier(). 172 getIdentifier(), 173 referencedConstraint.getQualifiedTable(). 174 getIdentifier()}); 175 } 176 throw ex; 177 } 178 } 179 180 public void findReferencedConstraint(_ServerSession serverSession, 181 TableConstraintDescriptor 182 referencedConstraint, Object [] columns) throws DException { 183 184 PreparedStatementGetter psg = ( (DataDictionary) serverSession.getDataDictionary()).getPreparedStatementGetter(); 185 _Executer referencedConstraintExecuter = psg.getReferencedConstraintExecuter(); 186 _SelectQueryIterator puIter = (_SelectQueryIterator) referencedConstraintExecuter.execute(new Object [] {referencedConstraint.table_catalog, 187 referencedConstraint.table_schema, referencedConstraint.table_name}); 188 189 193 194 if (!puIter.first()) { 195 throw new DException("DSE1086", new Object [] {referencedConstraint.getQualifiedTable().getIdentifier(), referencialConsDes.getQualifiedIdentifier().getIdentifier()}); 196 } 197 198 boolean found = false; 199 203 204 _Executer keyColumnExecuter = psg.getKeyColumnExecuter(); 205 206 207 do { 208 Object [] constraintValues = (Object []) puIter.getObject(); 209 referencedConstraint.constraint_catalog = (String ) constraintValues[ 210 SystemTablesFields.table_constraints_constraint_catalog]; 211 referencedConstraint.constraint_schema = (String ) constraintValues[ 212 SystemTablesFields.table_constraints_constraint_schema]; 213 referencedConstraint.constraint_name = (String ) constraintValues[ 214 SystemTablesFields.table_constraints_constraint_name]; 215 216 _SelectQueryIterator iter = (_SelectQueryIterator) keyColumnExecuter.execute(new Object [] {constraintValues[SystemTablesFields. 217 table_constraints_constraint_catalog], 218 constraintValues[SystemTablesFields. 219 table_constraints_constraint_schema], 220 constraintValues[SystemTablesFields. 221 table_constraints_constraint_name]}); 222 223 224 if (iter.first()) { 225 int columnMatchCount = 0; 226 do { 227 Object [] obj = (Object []) iter.getObject(); 228 String columnName = (String ) obj[SystemTablesFields. 229 key_column_usage_column_name]; 230 if (P.indexOfIgnoreCase( (String []) columns, columnName) != -1) { 231 columnMatchCount++; 232 } else { 233 columnMatchCount = 0; 234 break; 235 } 236 } while (iter.next()); 237 if (columnMatchCount == columns.length) { 238 found = true; 239 } 240 } 241 if (found) { 242 break; 243 } 244 } while (puIter.next()); 245 248 if (!found) { 249 throw new DException("DSE1086", new Object [] {referencedConstraint.getQualifiedTable().getIdentifier(), referencialConsDes.getQualifiedIdentifier().getIdentifier()}); 250 } 251 } 252 253 public String toString() { 254 StringBuffer sb = new StringBuffer (); 255 sb.append(" "); 256 sb.append(_tablename1); 257 sb.append(" "); 258 if (_Optparenreferencecolumnlist0 != null) { 259 sb.append(_Optparenreferencecolumnlist0); 260 } 261 return sb.toString(); 262 } 263 264 public Object clone() throws CloneNotSupportedException { 265 return this; 266 } 267 } 268 | Popular Tags |