1 package com.daffodilwoods.daffodildb.server.sql99.dql.tableexpression. 2 fromclause; 3 4 import java.util.*; 5 6 import com.daffodilwoods.daffodildb.server.serversystem.*; 7 import com.daffodilwoods.daffodildb.server.sql99.common.*; 8 import com.daffodilwoods.daffodildb.server.sql99.dql.common.*; 9 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*; 10 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*; 11 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.*; 12 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.condition.*; 13 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.order.*; 14 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.table.*; 15 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 16 import com.daffodilwoods.database.resource.*; 17 import com.daffodilwoods.database.sqlinitiator.*; 18 import com.daffodilwoods.database.utility.*; 19 import com.daffodilwoods.daffodildb.server.sql99.expression.expressionprimary. 20 subquery; 21 import com.daffodilwoods.daffodildb.server.sql99.dml.*; 22 import com.daffodilwoods.daffodildb.server.serversystem._ServerSession; 23 24 40 41 public class tablereferencelist 42 implements com.daffodilwoods.daffodildb.utils.parser.StatementExecuter { 43 44 48 49 public tablereference[] _OptRepScomma94843605tablereference0; 50 51 54 55 private TableDetails[] tableDetails1; 56 57 61 62 private TableDetails[] allTableDetails; 63 64 118 public _TablePlan[] getTablePlan(_ServerSession session, 119 _DatedFramework datedCondition, 120 _BVEPlan bvePlan, _OrderPlan orderPlan, 121 _QueryColumns queryColumns, 122 ConditionArray conditionArray) throws 123 DException { 124 if (isOrderSequencePlanPossible(orderPlan)) { 125 _TablePlan[] temp = getTablePlansAccordingToOrderSequencePlan(session, 126 datedCondition, bvePlan, orderPlan, queryColumns, conditionArray); 127 return temp; 128 } 129 return getTablePlansNotArrangedAccToOrderplan(session, datedCondition, 130 bvePlan, null, queryColumns, 131 conditionArray); 132 } 133 134 142 private boolean isOrderSequencePlanPossible(_OrderPlan orderPlan) throws 143 DException { 144 return orderPlan != null && orderPlan.getSingleTableOrderPlans() != null; 145 } 146 147 179 private _TablePlan[] getTablePlansAccordingToOrderSequencePlan(_ServerSession 180 session, _DatedFramework datedCondition, _BVEPlan bvePlan, 181 _OrderPlan orderPlan, 182 _QueryColumns queryColumns, ConditionArray conditionArray) throws 183 DException { 184 ConditionArray clonedConditionArray = getClonedConditionArray( 185 conditionArray); 186 OrderMappingWithTableRefernces[] orderPlansAccToTableReferences = 187 checkForOrderSequencePossibility(orderPlan, bvePlan, session, 188 queryColumns, clonedConditionArray); 189 if (orderPlansAccToTableReferences == null) { 190 reArrangeOrderPlansModified(orderPlan); 191 ArrayList allPlans = getAllTablePlans(session, datedCondition, bvePlan, null, 192 queryColumns, conditionArray); 193 return (_TablePlan[]) allPlans.toArray(new _TablePlan[allPlans.size()]); 194 } 195 return getTablePlansArrangedAccToOrderMappingWithTableReference(session, 196 datedCondition, bvePlan, orderPlan, queryColumns, conditionArray, 197 orderPlansAccToTableReferences); 198 } 199 200 206 private ConditionArray getClonedConditionArray(ConditionArray conditionArray) throws 207 DException { 208 try { 209 return (ConditionArray) conditionArray.clone(); 210 } 211 catch (CloneNotSupportedException ex) { 212 } 213 throw new DException("DSE0", 214 new Object [] {"Clone of ConditionArray is Not Successfull"}); 215 } 216 217 273 private OrderMappingWithTableRefernces[] checkForOrderSequencePossibility( 274 _OrderPlan orderPlan, _BVEPlan bvePlan, _ServerSession session, 275 _QueryColumns queryColumns, ConditionArray conditionArray) throws 276 DException { 277 boolean isPartiallyMatched = false; 278 ArrayList allGrouped = new ArrayList(5); 279 for (int i = 0; i < _OptRepScomma94843605tablereference0.length; i++) { 280 OrderMappingWithTableRefernces[] mapping = 281 _OptRepScomma94843605tablereference0[i].checkForOrderSequencePlan( 282 session, orderPlan, bvePlan, queryColumns, conditionArray, false); 283 if (mapping != null) { 284 for (int j = 0; j < mapping.length; j++) { 285 if (isPartiallyMatched || 286 mapping[j].getMatch() == TableExpressionConstants.NOTMATCHED) { 287 return null; 288 } 289 if (mapping[j].getMatch() == 290 TableExpressionConstants.PARTIALLYMATCHED) { 291 isPartiallyMatched = true; 292 } 293 allGrouped.add(mapping[j]); 294 } 295 } 296 } 297 OrderMappingWithTableRefernces[] ref = (OrderMappingWithTableRefernces[]) 298 allGrouped.toArray(new OrderMappingWithTableRefernces[allGrouped.size()]); 299 if (ref.length == 0 || isMixedSequencePresent(orderPlan, ref)) { 300 return null; 301 } 302 return ref; 303 } 304 305 326 private boolean isMixedSequencePresent(_OrderPlan orderPlan, 327 OrderMappingWithTableRefernces[] 328 mapping) throws DException { 329 _SingleTableOrderPlan[] stops = orderPlan.getSingleTableOrderPlans(); 330 HashSet sequence = new HashSet(); 331 int sequenceIndx = 0; 332 OrderMappingWithTableRefernces lastMapping = null; 333 for (int i = 0; i < stops.length; i++) { 334 for (int j = 0; j < mapping.length; j++) { 335 if (mapping[j].isBelongTo(stops[i])) { 336 if (sequence.add(mapping[j])) { 337 lastMapping = mapping[j]; 338 break; 339 } 340 else if (mapping[j] == lastMapping) { 341 break; 342 } 343 else { 344 return true; 345 } 346 } 347 } 348 } 349 return false; 350 } 351 352 365 private _TablePlan[] getTablePlansNotArrangedAccToOrderplan(_ServerSession 366 session, _DatedFramework datedCondition, _BVEPlan bvePlan, 367 _OrderPlan orderPlan, 368 _QueryColumns queryColumns, ConditionArray conditionArray) throws 369 DException { 370 int length = _OptRepScomma94843605tablereference0.length; 371 _TablePlan[] tablePlans = null; 372 for (int i = 0; i < length; i++) { 373 _TablePlan[] temp = _OptRepScomma94843605tablereference0[i].getTablePlans( 374 session, datedCondition, bvePlan, null, queryColumns, conditionArray); 375 tablePlans = getJoinTablePlans(tablePlans, temp); 376 } 377 return tablePlans; 378 } 379 380 402 private _TablePlan[] getTablePlansArrangedAccToOrderMappingWithTableReference( 403 _ServerSession session, _DatedFramework datedCondition, _BVEPlan bvePlan, 404 _OrderPlan orderPlan, _QueryColumns queryColumns, 405 ConditionArray conditionArray, OrderMappingWithTableRefernces[] mapping) throws 406 DException { 407 ArrayList tablePlans = getAllTablePlans(session, datedCondition, bvePlan, 408 orderPlan, queryColumns, 409 conditionArray); 410 ArrayList adjusted = new ArrayList(); 411 mapping = sortAccToOrder(mapping, orderPlan.getSingleTableOrderPlans()); 412 for (int i = 0; i < mapping.length; i++) { 413 TableDetails[] td = mapping[i].getTableDetails(); 414 _TablePlan found = getTablePlanForOrderMapping(td, tablePlans); 415 tablePlans.remove(found); 416 adjusted.add(found); 417 } 418 tablePlans.add(0, 419 new OrderSequencePlan( (_TablePlan[]) adjusted.toArray(new _TablePlan[ 420 adjusted.size()]))); 421 return (_TablePlan[]) tablePlans.toArray(new _TablePlan[tablePlans.size()]); 422 } 423 424 432 private OrderMappingWithTableRefernces[] sortAccToOrder( 433 OrderMappingWithTableRefernces[] mapping, _SingleTableOrderPlan[] stop) throws 434 DException { 435 ArrayList list = new ArrayList(5); 436 for (int i = 0; i < stop.length; i++) { 437 for (int j = 0; j < mapping.length; j++) { 438 if (mapping[j].isBelongTo(stop[i]) && !list.contains(mapping[j])) { 439 list.add(mapping[j]); 440 } 441 } 442 } 443 return (OrderMappingWithTableRefernces[]) list.toArray(new 444 OrderMappingWithTableRefernces[list.size()]); 445 } 446 447 455 private _TablePlan getTablePlanForOrderMapping(TableDetails[] mappingTables, 456 ArrayList tablePlans) throws 457 DException { 458 459 for (int i = 0, size = tablePlans.size(); i < size; i++) { 460 _TablePlan present = (_TablePlan) tablePlans.get(i); 461 TableDetails[] tablesOfReference = (TableDetails[]) present. 462 getTableDetails(); 463 464 if (isMappingPresentInTablePlans(tablesOfReference, mappingTables)) { 465 return present; 466 } 467 } 468 throw new DException("SOME THING WRONG", null); 469 } 470 471 482 private boolean isMappingPresentInTablePlans(TableDetails[] tablesOfReference, 483 TableDetails[] mappingTables) throws 484 DException { 485 for (int i = 0, j = 0; 486 i < mappingTables.length && j < tablesOfReference.length; i++, j++) { 487 if (mappingTables[i] != tablesOfReference[j]) { 488 return false; 489 } 490 } 491 return true; 492 } 493 494 506 private ArrayList getAllTablePlans(_ServerSession session, 507 _DatedFramework datedCondition, 508 _BVEPlan bvePlan, _OrderPlan orderPlan, 509 _QueryColumns queryColumns, 510 ConditionArray conditionArray) throws 511 DException { 512 ArrayList tablePlans = new ArrayList(); 513 for (int i = 0; i < _OptRepScomma94843605tablereference0.length; i++) { 514 tablePlans.addAll(Arrays.asList(_OptRepScomma94843605tablereference0[i]. 515 getTablePlans(session, datedCondition, 516 bvePlan, orderPlan, queryColumns, conditionArray))); 517 } 518 return tablePlans; 519 } 520 521 528 529 private _TablePlan[] getJoinTablePlans(_TablePlan[] source1, 530 _TablePlan[] source2) throws 531 DException { 532 if (source1 == null) { 533 return source2; 534 } 535 if (source2 == null) { 536 return source1; 537 } 538 _TablePlan buffered[] = new _TablePlan[source1.length + source2.length]; 539 System.arraycopy(source1, 0, buffered, 0, source1.length); 540 System.arraycopy(source2, 0, buffered, source1.length, source2.length); 541 return buffered; 542 } 543 544 552 553 private void reArrangeOrderPlansModified(_OrderPlan orderPlan) throws 554 DException { 555 _SingleTableOrderPlan[] stop = orderPlan.getSingleTableOrderPlans(); 556 _Order order = orderPlan.getJoinLevelOrder(); 557 if (order != null) { 558 orderPlan.setGroupByOrderPlan(order); 559 } 560 orderPlan.setJoinOrderPlan(OrderPlanMerger.getOrderFromSingleTableOrderPlan( 561 stop)); 562 orderPlan.setSingleTableOrderPlans(null); 563 } 564 565 569 public String toString() { 570 StringBuffer sb = new StringBuffer (); 571 sb.append(" "); 572 sb.append(_OptRepScomma94843605tablereference0[0]); 573 for (int i = 1; i < _OptRepScomma94843605tablereference0.length; i++) { 574 sb.append(",").append(_OptRepScomma94843605tablereference0[i]); 575 } 576 return sb.toString(); 577 } 578 579 584 public Object clone() throws CloneNotSupportedException { 585 tablereferencelist tempClass = new tablereferencelist(); 586 if (_OptRepScomma94843605tablereference0 != null) { 587 tablereference[] temp_OptRepScomma94843605tablereference0 = new 588 tablereference[_OptRepScomma94843605tablereference0.length]; 589 for (int i = 0; i < _OptRepScomma94843605tablereference0.length; i++) { 590 temp_OptRepScomma94843605tablereference0[i] = (tablereference) 591 _OptRepScomma94843605tablereference0[i].clone(); 592 } 593 tempClass._OptRepScomma94843605tablereference0 = 594 temp_OptRepScomma94843605tablereference0; 595 } 596 return tempClass; 597 } 598 599 604 605 614 public TableDetails[] getTablesForBlankInsert() throws DException { 615 int len = _OptRepScomma94843605tablereference0.length; 616 ArrayList arr = new ArrayList(); 617 for (int i = 0; i < len; i++) { 618 arr.addAll(Arrays.asList(_OptRepScomma94843605tablereference0[i]. 619 getTablesForBlankInsert())); 620 } 621 return (TableDetails[]) arr.toArray(new TableDetails[0]); 622 } 623 624 634 public void setTablesForInsertion(ColumnMappingHandler mappingHandler, 635 _VariableValueOperations vv) throws 636 DException { 637 int len = _OptRepScomma94843605tablereference0.length; 638 for (int i = 0; i < len; i++) { 639 _OptRepScomma94843605tablereference0[i].setTablesForInsertion( 640 mappingHandler, vv); 641 } 642 } 643 644 651 public _BVEPlan getBveExecutionPlan() throws DException { 652 int length = _OptRepScomma94843605tablereference0.length; 653 _BVEPlan bvePlan = _OptRepScomma94843605tablereference0[0]. 654 getBveExecutionPlan(); 655 for (int i = 1; i < length; i++) { 656 bvePlan = BVEPlanMerger.mergeTablePlansWithAnd(bvePlan, 657 _OptRepScomma94843605tablereference0[i].getBveExecutionPlan()); 658 } 659 return bvePlan; 660 } 661 662 668 public TableDetails[] getAllTableDetails() throws DException { 669 TableDetails[] tdToReturn = null; 670 int len = _OptRepScomma94843605tablereference0.length; 671 for (int i = 0; i < len; i++) { 672 tdToReturn = GeneralPurposeStaticClass.getJointTableDetails(tdToReturn, 673 _OptRepScomma94843605tablereference0[i].getAllTableDetails()); 674 } 675 return tdToReturn; 676 } 677 678 684 public _Reference[] getUnderlyingReferences() throws DException { 685 _Reference[] references = null; 686 for (int i = 0, length = _OptRepScomma94843605tablereference0.length; 687 i < length; i++) { 688 references = GeneralPurposeStaticClass.getJointReferences(references, 689 _OptRepScomma94843605tablereference0[i].getUnderlyingReferences()); 690 } 691 return references; 692 } 693 694 699 700 public TableDetails[] getViewTableDetails() throws DException { 701 TableDetails[] tdToReturn = null; 702 int len = _OptRepScomma94843605tablereference0.length; 703 for (int i = 0; i < len; i++) { 704 tdToReturn = GeneralPurposeStaticClass.getJointTableDetails(tdToReturn, 705 _OptRepScomma94843605tablereference0[i].getViewTableDetails()); 706 } 707 return tdToReturn; 708 } 709 710 718 719 public void verifyValues(_VariableValueOperations variableValueOperation) throws 720 DException { 721 int length = _OptRepScomma94843605tablereference0.length; 722 for (int i = 0; i < length; i++) { 723 _OptRepScomma94843605tablereference0[i].verifyValues( 724 variableValueOperation); 725 } 726 } 727 728 736 737 public void setDefaultValues(_VariableValueOperations variableValueOperation) throws 738 DException { 739 for (int i = 0; i < _OptRepScomma94843605tablereference0.length; i++) { 740 _OptRepScomma94843605tablereference0[i].setDefaultValues( 741 variableValueOperation); 742 } 743 } 744 745 751 752 public Object run(Object object) throws com.daffodilwoods.database.resource. 753 DException { 754 int length = _OptRepScomma94843605tablereference0.length; 755 Object [] result = new Object [length]; 756 for (int i = 0; i < length; i++) { 757 result[i] = _OptRepScomma94843605tablereference0[i].run(object); 758 } 759 return result; 760 } 761 762 771 772 public TableDetails[] getTableDetails(_ServerSession session, 773 ColumnDetails[] queryColumns) throws 774 DException { 775 ArrayList finalTableDetails = new ArrayList(); 776 for (int i = 0; i < _OptRepScomma94843605tablereference0.length; i++) { 777 finalTableDetails.addAll(Arrays.asList( 778 _OptRepScomma94843605tablereference0[i].getTableDetails(session, 779 queryColumns))); 780 } 781 return (TableDetails[]) finalTableDetails.toArray(new TableDetails[0]); 782 } 783 784 791 792 public ColumnDetails[] getColumnDetails() throws DException { 793 ArrayList aList = new ArrayList(5); 794 for (int i = 0; i < _OptRepScomma94843605tablereference0.length; i++) { 795 aList.addAll(Arrays.asList(_OptRepScomma94843605tablereference0[i]. 796 getColumnDetails())); 797 } 798 return (ColumnDetails[]) aList.toArray(new ColumnDetails[0]); 799 } 800 801 public com.daffodilwoods.daffodildb.server.sql99.utils._Reference[] 802 checkSemantic(com.daffodilwoods.daffodildb.server.serversystem. 803 _ServerSession parent, ColumnDetails[] queryColumns, 804 boolean checkUserRight) throws DException { 805 ArrayList aList = new ArrayList(); 806 for (int i = 0; i < _OptRepScomma94843605tablereference0.length; i++) { 807 _Reference refArray[] = _OptRepScomma94843605tablereference0[i]. 808 checkSemantic(parent, queryColumns, checkUserRight); 809 if (refArray != null) { 810 aList.addAll(Arrays.asList(refArray)); 811 } 812 } 813 814 if(aList.size()==0) 815 return null; 816 _Reference[] refs= (_Reference[]) aList.toArray(new _Reference[0]); 817 checkReferenceForAllTables(refs,parent); 818 return refs; 819 } 820 821 public void getColumnsIncluded(ArrayList aList) throws DException { 822 for (int i = 0; i < _OptRepScomma94843605tablereference0.length; i++) { 823 _OptRepScomma94843605tablereference0[i].getColumnsIncluded(aList); 824 } 825 } 826 827 public void getTablesIncluded(ArrayList aList) throws DException { 828 for (int i = 0; i < _OptRepScomma94843605tablereference0.length; i++) { 829 _OptRepScomma94843605tablereference0[i].getTablesIncluded(aList); 830 } 831 } 832 833 public ParameterInfo[] getParameterInfo() throws DException { 834 if (_OptRepScomma94843605tablereference0 == null) { 835 return null; 836 } 837 ArrayList aList = new ArrayList(5); 838 int length = _OptRepScomma94843605tablereference0.length; 839 for (int i = 0; i < length; i++) { 840 ParameterInfo[] pi = _OptRepScomma94843605tablereference0[i]. 841 getParameterInfo(); 842 if (pi != null) { 843 aList.addAll(Arrays.asList(pi)); 844 } 845 } 846 return (ParameterInfo[]) aList.toArray(new ParameterInfo[0]); 847 } 848 849 public Object [] getParameters(Object object) throws DException { 850 ArrayList result = new ArrayList(5); 851 for (int i = 0; i < _OptRepScomma94843605tablereference0.length; i++) { 852 Object [] param = _OptRepScomma94843605tablereference0[i].getParameters( 853 object); 854 if (param != null) { 855 result.addAll(Arrays.asList(param)); 856 } 857 } 858 return result.size() == 0 ? null : result.toArray(new Object [0]); 859 } 860 861 867 868 private Object [] addParameters(Object [] param1, Object [] param2) { 869 return param1 == null ? param2 : param2 == null ? param1 : 870 mergeParameters(param1, param2); 871 } 872 873 879 private Object [] mergeParameters(Object [] param1, Object [] param2) { 880 int len1 = param1.length; 881 int len2 = param2.length; 882 int len = len1 + len2; 883 Object [] result = new Object [len]; 884 System.arraycopy(param1, 0, result, 0, len1); 885 System.arraycopy(param2, 0, result, len1, len2); 886 return result; 887 } 888 889 public boolean isSimpleQuery(_ServerSession serverSession) throws DException { 890 891 if (_OptRepScomma94843605tablereference0.length > 2) 892 return false; 893 else if (_OptRepScomma94843605tablereference0.length == 1) { 894 if (_OptRepScomma94843605tablereference0[0] instanceof derivedtableOptSRESERVEDWORD1206543922correlationnameOptparenderivedcolumnlist) 895 return false; 896 897 if (_OptRepScomma94843605tablereference0[0] instanceof subquery || _OptRepScomma94843605tablereference0[0] instanceof parentablereference) 898 return false; 899 if (_OptRepScomma94843605tablereference0[0] instanceof joinedtable) { 900 return ( (joinedtable) _OptRepScomma94843605tablereference0[0]). 901 isSimpleQuery(serverSession); 902 } 903 TableDetails[] td = _OptRepScomma94843605tablereference0[0]. 904 getTableDetails(serverSession, null); 905 if (td.length > 2) 906 return false; 907 908 if (td.length == 2) { 909 if (td[0].getTableType() == TableDetails.VIEW || 910 td[1].getTableType() == TableDetails.VIEW) 911 return false; 912 } 913 915 try { 916 return td[0].getTableType() == TableDetails.VIEW ? ( (ViewAbstract) 917 _OptRepScomma94843605tablereference0[0]).getViewObject(). 918 getQueryExpression().isSimpleQuery(serverSession) : true; 919 } catch (Exception ex) { 920 return false; 921 } 922 923 } 924 else 925 return isSimpleQueryCompliance(serverSession, 926 _OptRepScomma94843605tablereference0); 927 } 928 929 final private boolean isSimpleQueryCompliance(_ServerSession serverSession, 930 tablereference tableReferences[]) throws 931 DException { 932 boolean firstTableFlag = true; 933 boolean secoundTableFlag = true; 934 if (tableReferences[0] instanceof subquery || 935 tableReferences[1] instanceof subquery || 936 tableReferences[0] instanceof derivedtableOptSRESERVEDWORD1206543922correlationnameOptparenderivedcolumnlist || 937 tableReferences[1] instanceof derivedtableOptSRESERVEDWORD1206543922correlationnameOptparenderivedcolumnlist) { 938 return false; 939 } 940 if (tableReferences[0] instanceof joinedtable) { 941 firstTableFlag = ( (joinedtable) tableReferences[0]). 942 isSimpleQuery(serverSession); 943 } 944 if (tableReferences[1] instanceof joinedtable) { 945 secoundTableFlag = ( (joinedtable) tableReferences[1]). 946 isSimpleQuery(serverSession); 947 } 948 else 949 return false; 950 ; 951 return firstTableFlag && secoundTableFlag; 952 } 953 962 private void checkReferenceForAllTables(_Reference[] refs, 963 com.daffodilwoods.daffodildb.server. 964 serversystem._ServerSession session) throws 965 DException { 966 if (refs == null) 967 return; 968 ArrayList list = new ArrayList(); 969 for (int i = 0; i < refs.length; i++) { 970 if (refs[i].getReferenceType() == SimpleConstants.COLUMNDETAIL) 971 list.add( (ColumnDetails) refs[i]); 972 } 973 if (list.size() == 0) 974 return; 975 ArrayList underLyingRefs = new ArrayList(); 976 SetColumnProperties.setTableNamesAndDatatypesOfAllColumns(session, 977 (ColumnDetails[]) list.toArray(new ColumnDetails[list.size()]), 978 getAllTableDetails(), underLyingRefs, new ArrayList(0)); 979 980 if (list.size() != underLyingRefs.size()) { if (underLyingRefs.size() == 0) 982 throw new DException("DSE256", 983 new Object [] { ( (ColumnDetails) list.get(0)). 984 getColumn()}); 985 else 986 for (int i = 0; i < underLyingRefs.size(); i++) { if (!underLyingRefs.contains(list.get(i))) 988 throw new DException("DSE256", 989 new Object [] { ( (ColumnDetails) list.get(i)). 990 getAppropriateColumn()}); 991 } 992 } 993 } 994 995 } 996 | Popular Tags |