1 package com.daffodilwoods.daffodildb.server.sql99.dql.resultsetmetadata; 2 3 import java.text.*; 4 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*; 5 import com.daffodilwoods.daffodildb.server.sql99.common.*; 6 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*; 7 import com.daffodilwoods.database.general.*; 8 import com.daffodilwoods.database.resource.*; 9 import com.daffodilwoods.daffodildb.server.sql99.expression.valueexpression; 10 import com.daffodilwoods.database.utility.P; 11 import com.daffodilwoods.daffodildb.server.sql99.dql.queryexpression.queryspecification.selectsublist; 12 13 26 27 public class SelectColumnCharacteristics implements _ColumnCharacteristics, _SelectColumnCharacteristics, TypeConstants { 28 29 32 33 private Object [][] tableDetailsCCMapping; 34 35 38 39 private ColumnDetails[] columnDetails; 40 41 44 45 private String [] groupByPrimaryColumns; 46 47 50 51 private _RowReader rowReader; 52 53 57 58 private boolean isDistinctOrGroup; 59 60 63 64 private int[] indexes; 65 66 71 72 private boolean allOrSelected; 73 74 77 78 private ForeignKeyColumns[] foreignKeyColumns = null; 79 80 86 public SelectColumnCharacteristics(ColumnDetails[] columnDetails1, Object [][] tableDetailsCCMapping1, boolean allOrSelected1) { 87 tableDetailsCCMapping = tableDetailsCCMapping1; 88 columnDetails = columnDetails1; 89 allOrSelected = allOrSelected1; 90 try { 91 for (int i = 0; i < columnDetails.length; i++) { 92 int type = columnDetails[i].getType(); 93 if ( (columnDetails[i].getAliasName() == null) && 94 (type == SCALARFUNCTION || type == CASEEXPRESSION || 95 type == FUNCTIONAL || type == GROUPING || type == USERFUNCTION)) { 96 97 columnDetails[i].setOriginalColumnName("Expr" + i); 98 } 99 } 100 } catch (DException de) { 101 } 102 103 } 104 105 113 public SelectColumnCharacteristics(ColumnDetails[] columnDetails1, Object [][] tableDetailsCCMapping1, String [] groupByPrimaryColumns1, boolean isDistinctOrGroup1, boolean allOrSelected1) { 114 tableDetailsCCMapping = tableDetailsCCMapping1; 115 columnDetails = columnDetails1; 116 groupByPrimaryColumns = groupByPrimaryColumns1; 117 allOrSelected = allOrSelected1; 118 isDistinctOrGroup = isDistinctOrGroup1; 119 try { 120 for (int i = 0; i < columnDetails.length; i++) { 121 int type = columnDetails[i].getType(); 122 if ( (columnDetails[i].getAliasName() == null) && (type == SCALARFUNCTION || type == CASEEXPRESSION || type == FUNCTIONAL || type == GROUPING || type == USERFUNCTION)) { 123 columnDetails[i].setAliasName("Expr" + i); 124 } 125 } 126 getPrimaryConditionColumns(); 127 } catch (DException de) { 128 } 129 } 130 131 138 139 public int getColumnType(int index) throws DException { 140 return columnDetails[index - 1].getDatatype(); 141 } 142 143 149 150 public int getColumnIndex(String columnName) throws DException { 151 int length = columnDetails.length; 152 for (int i = 0; i < length; i++) { 153 if (columnDetails[i].getAppropriateColumn().equalsIgnoreCase(columnName)) { 154 return i + 1; 155 } 156 } 157 throw new DException("DSE3517", new Object [] {columnName}); 158 } 159 160 166 167 private String getQualifiedColumnName(int index) throws DException { 168 return columnDetails[index - 1].getQualifiedColumnName(); 169 } 170 171 178 179 public String getColumnName(int index) throws DException { 180 String aliasName = columnDetails[index - 1].getAliasName(); 181 return (aliasName == null || aliasName.length() == 0) ? columnDetails[index - 1].getOriginalColumn() : aliasName; 182 } 183 184 191 192 public _ColumnCharacteristics getColumnCharacteristics(TableDetails tableDetails) throws DException { 193 if (tableDetails == null) { 194 return null; 195 } 196 int length = tableDetailsCCMapping.length; 197 for (int i = 0; i < length; i++) { 198 if (tableDetails == (TableDetails) tableDetailsCCMapping[i][0]) { 199 return (_ColumnCharacteristics) tableDetailsCCMapping[i][1]; 200 } 201 } 202 throw new DException("DSE3516", new Object [] {tableDetails.getNameOfTable()}); 203 } 204 205 213 214 public String getRelatedTable(int index) throws DatabaseException, DException { 215 TableDetails tableDetails = columnDetails[index - 1].getTable(); 216 if (tableDetails == null || columnDetails[index - 1].getType() == ColumnDetails.HAS_RECORD) { 217 return null; 218 } 219 _ColumnCharacteristics cc = columnDetails[index - 1].getColumnCharacteristics(); 220 return cc.getRelatedTable(getIndex(cc, columnDetails[index - 1])); 221 } 222 223 229 230 public int getColumnCount() throws DException { 231 return columnDetails.length; 232 } 233 234 239 240 public String [] getColumnNames() throws DException { 241 int length = columnDetails.length; 242 String [] names = new String [length]; 243 for (int i = 0; i < length; i++) { 244 String aliasName = columnDetails[i].getAliasName(); 245 names[i] = (aliasName == null || aliasName.length() == 0) ? columnDetails[i].getOriginalColumn() : aliasName; 246 names[i] = names[i] == null ? names[i] : columnDetails[i].getAppropriateColumn(); 247 } 248 return names; 249 } 250 251 257 258 public int getSize(int index) throws DException { 259 return columnDetails[index - 1].getSize(); 260 } 261 262 270 271 public String getRelation(int index) throws DatabaseException, DException { 272 ColumnDetails cd = columnDetails[index - 1]; 273 TableDetails tableDetails = cd.getTable(); 274 if (tableDetails == null || cd.getType() == ColumnDetails.HAS_RECORD) { 275 return null; 276 } 277 _ColumnCharacteristics cc = cd.getColumnCharacteristics(); 278 return cc.getRelation(getIndex(cc, cd)); 279 } 280 281 287 288 public int[] getColumnIndexes(String [] parm1) throws DException { 289 int length = parm1.length; 290 int[] result = new int[length]; 291 for (int i = 0; i < length; i++) { 292 result[i] = getColumnIndex(parm1[i]); 293 } 294 return result; 295 } 296 297 303 304 public int[] getPrimaryConditionColumns() throws DException { 305 if (!allOrSelected) { 306 throw new DException("DSE3513", null); 307 } 308 if (indexes == null) { 309 int length = tableDetailsCCMapping.length; 310 for (int i = 0; i < length; i++) { 311 _ColumnCharacteristics columnCharacteristics = (_ColumnCharacteristics) tableDetailsCCMapping[i][1]; 312 int[] indexesOfTable = columnCharacteristics.getPrimaryConditionColumns(); 313 int startingIndex = -1; 314 startingIndex = tableDetailsCCMapping[i][2].hashCode(); 315 indexes = indexes == null ? addStartingIndex(indexesOfTable, startingIndex) : 316 constructNewArray(indexes, addStartingIndex(indexesOfTable, startingIndex)); 317 } 318 } 319 return indexes; 320 } 321 322 329 330 private int[] constructNewArray(int[] OldIndexes, int[] newTableIndexes) throws DException { 331 int lengthOfOld = OldIndexes.length, lengthOfNew = newTableIndexes.length; 332 int[] newIndexes = new int[lengthOfOld + lengthOfNew]; 333 System.arraycopy(OldIndexes, 0, newIndexes, 0, lengthOfOld); 334 System.arraycopy(newTableIndexes, 0, newIndexes, lengthOfOld, lengthOfNew); 335 return newIndexes; 336 } 337 338 346 347 private int[] addStartingIndex(int[] indexes, int startingIndex) throws DException { 348 int length = indexes.length; 349 int[] indexesTemp = new int[length]; 350 for (int i = 0; i < length; i++) { 351 indexesTemp[i] = indexes[i] + startingIndex; 352 } 353 return indexesTemp; 354 } 355 356 365 366 public String getTableName(int index) throws DatabaseException, DException { 367 ColumnDetails cd = columnDetails[index - 1]; 368 TableDetails tableDetails = cd.getTableForDisplay(); 369 return tableDetails == null ? null : tableDetails.getName(); 370 } 371 372 379 380 public short getTableType() throws DException { 381 return VIEW; 382 } 383 384 392 393 public int getPrecision(int index) throws DException { 394 ColumnDetails cd = columnDetails[index - 1]; 395 TableDetails tableDetails = cd.getTable(); 396 if (cd.getType() == TypeConstants.GROUPING) { 397 if (cd.toString().trim().toLowerCase().startsWith("max") || cd.toString().trim().toLowerCase().startsWith("min")) { 398 int l = ( (valueexpression) cd.getObject()).getColumnDetails()[0].getSize(); 399 return l; 400 } 401 } 402 if (cd.getType() == TypeConstants.SCALARFUNCTION) { 403 int l = ( (selectsublist) cd.getObject()).getColumnDetails()[0].getSize(); 404 return l; 405 } 406 407 if (tableDetails == null || cd.getType() == ColumnDetails.HAS_RECORD) { 408 return GeneralPurposeStaticClass.isCharacterType(cd.getType()) ?cd.getSize(): GeneralPurposeStaticClass.getPrecision(cd.getDatatype()); 409 410 } 411 _ColumnCharacteristics columnCharacteristics = cd.getColumnCharacteristics(); 412 return columnCharacteristics.getPrecision(getIndex(columnCharacteristics, cd)); 413 } 414 415 423 424 public int getScale(int index) throws DException { 425 ColumnDetails cd = columnDetails[index - 1]; 426 TableDetails tableDetails = cd.getTable(); 427 if (tableDetails == null || cd.getType() == ColumnDetails.HAS_RECORD) { 428 return 0; } 430 _ColumnCharacteristics columnCharacteristics = cd.getColumnCharacteristics(); 431 return columnCharacteristics.getScale(getIndex(columnCharacteristics, cd)); 432 } 433 434 442 443 public String getSchemaName(int index) throws DException { 444 ColumnDetails cd = columnDetails[index - 1]; 445 TableDetails tableDetails = cd.getTable(); 446 if (tableDetails == null || cd.getType() == ColumnDetails.HAS_RECORD) { 447 return null; 448 } 449 _ColumnCharacteristics columnCharacteristics = cd.getColumnCharacteristics(); 450 return columnCharacteristics.getSchemaName(getIndex(columnCharacteristics, cd)); 451 } 452 453 461 462 public String getCatalogName(int index) throws DException { 463 ColumnDetails cd = columnDetails[index - 1]; 464 TableDetails tableDetails = cd.getTable(); 465 if (tableDetails == null || cd.getType() == ColumnDetails.HAS_RECORD) { 466 return null; 467 } 468 _ColumnCharacteristics columnCharacteristics = cd.getColumnCharacteristics(); 469 return columnCharacteristics.getCatalogName(getIndex(columnCharacteristics, cd)); 470 } 471 472 479 480 public int isNullable(int index) throws DException { 481 ColumnDetails cd = columnDetails[index - 1]; 482 TableDetails tableDetails = cd.getTable(); 483 if (tableDetails == null || cd.getType() == ColumnDetails.HAS_RECORD) { 484 return -1; 485 } 486 _ColumnCharacteristics columnCharacteristics = cd.getColumnCharacteristics(); 487 return columnCharacteristics.isNullable(getIndex(columnCharacteristics, cd)); 488 } 489 490 497 498 public boolean isAutoIncrement(int index) throws DException { 499 ColumnDetails cd = columnDetails[index - 1]; 500 TableDetails tableDetails = cd.getTable(); 501 if (tableDetails == null || cd.getType() == ColumnDetails.HAS_RECORD) { 502 return false; 503 } 504 _ColumnCharacteristics columnCharacteristics = cd.getColumnCharacteristics(); 505 return columnCharacteristics.isAutoIncrement(getIndex(columnCharacteristics, cd)); 506 } 507 508 517 518 public String getColumnLabel(int index) throws DException { 519 return columnDetails[index - 1].getAppropriateColumn(); 520 } 521 522 530 531 public String getQualifiedTableName(int index) throws DException { 532 ColumnDetails cd = columnDetails[index - 1]; 533 TableDetails tableDetails = cd.getTable(); 534 if (tableDetails == null || cd.getType() == ColumnDetails.HAS_RECORD) { 535 return null; 536 } 537 return cd.getQualifiedChainedTableName(); 538 } 539 540 550 551 public boolean isColumnUpdatable(int columnIndex) throws DException { 552 int type = columnDetails[columnIndex - 1].getType(); 553 if (type == SCALARFUNCTION || type == FUNCTIONAL || type == DERIVEDCOLUMN || type == CONSTANT || type == CASEEXPRESSION || type == USERFUNCTION) { 554 return false; 555 } 556 if (columnDetails[columnIndex - 1].getTableAliasArray() != null) { 557 return true; 558 } 559 String columnName = getQualifiedColumnName(columnIndex); 560 if (exists(columnName, type)) { 561 return true; 562 } 563 return false; 564 } 565 566 572 573 public boolean isUpdatable() throws DException { 574 int length = columnDetails.length; 575 for (int i = 0; i < length; i++) { 576 int type = columnDetails[i].getType(); 577 if (type == TypeConstants.REFERENCE || type == TypeConstants.HAS_RECORD) { 578 String columnName = columnDetails[i].getStringOfColumn(); 579 if (exists(columnName, type)) { 580 return true; 581 } 582 } 583 } 584 return false; 585 } 586 587 599 600 private boolean exists(String columnName, int type) throws DException { 601 if (groupByPrimaryColumns == null) { 602 if (isDistinctOrGroup) { 603 return false; 604 } 605 return type == REFERENCE || type == HAS_RECORD || type == CONSTANT; } 607 int length = groupByPrimaryColumns.length; 608 for (int i = 0; i < length; i++) { 609 if (groupByPrimaryColumns[i].equalsIgnoreCase(columnName)) { 610 return true; 611 } 612 } 613 return false; 614 } 615 616 621 622 public void setRowReader(_RowReader rowReader0) throws DException { 623 rowReader = rowReader0; 624 } 625 626 633 634 public String getExpression(int index) throws com.daffodilwoods.database.resource.DException { 635 return columnDetails[index - 1].getExpression(); 636 } 637 638 643 644 public void setForeignKeyMapping(ForeignKeyColumns[] foreignKeyCol) throws com.daffodilwoods.database.resource.DException { 645 this.foreignKeyColumns = foreignKeyCol; 646 } 647 648 655 656 public String [] getRelatedColumns(int index) throws DException { 657 ColumnDetails cd = columnDetails[index - 1]; 658 TableDetails tableDetails = cd.getTable(); 659 if (tableDetails == null || cd.getType() == ColumnDetails.HAS_RECORD) { 660 return null; 661 } 662 _ColumnCharacteristics cc = cd.getColumnCharacteristics(); 663 return cc.getRelatedColumns(getIndex(cc, cd)); 664 } 665 666 671 672 public Collator getCollator() throws DException { 673 return null; 674 } 675 676 684 685 private int getIndex(_ColumnCharacteristics columnCharacteristics, ColumnDetails column) throws DException { 686 int index = -1; 687 try { 688 index = column.getIndex(); 689 } catch (DException ex) { 690 index = columnCharacteristics.getColumnIndex(column.getColumn()); 691 column.setIndex(index); 692 } 693 return index; 694 } 695 696 705 706 public Object [] getCorresspondingColumnsForFromSubQueryColumn(Object column) throws DException { 707 for (int i = 0, length = columnDetails.length; i < length; i++) { 708 if (columnDetails[i].getAppropriateColumn().equalsIgnoreCase( ( (ColumnDetails) column).getColumn())) { 709 return columnDetails[i].getCorresspondingColumnsInvolved(); 710 } 711 } 712 throw new DException("DSE3517", new Object [] { ( (ColumnDetails) column).getTable().getQualifiedTableName() + "." + ( (ColumnDetails) column).getAppropriateColumn()}); 713 } 714 715 723 724 public _SelectColumnCharacteristics getColumnCharacteristics(int parm1) throws com.daffodilwoods.database.resource.DException { 725 if (foreignKeyColumns == null) { 726 throw new DException("DSE3513", (Object [])null); 727 } 728 int relatedIndex = getCorrespondingIndexInMapping(parm1); 729 ForeignTableColumnCharacteristics foreignTableCharacteristics = null; 730 if (relatedIndex != -1) { 731 foreignTableCharacteristics = new ForeignTableColumnCharacteristics(foreignKeyColumns[relatedIndex].getColumns(), foreignKeyColumns[relatedIndex].getIndexes()); 732 foreignTableCharacteristics.setParentSelectColumnCharacteristics(this); 733 foreignTableCharacteristics.setColumnDetails(columnDetails[parm1 - 1]); 734 } 735 return foreignTableCharacteristics; 736 } 737 738 742 743 private int getCorrespondingIndexInMapping(int parm1) throws DException { 744 ColumnDetails column = columnDetails[parm1 - 1]; 745 String [] columnToSearch = null; 746 747 if (column.getTableAliasArray() == null) { 748 columnToSearch = new String [1]; 749 columnToSearch[0] = column.getColumn(); 750 return checkForeignRecord(columnToSearch); 751 } 752 753 String [] columnName = column.getTableAliasArray(); 754 int length = columnName.length; 755 columnToSearch = new String [length + 1]; 756 System.arraycopy(columnName, 0, columnToSearch, 0, length); 757 columnToSearch[length] = column.getColumn(); 758 return checkForeignRecord(columnToSearch); 759 } 760 761 public boolean isForeignTableRecordFetched(int parm1) throws com.daffodilwoods.database.resource.DException { 762 ColumnDetails column = columnDetails[parm1 - 1]; 763 String [] columnToSearch = null; 764 if (column.getTableAliasArray() == null) { 765 columnToSearch = new String [1]; 766 columnToSearch[0] = column.getColumn(); 767 return checkForeignRecord(columnToSearch) >= 0; 768 } 769 String [] columnName = column.getTableAliasArray(); 770 int length = columnName.length; 771 columnToSearch = new String [length + 1]; 772 System.arraycopy(columnName, 0, columnToSearch, 0, length); 773 columnToSearch[length] = column.getColumn(); 774 return checkForeignRecord(columnToSearch) >= 0; 775 } 776 777 public int checkForeignRecord(String [] columns) throws DException { 778 if (foreignKeyColumns != null) { 779 for (int i = 0, length = foreignKeyColumns.length; i < length; i++) { 780 String [] columnNames = foreignKeyColumns[i].getColumns(); 781 int lengthOfFKC = columnNames.length, lengthOFPassed = columns.length; 782 boolean found = true; 783 if (lengthOfFKC == lengthOFPassed) { 784 for (int k = 0; k < lengthOfFKC; k++) { 785 if (!columns[k].equalsIgnoreCase(columnNames[k])) { 786 found = false; 787 break; 788 } 789 } 790 if (found) { 791 return i; 792 } 793 } 794 } 795 } 796 return -1; 797 } 798 799 805 806 public String [] getPrimaryKeys() throws DatabaseException, DException { 807 throw new UnsupportedOperationException (); 808 } 809 810 public boolean isColumnSelectable(int columnIndex) throws DException { 811 812 throw new java.lang.UnsupportedOperationException ("Method isColumnSelectable() not yet implemented."); 813 } 814 815 843 public _ColumnCharacteristics getCCFromIndexes(_ColumnCharacteristics cc, int offset, int[] columnIndexes) throws DException { 844 throw new UnsupportedOperationException ("getCCFromIndexes(cc, offset, columnIndexes) method not implemented yet"); 845 } 846 } 847 | Popular Tags |