1 package com.daffodilwoods.daffodildb.server.sql99.dql.tableexpression.fromclause; 2 3 import java.util.*; 4 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*; 5 import com.daffodilwoods.daffodildb.server.serversystem.*; 6 import com.daffodilwoods.daffodildb.server.sql99.common.*; 7 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*; 8 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*; 9 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.*; 10 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.condition.*; 11 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.order.*; 12 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.table.*; 13 import com.daffodilwoods.daffodildb.server.sql99.dql.queryexpression.*; 14 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*; 15 import com.daffodilwoods.daffodildb.server.sql99.token.*; 16 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 17 import com.daffodilwoods.daffodildb.utils.parser.*; 18 import com.daffodilwoods.database.resource.*; 19 import com.daffodilwoods.database.sqlinitiator.*; 20 21 32 33 public class naturaljoin implements com.daffodilwoods.daffodildb.utils.parser.StatementExecuter, joinedtable, Datatypes, TypeConstants, queryexpressionbody, non_joinqueryterm, tablereference { 34 39 public tableprimary _tableprimary0; 40 43 public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439221; 44 47 public jointype _Optjointype2; 48 51 public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439223; 52 55 public tablereference _tablereference4; 56 57 60 private joinedtable apprpriateJoinTable; 61 64 private Object [][] commonColumns; 65 66 69 70 private TableDetails[] table1; 71 72 75 76 private TableDetails[] table2; 77 78 81 82 private TableDetails[] tableDetails; 83 86 private joinspecification joinCondition; 87 88 92 93 private boolean firstTime = true; 94 95 public naturaljoin() { 96 } 97 98 113 public _TablePlan[] getTablePlans(_ServerSession serverSession, _DatedFramework datedFrameWork, _BVEPlan bvePlan0, _OrderPlan orderPlan0, _QueryColumns queryColumns0, ConditionArray conditionArray) throws DException { 114 if (joinCondition != null && firstTime) { 115 queryColumns0.addColumnDetails(joinCondition.getColumnDetails()); 116 } 117 _TablePlan[] tp = apprpriateJoinTable.getTablePlans(serverSession, datedFrameWork, bvePlan0, orderPlan0, queryColumns0, conditionArray); 118 if (tp.length == 1 && tp[0] instanceof QualifiedFullPlan) { 119 QualifiedFullPlan qfp = (QualifiedFullPlan) tp[0]; 120 return new _TablePlan[] {new NaturalFullJoinPlan(qfp.getFirstPlan(), qfp.getSecondPlan(), qfp.getCondition(), qfp.getLeftPlan(), serverSession, commonColumns)}; 121 } 122 return tp; 123 } 124 125 126 137 public _TablePlan[] getTablePlan(_ServerSession serverSession, _DatedFramework datedFramWork, _BVEPlan bvePlan0, _OrderPlan orderPlan0, _QueryColumns queryColumns0, ConditionArray conditionArray) throws DException { 138 return getTablePlans(serverSession, datedFramWork, bvePlan0, orderPlan0, queryColumns0, conditionArray); 139 } 140 141 150 151 public _Reference[] checkSemantic(com.daffodilwoods.daffodildb.server.serversystem._ServerSession serverSession, ColumnDetails[] queryColumns, boolean checkUserRight) throws DException { 152 _Reference[] references = _tablereference4.checkSemantic(serverSession, queryColumns, checkUserRight); 153 references = GeneralPurposeStaticClass.getJointReferences(_tableprimary0.checkSemantic(serverSession, queryColumns, checkUserRight), references); 154 return references; 155 } 156 157 162 private TableDetails[] getLeftTableDetails(int joinType) { 163 return joinType == RIGHT_OUTER_JOIN ? table2 : table1; 164 } 165 166 171 private TableDetails[] getRightTableDetails(int joinType) { 172 return joinType == RIGHT_OUTER_JOIN ? table1 : table2; 173 } 174 175 187 private joinedtable getAppropraieJoinedTable(int joinType, joinspecification joinCondition, Object [][] commonColumns) throws DException { 188 return new qualifiedjoin(joinCondition, _tablereference4, _tableprimary0, _Optjointype2, table1, table2, joinType); 189 } 190 191 208 private joinspecification getConditionOfCommonColumns(Object [][] commonColumns) throws DException { 209 booleanvalueexpression joinCondition = null; 210 for (int i = 0; i < commonColumns.length; i++) { 211 ColumnDetails[] columns = (ColumnDetails[]) commonColumns[i]; 212 joinCondition = BVEPlanMerger.addAndConditions(joinCondition, BVEPlanMerger.getBooleanFactor(new NaturalJoinPredicate(columns[0], columns[1]))); 213 } 214 joincondition condition = new joincondition(joinCondition); 215 return condition; 216 } 217 218 224 private void checkForValidQueryColumns(ColumnDetails[] queryColumns) throws DException { 225 for (int i = 0; i < queryColumns.length; i++) { 226 if (queryColumns[i].getColumn().equalsIgnoreCase("*") && queryColumns[i].getColumnName().length != 1) { 227 for (int j = 0; j < tableDetails.length; j++) { 228 if (ifMatch(queryColumns[i].getColumnName(), tableDetails[j].getTableName())) { 229 throw new DException("DSE3576", new Object [] {queryColumns[i].getAppropriateQualifiedName()}); 230 } 231 } 232 } 233 } 234 } 235 236 269 private Object [][] checkForCommonColumnsAndDatatypeCompatabilityModified(TableDetails[] leftTables, TableDetails[] rightTables, ColumnDetails[] queryColumns) throws DException { 270 ArrayList commonColumns = new ArrayList(5); 271 for (int i = 0; i < leftTables.length; i++) { 272 String [] leftColumns = leftTables[i].getColumnsExcludingCommonColumns(); 273 outerLoop:for (int j = 0; j < leftColumns.length; j++) { for (int k = 0; k < rightTables.length; k++) { 275 String [] rightColumns = rightTables[k].getColumnsExcludingCommonColumns(); 276 for (int l = 0; l < rightColumns.length; l++) { 277 if (leftColumns[j].equalsIgnoreCase(rightColumns[l])) { 278 if (isDataTypeCompatibile(leftColumns[j], leftTables[i], rightTables[k])) { 279 checkForIdenticalColumns(leftColumns[j], i + 1, leftTables); 280 checkForIdenticalColumns(rightColumns[l], k + 1, rightTables); 281 checkForQualifiedName(leftColumns[j], leftTables[i], rightTables[k], queryColumns); 282 rightTables[k].addCommonColumn(rightColumns[l]); 283 ColumnDetails leftColumn = getColumnDetail(rightColumns[l], leftTables[i]); 284 ColumnDetails rightColumn = getColumnDetail(rightColumns[l], rightTables[k]); 285 commonColumns.add(new ColumnDetails[] {leftColumn, rightColumn}); 286 continue outerLoop; 287 } 288 } 289 } 290 } 291 } 292 } 293 if (commonColumns.isEmpty()) { 294 throw new DException("DSE3577", null); 295 } 296 return (Object [][]) commonColumns.toArray(new Object [commonColumns.size()][2]); 297 } 298 299 307 private boolean isDataTypeCompatibile(String 308 columnName, TableDetails leftTable, TableDetails rightTable 309 ) throws DException { 310 int leftDataType = leftTable.getDataType(columnName); 311 int rightDataType = rightTable.getDataType(columnName); 312 try { 313 Check.checkForDatatype(leftDataType, rightDataType); 314 } catch (DException ex) { 315 return false; 316 } 317 return true; 318 } 319 320 334 private void checkForQualifiedName(String columnName, TableDetails leftTable, TableDetails rightTable, ColumnDetails[] queryColumns) throws DException { 335 for (int i = 0; i < queryColumns.length; i++) { 336 String queryColumnName = queryColumns[i].getAppropriateColumn(); 337 if (queryColumnName.equalsIgnoreCase(columnName)) { 338 if (ifQueryColumnExistInNJTable(leftTable, queryColumns[i]) || 339 ifQueryColumnExistInNJTable(rightTable, queryColumns[i])) { 340 throw new DException("DSE3576", new Object [] {queryColumns[i].getAppropriateQualifiedName()}); 341 } 342 } 343 } 344 } 345 346 362 private void checkForIdenticalColumns(String columnName, int indexToStart, TableDetails[] tables) throws DException { 363 for (int i = indexToStart; i < tables.length; i++) { 364 String [] columns = tables[i].getColumnsExcludingCommonColumns(); 365 for (int j = 0; j < columns.length; j++) { 366 if (columns[j].equalsIgnoreCase(columnName)) { 367 throw new DException("DSE3577", null); 368 } 369 } 370 } 371 } 372 373 382 private ColumnDetails getColumnDetail(String columnName, TableDetails table) throws 383 DException { 384 ColumnDetails columnDetail = new ColumnDetails(); 385 String [] tableName = table.getTableName(); 386 String [] column = new String [4]; 387 System.arraycopy(tableName, 0, column, 0, tableName.length); 388 column[3] = columnName; 389 columnDetail.setColumnName(column); 390 columnDetail.setTableDetails(table); 391 columnDetail.setTableForDisplay(table); 392 columnDetail.setDatatype(table.getDataType(columnName)); 393 columnDetail.setSize(table.getSize(columnName)); 394 columnDetail.setType(REFERENCE); 395 columnDetail.setExpression(column[2] + "." + column[3]); 396 return columnDetail; 397 } 398 399 407 408 private boolean ifQueryColumnExistInNJTable(TableDetails table, 409 ColumnDetails queryColumn) throws 410 DException { 411 String [] qualifiedColumnName = queryColumn.getColumnName(); 412 String [] qualifiedTableName = table.getTableName(); 413 return ifMatch(qualifiedColumnName, qualifiedTableName); 414 } 415 416 423 private boolean ifMatch(String [] qualifiedColumnName, String [] qualifiedTableName) throws DException { 424 int i = 0, j = 0; 425 for (; i < qualifiedColumnName.length - 1 && j < qualifiedTableName.length; ++j, ++i) { 426 if (!qualifiedColumnName[i].equalsIgnoreCase(qualifiedTableName[j])) { 427 --i; 428 } 429 } 430 if (i == qualifiedColumnName.length - 1 && j == qualifiedTableName.length) { 431 return true; 432 } 433 return false; 434 } 435 436 441 442 private int getJoinType() throws DException { 443 String joinType = ""; 444 if (_Optjointype2 != null) { 445 joinType = (String ) _Optjointype2.run(null); 446 } 447 joinType += SqlKeywords.JOIN; 448 joinType = joinType.trim(); 449 if (joinType.equalsIgnoreCase(SqlKeywords.INNERJOIN)) { 450 return INNERJOIN; 451 } else if (joinType.equalsIgnoreCase(SqlKeywords.LEFTOUTERJOIN)) { 452 return LEFT_OUTER_JOIN; 453 } else if (joinType.equalsIgnoreCase(SqlKeywords.LEFTJOIN)) { 454 return LEFT_OUTER_JOIN; 455 } else if (joinType.equalsIgnoreCase(SqlKeywords.RIGHTOUTERJOIN)) { 456 return RIGHT_OUTER_JOIN; 457 } else if (joinType.equalsIgnoreCase(SqlKeywords.RIGHTJOIN)) { 458 return RIGHT_OUTER_JOIN; 459 } else if (joinType.equalsIgnoreCase(SqlKeywords.FULLOUTERJOIN)) { 460 return FULL_OUTER_JOIN; 461 } else if (joinType.equalsIgnoreCase(SqlKeywords.FULLJOIN)) { 462 return FULL_OUTER_JOIN; 463 } 464 return INNERJOIN; 465 } 466 467 public Object run(Object object) throws com.daffodilwoods.database.resource.DException { 468 throw new DException("DSE16", new Object [] {"run"}); 469 } 470 471 501 502 public TableDetails[] getTableDetails(com.daffodilwoods.daffodildb.server.serversystem._ServerSession serverSession, ColumnDetails[] queryColumns) throws DException { 503 if (tableDetails == null) { 504 table1 = _tablereference4.getTableDetails(serverSession, queryColumns); 505 table2 = _tableprimary0.getTableDetails(serverSession, queryColumns); 506 tableDetails = GeneralPurposeStaticClass.getJointTableDetails(table1, table2); 507 int joinType = getJoinType(); 508 boolean isCross = false; 509 TableDetails[] leftTables = getLeftTableDetails(joinType); 510 TableDetails[] rightTables = getRightTableDetails(joinType); 511 checkForValidQueryColumns(queryColumns); try { 513 commonColumns = checkForCommonColumnsAndDatatypeCompatabilityModified(leftTables, rightTables, queryColumns); 514 joinCondition = getConditionOfCommonColumns(commonColumns); 515 apprpriateJoinTable = getAppropraieJoinedTable(joinType, joinCondition, commonColumns); 516 } catch (DException ex) { 517 if (ex.getDseCode().equalsIgnoreCase("DSE3577")) { 518 apprpriateJoinTable = new crossjoin(_tablereference4, _tableprimary0, 519 tableDetails); 520 isCross = true; 521 } else { 522 throw ex; 523 } 524 } 525 } 526 return tableDetails; 527 } 528 529 535 public ColumnDetails[] getColumnDetails() throws DException { 536 ColumnDetails[] columnDetails1 = _tablereference4.getColumnDetails(); 537 ColumnDetails[] columnDetails2 = _tableprimary0.getColumnDetails(); 538 ColumnDetails[] result = new ColumnDetails[columnDetails1.length + columnDetails2.length]; 539 int i; 540 for (i = 0; i < columnDetails1.length; i++) { 541 result[i] = columnDetails1[i]; 542 } 543 for (int j = 0; j < columnDetails2.length; j++, i++) { 544 result[i] = columnDetails2[j]; 545 } 546 return result; 547 } 548 549 552 553 public void getColumnsIncluded(ArrayList aList) throws DException { 554 apprpriateJoinTable.getColumnsIncluded(aList); 555 } 556 557 public void getTablesIncluded(ArrayList aList) throws DException { 558 apprpriateJoinTable.getTablesIncluded(aList); 559 } 560 561 public ParameterInfo[] getParameterInfo() throws DException { 562 return apprpriateJoinTable.getParameterInfo(); 563 } 564 565 public Object [] getParameters(Object object) throws DException { 566 return apprpriateJoinTable.getParameters(object); 567 } 568 569 public _Reference[] getUnderlyingReferences() throws DException { 570 return apprpriateJoinTable.getUnderlyingReferences(); 571 } 572 573 public TableDetails[] getTablesForBlankInsert() throws DException { 574 return apprpriateJoinTable.getTablesForBlankInsert(); 575 } 576 577 public void setTablesForInsertion(ColumnMappingHandler columnMapping, _VariableValueOperations vv) throws com.daffodilwoods.database.resource.DException { 578 apprpriateJoinTable.setTablesForInsertion(columnMapping, vv); 579 } 580 581 public _BVEPlan getBveExecutionPlan() throws DException { 582 return apprpriateJoinTable.getBveExecutionPlan(); 583 } 584 585 public TableDetails[] getAllTableDetails() throws DException { 586 return apprpriateJoinTable.getAllTableDetails(); 587 } 588 589 public TableDetails[] getViewTableDetails() throws DException { 590 return apprpriateJoinTable.getViewTableDetails(); 591 } 592 593 public void verifyValues(_VariableValueOperations variableValueOperation) throws DException { 594 apprpriateJoinTable.verifyValues(variableValueOperation); 595 } 596 597 public void setDefaultValues(_VariableValueOperations variableValueOperation) throws DException { 598 apprpriateJoinTable.setDefaultValues(variableValueOperation); 599 } 600 601 public String toString() { 602 StringBuffer clause = new StringBuffer (); 603 clause.append(" "); 604 clause.append(_tablereference4.toString()); 605 606 clause.append(" "); 607 clause.append(_SRESERVEDWORD12065439223.toString()); 608 609 if (_Optjointype2 != null) { 610 clause.append(" "); 611 clause.append(_Optjointype2.toString()); 612 } 613 clause.append(" "); 614 clause.append(_SRESERVEDWORD12065439221.toString()); 615 clause.append(" "); 616 clause.append(_tableprimary0.toString()); 617 return clause.toString().trim(); 618 619 } 620 621 public Object clone() throws CloneNotSupportedException { 622 tablereference Tablereference4 = (tablereference) _tablereference4.clone(); 623 SRESERVEDWORD1206543922 sNATURAL_18480732073 = (SRESERVEDWORD1206543922) _SRESERVEDWORD12065439223.clone(); 624 jointype Jointype = null; 625 if (_Optjointype2 != null) { 626 Jointype = (jointype) _Optjointype2.clone(); 627 } 628 SRESERVEDWORD1206543922 sJOIN22827941 = (SRESERVEDWORD1206543922) _SRESERVEDWORD12065439221.clone(); 629 tableprimary Tableprimary0 = (tableprimary) _tableprimary0.clone(); 630 631 naturaljoin Naturaljoin = new naturaljoin(); 632 Naturaljoin._tablereference4 = Tablereference4; 633 Naturaljoin._SRESERVEDWORD12065439223 = sNATURAL_18480732073; 634 if (_Optjointype2 != null) { 635 Naturaljoin._Optjointype2 = Jointype; 636 } 637 Naturaljoin._SRESERVEDWORD12065439221 = sJOIN22827941; 638 Naturaljoin._tableprimary0 = Tableprimary0; 639 return Naturaljoin; 640 } 641 642 654 655 public OrderMappingWithTableRefernces[] checkForOrderSequencePlan(_ServerSession session, _OrderPlan orderPlan, _BVEPlan bvePlan, _QueryColumns queryColumns, ConditionArray conditionArray, boolean isUnderLoj) throws DException { 656 if (joinCondition != null) { 657 firstTime = false; 658 queryColumns.addColumnDetails(joinCondition.getColumnDetails()); 659 } 660 return apprpriateJoinTable.checkForOrderSequencePlan(session, orderPlan, bvePlan, queryColumns, conditionArray, isUnderLoj); 661 } 662 663 664 671 672 public boolean isSimpleQuery(_ServerSession session) throws DException { 673 return false; 674 } 675 676 679 680 public _Reference[] checkSemantic(_ServerSession session,_OrderByClause orderClause,boolean checkUserRight,boolean checkSetOperatorPresent) throws DException { 681 throw new DException("DSE565",new Object []{"checkSemantic()"}); 682 } 683 public QueryProperty getStrings() throws com.daffodilwoods.database.resource.DException { 684 685 throw new java.lang.UnsupportedOperationException ("Method getStrings() not yet implemented."); 686 } 687 688 public _TablePlan getExecutionPlan(_ServerSession session, booleanvalueexpression bve, _DatedFramework datedCondition, _Order order, ColumnDetails[] cdsWithActualTableDetails, ConditionArray conditionArray) throws DException { 689 690 throw new java.lang.UnsupportedOperationException ("Method getExecutionPlan() not yet implemented."); 691 } 692 693 public void setFKeyColumnDetails(ColumnDetails[] parm1) throws com.daffodilwoods.database.resource.DException { 694 695 throw new java.lang.UnsupportedOperationException ("Method setFKeyColumnDetails() not yet implemented."); 696 } 697 698 public ColumnDetails[] getSelectedColumns() throws com.daffodilwoods.database.resource.DException { 699 700 throw new java.lang.UnsupportedOperationException ("Method getSelectedColumns() not yet implemented."); 701 } 702 703 public _TablePlan getExecutionPlan(_ServerSession parm1) throws com.daffodilwoods.database.resource.DException { 704 705 throw new java.lang.UnsupportedOperationException ("Method getExecutionPlan() not yet implemented."); 706 } 707 708 public _ColumnCharacteristics getColumnCharacteristics(Object parm1) throws com.daffodilwoods.database.resource.DException { 709 710 throw new java.lang.UnsupportedOperationException ("Method getColumnCharacteristics() not yet implemented."); 711 } 712 713 public _Reference[] getReferences(TableDetails[] tableDetails) throws DException { 714 throw new java.lang.UnsupportedOperationException ("Method getReferences( TableDetails[] ) not yet implemented."); 715 } 716 717 public boolean isViewOptimizationPossible() throws DException { 718 throw new UnsupportedOperationException ("Method isViewOptimizationPossible is not supported."); 719 } 720 721 public _TablePlan[] getTablePlans(_ServerSession session, booleanvalueexpression condition, _Order order, ColumnDetails[] columnDetails, ConditionArray conditionArray) throws DException { 722 throw new UnsupportedOperationException ("Method getTablePlans() not supported"); 723 } 724 725 public boolean hasConstantSelectedColumn(booleanvalueexpression bve) throws DException { 726 throw new java.lang.UnsupportedOperationException ("Method hasConstantSelectedColumn() not yet implemented."); 727 } 728 } 729 730 | Popular Tags |