| 1 2 package com.genimen.djeneric.repository.sqlparser.core; 3 4 import java.io.ByteArrayInputStream ; 5 6 import com.genimen.djeneric.repository.sqlparser.SqlParserEngineListener; 7 8 public class SqlParserEngine implements SqlParserEngineConstants 9 { 10 11 private SqlParserEngineListener _handler; 12 private String _result; 13 private TokenContext _currentContext = new TokenContext(); 14 15 public static SqlParserEngine parse(String src, SqlParserEngineListener handler) throws Exception  16 { 17 ByteArrayInputStream sr = new ByteArrayInputStream (src.getBytes("UTF8")); 18 SqlParserEngine parser = new SqlParserEngine(sr); 19 parser._handler = handler; 20 21 parser.parseSqlStatement(); 22 return parser; 23 } 24 25 public String getResult() 26 { 27 return _result; 28 } 29 30 33 final public String SQLAndExpr() throws ParseException 34 { 35 String rs = "", ts; 36 ts = SQLNotExpr(); 37 rs += ts; 38 label_1 : while (true) 39 { 40 if (jj_2_1(2)) 41 { 42 ; 43 } 44 else 45 { 46 break label_1; 47 } 48 jj_consume_token(AND); 49 rs += "\nand "; 50 ts = SQLNotExpr(); 51 rs += ts; 52 } 53 { 54 if (true) return rs; 55 } 56 throw new Error ("Missing return statement in function"); 57 } 58 59 final public String SQLNotExpr() throws ParseException 60 { 61 String rs = "", ts; 62 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) 63 { 64 case NOT : 65 jj_consume_token(NOT); 66 rs += "not "; 67 break; 68 default : 69 jj_la1[0] = jj_gen; 70 ; 71 } 72 ts = SQLCompareExpr(); 73 rs += ts; 74 { 75 if (true) return rs; 76 } 77 throw new Error ("Missing return statement in function"); 78 } 79 80 final public String SQLCompareExpr() throws ParseException 81 { 82 String rs = "", ts; 83 if (jj_2_2(2147483647)) 84 { 85 ts = SQLSelect(); 86 rs += ts; 87 } 88 else if (jj_2_3(2147483647)) 89 { 90 ts = SQLIsClause(); 91 rs += ts; 92 } 93 else if (jj_2_4(2147483647)) 94 { 95 ts = SQLExistsClause(); 96 rs += ts; 97 } 98 else 99 { 100 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) 101 { 102 case NULL : 103 case INTEGER_LITERAL : 104 case FLOATING_POINT_LITERAL : 105 case STRING_LITERAL : 106 case ID : 107 case OPENPAREN : 108 case ASTERISK : 109 case PLUS : 110 case MINUS : 111 case 73 : 112 ts = SQLSumExpr(); 113 rs += ts; 114 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) 115 { 116 case BETWEEN : 117 case IN : 118 case LIKE : 119 case NOT : 120 case LESS : 121 case LESSEQUAL : 122 case GREATER : 123 case GREATEREQUAL : 124 case EQUAL : 125 case NOTEQUAL : 126 case NOTEQUAL2 : 127 case JOINPLUS : 128 ts = SQLCompareExprRight(); 129 rs += ts; 130 break; 131 default : 132 jj_la1[1] = jj_gen; 133 ; 134 } 135 break; 136 default : 137 jj_la1[2] = jj_gen; 138 jj_consume_token(-1); 139 throw new ParseException(); 140 } 141 } 142 { 143 if (true) return rs; 144 } 145 throw new Error ("Missing return statement in function"); 146 } 147 148 final public String SQLCompareExprRight() throws ParseException 149 { 150 String rs = "", ts; 151 if (jj_2_5(2)) 152 { 153 ts = SQLLikeClause(); 154 rs += ts; 155 } 156 else if (jj_2_6(2)) 157 { 158 ts = SQLInClause(); 159 rs += ts; 160 } 161 else 162 { 163 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) 164 { 165 case JOINPLUS : 166 ts = SQLLeftJoinClause(); 167 rs += ts; 168 break; 169 default : 170 jj_la1[3] = jj_gen; 171 if (jj_2_7(2147483647)) 172 { 173 ts = SQLRightJoinClause(); 174 rs += ts; 175 } 176 else if (jj_2_8(2)) 177 { 178 ts = SQLBetweenClause(); 179 rs += ts; 180 } 181 else 182 { 183 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) 184 { 185 case LESS : 186 case LESSEQUAL : 187 case GREATER : 188 case GREATEREQUAL : 189 case EQUAL : 190 case NOTEQUAL : 191 case NOTEQUAL2 : 192 ts = SQLCompareOp(); 193 rs += ts; 194 ts = SQLSumExpr(); 195 rs += ts; 196 break; 197 default : 198 jj_la1[4] = jj_gen; 199 jj_consume_token(-1); 200 throw new ParseException(); 201 } 202 } 203 } 204 } 205 { 206 if (true) return rs; 207 } 208 throw new Error ("Missing return statement in function"); 209 } 210 211 final public String SQLCompareOp() throws ParseException 212 { 213 String rs = ""; 214 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) 215 { 216 case EQUAL : 217 jj_consume_token(EQUAL); 218 rs += " = "; 219 break; 220 case NOTEQUAL : 221 jj_consume_token(NOTEQUAL); 222 rs += " != "; 223 break; 224 case NOTEQUAL2 : 225 jj_consume_token(NOTEQUAL2); 226 rs += " != "; 227 break; 228 case GREATER : 229 jj_consume_token(GREATER); 230 rs += " > "; 231 break; 232 case GREATEREQUAL : 233 jj_consume_token(GREATEREQUAL); 234 rs += " >= "; 235 break; 236 case LESS : 237 jj_consume_token(LESS); 238 rs += " < "; 239 break; 240 case LESSEQUAL : 241 jj_consume_token(LESSEQUAL); 242 rs += " <= "; 243 break; 244 default : 245 jj_la1[5] = jj_gen; 246 jj_consume_token(-1); 247 throw new ParseException(); 248 } 249 { 250 if (true) return rs; 251 } 252 throw new Error ("Missing return statement in function"); 253 } 254 255 final public String SQLBetweenClause() throws ParseException 256 { 257 String rs = "", ts; 258 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) 259 { 260 case NOT : 261 jj_consume_token(NOT); 262 rs += " not "; 263 break; 264 default : 265 jj_la1[6] = jj_gen; 266 ; 267 } 268 jj_consume_token(BETWEEN); 269 rs += " between "; 270 ts = SQLSumExpr(); 271 rs += ts; 272 jj_consume_token(AND); 273 rs += " and "; 274 ts = SQLSumExpr(); 275 rs += ts; 276 { 277 if (true) return rs; 278 } 279 throw new Error ("Missing return statement in function"); 280 } 281 282 final public String SQLReference() throws ParseException 283 { 284 String s; 285 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) 286 { 287 case ID : 288 s = SQLColumn(); 289 break; 290 case 73 : 291 s = SQLParameter(); 292 break; 293 default : 294 jj_la1[7] = jj_gen; 295 jj_consume_token(-1); 296 throw new ParseException(); 297 } 298 { 299 if (true) return s; 300 } 301 throw new Error ("Missing return statement in function"); 302 } 303 304 final public String SQLDelete() throws ParseException 305 { 306 String rs = "", ts; 307 _currentContext.push(TokenContext.STMTDELETE); 308 _handler.enterStatement(_currentContext, TokenContext.STMT_TYPE_DELETE); 309 boolean hasWhere = false; 310 jj_consume_token(DELETE); 311 jj_consume_token(FROM); 312 rs += "delete from "; 313 ts = SQLTableList(); 314 rs += ts; 315 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) 316 { 317 case WHERE : 318 ts = SQLWhere(); 319 rs += ts; 320 hasWhere = true; 321 break; 322 default : 323 jj_la1[8] = jj_gen; 324 ; 325 } 326 if (!hasWhere) 327 { 328 StringBuffer sb = new StringBuffer (); 329 _handler.handleWhereClause(_currentContext, sb); 330 if (sb.length() > 0) rs += "\nwhere " + sb.toString(); 331 } 332 333 StringBuffer theResult = new StringBuffer (rs.length()); 334 theResult.append(rs); 335 _handler.exitStatement(_currentContext, TokenContext.STMT_TYPE_DELETE, theResult); 336 _currentContext.pop(); 337 { 338 if (true) return theResult.toString(); 339 } 340 throw new Error ("Missing return statement in function"); 341 } 342 343 final public String SQLExistsClause() throws ParseException 344 { 345 String rs = "", ts; 346 _currentContext.push(TokenContext.EXISTS); 347 jj_consume_token(EXISTS); 348 rs += " exists ("; 349 jj_consume_token(OPENPAREN); 350 ts = SQLSelect(); 351 rs += ts; 352 jj_consume_token(CLOSEPAREN); 353 _currentContext.pop(); 354 rs += ") "; 355 { 356 if (true) return rs; 357 } 358 throw new Error ("Missing return statement in function"); 359 } 360 361 final public String SQLFunction() throws ParseException 362 { 363 Token otherFunction; 364 String ts; 365 _currentContext.push(TokenContext.FUNCTION); 366 StringBuffer function = new StringBuffer (50); 367 StringBuffer args = new StringBuffer (50); 368 otherFunction = jj_consume_token(ID); 369 function.append(otherFunction.image); 370 ts = SQLFunctionArgs(); 371 args.append(ts.substring(1)); args.setLength(args.length() - 1); 374 _handler.handleFunction(_currentContext, function, args); 375 376 _currentContext.pop(); 377 { 378 if (true) return function.toString() + "(" + args.toString() + ")"; 379 } 380 throw new Error ("Missing return statement in function"); 381 } 382 383 final public String SQLFunctionArgs() throws ParseException 384 { 385 String rs = "", ts; 386 _currentContext.push(TokenContext.FUNCTIONARGS); 387 jj_consume_token(OPENPAREN); 388 rs += "("; 389 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) 390 { 391 case NULL : 392 case INTEGER_LITERAL : 393 case FLOATING_POINT_LITERAL : 394 case STRING_LITERAL : 395 case ID : 396 case OPENPAREN : 397 case ASTERISK : 398 case PLUS : 399 case MINUS : 400 case 73 : 401 ts = SQLSumExpr(); 402 rs += ts; 403 label_2 : while (true) 404 { 405 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) 406 { 407 case 72 : 408 ; 409 break; 410 default : 411 jj_la1[9] = jj_gen; 412 break label_2; 413 } 414 jj_consume_token(72); 415 rs += ", "; 416 ts = SQLSumExpr(); 417 rs += ts; 418 } 419 break; 420 default : 421 jj_la1[10] = jj_gen; 422 ; 423 } 424 jj_consume_token(CLOSEPAREN); 425 _currentContext.pop(); 426 rs += ")"; 427 { 428 if (true) return rs; 429 } 430 throw new Error ("Missing return statement in function"); 431 } 432 433 final public String SQLGroupBy() throws ParseException 434 { 435 String rs = "", ts; 436 StringBuffer having = new StringBuffer (100); 437 _currentContext.push(TokenContext.GROUPBY); 438 jj_consume_token(GROUP); 439 jj_consume_token(BY); 440 rs += " group by "; 441 ts = SQLOrderByList(); 442 StringBuffer sb = new StringBuffer (100); 443 sb.append(ts); 444 _handler.handleGroupBy(_currentContext, sb); 445 rs += sb.toString(); 446 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) 447 { 448 case HAVING : 449 jj_consume_token(HAVING); 450 _currentContext.push(TokenContext.HAVING); 451 ts = SQLSumExpr(); 452 having.append(ts); 453 ts = SQLCompareExprRight(); 454 having.append(ts); 455 _currentContext.pop(); 456 break; 457 default : 458 jj_la1[11] = jj_gen; 459 ; 460 } 461 if (having.length() > 0) 462 { 463 _handler.handleHaving(_currentContext, having); 464 if (having.length() > 0) 465 { 466 rs += " having " + having.toString(); 467 } 468 } 469 _currentContext.pop(); 470 { 471 if (true) return rs; 472 } 473 throw new Error ("Missing return statement in function"); 474 } 475 476 final public String SQLInClause() throws ParseException 477 { 478 String rs = "", ts; 479 _currentContext.push(TokenContext.IN); 480 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) 481 { 482 case NOT : 483 jj_consume_token(NOT); 484 rs += " not "; 485 break; 486 default : 487 jj_la1[12] = jj_gen; 488 ; 489 } 490 jj_consume_token(IN); 491 rs += " in "; 492 jj_consume_token(OPENPAREN); 493 rs += "("; 494 ts = SQLColumnList(); 495 rs += ts; 496 jj_consume_token(CLOSEPAREN); 497 rs += ")"; 498 _currentContext.pop(); 499 { 500 if (true) return rs; 501 } 502 throw new Error ("Missing return statement in function"); 503 } 504 505 final public String SQLInsert() throws ParseException 506 { 507 String rs = "", ts; 508 _currentContext.push(TokenContext.STMTINSERT); 509 StringBuffer selCols = new StringBuffer (100); 510 _handler.enterStatement(_currentContext, TokenContext.STMT_TYPE_INSERT); 511 jj_consume_token(INSERT); 512 jj_consume_token(INTO); 513 rs += "insert into "; 514 ts = SQLTableList(); 515 rs += ts; 516 if (jj_2_9(2)) 517 { 518 jj_consume_token(OPENPAREN); 519 ts = SQLInsertCols(); 520 selCols.append(ts); 521 jj_consume_token(CLOSEPAREN); 522 } 523 else 524 { 525 ; 526 } 527 _handler.handleInsertColumnList(_currentContext, selCols); 528 ts = selCols.toString(); 529 if (ts.length() > 0) 530 { 531 rs += "(" + ts + ") "; 532 } 533 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) 534 { 535 case VALUES : 536 jj_consume_token(VALUES); 537 rs += "\nvalues"; 538 jj_consume_token(OPENPAREN); 539 rs += " ("; 540 ts = SQLValueCols(); 541 rs += ts; 542 jj_consume_token(CLOSEPAREN); 543 rs += ")"; 544 break; 545 case SELECT : 546 ts = SQLSelect(); 547 rs += "\n" + ts; 548 break; 549 default : 550 jj_la1[13] = jj_gen; 551 jj_consume_token(-1); 552 throw new ParseException(); 553 } 554 StringBuffer theResult = new StringBuffer (rs.length()); 555 theResult.append(rs); 556 _handler.exitStatement(_currentContext, TokenContext.STMT_TYPE_INSERT, theResult); 557 _currentContext.pop(); 558 { 559 if (true) return theResult.toString(); 560 } 561 throw new Error ("Missing return statement in function"); 562 } 563 564 final public String SQLIsClause() throws ParseException 565 { 566 String rs = "", ts; 567 ts = SQLReference(); 568 rs += ts; 569 jj_consume_token(IS); 570 rs += " is"; 571 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) 572 { 573 case NOT : 574 jj_consume_token(NOT); 575 rs += " not"; 576 break; 577 default : 578 jj_la1[14] = jj_gen; 579 ; 580 } 581 jj_consume_token(NULL); 582 rs += " null"; 583 { 584 if (true) return rs; 585 } 586 throw new Error ("Missing return statement in function"); 587 } 588 589 final public String SQLLeftJoinClause() throws ParseException 590 { 591 String rs = "", ts; 592 jj_consume_token(JOINPLUS); 593 ts = SQLCompareOp(); 594 rs += " (+)"; 595 rs += ts; 596 ts = SQLSumExpr(); 597 rs += ts; 598 { 599 if (true) return rs; 600 } 601 throw new Error ("Missing return statement in function"); 602 } 603 604 final public String SQLLikeClause() throws ParseException 605 { 606 String rs = "", ts; 607 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) 608 { 609 case NOT : 610 jj_consume_token(NOT); 611 rs += " not"; 612 break; 613 default : 614 jj_la1[15] = jj_gen; 615 ; 616 } 617 jj_consume_token(LIKE); 618 rs += " like "; 619 ts = SQLPattern(); 620 rs += ts; 621 { 622 if (true) return rs; 623 } 624 throw new Error ("Missing return statement in function"); 625 } 626 627 final public String SQLLiteral() throws ParseException 628 { 629 Token x; 630 String rs = ""; 631 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) 632 { 633 case STRING_LITERAL : 634 x = jj_consume_token(STRING_LITERAL); 635 rs += x.image; 636 break; 637 case INTEGER_LITERAL : 638 x = jj_consume_token(INTEGER_LITERAL); 639 rs += x.image; 640 break; 641 case FLOATING_POINT_LITERAL : 642 x = jj_consume_token(FLOATING_POINT_LITERAL); 643 rs += x.image; 644 break; 645 case ASTERISK : 646 x = jj_consume_token(ASTERISK); 647 rs += x.image; 648 break; 649 case NULL : 650 x = jj_consume_token(NULL); 651 rs += x.image; 652 break; 653 default : 654 jj_la1[16] = jj_gen; 655 jj_consume_token(-1); 656 throw new ParseException(); 657 } 658 { 659 if (true) return rs; 660 } 661 throw new Error ("Missing return statement in function"); 662 } 663 664 final public String SQLColumn() throws ParseException 665 { 666 Token x, y; 667 String s = ""; 668 StringBuffer tableAlias = new StringBuffer (50); 669 StringBuffer columnName = new StringBuffer (50); 670 x = jj_consume_token(ID); 671 columnName.append(x.image); 672 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) 673 { 674 case DOT : 675 jj_consume_token(DOT); 676 y = jj_consume_token(ID); 677 tableAlias.append(x.image); 678 columnName.setLength(0); 679 columnName.append(y.image); 680 break; 681 default : 682 jj_la1[17] = jj_gen; 683 ; 684 } 685 _handler.handleColumn(_currentContext, tableAlias, columnName); 686 if (tableAlias.length() > 0) 687 { 688 s = tableAlias.toString() + "."; 689 } 690 s += columnName.toString(); 691 { 692 if (true) return s; 693 } 694 throw new Error ("Missing return statement in function"); 695 } 696 697 final public String SQLParameter() throws ParseException 698 { 699 Token x, y; 700 StringBuffer parameterName = new StringBuffer (50); 701 jj_consume_token(73); 702 x = jj_consume_token(ID); 703 parameterName.append(x.image); 704 label_3 : while (true) 705 { 706 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) 707 { 708 case DOT : 709 ; 710 break; 711 default : 712 jj_la1[18] = jj_gen; 713 break label_3; 714 } 715 jj_consume_token(DOT); 716 y = jj_consume_token(ID); 717 parameterName.append("."); 718 parameterName.append(y.image); 719 } 720 _handler.handleParameter(_currentContext, parameterName); 721 { 722 if (true) return ":" + parameterName.toString() + " "; 723 } 724 throw new Error ("Missing return statement in function"); 725 } 726 727 final public String SQLOrderBy() throws ParseException 728 { 729 String rs = "", ts; 730 _currentContext.push(TokenContext.ORDERBY); 731 jj_consume_token(ORDER); 732 jj_consume_token(BY); 733 rs += "\norder by "; 734 ts = SQLOrderByList(); 735 StringBuffer sb = new StringBuffer (100); 736 sb.append(ts); 737 _handler.handleOrderBy(_currentContext, sb); 738 rs += sb.toString(); 739 if (sb.length() == 0) rs = ""; 740 _currentContext.pop(); 741 { 742 if (true) return rs; 743 } 744 throw new Error ("Missing return statement in function"); 745 } 746 747 final public String SQLOrderByElem() throws ParseException 748 { 749 String rs = "", ts; 750 Token tok; 751 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) 752 { 753 case ID : 754 case 73 : 755 ts = SQLReference(); 756 rs += ts; 757 break; 758 case INTEGER_LITERAL : 759 tok = jj_consume_token(INTEGER_LITERAL); 760 rs += tok.image; 761 break; 762 default : 763 jj_la1[19] = jj_gen; 764 jj_consume_token(-1); 765 throw new ParseException(); 766 } 767 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) 768 { 769 case ASC : 770 case DESC : 771 ts = SQLOrderDirection(); 772 rs += ts; 773 break; 774 default : 775 jj_la1[20] = jj_gen; 776 ; 777 } 778 { 779 if (true) return rs; 780 } 781 throw new Error ("Missing return statement in function"); 782 } 783 784 final public String SQLOrderByList() throws ParseException 785 { 786 String rs = "", ts; 787 ts = SQLOrderByElem(); 788 rs += ts; 789 label_4 : while (true) 790 { 791 if (jj_2_10(2)) 792 { 793 ; 794 } 795 else 796 { 797 break label_4; 798 } 799 jj_consume_token(72); 800 rs += ", "; 801 ts = SQLOrderByElem(); 802 rs += ts; 803 } 804 { 805 if (true) return rs; 806 } 807 throw new Error ("Missing return statement in function"); 808 } 809 810 final public String SQLOrderDirection() throws ParseException 811 { 812 String rs = ""; 813 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) 814 { 815 case ASC : 816 jj_consume_token(ASC); 817 rs += " asc"; 818 break; 819 case DESC : 820 jj_consume_token(DESC); 821 rs += " desc"; 822 break; 823 default : 824 jj_la1[21] = jj_gen; 825 jj_consume_token(-1); 826 throw new ParseException(); 827 } 828 { 829 if (true) return rs; 830 } 831 throw new Error ("Missing return statement in function"); 832 } 833 834 final public String SQLOrExpr() throws ParseException 835 { 836 String rs = "", ts; 837 ts = SQLAndExpr(); 838 rs += ts; 839 label_5 : while (true) 840 { 841 if (jj_2_11(2)) 842 { 843 ; 844 } 845 else 846 { 847 break label_5; 848 } 849 jj_consume_token(OR); 850 rs += "\nor "; 851 ts = SQLAndExpr(); 852 rs += ts; 853 } 854 { 855 if (true) return rs; 856 } 857 throw new Error ("Missing return statement in function"); 858 } 859 860 final public String SQLPattern() throws ParseException 861 { 862 Token x; 863 String rs = "", ts; 864 switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) 865 { 866 case STRING_LITERAL : 867 x = jj_consume_token(STRING_LITERAL); 868 rs += x.image; 869 break; 870 case QUESTIONMARK : 871 jj_consume_token(QUESTIONMARK); 872 rs += "?"; 873 break; 874 case 73 : 875 ts = SQLParameter(); 876 rs += ts; 877 break; 878 case ID : 879 ts = SQLColumn(); 880 rs += ts; 881 break; 882 default : 883 jj_la1[22] = jj_gen; 884 jj_consume_token(-1); 885 throw new ParseException(); 886 } 887 { 888 if (true) return rs; 889 } 890 throw new Error ("Missing return statement in function"); 891
|