1 package com.daffodilwoods.daffodildb.server.sql99.dql.resultsetmetadata; 2 3 import java.util.*; 4 5 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*; 6 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*; 7 import com.daffodilwoods.daffodildb.server.sql99.common.*; 8 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*; 9 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*; 10 import com.daffodilwoods.daffodildb.utils.comparator.*; 11 import com.daffodilwoods.daffodildb.utils.field.*; 12 import com.daffodilwoods.database.general.*; 13 import com.daffodilwoods.database.general.TypeConverter; 14 import com.daffodilwoods.database.resource.*; 15 import com.daffodilwoods.database.utility.*; 16 17 29 30 public class AllColumnRowReader implements _AllColumnRowReader, _RowReader { 31 32 35 36 protected int[] selectColumnMapping; 37 38 41 42 protected int[] keyColumnMapping; 43 44 48 49 protected Object [][] tableDetailsCCMapping; 51 54 55 protected int allColumnLength; 56 57 60 61 protected Object [][] functionalKeyColumnMapping; 62 63 66 67 protected _ColumnCharacteristics selectColumnCharacteristics; 68 69 72 73 protected SelectKeyColumnInformation selectKeyColumnInformation; 74 75 78 79 protected String firstName; 80 81 84 85 protected ForeignKeyColumns[] columns; 86 87 90 91 protected _KeyColumnInformation[] keyColumnInformation; 92 93 96 97 protected ColumnDetails[] selectListColumnDetails; 98 99 113 public AllColumnRowReader(ColumnDetails[] selectColumnDetails, _KeyColumnInformation[] keyColumnInformation, Object [][] functionalKeyColumnMapping1, Object [][] tableDetailsCCMapping1, int allColumnLength1, _ColumnCharacteristics selectColumnCharacteristics1, ForeignKeyColumns[] columns0, boolean isAggregateAndNoGroup) throws DException { 114 tableDetailsCCMapping = tableDetailsCCMapping1; 115 selectListColumnDetails = selectColumnDetails; 116 functionalKeyColumnMapping = functionalKeyColumnMapping1; 117 this.keyColumnInformation = keyColumnInformation; 118 initializeSelectColumnMapping(selectColumnDetails, functionalKeyColumnMapping, tableDetailsCCMapping); 119 120 initializeKeyColumnMapping(keyColumnInformation, functionalKeyColumnMapping, tableDetailsCCMapping); 121 122 allColumnLength = allColumnLength1; 123 selectColumnCharacteristics = selectColumnCharacteristics1; 124 selectKeyColumnInformation = new SelectKeyColumnInformation(keyColumnInformation, keyColumnMapping, isAggregateAndNoGroup); 125 columns = columns0; 126 } 127 128 140 141 public AllColumnRowReader(_KeyColumnInformation[] keyColumnInformation1, Object [][] functionalKeyColumnMapping1, Object [][] tableDetailsCCMapping1, int allColumnLength1, _ColumnCharacteristics selectColumnCharacteristics1, String firstName1, boolean isAggregateAndNoGroup) throws DException { 142 tableDetailsCCMapping = tableDetailsCCMapping1; 143 functionalKeyColumnMapping = functionalKeyColumnMapping1; 144 keyColumnInformation = keyColumnInformation1; 145 initializeKeyColumnMapping(keyColumnInformation1, functionalKeyColumnMapping1, tableDetailsCCMapping1); 146 allColumnLength = allColumnLength1; 147 selectColumnCharacteristics = selectColumnCharacteristics1; 148 selectKeyColumnInformation = new SelectKeyColumnInformation(keyColumnInformation, keyColumnMapping, isAggregateAndNoGroup); 149 firstName = firstName1; 150 } 151 152 158 159 public Object getKey(Object row) throws DException { 160 return selectKeyColumnInformation.getKey(row); 161 } 162 163 169 170 protected int[] getIntArrayFromList(ArrayList aList) { 171 Object [] object = aList.toArray(); 172 int length = object.length; 173 int[] result = new int[length]; 174 for (int i = 0; i < length; i++) { 175 result[i] = object[i].hashCode(); 176 } 177 return result; 178 } 179 180 186 187 private boolean[] getOrderSpecification(_KeyColumnInformation[] keyColumnInformation) throws DException { 188 int length = keyColumnInformation.length; 189 boolean[] result = new boolean[length]; 190 for (int i = 0; i < length; i++) { 191 result[i] = keyColumnInformation[i].getOrderSpecification(); 192 } 193 return result; 194 } 195 196 205 206 private void initializeSelectColumnMapping(ColumnDetails[] selectColumnDetails, Object [][] functionalKeyColumnMapping, Object [][] tableDetailsCCMapping) throws DException { 207 int length = selectColumnDetails.length; 208 selectColumnMapping = new int[length]; 209 for (int i = 0; i < length; i++) { 210 try { 211 TableDetails tableDetails = selectColumnDetails[i].getTableDetails(); 212 selectColumnMapping[i] = (tableDetails == null || 213 selectColumnDetails[i].getTableAliasArray() != null) ? 214 getIndexFromFunctionalMapping(selectColumnDetails[i], 215 functionalKeyColumnMapping) 216 : getIndexFromCC(selectColumnDetails[i], tableDetails); 217 } catch (DException ex) { 218 throw ex; 219 } 220 } 221 } 222 223 232 233 private void initializeKeyColumnMapping(_KeyColumnInformation[] keyColumnInformation, Object [][] functionalKeyColumnMapping, Object [][] tableDetailsCCMapping) throws DException { 234 int length = keyColumnInformation.length; 235 keyColumnMapping = new int[length]; 236 for (int i = 0; i < length; i++) { 237 ColumnDetails columnDetails = keyColumnInformation[i].getColumnDetails(); 238 TableDetails tableDetail = columnDetails.getTableDetails(); 239 keyColumnMapping[i] = (tableDetail == null || columnDetails.getTableAliasArray() != null) ? 240 getIndexFromFunctionalMapping(columnDetails, functionalKeyColumnMapping) 241 : getIndexFromCC(columnDetails, tableDetail); 242 } 243 } 244 245 255 256 private int getIndexFromCCActual(ColumnDetails columnDetails, TableDetails tableDetails) throws DException { 257 if (tableDetails == null || columnDetails.getTableAliasArray() != null) { 258 return getIndexFromFunctionalMapping(columnDetails, functionalKeyColumnMapping); 259 } 260 int length = tableDetailsCCMapping.length; for (int i = 0; i < length; i++) { 262 if (tableDetails == tableDetailsCCMapping[i][0]) { 263 return ( (_ColumnCharacteristics) tableDetailsCCMapping[i][1]).getColumnIndex(columnDetails.getOriginalColumn()); 264 } 265 } 266 throw new DException("DSE3514", new Object [] {columnDetails.getColumn(), tableDetails.getNameOfTable()}); 267 } 268 269 278 279 private int getIndexFromCC(ColumnDetails columnDetails, TableDetails tableDetails) throws DException { 280 281 int length = tableDetailsCCMapping.length; QualifiedIdentifier tableName = tableDetails.getQualifiedIdentifier(); 283 for (int i = 0; i < length; i++) { 284 QualifiedIdentifier tblName = ((TableDetails)tableDetailsCCMapping[i][0]).getQualifiedIdentifier(); 285 if (tableName.equals(tblName) ){ 286 int ii = ( (_ColumnCharacteristics) tableDetailsCCMapping[i][1]).getColumnIndex(columnDetails.getColumn()) + tableDetailsCCMapping[i][2].hashCode(); 287 return ii; 288 } 289 } 290 throw new DException("DSE3514", new Object [] {columnDetails.getColumn(), tableDetails.getNameOfTable()}); 291 292 } 293 294 302 303 private int getIndexFromFunctionalMapping(ColumnDetails columnDetails, Object [][] functionalKeyColumnMapping) throws DException { 304 int length = functionalKeyColumnMapping.length; 305 for (int i = 0; i < length; i++) { 306 if (columnDetails.getAppropriateColumn().equalsIgnoreCase( ( (ColumnDetails) functionalKeyColumnMapping[i][0]).getAppropriateColumn())) { 307 return functionalKeyColumnMapping[i][1].hashCode(); 308 } 309 } 310 throw new DException("DSE3515", new Object [] {columnDetails.getColumn()}); 311 } 312 313 320 321 public Object getBlankRow() throws DException { 322 return new Object [allColumnLength]; 323 } 324 325 332 333 public void setObject(int columnIndex, Object value, Object row) throws DException { 334 335 if (value instanceof FieldBase) { 336 ( (Object []) row)[selectColumnMapping[columnIndex - 1] - 1] = value; 337 } else { 338 ( (FieldBase) ( (Object []) row)[selectColumnMapping[columnIndex - 1] - 1]).setObject(value); 339 } 340 341 } 342 343 350 351 public void setObject(String columnName, Object value, Object row) throws DException { 352 int columnIndex = selectColumnCharacteristics.getColumnIndex(columnName); 353 setObject(columnIndex, value, row); 354 } 355 356 365 366 public void setObject(_TableInfo tableInfo, int columnIndex, Object value, Object row) throws DException { 367 int index = getStartingIndex(tableInfo) + columnIndex; 368 if (value instanceof FieldBase) { 369 ( (Object []) row)[index - 1] = value; 370 } else { 371 ( (FieldBase) ( (Object []) row)[index - 1]).setObject(value); 372 } 373 } 374 375 382 383 public void setObjects(Object oldRow, Object newRow, int[] columnIndexes) throws DException { 384 for (int i = 0; i < columnIndexes.length; i++) { 385 int columnIndex = selectColumnMapping[columnIndexes[i] - 1] - 1; 386 ( (Object []) oldRow)[columnIndex] = ( (Object []) newRow)[columnIndex]; 387 } 388 } 389 390 399 400 public Object getObject(_TableInfo tableInfo, int columnIndex, Object row) throws DException { 401 int index = getStartingIndex(tableInfo) + columnIndex; 402 return ( (Object []) row)[index - 1]; 403 } 404 405 414 415 public Object getObject(_TableInfo tableInfo, String columnName, Object row) throws DException { 416 417 int index = getStartingIndex(tableInfo); 418 TableDetails td = getTableDetail(tableInfo); 419 index += td.getColumnCharacteristics().getColumnIndex(columnName); 420 return ( (Object []) row)[index - 1]; 421 422 } 423 424 431 432 private int getStartingIndex(_TableInfo tableInfo) throws DException { 433 int length = tableDetailsCCMapping.length; 434 for (int i = 0; i < length; i++) { 435 TableDetails table = (TableDetails) tableDetailsCCMapping[i][0]; 436 if (table.hashCode() == tableInfo.getUniqueID()) { 437 return tableDetailsCCMapping[i][2].hashCode(); 438 } 439 } 440 throw new DException("DSE3516", new Object [] {tableInfo}); 441 } 442 443 449 450 private TableDetails getTableDetail(_TableInfo tableInfo) throws DException { 451 452 int length = tableDetailsCCMapping.length; 453 for (int i = 0; i < length; i++) { 454 TableDetails td = ( (TableDetails) tableDetailsCCMapping[i][0]); 455 if (td.hashCode() == tableInfo.getUniqueID()) { 456 return td; 457 } 458 } 459 throw new DException("DSE3516", new Object [] {tableInfo}); 460 461 } 462 463 469 470 471 public SuperComparator getComparator() throws DException { 472 return selectKeyColumnInformation.getComparator(); 473 } 474 475 480 481 public int[] getPrimaryConditionColumns() throws DException { 482 return selectColumnCharacteristics.getPrimaryConditionColumns(); 483 } 484 485 491 492 public Object [] getPrimaryKeyConditionalColumnValues(Object [] row) throws DException { 493 int[] primaryIndexes = selectColumnCharacteristics.getPrimaryConditionColumns(); 494 int length = primaryIndexes.length; 495 Object [] values = new Object [length]; 496 for (int i = 0; i < length; i++) { 497 values[i] = row[primaryIndexes[i] - 1]; 498 } 499 return values; 500 } 501 502 507 508 public int[] getKeyColumnIndexes() throws DException { 509 return keyColumnMapping; 510 } 511 512 517 518 public String getFirstTableName() throws DException { 519 throw new UnsupportedOperationException ("Method getFirstTableName() Not Supported"); 520 } 521 522 530 531 public Object getObject(int index, Object row) throws DException { 532 try { 533 Object o = ( (Object []) row)[selectColumnMapping[index - 1] - 1]; 534 if (o instanceof FieldBase) { 535 o = ( (FieldBase) o).getObject(); 536 } 537 return o; 538 } catch (ArrayIndexOutOfBoundsException ex) { 539 throw ex; 540 } 541 } 542 543 549 550 public int getIndexOfFunctionalColumn(ColumnDetails column) throws DException { 551 return getIndexFromFunctionalMapping(column, functionalKeyColumnMapping); 552 } 553 554 560 561 public Object getRowClone(Object row) throws DException { 562 Object [] arr = (Object []) row; 563 Object [] clonedRow = new Object [arr.length]; 564 System.arraycopy(arr, 0, clonedRow, 0, arr.length); 565 return clonedRow; 566 } 567 568 573 574 public String [] getKeyColumns() throws DException { 575 ArrayList keyColumns = new ArrayList(); 576 int len = keyColumnInformation.length; 577 for (int j = 0; j < len; j++) { 578 keyColumns.add(keyColumnInformation[j].getUniqueQualifiedName()); 579 } 580 return (String []) keyColumns.toArray(new String [0]); 581 } 582 583 590 591 public String getColumnName(_TableInfo tableInfo, int index) throws DException { 592 return getTableDetail(tableInfo).getColumnCharacteristics().getColumnName(index); 593 } 594 595 601 602 public void updateHasRecordColumns(Object row, Object value) throws DException { 603 for (int i = 0, length = selectListColumnDetails.length; i < length; i++) { 604 if (selectListColumnDetails[i].getType() == ColumnDetails.HAS_RECORD) { 605 setObject(i, value, row); 606 } 607 } 608 } 609 610 614 615 public int[] getSelectedColumnMapping() { 616 return selectColumnMapping; 617 } 618 619 622 623 public String getQueryForTable(QualifiedIdentifier tableName) throws DException { 624 StringBuffer query = new StringBuffer (); 625 query.append("Create table \"").append(tableName.catalog) 626 .append("\".\"").append(tableName.schema) 627 .append("\".\"").append(tableName.name).append("\" ( "); 628 int len = tableDetailsCCMapping.length; 629 for (int j = 0; j < len; j++) { 630 String tableNm = ( (TableDetails) tableDetailsCCMapping[j][0]).getUnderscoreTableName(); 631 _ColumnCharacteristics cc = (_ColumnCharacteristics) tableDetailsCCMapping[j][1]; 632 int columnCount = cc.getColumnCount(); 633 for (int i = 0; i < columnCount; i++) { 634 if (i != 0 || j != 0) { 635 query.append(","); 636 } 637 String column = cc.getColumnName(i); 638 int type = cc.getColumnType(i); 639 int size = cc.getSize(i); 640 int precision = cc.getPrecision(i); 641 int scale = cc.getScale(i); 642 String subString = getColumnDefinition(tableNm, column, type, size, precision, scale); 643 query.append(subString); 644 } 645 } 646 if (functionalKeyColumnMapping != null) { 647 for (int i = 0; i < functionalKeyColumnMapping.length; i++) { 648 query.append(" , "); 649 ColumnDetails fnCD = (ColumnDetails) functionalKeyColumnMapping[i][0]; 650 String name = fnCD.getAliasName(); 651 name = name == null ? fnCD.getAppropriateColumn() : name; 652 int type = fnCD.getDatatype(); 653 int size = fnCD.getSize(); 654 String functionalColumnDefinition = getColumnDefinition(null, name, type, size, -1, -1); 655 query.append(functionalColumnDefinition); 656 } 657 } 658 query.append(" ) "); 659 660 return query.toString(); 661 } 662 663 682 private String getColumnDefinition(String table_name, String column_name, int type, int size, int precision, int scale) throws DException { 683 String columnDefinition = table_name != null ? 684 table_name + "_" + column_name : 685 column_name; 686 columnDefinition = "\"" + columnDefinition + "\" " + TypeConverter.getDataBaseTypeName(type); 687 if (type == Datatype.CHAR || type == Datatype.CHARACTER || 688 type == Datatype.BINARY || type == Datatype.BIT || 689 type == Datatype.VARCHAR || type == Datatype.CHARACTERVARYING || 690 type == Datatype.CHARVARYING || type == Datatype.VARBINARY || 691 type == Datatype.BITVARYING) { 692 columnDefinition += "(" + size + ")"; 693 694 } 695 if (precision != -1) { 696 if (type == Datatype.NUMERIC || type == Datatype.DEC || type == Datatype.DECIMAL || 697 type == Datatype.FLOAT || type == Datatype.BIGDECIMAL || type == Datatype.DOUBLE) { 698 columnDefinition += "(" + precision; 699 if (scale != -1) { 700 columnDefinition += "," + scale; 701 } 702 columnDefinition += ")"; 703 } 704 } 705 return columnDefinition; 706 } 707 708 } 709 | Popular Tags |