1 21 package oracle.toplink.essentials.internal.parsing; 23 24 import java.util.ArrayList ; 25 import java.util.List ; 26 27 50 public class NodeFactoryImpl implements NodeFactory { 51 52 53 private ParseTreeContext context; 54 55 56 private String currentIdentificationVariable; 57 58 59 public NodeFactoryImpl() { 60 initContext(); 61 } 62 63 64 public void initContext() { 65 context = new ParseTreeContext(); 66 } 67 68 72 73 public Object newSelectStatement(int line, int column, 74 Object select, Object from, 75 Object where, Object groupBy, 76 Object having, Object orderBy) { 77 QueryNode queryNode = (QueryNode)select; 78 EJBQLParseTree tree = new EJBQLParseTree(); 79 queryNode.setParseTree(tree); 80 tree.setContext(context); 81 tree.setQueryNode(queryNode); 82 tree.setFromNode((FromNode)from); 83 tree.setWhereNode((WhereNode)where); 84 tree.setGroupByNode((GroupByNode)groupBy); 85 tree.setHavingNode((HavingNode)having); 86 tree.setOrderByNode((OrderByNode)orderBy); 87 return tree; 88 } 89 90 91 public Object newUpdateStatement(int line, int column, 92 Object update, Object set, Object where) { 93 QueryNode queryNode = (QueryNode)update; 94 EJBQLParseTree tree = new EJBQLParseTree(); 95 queryNode.setParseTree(tree); 96 tree.setContext(context); 97 tree.setQueryNode(queryNode); 98 tree.setSetNode((SetNode)set); 99 tree.setWhereNode((WhereNode)where); 100 return tree; 101 } 102 103 104 public Object newDeleteStatement(int line, int column, 105 Object delete, Object where) { 106 QueryNode queryNode = (QueryNode)delete; 107 EJBQLParseTree tree = new EJBQLParseTree(); 108 queryNode.setParseTree(tree); 109 tree.setContext(context); 110 tree.setQueryNode(queryNode); 111 tree.setWhereNode((WhereNode)where); 112 return tree; 113 } 114 115 119 120 public Object newSelectClause(int line, int column, 121 boolean distinct, List selectExprs) { 122 SelectNode node = new SelectNode(); 123 node.setContext(context); 124 node.setSelectExpressions(selectExprs); 125 node.setDistinct(distinct); 126 setPosition(node, line, column); 127 return node; 128 } 129 130 131 public Object newFromClause(int line, int column, List decls) { 132 FromNode node = new FromNode(); 133 node.setContext(context); 134 node.setDeclarations(decls); 135 setPosition(node, line, column); 136 return node; 137 } 138 139 140 public Object newWhereClause(int line, int column, Object condition) { 141 WhereNode node = new WhereNode(); 142 node.setContext(context); 143 node.setLeft((Node)condition); 144 setPosition(node, line, column); 145 return node; 146 } 147 148 149 public Object newGroupByClause(int line, int column, List items) { 150 GroupByNode node = new GroupByNode(); 151 node.setContext(context); 152 node.setGroupByItems(items); 153 setPosition(node, line, column); 154 return node; 155 } 156 157 158 public Object newHavingClause(int line, int column, Object arg) { 159 HavingNode node = new HavingNode(); 160 node.setContext(context); 161 node.setHaving((Node)arg); 162 setPosition(node, line, column); 163 return node; 164 } 165 166 167 public Object newOrderByClause(int line, int column, List items) { 168 OrderByNode node = new OrderByNode(); 169 node.setContext(context); 170 node.setOrderByItems(items); 171 setPosition(node, line, column); 172 return node; 173 } 174 175 176 public Object newUpdateClause(int line, int column, 177 String schema, String variable) { 178 UpdateNode node = new UpdateNode(); 179 node.setContext(context); 180 node.setAbstractSchemaName(schema); 181 node.setAbstractSchemaIdentifier(variable); 182 setPosition(node, line, column); 183 registerSchema(variable, schema); 184 return node; 185 } 186 187 188 public Object newDeleteClause(int line, int column, 189 String schema, String variable) { 190 DeleteNode node = new DeleteNode(); 191 node.setContext(context); 192 node.setAbstractSchemaName(schema); 193 node.setAbstractSchemaIdentifier(variable); 194 setPosition(node, line, column); 195 registerSchema(variable, schema); 196 return node; 197 } 198 199 203 204 public Object newRangeVariableDecl(int line, int column, 205 String schema, String variable) { 206 RangeDeclNode node = new RangeDeclNode(); 207 node.setAbstractSchemaName(schema); 208 node.setVariableName(variable); 209 setPosition(node, line, column); 210 registerSchema(node.getCanonicalVariableName(), schema); 211 currentIdentificationVariable = variable; 212 return node; 213 } 214 215 216 public Object newJoinVariableDecl(int line, int column, boolean outerJoin, 217 Object path, String variable) { 218 DotNode dotNode = (DotNode)path; 219 AttributeNode rightNode = (AttributeNode)dotNode.getRight(); 220 rightNode.setOuterJoin(outerJoin); 221 JoinDeclNode node = new JoinDeclNode(); 222 node.setPath(dotNode); 223 node.setVariableName(variable); 224 node.setOuterJoin(outerJoin); 225 setPosition(node, line, column); 226 context.registerJoinVariable(node.getCanonicalVariableName(), dotNode); 227 currentIdentificationVariable = variable; 228 return node; 229 } 230 231 232 public Object newFetchJoin(int line, int column, 233 boolean outerJoin, Object path) { 234 DotNode dotNode = (DotNode)path; 235 AttributeNode rightNode = (AttributeNode)dotNode.getRight(); 236 rightNode.setOuterJoin(outerJoin); 237 FetchJoinNode node = new FetchJoinNode(); 239 node.setPath(dotNode); 240 node.setOuterJoin(outerJoin); 241 setPosition(node, line, column); 242 context.registerFetchJoin(currentIdentificationVariable, dotNode); 243 return node; 244 } 245 246 247 public Object newCollectionMemberVariableDecl(int line, int column, 248 Object path, String variable) { 249 DotNode dotNode = (DotNode)path; 250 AttributeNode rightNode = (AttributeNode)dotNode.getRight(); 251 rightNode.setRequiresCollectionAttribute(true); 253 CollectionMemberDeclNode node = new CollectionMemberDeclNode(); 254 node.setPath(dotNode); 255 node.setVariableName(variable); 256 setPosition(node, line, column); 257 context.registerJoinVariable(node.getCanonicalVariableName(), dotNode); 258 currentIdentificationVariable = variable; 259 return node; 260 } 261 262 263 public Object newVariableDecl(int line, int column, 264 Object path, String variable) { 265 DotNode dotNode = (DotNode)path; 266 AttributeNode rightNode = (AttributeNode)dotNode.getRight(); 267 JoinDeclNode node = new JoinDeclNode(); 268 node.setPath(dotNode); 269 node.setVariableName(variable); 270 setPosition(node, line, column); 271 context.registerJoinVariable(node.getCanonicalVariableName(), dotNode); 272 currentIdentificationVariable = variable; 273 return node; 274 } 275 276 280 281 public Object newDot(int line, int column, Object left, Object right) { 282 DotNode node = new DotNode(); 283 node.setLeft((Node)left); 284 node.setRight((Node)right); 285 setPosition(node, line, column); 286 return node; 287 } 288 289 290 public Object newVariableAccess(int line, int column, String identifier) { 291 VariableNode node = new VariableNode(identifier); 292 setPosition(node, line, column); 293 return node; 294 } 295 296 297 public Object newAttribute(int line, int column, String identifier) { 298 AttributeNode node = new AttributeNode(identifier); 299 setPosition(node, line, column); 300 return node; 301 } 302 303 307 308 public Object newAvg(int line, int column, boolean distinct, Object arg) { 309 AvgNode node = new AvgNode(); 310 node.setLeft((Node)arg); 311 node.setDistinct(distinct); 312 setPosition(node, line, column); 313 return node; 314 } 315 316 317 public Object newMax(int line, int column, boolean distinct, Object arg) { 318 MaxNode node = new MaxNode(); 319 node.setLeft((Node)arg); 320 node.setDistinct(distinct); 321 setPosition(node, line, column); 322 return node; 323 } 324 325 326 public Object newMin(int line, int column, boolean distinct, Object arg) { 327 MinNode node = new MinNode(); 328 node.setLeft((Node)arg); 329 node.setDistinct(distinct); 330 setPosition(node, line, column); 331 return node; 332 } 333 334 335 public Object newSum(int line, int column, boolean distinct, Object arg) { 336 SumNode node = new SumNode(); 337 node.setLeft((Node)arg); 338 node.setDistinct(distinct); 339 setPosition(node, line, column); 340 return node; 341 } 342 343 344 public Object newCount(int line, int column, boolean distinct, Object arg) { 345 CountNode node = new CountNode(); 346 node.setLeft((Node)arg); 347 node.setDistinct(distinct); 348 setPosition(node, line, column); 349 return node; 350 } 351 352 356 357 public Object newOr(int line, int column, Object left, Object right) { 358 OrNode node = new OrNode(); 359 node.setLeft((Node)left); 360 node.setRight((Node)right); 361 setPosition(node, line, column); 362 return node; 363 } 364 365 366 public Object newAnd(int line, int column, Object left, Object right) { 367 AndNode node = new AndNode(); 368 node.setLeft((Node)left); 369 node.setRight((Node)right); 370 setPosition(node, line, column); 371 return node; 372 } 373 374 375 public Object newEquals(int line, int column, Object left, Object right) { 376 EqualsNode node = new EqualsNode(); 377 node.setLeft((Node)left); 378 node.setRight((Node)right); 379 setPosition(node, line, column); 380 return node; 381 } 382 383 384 public Object newNotEquals(int line, int column, Object left, Object right) { 385 NotEqualsNode node = new NotEqualsNode(); 386 node.setLeft((Node)left); 387 node.setRight((Node)right); 388 setPosition(node, line, column); 389 return node; 390 } 391 392 393 public Object newGreaterThan(int line, int column, 394 Object left, Object right) { 395 GreaterThanNode node = new GreaterThanNode(); 396 node.setLeft((Node)left); 397 node.setRight((Node)right); 398 setPosition(node, line, column); 399 return node; 400 } 401 402 403 public Object newGreaterThanEqual(int line, int column, 404 Object left, Object right) { 405 GreaterThanEqualToNode node = new GreaterThanEqualToNode(); 406 node.setLeft((Node)left); 407 node.setRight((Node)right); 408 setPosition(node, line, column); 409 return node; 410 } 411 412 413 public Object newLessThan(int line, int column, Object left, Object right) { 414 LessThanNode node = new LessThanNode(); 415 node.setLeft((Node)left); 416 node.setRight((Node)right); 417 setPosition(node, line, column); 418 return node; 419 } 420 421 422 public Object newLessThanEqual(int line, int column, 423 Object left, Object right) { 424 LessThanEqualToNode node = new LessThanEqualToNode(); 425 node.setLeft((Node)left); 426 node.setRight((Node)right); 427 setPosition(node, line, column); 428 return node; 429 } 430 431 432 public Object newPlus(int line, int column, Object left, Object right) { 433 PlusNode node = new PlusNode(); 434 node.setLeft((Node)left); 435 node.setRight((Node)right); 436 setPosition(node, line, column); 437 return node; 438 } 439 440 441 public Object newMinus(int line, int column, Object left, Object right) { 442 MinusNode node = new MinusNode(); 443 node.setLeft((Node)left); 444 node.setRight((Node)right); 445 setPosition(node, line, column); 446 return node; 447 } 448 449 450 public Object newMultiply(int line, int column, Object left, Object right) { 451 MultiplyNode node = new MultiplyNode(); 452 node.setLeft((Node)left); 453 node.setRight((Node)right); 454 setPosition(node, line, column); 455 return node; 456 } 457 458 459 public Object newDivide(int line, int column, Object left, Object right) { 460 DivideNode node = new DivideNode(); 461 node.setLeft((Node)left); 462 node.setRight((Node)right); 463 setPosition(node, line, column); 464 return node; 465 } 466 467 471 472 public Object newUnaryPlus(int line, int column, Object arg) { 473 return arg; 474 } 475 476 477 public Object newUnaryMinus(int line, int column, Object arg) { 478 UnaryMinus node = new UnaryMinus(); 479 node.setLeft((Node)arg); 480 setPosition(node, line, column); 481 return node; 482 } 483 484 485 public Object newNot(int line, int column, Object arg) { 486 NotNode node = new NotNode(); 487 node.setLeft((Node)arg); 488 setPosition(node, line, column); 489 return node; 490 } 491 492 496 497 public Object newBetween(int line, int column, boolean not, Object arg, 498 Object lower, Object upper) { 499 BetweenNode node = new BetweenNode(); 500 node.setLeft((Node)arg); 501 node.setRightForBetween((Node)lower); 502 node.setRightForAnd((Node)upper); 503 setPosition(node, line, column); 504 return not? newNot(line, column, node) : node; 505 } 506 507 508 public Object newLike(int line, int column, boolean not, Object string, 509 Object pattern, Object escape) { 510 LikeNode node = new LikeNode(); 511 node.setLeft((Node)string); 512 node.setRight((Node)pattern); 513 node.setEscapeNode((EscapeNode)escape); 514 setPosition(node, line, column); 515 return not ? newNot(line, column, node) : node; 516 } 517 518 519 public Object newEscape(int line, int column, Object arg) { 520 EscapeNode node = new EscapeNode(); 521 node.setLeft((Node)arg); 522 setPosition(node, line, column); 523 return node; 524 } 525 526 527 public Object newIn(int line, int column, 528 boolean not, Object expr, List items) { 529 InNode node = new InNode(); 530 if (not) node.indicateNot(); 531 node.setLeft((Node)expr); 532 node.setTheObjects(items); 533 setPosition(node, line, column); 534 return node; 535 } 536 537 538 public Object newIsNull(int line, int column, boolean not, Object expr) { 539 NullComparisonNode node = new NullComparisonNode(); 540 node.setLeft((Node)expr); 541 setPosition(node, line, column); 542 return not ? newNot(line, column, node) : node; 543 } 544 545 546 public Object newIsEmpty(int line, int column, boolean not, Object expr) { 547 EmptyCollectionComparisonNode node = 548 new EmptyCollectionComparisonNode(); 549 node.setLeft((Node)expr); 550 if (not) node.indicateNot(); 551 setPosition(node, line, column); 552 return node; 553 } 554 555 556 public Object newMemberOf(int line, int column, 557 boolean not, Object expr, Object collection) { 558 MemberOfNode node = new MemberOfNode(); 559 node.setLeft((Node)expr); 560 node.setRight((Node)collection); 561 if (not) node.indicateNot(); 562 setPosition(node, line, column); 563 return node; 564 } 565 566 570 571 public Object newPositionalParameter(int line, int column, String position) { 572 ParameterNode node = new ParameterNode(position); 573 context.addParameter(position); 574 setPosition(node, line, column); 575 return node; 576 } 577 578 579 public Object newNamedParameter(int line, int column, String name) { 580 ParameterNode node = new ParameterNode(name); 581 context.addParameter(name); 582 setPosition(node, line, column); 583 return node; 584 } 585 586 590 591 public Object newBooleanLiteral(int line, int column, Object value) { 592 BooleanLiteralNode node = new BooleanLiteralNode(); 593 node.setLiteral(value); 594 setPosition(node, line, column); 595 return node; 596 } 597 598 599 public Object newIntegerLiteral(int line, int column, Object value) { 600 IntegerLiteralNode node = new IntegerLiteralNode(); 601 node.setLiteral(value); 602 setPosition(node, line, column); 603 return node; 604 } 605 606 607 public Object newLongLiteral(int line, int column, Object value) { 608 LongLiteralNode node = new LongLiteralNode(); 609 node.setLiteral(value); 610 setPosition(node, line, column); 611 return node; 612 } 613 614 615 public Object newFloatLiteral(int line, int column, Object value) { 616 FloatLiteralNode node = new FloatLiteralNode(); 617 node.setLiteral(value); 618 setPosition(node, line, column); 619 return node; 620 } 621 622 623 public Object newDoubleLiteral(int line, int column, Object value) { 624 DoubleLiteralNode node = new DoubleLiteralNode(); 625 node.setLiteral(value); 626 setPosition(node, line, column); 627 return node; 628 } 629 630 631 public Object newStringLiteral(int line, int column, Object value) { 632 StringLiteralNode node = new StringLiteralNode(); 633 node.setLiteral(value); 634 setPosition(node, line, column); 635 return node; 636 } 637 638 639 public Object newNullLiteral(int line, int column) { 640 LiteralNode node = new LiteralNode(); 641 node.setLiteral(null); 642 setPosition(node, line, column); 643 return node; 644 } 645 646 650 651 public Object newConcat(int line, int column, Object left, Object right) { 652 ConcatNode node = new ConcatNode(); 653 node.setLeft((Node)left); 654 node.setRight((Node)right); 655 setPosition(node, line, column); 656 return node; 657 } 658 659 660 public Object newSubstring(int line, int column, 661 Object string, Object start, Object length) { 662 SubstringNode node = new SubstringNode(); 663 node.setLeft((Node)string); 664 node.setStartPosition((Node)start); 665 node.setStringLength((Node)length); 666 setPosition(node, line, column); 667 return node; 668 } 669 670 671 public Object newTrim(int line, int column, TrimSpecification trimSpec, 672 Object trimChar, Object string) { 673 TrimNode node = new TrimNode(); 674 node.setLeft((Node)string); 675 node.setTrimChar((Node)trimChar); 676 switch (trimSpec) { 677 case LEADING: 678 node.setLeading(true); 679 break; 680 case TRAILING: 681 node.setTrailing(true); 682 break; 683 case BOTH: 684 node.setBoth(true); 685 break; 686 } 687 setPosition(node, line, column); 688 return node; 689 } 690 691 692 public Object newLower(int line, int column, Object arg) { 693 LowerNode node = new LowerNode(); 694 node.setLeft((Node)arg); 695 setPosition(node, line, column); 696 return node; 697 } 698 699 700 public Object newUpper(int line, int column, Object arg) { 701 UpperNode node = new UpperNode(); 702 node.setLeft((Node)arg); 703 setPosition(node, line, column); 704 return node; 705 } 706 707 711 712 public Object newLocate(int line, int column, 713 Object pattern, Object arg, Object startPos) { 714 LocateNode node = new LocateNode(); 715 node.setFind((Node)pattern); 716 node.setFindIn((Node)arg); 717 node.setStartPosition((Node)startPos); 718 setPosition(node, line, column); 719 return node; 720 } 721 722 723 public Object newLength(int line, int column, Object arg) { 724 LengthNode node = new LengthNode(); 725 node.setLeft((Node)arg); 726 setPosition(node, line, column); 727 return node; 728 } 729 730 731 public Object newAbs(int line, int column, Object arg) { 732 AbsNode node = new AbsNode(); 733 node.setLeft((Node)arg); 734 setPosition(node, line, column); 735 return node; 736 } 737 738 739 public Object newSqrt(int line, int column, Object arg) { 740 SqrtNode node = new SqrtNode(); 741 node.setLeft((Node)arg); 742 setPosition(node, line, column); 743 return node; 744 } 745 746 747 public Object newMod(int line, int column, Object left, Object right) { 748 ModNode node = new ModNode(); 749 node.setLeft((Node)left); 750 node.setDenominator((Node)right); 751 setPosition(node, line, column); 752 return node; 753 } 754 755 756 public Object newSize(int line, int column, Object arg) { 757 SizeNode node = new SizeNode(); 758 node.setLeft((Node)arg); 759 setPosition(node, line, column); 760 return node; 761 } 762 763 767 768 public Object newCurrentDate(int line, int column) { 769 DateFunctionNode node = new DateFunctionNode(); 770 node.useCurrentDate(); 771 setPosition(node, line, column); 772 return node; 773 } 774 775 776 public Object newCurrentTime(int line, int column) { 777 DateFunctionNode node = new DateFunctionNode(); 778 node.useCurrentTime(); 779 setPosition(node, line, column); 780 781 return node; 782 } 783 784 785 public Object newCurrentTimestamp(int line, int column) { 786 DateFunctionNode node = new DateFunctionNode(); 787 node.useCurrentTimestamp(); 788 setPosition(node, line, column); 789 return node; 790 } 791 792 796 797 public Object newSubquery(int line, int column, 798 Object select, Object from, Object where, 799 Object groupBy, Object having) { 800 QueryNode queryNode = (QueryNode)select; 801 EJBQLParseTree tree = new EJBQLParseTree(); 802 queryNode.setParseTree(tree); 803 tree.setQueryNode(queryNode); 804 tree.setFromNode((FromNode)from); 805 tree.setWhereNode((WhereNode)where); 806 tree.setGroupByNode((GroupByNode)groupBy); 807 tree.setHavingNode((HavingNode)having); 808 tree.setContext(context); 809 SubqueryNode node = new SubqueryNode(); 810 node.setParseTree(tree); 811 setPosition(node, line, column); 812 return node; 813 } 814 815 816 public Object newExists(int line, int column, boolean not, Object subquery) { 817 ExistsNode node = new ExistsNode(); 818 if (not) node.indicateNot(); 819 node.setLeft((Node)subquery); 820 setPosition(node, line, column); 821 return node; 822 } 823 824 825 public Object newIn(int line, int column, 826 boolean not, Object expr, Object subquery) { 827 InNode node = new InNode(); 828 if (not) node.indicateNot(); 829 node.setLeft((Node)expr); 830 node.addNodeToTheObjects((Node)subquery); 831 setPosition(node, line, column); 832 return node; 833 } 834 835 836 public Object newAll(int line, int column, Object subquery) { 837 AllNode node = new AllNode(); 838 node.setLeft((Node)subquery); 839 setPosition(node, line, column); 840 return node; 841 } 842 843 844 public Object newAny(int line, int column, Object subquery) { 845 AnyNode node = new AnyNode(); 846 node.setLeft((Node)subquery); 847 setPosition(node, line, column); 848 return node; 849 } 850 851 852 public Object newSome(int line, int column, Object subquery) { 853 SomeNode node = new SomeNode(); 854 node.setLeft((Node)subquery); 855 setPosition(node, line, column); 856 return node; 857 } 858 859 863 864 public Object newAscOrdering(int line, int column, Object arg) { 865 OrderByItemNode node = new OrderByItemNode(); 866 SortDirectionNode sortDirection = new SortDirectionNode(); 867 sortDirection.useAscending(); 868 node.setDirection(sortDirection); 869 node.setOrderByItem((Node)arg); 870 setPosition(node, line, column); 871 return node; 872 } 873 874 875 public Object newDescOrdering(int line, int column, Object arg) { 876 OrderByItemNode node = new OrderByItemNode(); 877 SortDirectionNode sortDirection = new SortDirectionNode(); 878 sortDirection.useDescending(); 879 node.setDirection(sortDirection); 880 node.setOrderByItem((Node)arg); 881 setPosition(node, line, column); 882 return node; 883 } 884 885 886 public Object newConstructor(int line, int column, 887 String className, List args) { 888 ConstructorNode node = new ConstructorNode(className); 889 node.setConstructorItems(args); 890 setPosition(node, line, column); 891 return node; 892 } 893 894 895 public Object newSetClause(int line, int column, List assignments) { 896 SetNode node = new SetNode(); 897 node.setAssignmentNodes(assignments); 898 setPosition(node, line, column); 899 return node; 900 } 901 902 903 public Object newSetAssignmentClause(int line, int column, 904 Object target, Object value) { 905 EqualsAssignmentNode node = new EqualsAssignmentNode(); 906 node.setLeft((Node)target); 907 node.setRight((Node)value); 908 return node; 909 } 910 911 915 916 private void setPosition(Node node, int line, int column) { 917 node.setLine(line); 918 node.setColumn(column); 919 } 920 921 922 private void registerSchema(String variable, String schema) { 923 if (variable != null) { 924 context.registerSchema(variable, schema); 925 } 926 else { 927 context.registerSchema(schema, schema); 930 } 931 } 932 933 } 934 | Popular Tags |