Unverified 提交 cc9c9bb1 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov 提交者: GitHub

Merge pull request #1275 from katzyn/misc

Do not quote other lower case characters
...@@ -3911,18 +3911,18 @@ public class Parser { ...@@ -3911,18 +3911,18 @@ public class Parser {
} }
case CHAR_SPECIAL_2: case CHAR_SPECIAL_2:
if (types[i] == CHAR_SPECIAL_2) { if (types[i] == CHAR_SPECIAL_2) {
i++; char c1 = chars[i++];
currentToken = sqlCommand.substring(start, i); currentToken = sqlCommand.substring(start, i);
currentTokenType = getSpecialType2(currentToken); currentTokenType = getSpecialType2(c, c1);
} else { } else {
currentToken = sqlCommand.substring(start, i); currentToken = sqlCommand.substring(start, i);
currentTokenType = getSpecialType1(currentToken); currentTokenType = getSpecialType1(c);
} }
parseIndex = i; parseIndex = i;
return; return;
case CHAR_SPECIAL_1: case CHAR_SPECIAL_1:
currentToken = sqlCommand.substring(start, i); currentToken = sqlCommand.substring(start, i);
currentTokenType = getSpecialType1(currentToken); currentTokenType = getSpecialType1(c);
parseIndex = i; parseIndex = i;
return; return;
case CHAR_VALUE: case CHAR_VALUE:
...@@ -4352,8 +4352,8 @@ public class Parser { ...@@ -4352,8 +4352,8 @@ public class Parser {
} }
} }
private int getSpecialType1(String s) { private int getSpecialType1(char c0) {
switch (s.charAt(0)) { switch (c0) {
case '?': case '?':
case '$': case '$':
return PARAMETER; return PARAMETER;
...@@ -4392,9 +4392,8 @@ public class Parser { ...@@ -4392,9 +4392,8 @@ public class Parser {
} }
} }
private int getSpecialType2(String s) { private int getSpecialType2(char c0, char c1) {
char c1 = s.charAt(1); switch (c0) {
switch (s.charAt(0)) {
case ':': case ':':
if (c1 == ':' || c1 == '=') { if (c1 == ':' || c1 == '=') {
return KEYWORD; return KEYWORD;
......
...@@ -212,6 +212,16 @@ public class ParserUtil { ...@@ -212,6 +212,16 @@ public class ParserUtil {
*/ */
public static final int WITH = WHERE + 1; public static final int WITH = WHERE + 1;
private static final int UPPER_OR_OTHER_LETTER =
1 << Character.UPPERCASE_LETTER
| 1 << Character.TITLECASE_LETTER
| 1 << Character.MODIFIER_LETTER
| 1 << Character.OTHER_LETTER;
private static final int UPPER_OR_OTHER_LETTER_OR_DIGIT =
UPPER_OR_OTHER_LETTER
| 1 << Character.DECIMAL_DIGIT_NUMBER;
private ParserUtil() { private ParserUtil() {
// utility class // utility class
} }
...@@ -223,7 +233,7 @@ public class ParserUtil { ...@@ -223,7 +233,7 @@ public class ParserUtil {
* @return true if it is a keyword * @return true if it is a keyword
*/ */
public static boolean isKeyword(String s) { public static boolean isKeyword(String s) {
if (s == null || s.length() == 0) { if (s.length() == 0) {
return false; return false;
} }
return getSaveTokenType(s, false) != IDENTIFIER; return getSaveTokenType(s, false) != IDENTIFIER;
...@@ -242,13 +252,12 @@ public class ParserUtil { ...@@ -242,13 +252,12 @@ public class ParserUtil {
} }
char c = s.charAt(0); char c = s.charAt(0);
// lowercase a-z is quoted as well // lowercase a-z is quoted as well
if ((!Character.isLetter(c) && c != '_') || Character.isLowerCase(c)) { if ((UPPER_OR_OTHER_LETTER >>> Character.getType(c) & 1) == 0 && c != '_') {
return false; return false;
} }
for (int i = 1, length = s.length(); i < length; i++) { for (int i = 1, length = s.length(); i < length; i++) {
c = s.charAt(i); c = s.charAt(i);
if ((!Character.isLetterOrDigit(c) && c != '_') || if ((UPPER_OR_OTHER_LETTER_OR_DIGIT >>> Character.getType(c) & 1) == 0 && c != '_') {
Character.isLowerCase(c)) {
return false; return false;
} }
} }
...@@ -270,7 +279,10 @@ public class ParserUtil { ...@@ -270,7 +279,10 @@ public class ParserUtil {
*/ */
switch (s.charAt(0)) { switch (s.charAt(0)) {
case 'A': case 'A':
return getKeywordOrIdentifier(s, "ALL", ALL); if ("ALL".equals(s)) {
return ALL;
}
return IDENTIFIER;
case 'C': case 'C':
if ("CHECK".equals(s)) { if ("CHECK".equals(s)) {
return CHECK; return CHECK;
...@@ -286,12 +298,17 @@ public class ParserUtil { ...@@ -286,12 +298,17 @@ public class ParserUtil {
} }
return IDENTIFIER; return IDENTIFIER;
case 'D': case 'D':
return getKeywordOrIdentifier(s, "DISTINCT", DISTINCT); if ("DISTINCT".equals(s)) {
return DISTINCT;
}
return IDENTIFIER;
case 'E': case 'E':
if ("EXCEPT".equals(s)) { if ("EXCEPT".equals(s)) {
return EXCEPT; return EXCEPT;
} else if ("EXISTS".equals(s)) {
return EXISTS;
} }
return getKeywordOrIdentifier(s, "EXISTS", EXISTS); return IDENTIFIER;
case 'F': case 'F':
if ("FETCH".equals(s)) { if ("FETCH".equals(s)) {
return FETCH; return FETCH;
...@@ -303,12 +320,20 @@ public class ParserUtil { ...@@ -303,12 +320,20 @@ public class ParserUtil {
return FOREIGN; return FOREIGN;
} else if ("FULL".equals(s)) { } else if ("FULL".equals(s)) {
return FULL; return FULL;
} else if ("FALSE".equals(s)) {
return FALSE;
} }
return getKeywordOrIdentifier(s, "FALSE", FALSE); return IDENTIFIER;
case 'G': case 'G':
return getKeywordOrIdentifier(s, "GROUP", GROUP); if ("GROUP".equals(s)) {
return GROUP;
}
return IDENTIFIER;
case 'H': case 'H':
return getKeywordOrIdentifier(s, "HAVING", HAVING); if ("HAVING".equals(s)) {
return HAVING;
}
return IDENTIFIER;
case 'I': case 'I':
if ("INNER".equals(s)) { if ("INNER".equals(s)) {
return INNER; return INNER;
...@@ -324,7 +349,10 @@ public class ParserUtil { ...@@ -324,7 +349,10 @@ public class ParserUtil {
} }
return IDENTIFIER; return IDENTIFIER;
case 'J': case 'J':
return getKeywordOrIdentifier(s, "JOIN", JOIN); if ("JOIN".equals(s)) {
return JOIN;
}
return IDENTIFIER;
case 'L': case 'L':
if ("LIMIT".equals(s)) { if ("LIMIT".equals(s)) {
return LIMIT; return LIMIT;
...@@ -338,25 +366,38 @@ public class ParserUtil { ...@@ -338,25 +366,38 @@ public class ParserUtil {
} }
return IDENTIFIER; return IDENTIFIER;
case 'M': case 'M':
return getKeywordOrIdentifier(s, "MINUS", MINUS); if ("MINUS".equals(s)) {
return MINUS;
}
return IDENTIFIER;
case 'N': case 'N':
if ("NOT".equals(s)) { if ("NOT".equals(s)) {
return NOT; return NOT;
} else if ("NATURAL".equals(s)) { } else if ("NATURAL".equals(s)) {
return NATURAL; return NATURAL;
} else if ("NULL".equals(s)) {
return NULL;
} }
return getKeywordOrIdentifier(s, "NULL", NULL); return IDENTIFIER;
case 'O': case 'O':
if ("OFFSET".equals(s)) { if ("OFFSET".equals(s)) {
return OFFSET; return OFFSET;
} else if ("ON".equals(s)) { } else if ("ON".equals(s)) {
return ON; return ON;
} else if ("ORDER".equals(s)) {
return ORDER;
} }
return getKeywordOrIdentifier(s, "ORDER", ORDER); return IDENTIFIER;
case 'P': case 'P':
return getKeywordOrIdentifier(s, "PRIMARY", PRIMARY); if ("PRIMARY".equals(s)) {
return PRIMARY;
}
return IDENTIFIER;
case 'R': case 'R':
return getKeywordOrIdentifier(s, "ROWNUM", ROWNUM); if ("ROWNUM".equals(s)) {
return ROWNUM;
}
return IDENTIFIER;
case 'S': case 'S':
if ("SELECT".equals(s)) { if ("SELECT".equals(s)) {
return SELECT; return SELECT;
...@@ -380,24 +421,20 @@ public class ParserUtil { ...@@ -380,24 +421,20 @@ public class ParserUtil {
case 'U': case 'U':
if ("UNIQUE".equals(s)) { if ("UNIQUE".equals(s)) {
return UNIQUE; return UNIQUE;
} else if ("UNION".equals(s)) {
return UNION;
} }
return getKeywordOrIdentifier(s, "UNION", UNION); return IDENTIFIER;
case 'W': case 'W':
if ("WITH".equals(s)) { if ("WITH".equals(s)) {
return WITH; return WITH;
} else if ("WHERE".equals(s)) {
return WHERE;
} }
return getKeywordOrIdentifier(s, "WHERE", WHERE); return IDENTIFIER;
default: default:
return IDENTIFIER; return IDENTIFIER;
} }
} }
private static int getKeywordOrIdentifier(String s1, String s2,
int keywordType) {
if (s1.equals(s2)) {
return keywordType;
}
return IDENTIFIER;
}
} }
...@@ -336,12 +336,16 @@ public class TestStatement extends TestDb { ...@@ -336,12 +336,16 @@ public class TestStatement extends TestDb {
assertEquals("\"FROM\"", statBC.enquoteIdentifier("FROM", false)); assertEquals("\"FROM\"", statBC.enquoteIdentifier("FROM", false));
assertEquals("\"Test\"", statBC.enquoteIdentifier("Test", false)); assertEquals("\"Test\"", statBC.enquoteIdentifier("Test", false));
assertEquals("\"TODAY\"", statBC.enquoteIdentifier("TODAY", false)); assertEquals("\"TODAY\"", statBC.enquoteIdentifier("TODAY", false));
// Other lower case characters don't have upper case mappings
assertEquals("\u02B0", statBC.enquoteIdentifier("\u02B0", false));
assertTrue(statBC.isSimpleIdentifier("SOME_ID")); assertTrue(statBC.isSimpleIdentifier("SOME_ID"));
assertFalse(statBC.isSimpleIdentifier("SOME ID")); assertFalse(statBC.isSimpleIdentifier("SOME ID"));
assertFalse(statBC.isSimpleIdentifier("FROM")); assertFalse(statBC.isSimpleIdentifier("FROM"));
assertFalse(statBC.isSimpleIdentifier("Test")); assertFalse(statBC.isSimpleIdentifier("Test"));
assertFalse(statBC.isSimpleIdentifier("TODAY")); assertFalse(statBC.isSimpleIdentifier("TODAY"));
// Other lower case characters don't have upper case mappings
assertTrue(statBC.isSimpleIdentifier("\u02B0"));
stat.close(); stat.close();
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论