Unverified 提交 5427bf61 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #788 from katzyn/keywords

Update lists of keywords
...@@ -500,10 +500,10 @@ There is a list of keywords that can't be used as identifiers (table names, colu ...@@ -500,10 +500,10 @@ There is a list of keywords that can't be used as identifiers (table names, colu
unless they are quoted (surrounded with double quotes). The list is currently: unless they are quoted (surrounded with double quotes). The list is currently:
</p><p> </p><p>
<code> <code>
CROSS, CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP, DISTINCT, EXCEPT, EXISTS, FALSE, ALL, CHECK, CONSTRAINT, CROSS, CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP, DISTINCT, EXCEPT,
FETCH, FOR, FROM, FULL, GROUP, HAVING, INNER, INTERSECT, IS, JOIN, LIKE, LIMIT, MINUS, NATURAL, EXISTS, FALSE, FETCH, FOR, FOREIGN, FROM, FULL, GROUP, HAVING, INNER, INTERSECT, IS, JOIN,
NOT, NULL, OFFSET, ON, ORDER, PRIMARY, ROWNUM, SELECT, SYSDATE, SYSTIME, SYSTIMESTAMP, TODAY, LIKE, LIMIT, MINUS, NATURAL, NOT, NULL, OFFSET, ON, ORDER, PRIMARY, ROWNUM, SELECT, SYSDATE,
TRUE, UNION, UNIQUE, WHERE SYSTIME, SYSTIMESTAMP, TODAY, TRUE, UNION, UNIQUE, WHERE, WITH
</code> </code>
</p><p> </p><p>
Certain words of this list are keywords because they are functions that can be used without '()' for compatibility, Certain words of this list are keywords because they are functions that can be used without '()' for compatibility,
......
...@@ -4146,7 +4146,7 @@ public class Parser { ...@@ -4146,7 +4146,7 @@ public class Parser {
// if not yet converted to uppercase, do it now // if not yet converted to uppercase, do it now
s = StringUtils.toUpperEnglish(s); s = StringUtils.toUpperEnglish(s);
} }
return getSaveTokenType(s, database.getMode().supportOffsetFetch); return getSaveTokenType(s, database.getMode().supportOffsetFetch, false);
} }
private boolean isKeyword(String s) { private boolean isKeyword(String s) {
...@@ -4168,20 +4168,27 @@ public class Parser { ...@@ -4168,20 +4168,27 @@ public class Parser {
if (s == null || s.length() == 0) { if (s == null || s.length() == 0) {
return false; return false;
} }
return getSaveTokenType(s, supportOffsetFetch) != IDENTIFIER; return getSaveTokenType(s, supportOffsetFetch, false) != IDENTIFIER;
} }
private static int getSaveTokenType(String s, boolean supportOffsetFetch) { private static int getSaveTokenType(String s, boolean supportOffsetFetch, boolean functionsAsKeywords) {
switch (s.charAt(0)) { switch (s.charAt(0)) {
case 'A': case 'A':
return getKeywordOrIdentifier(s, "ALL", KEYWORD); return getKeywordOrIdentifier(s, "ALL", KEYWORD);
case 'C': case 'C':
if (s.equals("CHECK")) { if ("CHECK".equals(s)) {
return KEYWORD; return KEYWORD;
} else if ("CONSTRAINT".equals(s)) { } else if ("CONSTRAINT".equals(s)) {
return KEYWORD; return KEYWORD;
} else if ("CROSS".equals(s)) {
return KEYWORD;
}
if (functionsAsKeywords) {
if ("CURRENT_DATE".equals(s) || "CURRENT_TIME".equals(s) || "CURRENT_TIMESTAMP".equals(s)) {
return KEYWORD;
}
} }
return getKeywordOrIdentifier(s, "CROSS", KEYWORD); return IDENTIFIER;
case 'D': case 'D':
return getKeywordOrIdentifier(s, "DISTINCT", KEYWORD); return getKeywordOrIdentifier(s, "DISTINCT", KEYWORD);
case 'E': case 'E':
...@@ -4241,9 +4248,25 @@ public class Parser { ...@@ -4241,9 +4248,25 @@ public class Parser {
case 'R': case 'R':
return getKeywordOrIdentifier(s, "ROWNUM", ROWNUM); return getKeywordOrIdentifier(s, "ROWNUM", ROWNUM);
case 'S': case 'S':
return getKeywordOrIdentifier(s, "SELECT", KEYWORD); if ("SELECT".equals(s)) {
return KEYWORD;
}
if (functionsAsKeywords) {
if ("SYSDATE".equals(s) || "SYSTIME".equals(s) || "SYSTIMESTAMP".equals(s)) {
return KEYWORD;
}
}
return IDENTIFIER;
case 'T': case 'T':
return getKeywordOrIdentifier(s, "TRUE", TRUE); if ("TRUE".equals(s)) {
return TRUE;
}
if (functionsAsKeywords) {
if ("TODAY".equals(s)) {
return KEYWORD;
}
}
return IDENTIFIER;
case 'U': case 'U':
if ("UNIQUE".equals(s)) { if ("UNIQUE".equals(s)) {
return KEYWORD; return KEYWORD;
...@@ -6851,7 +6874,7 @@ public class Parser { ...@@ -6851,7 +6874,7 @@ public class Parser {
if (s == null) { if (s == null) {
return "\"\""; return "\"\"";
} }
if (isSimpleIdentifier(s)) if (isSimpleIdentifier(s, false))
return s; return s;
return StringUtils.quoteIdentifier(s); return StringUtils.quoteIdentifier(s);
} }
...@@ -6859,10 +6882,12 @@ public class Parser { ...@@ -6859,10 +6882,12 @@ public class Parser {
/** /**
* @param s * @param s
* identifier to check * identifier to check
* @param functionsAsKeywords
* treat system functions as keywords
* @return is specified identifier may be used without quotes * @return is specified identifier may be used without quotes
* @throws NullPointerException if s is {@code null} * @throws NullPointerException if s is {@code null}
*/ */
public static boolean isSimpleIdentifier(String s) { public static boolean isSimpleIdentifier(String s, boolean functionsAsKeywords) {
if (s.length() == 0) { if (s.length() == 0) {
return false; return false;
} }
...@@ -6878,7 +6903,7 @@ public class Parser { ...@@ -6878,7 +6903,7 @@ public class Parser {
return false; return false;
} }
} }
return !isKeyword(s, true); return getSaveTokenType(s, true, functionsAsKeywords) == IDENTIFIER;
} }
public void setLiteralsChecked(boolean literalsChecked) { public void setLiteralsChecked(boolean literalsChecked) {
......
...@@ -1536,18 +1536,18 @@ public class JdbcDatabaseMetaData extends TraceObject implements ...@@ -1536,18 +1536,18 @@ public class JdbcDatabaseMetaData extends TraceObject implements
/** /**
* Gets the comma-separated list of all SQL keywords that are not supported * Gets the comma-separated list of all SQL keywords that are not supported
* as table/column/index name, in addition to the SQL-92 keywords. The list * as table/column/index name, in addition to the SQL-2003 keywords. The list
* returned is: * returned is:
* <pre> * <pre>
* LIMIT,MINUS,ROWNUM,SYSDATE,SYSTIME,SYSTIMESTAMP,TODAY * LIMIT,MINUS,OFFSET,ROWNUM,SYSDATE,SYSTIME,SYSTIMESTAMP,TODAY
* </pre> * </pre>
* The complete list of keywords (including SQL-92 keywords) is: * The complete list of keywords (including SQL-2003 keywords) is:
* <pre> * <pre>
* CROSS, CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP, DISTINCT, * ALL, CHECK, CONSTRAINT, CROSS, CURRENT_DATE, CURRENT_TIME,
* EXCEPT, EXISTS, FALSE, FOR, FROM, FULL, GROUP, HAVING, INNER, * CURRENT_TIMESTAMP, DISTINCT, EXCEPT, EXISTS, FALSE, FETCH, FOR, FOREIGN,
* INTERSECT, IS, JOIN, LIKE, LIMIT, MINUS, NATURAL, NOT, NULL, ON, * FROM, FULL, GROUP, HAVING, INNER, INTERSECT, IS, JOIN, LIKE, LIMIT,
* ORDER, PRIMARY, ROWNUM, SELECT, SYSDATE, SYSTIME, SYSTIMESTAMP, * MINUS, NATURAL, NOT, NULL, OFFSET, ON, ORDER, PRIMARY, ROWNUM, SELECT,
* TODAY, TRUE, UNION, UNIQUE, WHERE * SYSDATE, SYSTIME, SYSTIMESTAMP, TODAY, TRUE, UNION, UNIQUE, WHERE, WITH
* </pre> * </pre>
* *
* @return a list of additional the keywords * @return a list of additional the keywords
...@@ -1555,7 +1555,7 @@ public class JdbcDatabaseMetaData extends TraceObject implements ...@@ -1555,7 +1555,7 @@ public class JdbcDatabaseMetaData extends TraceObject implements
@Override @Override
public String getSQLKeywords() { public String getSQLKeywords() {
debugCodeCall("getSQLKeywords"); debugCodeCall("getSQLKeywords");
return "LIMIT,MINUS,ROWNUM,SYSDATE,SYSTIME,SYSTIMESTAMP,TODAY"; return "LIMIT,MINUS,OFFSET,ROWNUM,SYSDATE,SYSTIME,SYSTIMESTAMP,TODAY";
} }
/** /**
......
...@@ -1320,9 +1320,10 @@ public class JdbcStatement extends TraceObject implements Statement, JdbcStateme ...@@ -1320,9 +1320,10 @@ public class JdbcStatement extends TraceObject implements Statement, JdbcStateme
*/ */
@Override @Override
public String enquoteIdentifier(String identifier, boolean alwaysQuote) throws SQLException { public String enquoteIdentifier(String identifier, boolean alwaysQuote) throws SQLException {
if (alwaysQuote) if (alwaysQuote || !isSimpleIdentifier(identifier)) {
return StringUtils.quoteIdentifier(identifier); return StringUtils.quoteIdentifier(identifier);
return Parser.quoteIdentifier(identifier); }
return identifier;
} }
/** /**
...@@ -1332,7 +1333,7 @@ public class JdbcStatement extends TraceObject implements Statement, JdbcStateme ...@@ -1332,7 +1333,7 @@ public class JdbcStatement extends TraceObject implements Statement, JdbcStateme
*/ */
@Override @Override
public boolean isSimpleIdentifier(String identifier) throws SQLException { public boolean isSimpleIdentifier(String identifier) throws SQLException {
return Parser.isSimpleIdentifier(identifier); return Parser.isSimpleIdentifier(identifier, true);
} }
/** /**
......
...@@ -435,7 +435,7 @@ public class TestMetaData extends TestBase { ...@@ -435,7 +435,7 @@ public class TestMetaData extends TestBase {
assertEquals("schema", meta.getSchemaTerm()); assertEquals("schema", meta.getSchemaTerm());
assertEquals("\\", meta.getSearchStringEscape()); assertEquals("\\", meta.getSearchStringEscape());
assertEquals("LIMIT,MINUS,ROWNUM,SYSDATE,SYSTIME,SYSTIMESTAMP,TODAY", assertEquals("LIMIT,MINUS,OFFSET,ROWNUM,SYSDATE,SYSTIME,SYSTIMESTAMP,TODAY",
meta.getSQLKeywords()); meta.getSQLKeywords());
assertTrue(meta.getURL().startsWith("jdbc:h2:")); assertTrue(meta.getURL().startsWith("jdbc:h2:"));
......
...@@ -334,11 +334,13 @@ public class TestStatement extends TestBase { ...@@ -334,11 +334,13 @@ public class TestStatement extends TestBase {
assertEquals("\"SOME_ID\"", statBC.enquoteIdentifier("SOME_ID", true)); assertEquals("\"SOME_ID\"", statBC.enquoteIdentifier("SOME_ID", true));
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));
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"));
stat.close(); stat.close();
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论