@@ -12,6 +12,8 @@ static int predictNextStatementBegin(String sqlEditorText, int startPos, ParseTe
1212 int openBracketsCount = 0 ;
1313 int literalDelimsCount = 0 ;
1414
15+ UnionKeyWordCheck unionKeyWordCheck = new UnionKeyWordCheck ();
16+
1517 while ( sqlEditorText .length () > ret )
1618 {
1719 if ('\'' == sqlEditorText .charAt (ret ))
@@ -24,22 +26,21 @@ static int predictNextStatementBegin(String sqlEditorText, int startPos, ParseTe
2426
2527 if (false == sqlCommentHelper .isInComment (ret ) && false == SqlLiteralHelper .isInLiteral (literalDelimsCount ))
2628 {
27- if ('(' == sqlEditorText .charAt (ret ))
29+ if ('(' == sqlEditorText .charAt (ret ))
2830 {
2931 ++openBracketsCount ;
3032 }
31- else if (')' == sqlEditorText .charAt (ret ))
33+ else if (')' == sqlEditorText .charAt (ret ))
3234 {
33- --openBracketsCount ;
35+ --openBracketsCount ;
3436 }
3537 }
3638
37-
3839 if (
3940 false == SqlLiteralHelper .isInLiteral (literalDelimsCount )
4041 && false == sqlCommentHelper .isInComment (ret )
4142 && false == isInBrackets (openBracketsCount )
42- && startsWithBeginKeyWord (sqlEditorText , ret )
43+ && startsWithBeginKeyWord (sqlEditorText , ret , unionKeyWordCheck )
4344 )
4445 {
4546 break ;
@@ -62,50 +63,44 @@ private static boolean isInBrackets(int openBracketsCount)
6263 }
6364
6465
65- private static boolean startsWithBeginKeyWord (String sqlEditorText , int ret )
66+ private static boolean startsWithBeginKeyWord (String sqlEditorText , int beginPos , UnionKeyWordCheck unionKeyWordCheck )
6667 {
67- return startsWithIgnoreCase (sqlEditorText ,ret , "SELECT" )
68- || startsWithIgnoreCase (sqlEditorText , ret , "UPDATE" )
69- || startsWithIgnoreCase (sqlEditorText , ret , "DELETE" )
70- || startsWithIgnoreCase (sqlEditorText , ret , "INSERT" )
71- || startsWithIgnoreCase (sqlEditorText , ret , "ALTER" )
72- || startsWithIgnoreCase (sqlEditorText , ret , "CREATE" )
73- || startsWithIgnoreCase (sqlEditorText , ret , "DROP" );
74- }
7568
76- private static boolean startsWithIgnoreCase (String sqlEditorText , int ret , String keyWord )
77- {
78- int beginPos = ret ;
79- int endPos ;
69+ boolean ret = isSelectBegin (sqlEditorText , beginPos , unionKeyWordCheck )
70+ || StatementBeginPredictionUtil .startsWithIgnoreCase (sqlEditorText , beginPos , "UPDATE" )
71+ || StatementBeginPredictionUtil .startsWithIgnoreCase (sqlEditorText , beginPos , "DELETE" )
72+ || StatementBeginPredictionUtil .startsWithIgnoreCase (sqlEditorText , beginPos , "INSERT" )
73+ || StatementBeginPredictionUtil .startsWithIgnoreCase (sqlEditorText , beginPos , "ALTER" )
74+ || StatementBeginPredictionUtil .startsWithIgnoreCase (sqlEditorText , beginPos , "CREATE" )
75+ || StatementBeginPredictionUtil .startsWithIgnoreCase (sqlEditorText , beginPos , "DROP" );
8076
81- if (ret == 0 )
77+ if (ret )
8278 {
83- // Either are at teh beginning ...
84- beginPos = 0 ;
79+ unionKeyWordCheck .reset ();
8580 }
86- else if (Character .isWhitespace (sqlEditorText .charAt (ret -1 )))
87- {
88- // or a white space must be in front of the keyword.
89- beginPos = ret ;
90- }
91- else
81+
82+ return ret ;
83+ }
84+
85+ private static boolean isSelectBegin (String sqlEditorText , int beginPos , UnionKeyWordCheck unionKeyWordCheck )
86+ {
87+ unionKeyWordCheck .check (sqlEditorText , beginPos );
88+ boolean isSelectStart = StatementBeginPredictionUtil .startsWithIgnoreCase (sqlEditorText , beginPos , "SELECT" );
89+
90+ if (false == isSelectStart )
9291 {
9392 return false ;
9493 }
9594
96- if (sqlEditorText . length () == beginPos + keyWord . length ())
95+ if (false == unionKeyWordCheck . previousWasUnionOrUnionAll ())
9796 {
98- endPos = beginPos + keyWord .length ();
99- }
100- else if (sqlEditorText .length () > beginPos + keyWord .length () && Character .isWhitespace (sqlEditorText .charAt (beginPos + keyWord .length ())))
101- {
102- endPos = beginPos + keyWord .length ();
97+ return true ;
10398 }
10499 else
105100 {
101+ unionKeyWordCheck .reset ();
106102 return false ;
107103 }
108-
109- return keyWord .equalsIgnoreCase (sqlEditorText .substring (beginPos , endPos ));
110104 }
105+
111106}
0 commit comments