1 package com.daffodilwoods.daffodildb.server.datadictionarysystem.information; 2 3 import java.util.*; 4 5 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*; 6 import com.daffodilwoods.daffodildb.server.serversystem.dmlvalidation.constraintsystem.*; 7 import com.daffodilwoods.daffodildb.server.sessionsystem.*; 8 import com.daffodilwoods.daffodildb.server.sql99.common.*; 9 import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*; 10 import com.daffodilwoods.database.general.*; 11 import com.daffodilwoods.database.resource.*; 12 13 public class TableInformation implements _TableInformation { 14 private String type; 15 private String remarks; 16 _DataDictionary dataDictionary; 17 QualifiedIdentifier tableName; 18 private _ColumnsInformation columnInformation; 19 private _IndexInformation indexInformation; 20 private _FullTextIndexInformation fullTextIndexInformation; 21 private _ConstraintInformation constraintInformation; 22 private _TriggerInformation triggerInformation; 23 private boolean searchedTriggerInformation; 24 private boolean searchedIndexInformation; 25 private boolean searchedFullTextIndexInformation; 26 private boolean searchedConstraintInformation; 27 28 public TableInformation(_DataDictionary dataDictionary, QualifiedIdentifier tableName) { 29 this.dataDictionary = dataDictionary; 30 this.tableName = tableName; 31 } 32 33 public String getQualifiedName() { 34 return tableName.getIdentifier(); 35 } 36 37 public ArrayList getAllColumns() { 38 if (columnInformation == null) 39 getColumnInformation(); 40 String [] columnNames = columnInformation.getColumnNames(); 41 ArrayList columnsList = new ArrayList(); 42 for (int i = 0; i < columnNames.length; i++) 43 columnsList.add(columnNames[i]); 44 return columnsList; 45 } 46 47 public ArrayList getAllIndexes() { 48 if (!searchedIndexInformation) 49 getIndexInformation(); 50 return indexInformation == null ? null : ( (IndexInformation) indexInformation).getAllIndexes(); 51 } 52 53 public ArrayList getAllFullTextIndexes() { 54 if (!searchedFullTextIndexInformation) 55 getFullTextIndexInformation(); 56 return fullTextIndexInformation == null ? null : ( (FullTextIndexInformation) fullTextIndexInformation).getAllIndexes(); 57 } 58 59 public ArrayList getAllTriggers() { 60 if (!searchedTriggerInformation) 61 getTriggerInformation(); 62 return triggerInformation == null ? null : ( (TriggerInformation) triggerInformation).getAllTriggers(); 63 } 64 65 public _ColumnsInformation getColumnInformation() { 66 if (columnInformation != null) 67 return columnInformation; 68 columnInformation = new ColumnsInformation(dataDictionary, tableName); 69 return columnInformation; 70 } 71 72 public _ConstraintInformation getConstraintInformation() { 73 if (searchedConstraintInformation) 74 return constraintInformation; 75 ArrayList constrintInfoList = new ArrayList(); 76 searchedConstraintInformation = true; 77 addConstriantsInfo(constrintInfoList, getCheckConstraints()); 78 addConstriantsInfo(constrintInfoList, getPrimaryAndUniqueConstraints()); 79 addConstriantsInfo(constrintInfoList, getReferencedConstraints()); 80 addConstriantsInfo(constrintInfoList, getReferencingConstraints()); 81 if (constrintInfoList.size() == 0) 82 return null; 83 constraintInformation = new ConstraintInformation( (_ConstraintInfo[]) constrintInfoList.toArray(new _ConstraintInfo[0])); 84 return constraintInformation; 85 } 86 87 private void addConstriantsInfo(ArrayList constraintInfoList, _ConstraintInfo[] constraintInfo) { 88 if (constraintInfo == null) 89 return; 90 int len = constraintInfo.length; 91 for (int i = 0; i < len; i++) 92 constraintInfoList.add(constraintInfo[i]); 93 } 94 95 public ArrayList getAllConstraints() { 96 if (!searchedConstraintInformation) 97 getConstraintInformation(); 98 return constraintInformation == null ? null : ( (ConstraintInformation) constraintInformation).getAllConstraints(); 99 } 100 101 public _IndexInformation getIndexInformation() { 102 if (searchedIndexInformation) 103 return indexInformation; 104 try { 105 _IndexCharacteristics indexCharacteristics = dataDictionary.getIndexCharacteristics(tableName); 106 indexCharacteristics.refresh(); 107 com.daffodilwoods.daffodildb.server.datasystem.indexsystem._IndexInformation[] dsIndexInformation = indexCharacteristics.getIndexInformations(); 108 searchedIndexInformation = true; 109 if (dsIndexInformation == null) 110 return null; 111 int len = dsIndexInformation.length; 112 ArrayList indexInfoList = new ArrayList(); 113 boolean onlyRowId = ( (GlobalSession) dataDictionary.getServerSession()).getSessionVersionHandler().hasOnlyRowidAsColumnIndex(); 114 int indexCount = onlyRowId ? 1 : 3; 115 for (int i = 0; i < len; i++) { 116 if (!dsIndexInformation[i].isSystemGenerated()) { 117 IndexInfo tempIndexInfo = new IndexInfo(); 118 tempIndexInfo.name = dsIndexInformation[i].getQualifiedIdentifier().getName(); 119 if (dsIndexInformation[i].isUpdated()) { 120 String [] tempcolumns = dsIndexInformation[i].getColumns(); 121 int numberOfColumns = tempcolumns.length; 122 String [] columns = new String [numberOfColumns - indexCount]; 123 boolean[] order = new boolean[numberOfColumns - indexCount]; 124 System.arraycopy(tempcolumns, 0, columns, 0, numberOfColumns - indexCount); 125 System.arraycopy(dsIndexInformation[i].getOrderOfColumns(), 0, order, 0, numberOfColumns - indexCount); 126 tempIndexInfo.columns = columns; 127 tempIndexInfo.orderOfColumns = order; 128 } else { 129 tempIndexInfo.columns = dsIndexInformation[i].getColumns(); 130 tempIndexInfo.orderOfColumns = dsIndexInformation[i].getOrderOfColumns(); 131 } 132 indexInfoList.add(tempIndexInfo); 133 } 134 } 135 if (indexInfoList.size() == 0) 136 return null; 137 IndexInfo[] indexInfo = (IndexInfo[]) indexInfoList.toArray(new IndexInfo[0]); 138 indexInformation = new IndexInformation(indexInfo); 139 return indexInformation; 140 } catch (Exception ex) { 141 return null; 142 } 143 } 144 145 public _FullTextIndexInformation getFullTextIndexInformation() { 146 if (searchedFullTextIndexInformation) 147 return fullTextIndexInformation; 148 try { 149 _FullTextIndexCharacteristics indexCharacteristics = dataDictionary.getFullTextIndexCharacteristics(tableName); 150 indexCharacteristics.refresh(); 151 com.daffodilwoods.daffodildb.server.datasystem.indexsystem._FullTextIndexInformation[] dsIndexInformation = indexCharacteristics.getFullTextIndexInformations(); 152 searchedFullTextIndexInformation = true; 153 if (dsIndexInformation == null) 154 return null; 155 int len = dsIndexInformation.length; 156 ArrayList fullTextIndexInfoList = new ArrayList(); 157 boolean onlyRowId = ( (GlobalSession) dataDictionary.getServerSession()).getSessionVersionHandler().hasOnlyRowidAsColumnIndex(); 158 int indexCount = onlyRowId ? 1 : 3; 159 for (int i = 0; i < len; i++) { 160 FullTextIndexInfo tempIndexInfo = new FullTextIndexInfo(); 161 tempIndexInfo.name = dsIndexInformation[i].getQualifiedIdentifier().getName(); 162 if (dsIndexInformation[i].isUpdated()) { 163 String [] tempcolumns = dsIndexInformation[i].getColumns(); 164 int numberOfColumns = tempcolumns.length; 165 String [] columns = new String [numberOfColumns - indexCount]; 166 boolean[] order = new boolean[numberOfColumns - indexCount]; 167 System.arraycopy(tempcolumns, 0, columns, 0, numberOfColumns - indexCount); 168 tempIndexInfo.columns = columns; 169 } else { 170 tempIndexInfo.columns = dsIndexInformation[i].getColumns(); 171 } 172 fullTextIndexInfoList.add(tempIndexInfo); 173 } 174 if (fullTextIndexInfoList.size() == 0) 175 return null; 176 FullTextIndexInfo[] indexInfo = (FullTextIndexInfo[]) fullTextIndexInfoList.toArray(new FullTextIndexInfo[0]); 177 fullTextIndexInformation = new FullTextIndexInformation(indexInfo); 178 return fullTextIndexInformation; 179 } catch (Exception ex) { 180 return null; 181 } 182 } 183 184 public _TriggerInformation getTriggerInformation() { 185 if (searchedTriggerInformation) 186 return triggerInformation; 187 ArrayList triggerInfoList = new ArrayList(); 188 try { 189 _TriggerCharacteristics triggerCharacteristics = dataDictionary.getDDSTriggerOperation().getTriggerCharacteristics(tableName, true); 190 addTriggerInfo(triggerInfoList, triggerCharacteristics.getAfterDeleteTriggers(), SqlKeywords.AFTER, SqlKeywords.ROW, SqlKeywords.DELETE); 191 addTriggerInfo(triggerInfoList, triggerCharacteristics.getAfterInsertTriggers(), SqlKeywords.AFTER, SqlKeywords.ROW, SqlKeywords.INSERT); 192 addTriggerInfo(triggerInfoList, triggerCharacteristics.getAfterUpdateTriggers(null), SqlKeywords.AFTER, SqlKeywords.ROW, SqlKeywords.UPDATE); 193 addTriggerInfo(triggerInfoList, triggerCharacteristics.getBeforeDeleteTriggers(), SqlKeywords.BEFORE, SqlKeywords.ROW, SqlKeywords.DELETE); 194 addTriggerInfo(triggerInfoList, triggerCharacteristics.getBeforeInsertTriggers(), SqlKeywords.BEFORE, SqlKeywords.ROW, SqlKeywords.INSERT); 195 addTriggerInfo(triggerInfoList, triggerCharacteristics.getBeforeUpdateTriggers(null), SqlKeywords.BEFORE, SqlKeywords.ROW, SqlKeywords.UPDATE); 196 triggerCharacteristics = dataDictionary.getDDSTriggerOperation().getTriggerCharacteristics(tableName, false); 197 addTriggerInfo(triggerInfoList, triggerCharacteristics.getAfterDeleteTriggers(), SqlKeywords.AFTER, SqlKeywords.STATEMENT, SqlKeywords.DELETE); 198 addTriggerInfo(triggerInfoList, triggerCharacteristics.getAfterInsertTriggers(), SqlKeywords.AFTER, SqlKeywords.STATEMENT, SqlKeywords.INSERT); 199 addTriggerInfo(triggerInfoList, triggerCharacteristics.getAfterUpdateTriggers(null), SqlKeywords.AFTER, SqlKeywords.STATEMENT, SqlKeywords.UPDATE); 200 addTriggerInfo(triggerInfoList, triggerCharacteristics.getBeforeDeleteTriggers(), SqlKeywords.BEFORE, SqlKeywords.STATEMENT, SqlKeywords.DELETE); 201 addTriggerInfo(triggerInfoList, triggerCharacteristics.getBeforeInsertTriggers(), SqlKeywords.BEFORE, SqlKeywords.STATEMENT, SqlKeywords.INSERT); 202 addTriggerInfo(triggerInfoList, triggerCharacteristics.getBeforeUpdateTriggers(null), SqlKeywords.BEFORE, SqlKeywords.STATEMENT, SqlKeywords.UPDATE); 203 } catch (Exception e) { 204 e.printStackTrace(); 205 } 206 searchedTriggerInformation = true; 207 if (triggerInfoList.size() == 0) 208 return null; 209 triggerInformation = new TriggerInformation( (_TriggerInfo[]) triggerInfoList.toArray(new _TriggerInfo[0])); 210 return triggerInformation; 211 } 212 213 private void addTriggerInfo(ArrayList triggerInfoList, _Trigger[] triggers, String actionTime, String orentiation, String event) { 214 if (triggers == null) 215 return; 216 int len = triggers.length; 217 try { 218 for (int i = 0; i < len; i++) { 219 TriggerInfo triggerInfo = new TriggerInfo(); 220 triggerInfo.catalog = triggers[i].getQualifiedIdentifier().catalog; 221 triggerInfo.schema = triggers[i].getQualifiedIdentifier().schema; 222 triggerInfo.name = triggers[i].getQualifiedIdentifier().getName(); 223 triggerInfo.actionTime = actionTime; 224 triggerInfo.triggerEvent = event; 225 triggerInfo.actionOrentiation = orentiation; 226 com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.booleanvalueexpression bve = triggers[i].getSearchCondition(); 227 triggerInfo.whenCondition = bve == null ? "" : bve.toString(); 228 com.daffodilwoods.daffodildb.server.sql99.SQLexecutablestatement[] statements = triggers[i].getStatement(); 229 String statementClause = ""; 230 if (statements != null) { 231 for (int j = 0; j < statements.length; j++) 232 statementClause += statements[j].toString() + " "; 233 } 234 triggerInfo.triggerStatements = statementClause; 235 triggerInfo.oldAlias = triggers[i].getOldAliasName(); 236 triggerInfo.newAlias = triggers[i].getNewAliasName(); 237 triggerInfoList.add(triggerInfo); 238 } 239 } catch (Exception e) { 240 e.printStackTrace(); 241 } 242 } 243 244 private _CheckConstraintInfo[] getCheckConstraints() { 245 try { 246 _CheckConstraintCharacteristics checkConstraintCharacteristics = dataDictionary.getDDSConstraintsOperation().getCheckConstraintCharacteristics(tableName, false); 247 _CheckConstraint[] constraints = checkConstraintCharacteristics.getCheckConstraintsForDelete(); 248 CheckConstraintInfo[] nonDeferableConstraintInfo = getCheckConstraintInfo(constraints, "NO"); 249 checkConstraintCharacteristics = dataDictionary.getDDSConstraintsOperation().getCheckConstraintCharacteristics(tableName, true); 250 constraints = checkConstraintCharacteristics.getCheckConstraintsForDelete(); 251 CheckConstraintInfo[] deferableConstraintInfo = getCheckConstraintInfo(constraints, "YES"); 252 if (deferableConstraintInfo == null && nonDeferableConstraintInfo == null) 253 return null; 254 int noOfdefereableConstraint = deferableConstraintInfo == null ? 0 : deferableConstraintInfo.length; 255 int noOfnondefereableConstraint = nonDeferableConstraintInfo == null ? 0 : nonDeferableConstraintInfo.length; 256 CheckConstraintInfo[] constraintInfo = new CheckConstraintInfo[noOfdefereableConstraint + noOfnondefereableConstraint]; 257 if (noOfdefereableConstraint != 0) 258 System.arraycopy(deferableConstraintInfo, 0, constraintInfo, 0, noOfdefereableConstraint); 259 if (noOfnondefereableConstraint != 0) 260 System.arraycopy(nonDeferableConstraintInfo, 0, constraintInfo, noOfdefereableConstraint, noOfnondefereableConstraint); 261 return (_CheckConstraintInfo[]) constraintInfo; 262 } catch (DException ex) { 263 throw new RuntimeException (ex.getMessage()); 264 } 265 } 266 267 private CheckConstraintInfo[] getCheckConstraintInfo(_CheckConstraint[] constraints, String deferrable) throws DException { 268 try { 269 if (constraints == null) 270 return null; 271 int len = constraints.length; 272 CheckConstraintInfo[] checkInfo = new CheckConstraintInfo[len]; 273 for (int i = 0; i < len; i++) { 274 checkInfo[i] = new CheckConstraintInfo(); 275 checkInfo[i].name = constraints[i].getQualifiedIdentifier().getName(); 276 checkInfo[i].deferrable = deferrable; 277 checkInfo[i].checkCondition = constraints[i].getCondition().toString(); 278 } 279 return checkInfo; 280 } catch (DException ex) { 281 throw new DException(ex.getDseCode(), ex.getParameters()); 282 } 283 } 284 285 private _UniqueConstraintInfo[] getPrimaryAndUniqueConstraints() { 286 try { 287 _PrimaryAndUniqueConstraintCharacteristics constraintCharacteristics = dataDictionary.getDDSConstraintsOperation().getPrimaryAndUniqueConstraintCharacteristics(tableName, true); 288 _UniqueConstraint[] constraints = constraintCharacteristics.getConstraints(); 289 _UniqueConstraintInfo[] deferableConstraintInfo = getUniqueConstraintInfo(constraints, "YES"); 290 constraintCharacteristics = dataDictionary.getDDSConstraintsOperation().getPrimaryAndUniqueConstraintCharacteristics(tableName, false); 291 constraints = constraintCharacteristics.getConstraints(); 292 _UniqueConstraintInfo[] nonDeferableConstraintInfo = getUniqueConstraintInfo(constraints, "NO"); 293 if (deferableConstraintInfo == null && nonDeferableConstraintInfo == null) 294 return null; 295 int noOfdefereableConstraint = deferableConstraintInfo == null ? 0 : deferableConstraintInfo.length; 296 int noOfnondefereableConstraint = nonDeferableConstraintInfo == null ? 0 : nonDeferableConstraintInfo.length; 297 _UniqueConstraintInfo[] constraintInfo = new _UniqueConstraintInfo[noOfdefereableConstraint + noOfnondefereableConstraint]; 298 if (noOfdefereableConstraint != 0) 299 System.arraycopy(deferableConstraintInfo, 0, constraintInfo, 0, noOfdefereableConstraint); 300 if (noOfnondefereableConstraint != 0) 301 System.arraycopy(nonDeferableConstraintInfo, 0, constraintInfo, noOfdefereableConstraint, noOfnondefereableConstraint); 302 return constraintInfo; 303 } catch (Exception ex) { 304 return null; 305 } 306 } 307 308 private _UniqueConstraintInfo[] getUniqueConstraintInfo(_UniqueConstraint[] constraints, String deferrable) throws com.daffodilwoods.database.resource.DException { 309 if (constraints == null) 310 return null; 311 int len = constraints.length; 312 UniqueConstraintInfo[] uniqueInfo = new UniqueConstraintInfo[len]; 313 for (int i = 0; i < len; i++) { 314 uniqueInfo[i] = new UniqueConstraintInfo(); 315 uniqueInfo[i].name = constraints[i].getQualifiedIdentifier().getName(); 316 uniqueInfo[i].columns = constraints[i].getColumnNames(); 317 uniqueInfo[i].type = constraints[i].getType(); 318 uniqueInfo[i].deferrable = deferrable; 319 } 320 return (_UniqueConstraintInfo[]) uniqueInfo; 321 } 322 323 private _ReferentialConstraintInfo[] getReferencingConstraints() { 324 try { 325 _ReferencingConstraintCharacteristics constraintCharacteristics = dataDictionary.getDDSConstraintsOperation().getReferencingConstraintCharacteristics(tableName, true); 326 _ReferentialConstraintInfo[] deferableConstraintInfo = getReferentialInfo(constraintCharacteristics.getReferencingConstraints(), "YES", "Foreign/Referencing"); 327 constraintCharacteristics = dataDictionary.getDDSConstraintsOperation().getReferencingConstraintCharacteristics(tableName, false); 328 _ReferentialConstraintInfo[] nonDeferableConstraintInfo = getReferentialInfo(constraintCharacteristics.getReferencingConstraints(), "NO", "Foreign/Referencing"); 329 return mergerReferentialInfo(deferableConstraintInfo, nonDeferableConstraintInfo); 330 } catch (DException ex) { 331 throw new RuntimeException (ex.getMessage()); 332 } 333 } 334 335 private _ReferentialConstraintInfo[] mergerReferentialInfo(_ReferentialConstraintInfo[] deferableConstraintInfo, _ReferentialConstraintInfo[] nonDeferableConstraintInfo) { 336 if (deferableConstraintInfo == null && nonDeferableConstraintInfo == null) 337 return null; 338 int noOfdefereableConstraint = deferableConstraintInfo == null ? 0 : deferableConstraintInfo.length; 339 int noOfnondefereableConstraint = nonDeferableConstraintInfo == null ? 0 : nonDeferableConstraintInfo.length; 340 _ReferentialConstraintInfo[] constraintInfo = new _ReferentialConstraintInfo[noOfdefereableConstraint + noOfnondefereableConstraint]; 341 if (noOfdefereableConstraint != 0) 342 System.arraycopy(deferableConstraintInfo, 0, constraintInfo, 0, noOfdefereableConstraint); 343 if (noOfnondefereableConstraint != 0) 344 System.arraycopy(nonDeferableConstraintInfo, 0, constraintInfo, noOfdefereableConstraint, noOfnondefereableConstraint); 345 return (_ReferentialConstraintInfo[]) constraintInfo; 346 } 347 348 public _ReferentialConstraintInfo[] getReferencedConstraints() { 349 try { 350 _ReferencedConstraintCharacteristics constraintCharacteristics = dataDictionary.getDDSConstraintsOperation().getReferencedConstraintCharacteristics(tableName, true); 351 _ReferentialConstraintInfo[] deferableConstraintInfo = getReferentialInfo(constraintCharacteristics.getReferencedConstraintsForDelete(), "YES", "Foreign/Referenced"); 352 constraintCharacteristics = dataDictionary.getDDSConstraintsOperation().getReferencedConstraintCharacteristics(tableName, false); 353 _ReferentialConstraintInfo[] nonDeferableConstraintInfo = getReferentialInfo(constraintCharacteristics.getReferencedConstraintsForDelete(), "NO", "Foreign/Referenced"); 354 return mergerReferentialInfo(deferableConstraintInfo, nonDeferableConstraintInfo); 355 } catch (DException ex) { 356 throw new RuntimeException (ex.getMessage()); 357 } 358 } 359 360 private _ReferentialConstraintInfo[] getReferentialInfo(_ReferentialConstraint[] constraints, String deferrable, String type) throws DException { 361 try { 362 if (constraints == null) 363 return null; 364 int len = constraints.length; 365 ReferentialConstraintInfo[] referentialInfo = new ReferentialConstraintInfo[len]; 366 for (int i = 0; i < len; i++) { 367 referentialInfo[i] = new ReferentialConstraintInfo(); 368 referentialInfo[i].name = constraints[i].getQualifiedIdentifier().getName(); 369 referentialInfo[i].referencedTable = constraints[i].getReferencedTable().getIdentifier(); 370 referentialInfo[i].referencedColumns = constraints[i].getReferencedColumnNames(); 371 referentialInfo[i].matchOption = getMatchOption(constraints[i].getMatch_Option()); 372 referentialInfo[i].updateRule = getRuleName(constraints[i].getUpdate_Rule()); 373 referentialInfo[i].deleteRule = getRuleName(constraints[i].getDelete_Rule()); 374 referentialInfo[i].deferrable = deferrable; 375 referentialInfo[i].type = type; 376 referentialInfo[i].referencingColumns = (String []) ( (ReferentialConstraintDescriptor) constraints[i]).tableConstraintDescriptor.getReferencingColumns().toArray(new String [0]); 377 } 378 return referentialInfo; 379 } catch (DException ex) { 380 throw new DException(ex.getDseCode(), ex.getParameters()); 381 } 382 } 383 384 private String getMatchOption(int match_option) { 385 if (match_option == TypeConstant.Match_Simple) 386 return "Simple"; 387 else if (match_option == TypeConstant.Match_Partial) 388 return "Partial"; 389 else if (match_option == TypeConstant.Match_Full) 390 return "Full"; 391 return ""; 392 } 393 394 private String getRuleName(int rule) { 395 if (rule == TypeConstant.CASCADE) 396 return "Cascade"; 397 else if (rule == TypeConstant.SETNULL) 398 return "Set Null"; 399 else if (rule == TypeConstant.SETDEFAULT) 400 return "Set Default"; 401 else if (rule == TypeConstant.RESTRICT) 402 return "Restrict"; 403 return ""; 404 } 405 406 public String getName() { 407 return tableName.name; 408 } 409 410 public String getCatalog() { 411 return tableName.catalog; 412 } 413 414 public String getSchema() { 415 return tableName.schema; 416 } 417 418 public String getType() { 419 return type; 420 } 421 422 public String getRemarks() { 423 return remarks; 424 } 425 } 426 | Popular Tags |