Skip to content

Commit a50976f

Browse files
committed
Bug fix: SQL editor: UNION keyword was marked red as error.
Feature: Upgraded JSQL-Parser (https://github.com/JSQLParser/JSqlParser) to version 5.1.
1 parent d63ad70 commit a50976f

File tree

7 files changed

+113
-40
lines changed

7 files changed

+113
-40
lines changed

sql12/core/doc/changes.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Not yet released, available in our GIT repository, snapshots and future releases
66

77
Enhancements:
88

9+
Upgraded JSQL-Parser (https://github.com/JSQLParser/JSqlParser) to version 5.1.
10+
911
Look and Feel Plugin: The Look and Feel preferences now allows to adjust several controls,
1012
e.g the SQL editor and the message panel, to dark Look and Feels.
1113
See menu File -- > Global Preferences --> tab "L & F"
@@ -128,6 +130,8 @@ Table cell data popup now offers find, Xml/Json-reformatting and export function
128130

129131
Bug fixes:
130132

133+
SQL editor: UNION keyword was marked red as error.
134+
131135
Look and Feel Plugin: Cleaned up L&F Preferences UI.
132136

133137
#1532 MS Excel export: Fixed NoSuchMethodError

sql12/core/lib/jsqlparser-4.3.jar

-848 KB
Binary file not shown.

sql12/core/lib/jsqlparser-5.1.jar

1.11 MB
Binary file not shown.

sql12/core/src/net/sourceforge/squirrel_sql/client/session/parser/kernel/JSqlParserAdapter.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package net.sourceforge.squirrel_sql.client.session.parser.kernel;
22

3-
import java.util.ArrayList;
4-
53
import net.sf.jsqlparser.parser.CCJSqlParser;
64
import net.sf.jsqlparser.parser.ParseException;
75
import net.sf.jsqlparser.parser.StringProvider;
86
import net.sf.jsqlparser.schema.Table;
97
import net.sf.jsqlparser.statement.Statement;
108
import net.sf.jsqlparser.util.TablesNamesFinder;
119

10+
import java.util.ArrayList;
11+
1212
public class JSqlParserAdapter
1313
{
1414
static ParsingResult executeParsing(StatementBounds statementBounds) throws ParseException
@@ -20,14 +20,24 @@ static ParsingResult executeParsing(StatementBounds statementBounds) throws Pars
2020

2121
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder()
2222
{
23+
//@Override
24+
//public void visit(Table table)
25+
//{
26+
// if( null != table )
27+
// {
28+
// tables.add(table);
29+
// }
30+
// super.visit(table);
31+
//}
32+
2333
@Override
24-
public void visit(Table table)
34+
protected String extractTableName(Table table)
2535
{
26-
if( null != table )
36+
if( null != table && false == tables.contains(table))
2737
{
28-
super.visit(table);
2938
tables.add(table);
3039
}
40+
return super.extractTableName(table);
3141
}
3242
};
3343

sql12/core/src/net/sourceforge/squirrel_sql/client/session/parser/kernel/StatementBeginPrediction.java

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package net.sourceforge.squirrel_sql.client.session.parser.kernel;
2+
3+
public class StatementBeginPredictionUtil
4+
{
5+
static boolean startsWithIgnoreCase(String sqlEditorText, int beginPos, String keyWord)
6+
{
7+
int curPos = beginPos;
8+
int endPos;
9+
10+
if(beginPos == 0)
11+
{
12+
// Either are at teh beginning ...
13+
curPos = 0;
14+
}
15+
else if(Character.isWhitespace(sqlEditorText.charAt(beginPos - 1)))
16+
{
17+
// or a white space must be in front of the keyword.
18+
curPos = beginPos;
19+
}
20+
else
21+
{
22+
return false;
23+
}
24+
25+
if(sqlEditorText.length() == curPos + keyWord.length())
26+
{
27+
endPos = curPos + keyWord.length();
28+
}
29+
else if(sqlEditorText.length() > curPos + keyWord.length() && Character.isWhitespace(sqlEditorText.charAt(curPos + keyWord.length())))
30+
{
31+
endPos = curPos + keyWord.length();
32+
}
33+
else
34+
{
35+
return false;
36+
}
37+
38+
return keyWord.equalsIgnoreCase(sqlEditorText.substring(curPos, endPos));
39+
}
40+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package net.sourceforge.squirrel_sql.client.session.parser.kernel;
2+
3+
public class UnionKeyWordCheck
4+
{
5+
private boolean _inUnion;
6+
7+
public boolean previousWasUnionOrUnionAll()
8+
{
9+
return _inUnion ;
10+
}
11+
12+
public void reset()
13+
{
14+
_inUnion = false;
15+
}
16+
17+
public void check(String sqlEditorText, int beginPos)
18+
{
19+
if(false == _inUnion)
20+
{
21+
_inUnion = StatementBeginPredictionUtil.startsWithIgnoreCase(sqlEditorText, beginPos, "UNION");
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)