1 package com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors; 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.serversystem.dmlvalidation.constraintsystem.*; 8 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 9 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*; 10 import com.daffodilwoods.daffodildb.server.sql99.utils.parser.*; 11 import com.daffodilwoods.database.general.*; 12 import com.daffodilwoods.database.resource.*; 13 import com.daffodilwoods.daffodildb.utils.QuotedUtility; 14 15 public class ReferentialConstraintDescriptor extends Descriptor implements _ReferentialConstraint { 16 17 private static String referentialConstraintQuery = "select * from " + SqlSchemaConstants.referential_constraints_TableName 18 + " where constraint_catalog = ? and constraint_schema = ? " 19 + " and constraint_name = ? "; 20 21 public String constraint_catalog; 22 public String constraint_schema; 23 public String constraint_name; 24 25 26 public String unique_constraint_catalog; 27 public String unique_constraint_schema; 28 public String unique_constraint_name; 29 public String match_option; 30 public String update_rule; 31 public String delete_rule; 32 public TableConstraintDescriptor tableConstraintDescriptor; 33 public TableConstraintDescriptor referencedTableConstraintDescriptor; 34 35 public ReferentialConstraintDescriptor() throws DException { 36 } 37 38 public void load(_ServerSession serverSession) throws com.daffodilwoods.database.resource.DException { 39 _SelectQueryIterator constraintIterator = SqlSchemaConstants.getIterator(serverSession, referentialConstraintQuery, getParameters()); 40 if (!constraintIterator.first()) { 41 throw new DException("DSE857", null); 42 } 43 44 Object [] columnsData = (Object []) constraintIterator.getObject(); 45 load(columnsData); 46 referencedTableConstraintDescriptor.load(serverSession); 47 } 48 49 public void load(PreparedStatementGetter preparedStatementGetter, Object [] columnsData, _DataDictionary datadictionary) throws DException { 50 load(columnsData); 51 52 tableConstraintDescriptor.constraint_catalog = (String ) columnsData[SystemTablesFields.ref_join_table_constraints_constraint_catalog]; 53 tableConstraintDescriptor.constraint_schema = (String ) columnsData[SystemTablesFields.ref_join_table_constraints_constraint_schema]; 54 tableConstraintDescriptor.constraint_name = (String ) columnsData[SystemTablesFields.ref_join_table_constraints_constraint_name]; 55 tableConstraintDescriptor.constraint_type = (String ) columnsData[SystemTablesFields.ref_join_table_constraints_constraint_type]; 56 tableConstraintDescriptor.table_catalog = (String ) columnsData[SystemTablesFields.ref_join_table_constraints_table_catalog]; 57 tableConstraintDescriptor.table_schema = (String ) columnsData[SystemTablesFields.ref_join_table_constraints_table_schema]; 58 tableConstraintDescriptor.table_name = (String ) columnsData[SystemTablesFields.ref_join_table_constraints_table_name]; 59 tableConstraintDescriptor.is_deferrable = (String ) columnsData[SystemTablesFields.ref_join_table_constraints_is_deferrable]; 60 tableConstraintDescriptor.initially_deferred = (String ) columnsData[SystemTablesFields.ref_join_table_constraints_initially_deferred]; 61 tableConstraintDescriptor.loadConstraintColumns(preparedStatementGetter, datadictionary.getColumnCharacteristics(tableConstraintDescriptor.getQualifiedTable(), true)); 62 63 referencedTableConstraintDescriptor.load(preparedStatementGetter, datadictionary); 64 } 65 66 booleanvalueexpression referencingMatchingRowsBVE; 67 booleanvalueexpression referencedMatchingRowsBVE; 68 69 public booleanvalueexpression getMatchingRowsBVE(boolean flag) throws DException { 70 StringBuffer query = new StringBuffer (); 71 ArrayList columnNames = null; 72 if (!flag) { 73 columnNames = tableConstraintDescriptor.getConstraintColumns(); 74 if (referencingMatchingRowsBVE != null) { 75 return referencingMatchingRowsBVE; 76 } 77 } else { 78 columnNames = referencedTableConstraintDescriptor.getConstraintColumns(); 79 if (referencedMatchingRowsBVE != null) { 80 return referencedMatchingRowsBVE; 81 } 82 } 83 if (columnNames.size() > 0) { 84 if (match_option.equalsIgnoreCase("Full")) { 85 query.append(" ( "); 86 for (int i = 0; i < columnNames.size(); i++) { 87 if (i != 0) { 88 query.append(" AND "); 89 } 90 query.append(QuotedUtility.getUserIdentifier(columnNames.get(i).toString())); 91 query.append(" = ? "); 92 } 93 query.append(" ) OR ( "); 94 for (int i = 0; i < columnNames.size(); i++) { 95 if (i != 0) { 96 query.append(" AND "); 97 } 98 query.append(QuotedUtility.getUserIdentifier(columnNames.get(i).toString())); 99 query.append(" is null "); 100 } 101 query.append(" ) "); 102 } else if (match_option.equalsIgnoreCase("Simple")) { 103 for (int i = 0; i < columnNames.size(); i++) { 104 query.append(QuotedUtility.getUserIdentifier(columnNames.get(i).toString())); 105 query.append(" = ? "); 106 if (i < columnNames.size() - 1) { 107 query.append(" AND "); 108 } 109 } 110 111 } else { if (!flag) { 113 for (int i = 0; i < columnNames.size(); i++) { 114 if (i != 0) { 115 query.append(" AND "); 116 } 117 query.append(" ( "); 118 query.append(QuotedUtility.getUserIdentifier(columnNames.get(i).toString())); 119 query.append(" = ? ) "); 120 } 121 } else { 122 for (int i = 0; i < columnNames.size(); i++) { 123 if (i != 0) { 124 query.append(" AND "); 125 } 126 query.append(" ( "); 127 query.append(" ? "); 128 query.append(" is null OR "); 129 query.append(QuotedUtility.getUserIdentifier(columnNames.get(i).toString())); 130 query.append(" = ? ) "); 131 } 132 } 133 } 134 } 135 booleanvalueexpression cond = (booleanvalueexpression) ConditionParser.parseCondition(query.toString()); 136 if (flag) { 137 referencedMatchingRowsBVE = cond; 138 } else { 139 referencingMatchingRowsBVE = cond; 140 } 141 return cond; 142 } 143 144 145 private void load(Object [] columnsData) throws DException { 146 constraint_catalog = (String ) columnsData[SystemTablesFields.referential_constraints_constraint_catalog]; 147 constraint_schema = (String ) columnsData[SystemTablesFields.referential_constraints_constraint_schema]; 148 constraint_name = (String ) columnsData[SystemTablesFields.referential_constraints_constraint_name]; 149 unique_constraint_catalog = (String ) columnsData[SystemTablesFields.referential_constraints_unique_constraint_catalog]; 150 unique_constraint_schema = (String ) columnsData[SystemTablesFields.referential_constraints_unique_constraint_schema]; 151 unique_constraint_name = (String ) columnsData[SystemTablesFields.referential_constraints_unique_constraint_name]; 152 153 match_option = (String ) columnsData[SystemTablesFields.referential_constraints_match_option]; 154 update_rule = (String ) columnsData[SystemTablesFields.referential_constraints_update_rule]; 155 delete_rule = (String ) columnsData[SystemTablesFields.referential_constraints_delete_rule]; 156 157 referencedTableConstraintDescriptor = new TableConstraintDescriptor(); 158 referencedTableConstraintDescriptor.constraint_catalog = unique_constraint_catalog; 159 referencedTableConstraintDescriptor.constraint_schema = unique_constraint_schema; 160 referencedTableConstraintDescriptor.constraint_name = unique_constraint_name; 161 } 162 163 166 167 public boolean isConstraintColumn(int columnIndex) throws DException { 168 return tableConstraintDescriptor.isConstraintColumn(columnIndex); 169 } 170 171 public void save(_ServerSession serverSession) throws com.daffodilwoods.database.resource.DException { 172 Object [] columnValues = new Object [] {constraint_catalog, constraint_schema, 173 constraint_name, unique_constraint_catalog, unique_constraint_schema, 174 unique_constraint_name, match_option, update_rule, delete_rule}; 175 SqlSchemaConstants.insert(serverSession, 176 SqlSchemaConstants.referential_constraints_TableName, null, columnValues); 177 } 178 179 public void delete(_ServerSession serverSession) throws com.daffodilwoods.database.resource.DException { 180 booleanvalueexpression condition = ( (DataDictionary) serverSession.getDataDictionary()).getPreparedStatementGetter().getReferentialConstraintsTableCondition(); 181 super.delete(serverSession, SqlSchemaConstants.referential_constraints_TableName, condition, new Object [] {constraint_catalog, constraint_schema, constraint_name}); 182 183 QualifiedIdentifier tableName = new QualifiedIdentifier(tableConstraintDescriptor.table_catalog, tableConstraintDescriptor.table_schema, tableConstraintDescriptor.table_name); 184 QualifiedIdentifier parentTableName = new QualifiedIdentifier(referencedTableConstraintDescriptor.table_catalog, referencedTableConstraintDescriptor.table_schema, referencedTableConstraintDescriptor.table_name); 185 serverSession.refereshTable(tableName, false); 186 serverSession.refereshTable(parentTableName, false); 187 } 188 189 public int getMatch_Option() throws DException { 190 if (match_option.equalsIgnoreCase("Simple")) { 191 return TypeConstant.Match_Simple; 192 } else if (match_option.equalsIgnoreCase("Partial")) { 193 return TypeConstant.Match_Partial; 194 } else { 195 return TypeConstant.Match_Full; 196 } 197 } 198 199 public int getDelete_Rule() throws DException { 200 if (delete_rule.equalsIgnoreCase("Cascade")) { 201 return TypeConstant.CASCADE; 202 } else if (delete_rule.equalsIgnoreCase("Set Null")) { 203 return TypeConstant.SETNULL; 204 } else if (delete_rule.equalsIgnoreCase("Set Default")) { 205 return TypeConstant.SETDEFAULT; 206 } else if (delete_rule.equalsIgnoreCase("Restrict")) { 207 return TypeConstant.RESTRICT; 208 } 209 return TypeConstant.NO_ACTION; 210 } 211 212 public int getUpdate_Rule() throws DException { 213 if (update_rule.equalsIgnoreCase("Cascade")) { 214 return TypeConstant.CASCADE; 215 } else if (update_rule.equalsIgnoreCase("Set Null")) { 216 return TypeConstant.SETNULL; 217 } else if (update_rule.equalsIgnoreCase("Set Default")) { 218 return TypeConstant.SETDEFAULT; 219 } else if (update_rule.equalsIgnoreCase("Restrict")) { 220 return TypeConstant.RESTRICT; 221 } 222 return TypeConstant.NO_ACTION; 223 } 224 225 public QualifiedIdentifier getReferencedTable() throws DException { 226 return referencedTableConstraintDescriptor.getQualifiedTable(); 227 } 228 229 private String [] getParameters() throws DException { 230 return new String [] {constraint_catalog, constraint_schema, constraint_name}; 231 } 232 233 public int[] getReferencedColumns() throws DException { 234 int[] refColumnIndexes = referencedTableConstraintDescriptor.columnIndexes; 235 int refColumnCount = refColumnIndexes.length; 236 int[] referencedColumnIndexes = new int[refColumnCount]; 237 ArrayList referencingColumnsDescriptor = tableConstraintDescriptor.getConstriantColumnDescriptors(); 238 for (int i = 0; i < refColumnCount; i++) { 239 KeyColumnUsageDescriptor kcud = (KeyColumnUsageDescriptor) referencingColumnsDescriptor.get(i); 240 referencedColumnIndexes[i] = refColumnIndexes[kcud.ordinal_position - 1]; 241 } 242 return referencedColumnIndexes; 243 } 244 245 public String [] getReferencedColumnNames() throws DException { 246 String [] refColumns = (String []) referencedTableConstraintDescriptor.getConstraintColumns().toArray(new String [0]); 247 int refColumnCount = refColumns.length; 248 String [] referencedColumns = new String [refColumnCount]; 249 ArrayList referencingColumnsDescriptor = tableConstraintDescriptor.getConstriantColumnDescriptors(); 250 for (int i = 0; i < refColumnCount; i++) { 251 KeyColumnUsageDescriptor kcud = (KeyColumnUsageDescriptor) referencingColumnsDescriptor.get(i); 252 referencedColumns[i] = refColumns[kcud.ordinal_position - 1]; 253 } 254 return referencedColumns; 255 } 256 257 public int[] getReferencingColumns() throws DException { 258 return tableConstraintDescriptor.columnIndexes; 259 } 260 261 262 public QualifiedIdentifier getReferencingTable() throws DException { 263 return tableConstraintDescriptor.getQualifiedTable(); 264 } 265 266 public ArrayList getSub_SuperTables() throws DException { 267 return tableConstraintDescriptor.getSub_SuperTables(); 268 } 269 270 public booleanvalueexpression getReferencedCondition(int[] columns) throws DException { 271 return referencedTableConstraintDescriptor.getCondition(columns); 272 } 273 274 public booleanvalueexpression getReferencingCondition(int[] columns) throws DException { 275 return tableConstraintDescriptor.getCondition(columns); 276 } 277 278 public String getConstraintName() throws DException { 279 return tableConstraintDescriptor.constraint_catalog + "." + tableConstraintDescriptor.constraint_schema + "." + tableConstraintDescriptor.constraint_name; 280 } 281 282 public QualifiedIdentifier getQualifiedIdentifier() throws DException { 283 return new QualifiedIdentifier(tableConstraintDescriptor.constraint_catalog, tableConstraintDescriptor.constraint_schema, tableConstraintDescriptor.constraint_name); 284 } 285 286 public BitSet getReferencedBitSet() throws DException { 287 int[] columnIndexes = getReferencedColumns(); 288 BitSet bitSet = new BitSet(columnIndexes.length); 289 for (int i = 0; i < columnIndexes.length; i++) { 290 bitSet.set(columnIndexes[i]); 291 } 292 return bitSet; 293 } 294 295 public BitSet getReferencingBitSet() throws DException { 296 return tableConstraintDescriptor.getBitSet(); 297 } 298 299 public int getReferencialColumnIndex() { 300 return tableConstraintDescriptor.refercencialColumnIndex; 301 } 302 303 public int getDescriptorType() { 304 return REFERENTIAL_CONSTRAINT_DESCRIPTOR; 305 } 306 } 307 | Popular Tags |