提交 6a9275fb 编写于 作者: T terrymanu

refactor token => keyword, symbol and dataType

上级 b598659e
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package com.alibaba.druid.sql.dialect.mysql.lexer;
import com.alibaba.druid.sql.lexer.Keyword;
/**
* MySQL关键词.
*
* @author zhangliang
*/
public enum MySQLKeyword implements Keyword {
SHOW,
DUAL,
LIMIT,
OFFSET,
VALUE,
BEGIN,
IF,
TRUE,
FALSE,
FORCE,
ROW,
PARTITION,
KILL,
QUICK,
BINARY,
CACHE,
SQL_CACHE,
SQL_NO_CACHE,
SQL_SMALL_RESULT,
SQL_BIG_RESULT,
SQL_BUFFER_RESULT,
SQL_CALC_FOUND_ROWS,
LOW_PRIORITY,
HIGH_PRIORITY,
OPTIMIZE,
ANALYZE,
IGNORE,
IDENTIFIED
}
......@@ -13,15 +13,22 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.mysql.parser;
package com.alibaba.druid.sql.dialect.mysql.lexer;
import com.alibaba.druid.sql.lexer.Dictionary;
import com.alibaba.druid.sql.lexer.Lexer;
import com.alibaba.druid.sql.lexer.Token;
public final class MySqlLexer extends Lexer {
private static Dictionary dictionary = new Dictionary();
static {
dictionary.fill(MySQLKeyword.values());
}
public MySqlLexer(final String input) {
super(input, Token.getMysqlKeywords());
super(input, dictionary);
}
@Override
......@@ -35,7 +42,7 @@ public final class MySqlLexer extends Lexer {
@Override
protected boolean isHint() {
return ('/' == charAt(getCurrentPosition()) && '*' == charAt(getCurrentPosition() + 1) && '!' == charAt(getCurrentPosition() + 2));
return '/' == charAt(getCurrentPosition()) && '*' == charAt(getCurrentPosition() + 1) && '!' == charAt(getCurrentPosition() + 2);
}
@Override
......
package com.alibaba.druid.sql.dialect.mysql.parser;
import com.alibaba.druid.sql.lexer.Token;
import com.alibaba.druid.sql.dialect.mysql.lexer.MySQLKeyword;
import com.alibaba.druid.sql.lexer.DefaultKeyword;
import com.alibaba.druid.sql.parser.AbstractDeleteParser;
import com.alibaba.druid.sql.parser.SQLExprParser;
......@@ -17,9 +18,9 @@ public final class MySQLDeleteParser extends AbstractDeleteParser {
@Override
protected void skipBetweenDeleteAndTable() {
while (getExprParser().getLexer().equalToken(Token.LOW_PRIORITY) || getExprParser().getLexer().equalToken(Token.QUICK) || getExprParser().getLexer().equalToken(Token.IGNORE)) {
while (getExprParser().getLexer().equalToken(MySQLKeyword.LOW_PRIORITY) || getExprParser().getLexer().equalToken(MySQLKeyword.QUICK) || getExprParser().getLexer().equalToken(MySQLKeyword.IGNORE)) {
getExprParser().getLexer().nextToken();
}
getExprParser().getLexer().skipIfEqual(Token.FROM);
getExprParser().getLexer().skipIfEqual(DefaultKeyword.FROM);
}
}
......@@ -5,7 +5,11 @@ import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.ast.expr.SQLNullExpr;
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
import com.alibaba.druid.sql.lexer.Token;
import com.alibaba.druid.sql.dialect.mysql.lexer.MySQLKeyword;
import com.alibaba.druid.sql.lexer.DataType;
import com.alibaba.druid.sql.lexer.DefaultKeyword;
import com.alibaba.druid.sql.lexer.Keyword;
import com.alibaba.druid.sql.lexer.Symbol;
import com.alibaba.druid.sql.parser.AbstractInsertParser;
import com.alibaba.druid.sql.parser.SQLExprParser;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
......@@ -40,43 +44,43 @@ public final class MySQLInsertParser extends AbstractInsertParser {
getExprParser().getLexer().nextToken();
Condition.Column column = getColumn(autoIncrementColumns);
getExprParser().getLexer().nextToken();
getExprParser().getLexer().accept(Token.EQ);
getExprParser().getLexer().accept(Symbol.EQ);
SQLExpr sqlExpr;
if (getExprParser().getLexer().equalToken(Token.LITERAL_INT)) {
if (getExprParser().getLexer().equalToken(DataType.LITERAL_INT)) {
sqlExpr = new SQLIntegerExpr(Integer.parseInt(getExprParser().getLexer().getLiterals()));
} else if (getExprParser().getLexer().equalToken(Token.LITERAL_FLOAT)) {
} else if (getExprParser().getLexer().equalToken(DataType.LITERAL_FLOAT)) {
sqlExpr = new SQLIntegerExpr(Double.parseDouble(getExprParser().getLexer().getLiterals()));
} else if (getExprParser().getLexer().equalToken(Token.LITERAL_CHARS)) {
} else if (getExprParser().getLexer().equalToken(DataType.LITERAL_CHARS)) {
sqlExpr = new SQLCharExpr(getExprParser().getLexer().getLiterals());
} else if (getExprParser().getLexer().equalToken(Token.NULL)) {
} else if (getExprParser().getLexer().equalToken(DefaultKeyword.NULL)) {
sqlExpr = new SQLNullExpr();
} else if (getExprParser().getLexer().equalToken(Token.VARIANT)) {
} else if (getExprParser().getLexer().equalToken(DataType.VARIANT)) {
sqlExpr = new SQLVariantRefExpr("?");
} else {
throw new UnsupportedOperationException("");
}
getExprParser().getLexer().nextToken();
if (getExprParser().getLexer().equalToken(Token.COMMA, Token.ON, Token.EOF)) {
if (getExprParser().getLexer().equalToken(Symbol.COMMA, DefaultKeyword.ON, DataType.EOF)) {
parseContext.addCondition(column.getColumnName(), column.getTableName(), Condition.BinaryOperator.EQUAL, sqlExpr, getParameters());
} else {
getExprParser().getLexer().skipUntil(Token.COMMA, Token.ON);
getExprParser().getLexer().skipUntil(Symbol.COMMA, DefaultKeyword.ON);
}
} while (getExprParser().getLexer().equalToken(Token.COMMA));
} while (getExprParser().getLexer().equalToken(Symbol.COMMA));
getSqlContext().getConditionContexts().add(parseContext.getCurrentConditionContext());
}
@Override
protected Set<Token> getSkippedTokensBetweenTableAndValues() {
return Sets.newHashSet(Token.PARTITION);
protected Set<Keyword> getSkippedTokensBetweenTableAndValues() {
return Sets.<Keyword>newHashSet(MySQLKeyword.PARTITION);
}
@Override
protected Set<Token> getValuesTokens() {
return Sets.newHashSet(Token.VALUES, Token.VALUE);
protected Set<Keyword> getValuesKeywords() {
return Sets.<Keyword>newHashSet(DefaultKeyword.VALUES, MySQLKeyword.VALUE);
}
@Override
protected Set<Token> getCustomizedInsertTokens() {
return Sets.newHashSet(Token.SET);
protected Set<Keyword> getCustomizedInsertTokens() {
return Sets.<Keyword>newHashSet(DefaultKeyword.SET);
}
}
package com.alibaba.druid.sql.dialect.mysql.parser;
import com.alibaba.druid.sql.lexer.Token;
import com.alibaba.druid.sql.dialect.mysql.lexer.MySQLKeyword;
import com.alibaba.druid.sql.parser.AbstractUpdateParser;
import com.alibaba.druid.sql.parser.SQLExprParser;
......@@ -17,7 +17,7 @@ public final class MySQLUpdateParser extends AbstractUpdateParser {
@Override
protected void skipBetweenUpdateAndTable() {
while (getExprParser().getLexer().equalToken(Token.LOW_PRIORITY) || getExprParser().getLexer().equalToken(Token.IGNORE)) {
while (getExprParser().getLexer().equalToken(MySQLKeyword.LOW_PRIORITY) || getExprParser().getLexer().equalToken(MySQLKeyword.IGNORE)) {
getExprParser().getLexer().nextToken();
}
}
......
......@@ -20,7 +20,10 @@ import com.alibaba.druid.sql.context.LimitContext;
import com.alibaba.druid.sql.context.OffsetLimitToken;
import com.alibaba.druid.sql.context.RowCountLimitToken;
import com.alibaba.druid.sql.context.SelectSQLContext;
import com.alibaba.druid.sql.lexer.Token;
import com.alibaba.druid.sql.dialect.mysql.lexer.MySQLKeyword;
import com.alibaba.druid.sql.dialect.mysql.lexer.MySqlLexer;
import com.alibaba.druid.sql.lexer.DataType;
import com.alibaba.druid.sql.lexer.Symbol;
import com.alibaba.druid.sql.parser.ParserException;
import com.alibaba.druid.sql.parser.SQLExprParser;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
......@@ -36,15 +39,15 @@ public class MySqlExprParser extends SQLExprParser {
}
public LimitContext parseLimit(final int parametersIndex, final SelectSQLContext sqlContext) {
getLexer().skipIfEqual(Token.LIMIT);
getLexer().skipIfEqual(MySQLKeyword.LIMIT);
int valueIndex = -1;
int valueBeginPosition = getLexer().getCurrentPosition();
int value;
boolean isParameterForValue = false;
if (getLexer().equalToken(Token.LITERAL_INT)) {
if (getLexer().equalToken(DataType.LITERAL_INT)) {
value = Integer.parseInt(getLexer().getLiterals());
valueBeginPosition = valueBeginPosition - (value + "").length();
} else if (getLexer().equalToken(Token.QUESTION)) {
} else if (getLexer().equalToken(Symbol.QUESTION)) {
valueIndex = parametersIndex;
value = (int) getParameters().get(valueIndex);
valueBeginPosition--;
......@@ -53,10 +56,10 @@ public class MySqlExprParser extends SQLExprParser {
throw new ParserException(getLexer());
}
getLexer().nextToken();
if (getLexer().skipIfEqual(Token.COMMA)) {
if (getLexer().skipIfEqual(Symbol.COMMA)) {
return getLimitContextWithComma(parametersIndex, sqlContext, valueIndex, valueBeginPosition, value, isParameterForValue);
}
if (getLexer().skipIfEqual(Token.OFFSET)) {
if (getLexer().skipIfEqual(MySQLKeyword.OFFSET)) {
return getLimitContextWithOffset(parametersIndex, sqlContext, valueIndex, valueBeginPosition, value, isParameterForValue);
}
if (!isParameterForValue) {
......@@ -73,10 +76,10 @@ public class MySqlExprParser extends SQLExprParser {
int rowCount;
int rowCountIndex = -1;
boolean isParameterForRowCount = false;
if (getLexer().equalToken(Token.LITERAL_INT)) {
if (getLexer().equalToken(DataType.LITERAL_INT)) {
rowCount = Integer.parseInt(getLexer().getLiterals());
rowCountBeginPosition = rowCountBeginPosition - (rowCount + "").length();
} else if (getLexer().equalToken(Token.QUESTION)) {
} else if (getLexer().equalToken(Symbol.QUESTION)) {
rowCountIndex = -1 == valueIndex ? parametersIndex : valueIndex + 1;
rowCount = (int) getParameters().get(rowCountIndex);
rowCountBeginPosition--;
......@@ -102,10 +105,10 @@ public class MySqlExprParser extends SQLExprParser {
int offset;
int offsetIndex = -1;
boolean isParameterForOffset = false;
if (getLexer().equalToken(Token.LITERAL_INT)) {
if (getLexer().equalToken(DataType.LITERAL_INT)) {
offset = Integer.parseInt(getLexer().getLiterals());
offsetBeginPosition = offsetBeginPosition - (offset + "").length();
} else if (getLexer().equalToken(Token.QUESTION)) {
} else if (getLexer().equalToken(Symbol.QUESTION)) {
offsetIndex = -1 == valueIndex ? parametersIndex : valueIndex + 1;
offset = (int) getParameters().get(offsetIndex);
offsetBeginPosition--;
......
......@@ -16,7 +16,10 @@
package com.alibaba.druid.sql.dialect.mysql.parser;
import com.alibaba.druid.sql.lexer.Token;
import com.alibaba.druid.sql.dialect.mysql.lexer.MySQLKeyword;
import com.alibaba.druid.sql.dialect.oracle.lexer.OracleKeyword;
import com.alibaba.druid.sql.lexer.DataType;
import com.alibaba.druid.sql.lexer.DefaultKeyword;
import com.alibaba.druid.sql.parser.AbstractSelectParser;
import com.alibaba.druid.sql.parser.ParserUnsupportedException;
import com.alibaba.druid.sql.parser.SQLExprParser;
......@@ -29,15 +32,17 @@ public class MySqlSelectParser extends AbstractSelectParser {
@Override
public void query() {
if (getExprParser().getLexer().equalToken(Token.SELECT)) {
if (getExprParser().getLexer().equalToken(DefaultKeyword.SELECT)) {
getExprParser().getLexer().nextToken();
while (getExprParser().getLexer().equalToken(Token.HINT) || getExprParser().getLexer().equalToken(Token.COMMENT)) {
while (getExprParser().getLexer().equalToken(DataType.HINT) || getExprParser().getLexer().equalToken(DataType.COMMENT)) {
getExprParser().getLexer().nextToken();
}
parseDistinct();
while (getExprParser().getLexer().equalToken(Token.HIGH_PRIORITY) || getExprParser().getLexer().equalToken(Token.STRAIGHT_JOIN) || getExprParser().getLexer().equalToken(Token.SQL_SMALL_RESULT)
|| getExprParser().getLexer().equalToken(Token.SQL_BIG_RESULT) || getExprParser().getLexer().equalToken(Token.SQL_BUFFER_RESULT) || getExprParser().getLexer().equalToken(Token.SQL_CACHE)
|| getExprParser().getLexer().equalToken(Token.SQL_NO_CACHE) || getExprParser().getLexer().equalToken(Token.SQL_CALC_FOUND_ROWS)) {
while (getExprParser().getLexer().equalToken(MySQLKeyword.HIGH_PRIORITY) || getExprParser().getLexer().equalToken(DefaultKeyword.STRAIGHT_JOIN)
|| getExprParser().getLexer().equalToken(MySQLKeyword.SQL_SMALL_RESULT)
|| getExprParser().getLexer().equalToken(MySQLKeyword.SQL_BIG_RESULT) || getExprParser().getLexer().equalToken(MySQLKeyword.SQL_BUFFER_RESULT)
|| getExprParser().getLexer().equalToken(MySQLKeyword.SQL_CACHE)
|| getExprParser().getLexer().equalToken(MySQLKeyword.SQL_NO_CACHE) || getExprParser().getLexer().equalToken(MySQLKeyword.SQL_CALC_FOUND_ROWS)) {
getExprParser().getLexer().nextToken();
}
parseSelectList();
......@@ -47,35 +52,35 @@ public class MySqlSelectParser extends AbstractSelectParser {
parseWhere();
parseGroupBy();
getSqlContext().getOrderByContexts().addAll(getExprParser().parseOrderBy(getSqlContext()));
if (getExprParser().getLexer().equalToken(Token.LIMIT)) {
if (getExprParser().getLexer().equalToken(MySQLKeyword.LIMIT)) {
getSqlContext().setLimitContext(((MySqlExprParser) getExprParser()).parseLimit(getParametersIndex(), getSqlContext()));
}
if (getExprParser().getLexer().equalToken(Token.PROCEDURE)) {
if (getExprParser().getLexer().equalToken(DefaultKeyword.PROCEDURE)) {
throw new ParserUnsupportedException(getExprParser().getLexer().getToken());
}
queryRest();
}
private void skipToFrom() {
while (!getExprParser().getLexer().equalToken(Token.FROM) && !getExprParser().getLexer().equalToken(Token.EOF)) {
while (!getExprParser().getLexer().equalToken(DefaultKeyword.FROM) && !getExprParser().getLexer().equalToken(DataType.EOF)) {
getExprParser().getLexer().nextToken();
}
}
@Override
protected void parseJoinTable() {
if (getExprParser().getLexer().equalToken(Token.USING)) {
if (getExprParser().getLexer().equalToken(DefaultKeyword.USING)) {
return;
}
if (getExprParser().getLexer().equalToken(Token.USE)) {
if (getExprParser().getLexer().equalToken(DefaultKeyword.USE)) {
getExprParser().getLexer().nextToken();
parseIndexHint();
}
if (getExprParser().getLexer().equalToken(Token.IGNORE)) {
if (getExprParser().getLexer().equalToken(OracleKeyword.IGNORE)) {
getExprParser().getLexer().nextToken();
parseIndexHint();
}
if (getExprParser().getLexer().equalToken(Token.FORCE)) {
if (getExprParser().getLexer().equalToken(OracleKeyword.FORCE)) {
getExprParser().getLexer().nextToken();
parseIndexHint();
}
......@@ -83,21 +88,21 @@ public class MySqlSelectParser extends AbstractSelectParser {
}
private void parseIndexHint() {
if (getExprParser().getLexer().equalToken(Token.INDEX)) {
if (getExprParser().getLexer().equalToken(DefaultKeyword.INDEX)) {
getExprParser().getLexer().nextToken();
} else {
getExprParser().getLexer().accept(Token.KEY);
getExprParser().getLexer().accept(DefaultKeyword.KEY);
}
if (getExprParser().getLexer().equalToken(Token.FOR)) {
if (getExprParser().getLexer().equalToken(DefaultKeyword.FOR)) {
getExprParser().getLexer().nextToken();
if (getExprParser().getLexer().equalToken(Token.JOIN)) {
if (getExprParser().getLexer().equalToken(DefaultKeyword.JOIN)) {
getExprParser().getLexer().nextToken();
} else if (getExprParser().getLexer().equalToken(Token.ORDER)) {
} else if (getExprParser().getLexer().equalToken(DefaultKeyword.ORDER)) {
getExprParser().getLexer().nextToken();
getExprParser().getLexer().accept(Token.BY);
getExprParser().getLexer().accept(DefaultKeyword.BY);
} else {
getExprParser().getLexer().accept(Token.GROUP);
getExprParser().getLexer().accept(Token.BY);
getExprParser().getLexer().accept(DefaultKeyword.GROUP);
getExprParser().getLexer().accept(DefaultKeyword.BY);
}
}
getExprParser().getLexer().skipParentheses();
......
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License,Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package com.alibaba.druid.sql.dialect.oracle.lexer;
import com.alibaba.druid.sql.lexer.Keyword;
/**
* Oracle关键词.
*
* @author zhangliang
*/
public enum OracleKeyword implements Keyword {
LOCKED,
COMMIT,
CREATION,
UPDATED,
UPSERT,
STORE,
MERGE,
PURGE,
ROW,
ROWS,
IF,
OF,
GOTO,
ONLY,
AUTOMATIC,
MAIN,
PCTINCREASE,
CHUNK,
LIMIT,
UNLIMITED,
SIBLINGS,
INCLUDE,
EXCLUDE,
PIVOT,
UNPIVOT,
EXCEPTION,
EXCEPTIONS,
ERRORS,
DEFERRED,
CONNECT,
EXCLUSIVE,
CACHE,
NOCACHE,
NAV,
IDENTIFIED,
VERSIONS,
WAIT,
NOWAIT,
STORAGE,
SAMPLE,
CONTINUE,
SYSDATE,
TIMESTAMP,
SEGMENT,
PARTITION,
SUBPARTITION,
RETURN,
RETURNING,
REJECT,
MAXTRANS,
MINEXTENTS,
MAXEXTENTS,
BEGIN,
START,
SAVEPOINT,
MATCHED,
LOB,
DIMENSION,
FORCE,
MODEL,
FIRST,
NEXT,
LAST,
SHARE,
EXTRACT,
NOCOMPRESS,
MODE,
RULES,
INITIALLY,
KEEP,
KEEP_DUPLICATES,
REFERENCE,
SEED,
SESSION,
IGNORE,
MEASURES,
LOGGING,
MAXSIZE,
FLASH_CACHE,
CELL_FLASH_CACHE,
NOCYCLE,
SKIP,
NONE,
NULLS,
SINGLE,
SCN,
INITRANS,
BLOCK,
IMMEDIATE,
SEQUENTIAL,
PCTFREE,
PRIOR,
XML
}
......@@ -13,22 +13,28 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.oracle.parser;
package com.alibaba.druid.sql.dialect.oracle.lexer;
import com.alibaba.druid.sql.lexer.Dictionary;
import com.alibaba.druid.sql.lexer.Lexer;
import com.alibaba.druid.sql.lexer.Token;
public class OracleLexer extends Lexer {
private static Dictionary dictionary = new Dictionary();
static {
dictionary.fill(OracleKeyword.values());
}
public OracleLexer(final String input) {
super(input, Token.getOracleKeywords());
super(input, dictionary);
}
@Override
public void scanVariable() {
if ('@' == charAt(getCurrentPosition())) {
increaseCurrentPosition();
setToken(Token.MONKEYS_AT);
return;
}
super.scanVariable();
......@@ -36,6 +42,6 @@ public class OracleLexer extends Lexer {
@Override
protected boolean isHint() {
return ('/' == charAt(getCurrentPosition()) && '*' == charAt(getCurrentPosition() + 1) && '+' == charAt(getCurrentPosition() + 2));
return '/' == charAt(getCurrentPosition()) && '*' == charAt(getCurrentPosition() + 1) && '+' == charAt(getCurrentPosition() + 2);
}
}
......@@ -16,7 +16,9 @@
package com.alibaba.druid.sql.dialect.oracle.parser;
import com.alibaba.druid.sql.lexer.Token;
import com.alibaba.druid.sql.dialect.oracle.lexer.OracleKeyword;
import com.alibaba.druid.sql.lexer.DataType;
import com.alibaba.druid.sql.lexer.DefaultKeyword;
import com.alibaba.druid.sql.parser.AbstractDeleteParser;
import com.alibaba.druid.sql.parser.SQLExprParser;
......@@ -28,9 +30,9 @@ public class OracleDeleteParser extends AbstractDeleteParser {
@Override
protected void skipBetweenDeleteAndTable() {
getExprParser().getLexer().skipIfEqual(Token.HINT);
getExprParser().getLexer().skipIfEqual(Token.FROM);
getExprParser().getLexer().skipIfEqual(Token.COMMENT);
getExprParser().getLexer().skipIfEqual(Token.ONLY);
getExprParser().getLexer().skipIfEqual(DataType.HINT);
getExprParser().getLexer().skipIfEqual(DefaultKeyword.FROM);
getExprParser().getLexer().skipIfEqual(DataType.COMMENT);
getExprParser().getLexer().skipIfEqual(OracleKeyword.ONLY);
}
}
......@@ -18,7 +18,8 @@ package com.alibaba.druid.sql.dialect.oracle.parser;
import com.alibaba.druid.sql.context.OrderByContext;
import com.alibaba.druid.sql.context.SQLContext;
import com.alibaba.druid.sql.lexer.Token;
import com.alibaba.druid.sql.dialect.oracle.lexer.OracleKeyword;
import com.alibaba.druid.sql.dialect.oracle.lexer.OracleLexer;
import com.alibaba.druid.sql.parser.ParserUnsupportedException;
import com.alibaba.druid.sql.parser.SQLExprParser;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
......@@ -35,7 +36,7 @@ public class OracleExprParser extends SQLExprParser {
@Override
protected Optional<String> as() {
if (getLexer().equalToken(Token.CONNECT)) {
if (getLexer().equalToken(OracleKeyword.CONNECT)) {
return null;
}
return super.as();
......@@ -44,9 +45,9 @@ public class OracleExprParser extends SQLExprParser {
@Override
public OrderByContext parseSelectOrderByItem(final SQLContext sqlContext) {
OrderByContext result = super.parseSelectOrderByItem(sqlContext);
if (getLexer().skipIfEqual(Token.NULLS)) {
if (getLexer().skipIfEqual(OracleKeyword.NULLS)) {
getLexer().nextToken();
if (!getLexer().skipIfEqual(Token.FIRST, Token.LAST)) {
if (!getLexer().skipIfEqual(OracleKeyword.FIRST, OracleKeyword.LAST)) {
throw new ParserUnsupportedException(getLexer().getToken());
}
}
......
package com.alibaba.druid.sql.dialect.oracle.parser;
import com.alibaba.druid.sql.lexer.Token;
import com.alibaba.druid.sql.dialect.oracle.lexer.OracleKeyword;
import com.alibaba.druid.sql.lexer.DefaultKeyword;
import com.alibaba.druid.sql.lexer.Keyword;
import com.alibaba.druid.sql.parser.AbstractInsertParser;
import com.alibaba.druid.sql.parser.SQLExprParser;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
......@@ -21,7 +23,7 @@ public final class OracleInsertParser extends AbstractInsertParser {
}
@Override
protected Set<Token> getUnsupportedTokens() {
return Sets.newHashSet(Token.ALL, Token.FIRST);
protected Set<Keyword> getUnsupportedKeywords() {
return Sets.<Keyword>newHashSet(DefaultKeyword.ALL, OracleKeyword.FIRST);
}
}
......@@ -17,7 +17,10 @@
package com.alibaba.druid.sql.dialect.oracle.parser;
import com.alibaba.druid.sql.context.TableContext;
import com.alibaba.druid.sql.lexer.Token;
import com.alibaba.druid.sql.dialect.oracle.lexer.OracleKeyword;
import com.alibaba.druid.sql.lexer.DataType;
import com.alibaba.druid.sql.lexer.DefaultKeyword;
import com.alibaba.druid.sql.lexer.Symbol;
import com.alibaba.druid.sql.parser.AbstractSelectParser;
import com.alibaba.druid.sql.parser.ParserUnsupportedException;
import com.alibaba.druid.sql.parser.SQLExprParser;
......@@ -33,7 +36,7 @@ public class OracleSelectParser extends AbstractSelectParser {
@Override
protected void customizedSelect() {
if (getExprParser().getLexer().equalToken(Token.FOR)) {
if (getExprParser().getLexer().equalToken(DefaultKeyword.FOR)) {
skipForUpdate();
}
if (getSqlContext().getOrderByContexts().isEmpty()) {
......@@ -43,13 +46,13 @@ public class OracleSelectParser extends AbstractSelectParser {
@Override
public void query() {
if (getExprParser().getLexer().equalToken(Token.SELECT)) {
if (getExprParser().getLexer().equalToken(DefaultKeyword.SELECT)) {
getExprParser().getLexer().nextToken();
while (getExprParser().getLexer().equalToken(Token.HINT) || getExprParser().getLexer().equalToken(Token.COMMENT)) {
while (getExprParser().getLexer().equalToken(DataType.HINT) || getExprParser().getLexer().equalToken(DataType.COMMENT)) {
getExprParser().getLexer().nextToken();
}
parseDistinct();
getExprParser().getLexer().skipIfEqual(Token.HINT);
getExprParser().getLexer().skipIfEqual(DataType.HINT);
parseSelectList();
}
skipInto();
......@@ -62,7 +65,7 @@ public class OracleSelectParser extends AbstractSelectParser {
}
private void skipInto() {
if (getExprParser().getLexer().equalToken(Token.INTO)) {
if (getExprParser().getLexer().equalToken(DefaultKeyword.INTO)) {
throw new ParserUnsupportedException(getExprParser().getLexer().getToken());
}
}
......@@ -75,35 +78,35 @@ public class OracleSelectParser extends AbstractSelectParser {
}
private void skipStart() {
if (getExprParser().getLexer().skipIfEqual(Token.START)) {
getExprParser().getLexer().accept(Token.WITH);
if (getExprParser().getLexer().skipIfEqual(OracleKeyword.START)) {
getExprParser().getLexer().accept(DefaultKeyword.WITH);
getExprParser().parseComparisonCondition(getSqlContext(), new ParseContext(0));
}
}
private void skipConnect() {
if (getExprParser().getLexer().skipIfEqual(Token.CONNECT)) {
getExprParser().getLexer().accept(Token.BY);
getExprParser().getLexer().skipIfEqual(Token.PRIOR);
if (getExprParser().getLexer().skipIfEqual(Token.NOCYCLE)) {
getExprParser().getLexer().skipIfEqual(Token.PRIOR);
if (getExprParser().getLexer().skipIfEqual(OracleKeyword.CONNECT)) {
getExprParser().getLexer().accept(DefaultKeyword.BY);
getExprParser().getLexer().skipIfEqual(OracleKeyword.PRIOR);
if (getExprParser().getLexer().skipIfEqual(OracleKeyword.NOCYCLE)) {
getExprParser().getLexer().skipIfEqual(OracleKeyword.PRIOR);
}
getExprParser().parseComparisonCondition(getSqlContext(), new ParseContext(1));
}
}
private void skipModelClause() {
if (!getExprParser().getLexer().skipIfEqual(Token.MODEL)) {
if (!getExprParser().getLexer().skipIfEqual(OracleKeyword.MODEL)) {
return;
}
skipCellReferenceOptions();
getExprParser().getLexer().skipIfEqual(Token.RETURN);
getExprParser().getLexer().skipIfEqual(Token.ALL);
getExprParser().getLexer().skipIfEqual(Token.UPDATED);
getExprParser().getLexer().skipIfEqual(Token.ROWS);
while (getExprParser().getLexer().skipIfEqual(Token.REFERENCE)) {
getExprParser().getLexer().skipIfEqual(OracleKeyword.RETURN);
getExprParser().getLexer().skipIfEqual(DefaultKeyword.ALL);
getExprParser().getLexer().skipIfEqual(OracleKeyword.UPDATED);
getExprParser().getLexer().skipIfEqual(OracleKeyword.ROWS);
while (getExprParser().getLexer().skipIfEqual(OracleKeyword.REFERENCE)) {
getExprParser().getLexer().nextToken();
getExprParser().getLexer().accept(Token.ON);
getExprParser().getLexer().accept(DefaultKeyword.ON);
getExprParser().getLexer().skipParentheses();
skipModelColumnClause();
skipCellReferenceOptions();
......@@ -112,44 +115,44 @@ public class OracleSelectParser extends AbstractSelectParser {
}
private void skipCellReferenceOptions() {
if (getExprParser().getLexer().skipIfEqual(Token.IGNORE)) {
getExprParser().getLexer().accept(Token.NAV);
} else if (getExprParser().getLexer().skipIfEqual(Token.KEEP)) {
getExprParser().getLexer().accept(Token.NAV);
if (getExprParser().getLexer().skipIfEqual(OracleKeyword.IGNORE)) {
getExprParser().getLexer().accept(OracleKeyword.NAV);
} else if (getExprParser().getLexer().skipIfEqual(OracleKeyword.KEEP)) {
getExprParser().getLexer().accept(OracleKeyword.NAV);
}
if (getExprParser().getLexer().skipIfEqual(Token.UNIQUE)) {
getExprParser().getLexer().skipIfEqual(Token.DIMENSION, Token.SINGLE);
getExprParser().getLexer().skipIfEqual(Token.REFERENCE);
if (getExprParser().getLexer().skipIfEqual(DefaultKeyword.UNIQUE)) {
getExprParser().getLexer().skipIfEqual(OracleKeyword.DIMENSION, OracleKeyword.SINGLE);
getExprParser().getLexer().skipIfEqual(OracleKeyword.REFERENCE);
}
}
private void skipMainModelClause() {
if (getExprParser().getLexer().skipIfEqual(Token.MAIN)) {
if (getExprParser().getLexer().skipIfEqual(OracleKeyword.MAIN)) {
getExprParser().getLexer().nextToken();
}
skipQueryPartitionClause();
getExprParser().getLexer().accept(Token.DIMENSION);
getExprParser().getLexer().accept(Token.BY);
getExprParser().getLexer().accept(OracleKeyword.DIMENSION);
getExprParser().getLexer().accept(DefaultKeyword.BY);
getExprParser().getLexer().skipParentheses();
getExprParser().getLexer().accept(Token.MEASURES);
getExprParser().getLexer().accept(OracleKeyword.MEASURES);
getExprParser().getLexer().skipParentheses();
skipCellReferenceOptions();
skipModelRulesClause();
}
private void skipModelRulesClause() {
if (getExprParser().getLexer().skipIfEqual(Token.RULES)) {
getExprParser().getLexer().skipIfEqual(Token.UPDATE);
getExprParser().getLexer().skipIfEqual(Token.UPSERT);
if (getExprParser().getLexer().skipIfEqual(Token.AUTOMATIC)) {
getExprParser().getLexer().accept(Token.ORDER);
} else if (getExprParser().getLexer().skipIfEqual(Token.SEQUENTIAL)) {
getExprParser().getLexer().accept(Token.ORDER);
if (getExprParser().getLexer().skipIfEqual(OracleKeyword.RULES)) {
getExprParser().getLexer().skipIfEqual(DefaultKeyword.UPDATE);
getExprParser().getLexer().skipIfEqual(OracleKeyword.UPSERT);
if (getExprParser().getLexer().skipIfEqual(OracleKeyword.AUTOMATIC)) {
getExprParser().getLexer().accept(DefaultKeyword.ORDER);
} else if (getExprParser().getLexer().skipIfEqual(OracleKeyword.SEQUENTIAL)) {
getExprParser().getLexer().accept(DefaultKeyword.ORDER);
}
}
if (getExprParser().getLexer().skipIfEqual(Token.ITERATE)) {
if (getExprParser().getLexer().skipIfEqual(DefaultKeyword.ITERATE)) {
getExprParser().getLexer().skipParentheses();
if (getExprParser().getLexer().skipIfEqual(Token.UNTIL)) {
if (getExprParser().getLexer().skipIfEqual(DefaultKeyword.UNTIL)) {
getExprParser().getLexer().skipParentheses();
}
}
......@@ -157,9 +160,9 @@ public class OracleSelectParser extends AbstractSelectParser {
}
private void skipQueryPartitionClause() {
if (getExprParser().getLexer().skipIfEqual(Token.PARTITION)) {
getExprParser().getLexer().accept(Token.BY);
if (getExprParser().getLexer().equalToken(Token.LEFT_PAREN)) {
if (getExprParser().getLexer().skipIfEqual(OracleKeyword.PARTITION)) {
getExprParser().getLexer().accept(DefaultKeyword.BY);
if (getExprParser().getLexer().equalToken(Symbol.LEFT_PAREN)) {
getExprParser().getLexer().skipParentheses();
} else {
throw new UnsupportedOperationException("Cannot support PARTITION BY without ()");
......@@ -174,15 +177,15 @@ public class OracleSelectParser extends AbstractSelectParser {
@Override
protected void parseGroupBy() {
// TODO
// if (getExprParser().getLexer().equalToken(Token.GROUP)) {
// if (getExprParser().getLexer().equalToken(DefaultKeyword.GROUP)) {
// getExprParser().getLexer().nextToken();
// getExprParser().getLexer().accept(Token.BY);
// getExprParser().getLexer().accept(DefaultKeyword.BY);
// while (true) {
// if (getExprParser().getLexer().identifierEquals("GROUPING")) {
// throw new UnsupportedOperationException("Cannot support GROUPING SETS");
// }
// addGroupByItem(getExprParser().expr());
// if (!getExprParser().getLexer().equalToken(Token.COMMA)) {
// if (!getExprParser().getLexer().equalToken(Symbol.COMMA)) {
// break;
// }
// getExprParser().getLexer().nextToken();
......@@ -193,14 +196,14 @@ public class OracleSelectParser extends AbstractSelectParser {
// } else if (getExprParser().getLexer().skipIfEqual(Token.HAVING)) {
// SQLSelectGroupByClause groupBy = new SQLSelectGroupByClause();
// groupBy.setHaving(getExprParser().expr());
// if (getExprParser().getLexer().skipIfEqual(Token.GROUP)) {
// getExprParser().getLexer().accept(Token.BY);
// if (getExprParser().getLexer().skipIfEqual(DefaultKeyword.GROUP)) {
// getExprParser().getLexer().accept(DefaultKeyword.BY);
// while (true) {
// if (getExprParser().getLexer().identifierEquals("GROUPING")) {
// throw new UnsupportedOperationException("Cannot support GROUPING SETS");
// }
// addGroupByItem(getExprParser().expr());
// if (!getExprParser().getLexer().equalToken(Token.COMMA)) {
// if (!getExprParser().getLexer().equalToken(Symbol.COMMA)) {
// break;
// }
// getExprParser().getLexer().nextToken();
......@@ -211,16 +214,16 @@ public class OracleSelectParser extends AbstractSelectParser {
@Override
public final List<TableContext> parseTable() {
if (getExprParser().getLexer().equalToken(Token.LEFT_PAREN)) {
if (getExprParser().getLexer().equalToken(Symbol.LEFT_PAREN)) {
throw new UnsupportedOperationException("Cannot support subquery");
}
if (getExprParser().getLexer().equalToken(Token.SELECT)) {
if (getExprParser().getLexer().equalToken(DefaultKeyword.SELECT)) {
throw new ParserUnsupportedException(getExprParser().getLexer().getToken());
}
if (getExprParser().getLexer().skipIfEqual(Token.ONLY)) {
getExprParser().getLexer().skipIfEqual(Token.LEFT_PAREN);
if (getExprParser().getLexer().skipIfEqual(OracleKeyword.ONLY)) {
getExprParser().getLexer().skipIfEqual(Symbol.LEFT_PAREN);
parseQueryTableExpression();
getExprParser().getLexer().skipIfEqual(Token.RIGHT_PAREN);
getExprParser().getLexer().skipIfEqual(Symbol.RIGHT_PAREN);
skipFlashbackQueryClause();
} else {
parseQueryTableExpression();
......@@ -234,53 +237,53 @@ public class OracleSelectParser extends AbstractSelectParser {
private void parseQueryTableExpression() {
parseTableFactor();
parseSample();
parsePartition();
skipPartition();
}
private void parseSample() {
if (getExprParser().getLexer().skipIfEqual(Token.SAMPLE)) {
getExprParser().getLexer().skipIfEqual(Token.BLOCK);
if (getExprParser().getLexer().skipIfEqual(OracleKeyword.SAMPLE)) {
getExprParser().getLexer().skipIfEqual(OracleKeyword.BLOCK);
getExprParser().getLexer().skipParentheses();
if (getExprParser().getLexer().skipIfEqual(Token.SEED)) {
if (getExprParser().getLexer().skipIfEqual(OracleKeyword.SEED)) {
getExprParser().getLexer().skipParentheses();
}
}
}
private void parsePartition() {
parsePartition(Token.PARTITION);
parsePartition(Token.SUBPARTITION);
private void skipPartition() {
skipPartition(OracleKeyword.PARTITION);
skipPartition(OracleKeyword.SUBPARTITION);
}
private void parsePartition(final Token token) {
if (getExprParser().getLexer().skipIfEqual(token)) {
private void skipPartition(final OracleKeyword keyword) {
if (getExprParser().getLexer().skipIfEqual(keyword)) {
getExprParser().getLexer().skipParentheses();
if (getExprParser().getLexer().skipIfEqual(Token.FOR)) {
if (getExprParser().getLexer().skipIfEqual(DefaultKeyword.FOR)) {
getExprParser().getLexer().skipParentheses();
}
}
}
private void skipPivotClause() {
if (getExprParser().getLexer().skipIfEqual(Token.PIVOT)) {
getExprParser().getLexer().skipIfEqual(Token.XML);
if (getExprParser().getLexer().skipIfEqual(OracleKeyword.PIVOT)) {
getExprParser().getLexer().skipIfEqual(OracleKeyword.XML);
getExprParser().getLexer().skipParentheses();
} else if (getExprParser().getLexer().skipIfEqual(Token.UNPIVOT)) {
if (getExprParser().getLexer().skipIfEqual(Token.INCLUDE)) {
getExprParser().getLexer().accept(Token.NULLS);
} else if (getExprParser().getLexer().skipIfEqual(Token.EXCLUDE)) {
getExprParser().getLexer().accept(Token.NULLS);
} else if (getExprParser().getLexer().skipIfEqual(OracleKeyword.UNPIVOT)) {
if (getExprParser().getLexer().skipIfEqual(OracleKeyword.INCLUDE)) {
getExprParser().getLexer().accept(OracleKeyword.NULLS);
} else if (getExprParser().getLexer().skipIfEqual(OracleKeyword.EXCLUDE)) {
getExprParser().getLexer().accept(OracleKeyword.NULLS);
}
getExprParser().getLexer().skipParentheses();
}
}
private void skipFlashbackQueryClause() {
if (getExprParser().getLexer().equalToken(Token.VERSIONS)) {
if (getExprParser().getLexer().equalToken(OracleKeyword.VERSIONS)) {
throw new UnsupportedOperationException("Cannot support Flashback Query");
} else if (getExprParser().getLexer().skipIfEqual(Token.AS)) {
if (getExprParser().getLexer().skipIfEqual(Token.OF)) {
if (getExprParser().getLexer().skipIfEqual(Token.SCN) || getExprParser().getLexer().skipIfEqual(Token.TIMESTAMP)) {
} else if (getExprParser().getLexer().skipIfEqual(DefaultKeyword.AS)) {
if (getExprParser().getLexer().skipIfEqual(OracleKeyword.OF)) {
if (getExprParser().getLexer().skipIfEqual(OracleKeyword.SCN) || getExprParser().getLexer().skipIfEqual(OracleKeyword.TIMESTAMP)) {
throw new UnsupportedOperationException("Cannot support Flashback Query");
}
}
......@@ -289,16 +292,16 @@ public class OracleSelectParser extends AbstractSelectParser {
private void skipForUpdate() {
getExprParser().getLexer().nextToken();
getExprParser().getLexer().accept(Token.UPDATE);
if (getExprParser().getLexer().skipIfEqual(Token.OF)) {
getExprParser().getLexer().accept(DefaultKeyword.UPDATE);
if (getExprParser().getLexer().skipIfEqual(OracleKeyword.OF)) {
do {
getExprParser().parseExpr();
} while (getExprParser().getLexer().skipIfEqual(Token.COMMA));
} while (getExprParser().getLexer().skipIfEqual(Symbol.COMMA));
}
if (getExprParser().getLexer().equalToken(Token.NOWAIT, Token.WAIT)) {
if (getExprParser().getLexer().equalToken(OracleKeyword.NOWAIT, OracleKeyword.WAIT)) {
getExprParser().getLexer().nextToken();
} else if (getExprParser().getLexer().skipIfEqual(Token.SKIP)) {
getExprParser().getLexer().accept(Token.LOCKED);
} else if (getExprParser().getLexer().skipIfEqual(OracleKeyword.SKIP)) {
getExprParser().getLexer().accept(OracleKeyword.LOCKED);
}
}
}
......@@ -16,7 +16,8 @@
package com.alibaba.druid.sql.dialect.oracle.parser;
import com.alibaba.druid.sql.lexer.Token;
import com.alibaba.druid.sql.dialect.oracle.lexer.OracleKeyword;
import com.alibaba.druid.sql.lexer.DataType;
import com.alibaba.druid.sql.parser.AbstractUpdateParser;
import com.alibaba.druid.sql.parser.SQLExprParser;
......@@ -28,7 +29,7 @@ public class OracleUpdateParser extends AbstractUpdateParser {
@Override
protected void skipBetweenUpdateAndTable() {
getExprParser().getLexer().skipIfEqual(Token.HINT);
getExprParser().getLexer().skipIfEqual(Token.ONLY);
getExprParser().getLexer().skipIfEqual(DataType.HINT);
getExprParser().getLexer().skipIfEqual(OracleKeyword.ONLY);
}
}
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License,Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package com.alibaba.druid.sql.dialect.postgresql.lexer;
import com.alibaba.druid.sql.lexer.Keyword;
/**
* PostgreSQL关键词.
*
* @author zhangliang
*/
public enum PostgreSQLKeyword implements Keyword {
SHOW,
OF,
IF,
ONLY,
TRUE,
FALSE,
ARRAY,
FIRST,
NEXT,
LAST,
ROW,
LIMIT,
OFFSET,
SIBLINGS,
RESTART,
RECURSIVE,
CASCADE,
CURRENT,
RESTRICT,
NOWAIT,
TYPE,
UNLOGGED,
CONTINUE,
RETURNING,
ROWS,
SHARE,
TEMP,
TEMPORARY,
IDENTITY,
WINDOW
}
/*
* Copyright 1999-2101 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.postgresql.parser;
import com.alibaba.druid.sql.lexer.Lexer;
import com.alibaba.druid.sql.lexer.Token;
public class PGLexer extends Lexer {
public PGLexer(final String input) {
super(input, Token.getPostgresqlKeywords());
}
}
/*
* Copyright 1999-2101 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.postgresql.lexer;
import com.alibaba.druid.sql.lexer.Dictionary;
import com.alibaba.druid.sql.lexer.Lexer;
public class PostgreSQLLexer extends Lexer {
private static Dictionary dictionary = new Dictionary();
static {
dictionary.fill(PostgreSQLKeyword.values());
}
public PostgreSQLLexer(final String input) {
super(input, dictionary);
}
}
......@@ -16,6 +16,7 @@
package com.alibaba.druid.sql.dialect.postgresql.parser;
import com.alibaba.druid.sql.dialect.postgresql.lexer.PostgreSQLLexer;
import com.alibaba.druid.sql.parser.SQLExprParser;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
......@@ -24,7 +25,7 @@ import java.util.List;
public class PGExprParser extends SQLExprParser {
public PGExprParser(final ShardingRule shardingRule, final List<Object> parameters, final String sql) {
super(shardingRule, parameters, new PGLexer(sql));
super(shardingRule, parameters, new PostgreSQLLexer(sql));
getLexer().nextToken();
}
}
......@@ -17,7 +17,10 @@
package com.alibaba.druid.sql.dialect.postgresql.parser;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.lexer.Token;
import com.alibaba.druid.sql.dialect.postgresql.lexer.PostgreSQLKeyword;
import com.alibaba.druid.sql.lexer.DataType;
import com.alibaba.druid.sql.lexer.DefaultKeyword;
import com.alibaba.druid.sql.lexer.Symbol;
import com.alibaba.druid.sql.parser.AbstractSelectParser;
import com.alibaba.druid.sql.parser.ParserException;
import com.alibaba.druid.sql.parser.ParserUnsupportedException;
......@@ -31,13 +34,13 @@ public class PGSelectParser extends AbstractSelectParser {
@Override
public void query() {
if (getExprParser().getLexer().skipIfEqual(Token.SELECT)) {
getExprParser().getLexer().skipIfEqual(Token.COMMENT);
if (getExprParser().getLexer().skipIfEqual(DefaultKeyword.SELECT)) {
getExprParser().getLexer().skipIfEqual(DataType.COMMENT);
parseDistinct();
parseSelectList();
if (getExprParser().getLexer().skipIfEqual(Token.INTO)) {
getExprParser().getLexer().skipIfEqual(Token.TEMPORARY, Token.TEMP, Token.UNLOGGED);
getExprParser().getLexer().skipIfEqual(Token.TABLE);
if (getExprParser().getLexer().skipIfEqual(DefaultKeyword.INTO)) {
getExprParser().getLexer().skipIfEqual(PostgreSQLKeyword.TEMPORARY, PostgreSQLKeyword.TEMP, PostgreSQLKeyword.UNLOGGED);
getExprParser().getLexer().skipIfEqual(DefaultKeyword.TABLE);
// TODO
// getExprParser().name();
}
......@@ -45,20 +48,20 @@ public class PGSelectParser extends AbstractSelectParser {
parseFrom();
parseWhere();
parseGroupBy();
if (getExprParser().getLexer().equalToken(Token.WINDOW)) {
throw new ParserUnsupportedException(Token.WINDOW);
if (getExprParser().getLexer().equalToken(PostgreSQLKeyword.WINDOW)) {
throw new ParserUnsupportedException(PostgreSQLKeyword.WINDOW);
}
getSqlContext().getOrderByContexts().addAll(getExprParser().parseOrderBy(getSqlContext()));
parseLimit();
if (getExprParser().getLexer().skipIfEqual(Token.FETCH)) {
throw new ParserUnsupportedException(Token.FETCH);
if (getExprParser().getLexer().skipIfEqual(DefaultKeyword.FETCH)) {
throw new ParserUnsupportedException(DefaultKeyword.FETCH);
}
if (getExprParser().getLexer().skipIfEqual(Token.FOR)) {
getExprParser().getLexer().skipIfEqual(Token.UPDATE, Token.SHARE);
if (getExprParser().getLexer().equalToken(Token.OF)) {
throw new ParserUnsupportedException(Token.OF);
if (getExprParser().getLexer().skipIfEqual(DefaultKeyword.FOR)) {
getExprParser().getLexer().skipIfEqual(DefaultKeyword.UPDATE, PostgreSQLKeyword.SHARE);
if (getExprParser().getLexer().equalToken(PostgreSQLKeyword.OF)) {
throw new ParserUnsupportedException(PostgreSQLKeyword.OF);
}
getExprParser().getLexer().skipIfEqual(Token.NOWAIT);
getExprParser().getLexer().skipIfEqual(PostgreSQLKeyword.NOWAIT);
}
queryRest();
}
......@@ -66,31 +69,31 @@ public class PGSelectParser extends AbstractSelectParser {
// TODO 解析和改写limit
private void parseLimit() {
while (true) {
if (getExprParser().getLexer().equalToken(Token.LIMIT)) {
if (getExprParser().getLexer().equalToken(PostgreSQLKeyword.LIMIT)) {
getExprParser().getLexer().nextToken();
if (getExprParser().getLexer().equalToken(Token.ALL)) {
if (getExprParser().getLexer().equalToken(DefaultKeyword.ALL)) {
new SQLIdentifierExpr("ALL");
getExprParser().getLexer().nextToken();
} else {
// rowCount
if (getExprParser().getLexer().equalToken(Token.LITERAL_INT)) {
} else if (getExprParser().getLexer().equalToken(Token.QUESTION)) {
if (getExprParser().getLexer().equalToken(DataType.LITERAL_INT)) {
} else if (getExprParser().getLexer().equalToken(Symbol.QUESTION)) {
} else {
throw new ParserException(getExprParser().getLexer());
}
getExprParser().getLexer().nextToken();
}
} else if (getExprParser().getLexer().equalToken(Token.OFFSET)) {
} else if (getExprParser().getLexer().equalToken(PostgreSQLKeyword.OFFSET)) {
getExprParser().getLexer().nextToken();
// offset
if (getExprParser().getLexer().equalToken(Token.LITERAL_INT)) {
} else if (getExprParser().getLexer().equalToken(Token.QUESTION)) {
if (getExprParser().getLexer().equalToken(DataType.LITERAL_INT)) {
} else if (getExprParser().getLexer().equalToken(Symbol.QUESTION)) {
} else {
throw new ParserException(getExprParser().getLexer());
}
getExprParser().getLexer().nextToken();
getExprParser().getLexer().skipIfEqual(Token.ROW, Token.ROWS);
getExprParser().getLexer().skipIfEqual(PostgreSQLKeyword.ROW, PostgreSQLKeyword.ROWS);
} else {
break;
}
......
package com.alibaba.druid.sql.dialect.postgresql.parser;
import com.alibaba.druid.sql.lexer.Token;
import com.alibaba.druid.sql.dialect.postgresql.lexer.PostgreSQLKeyword;
import com.alibaba.druid.sql.lexer.DefaultKeyword;
import com.alibaba.druid.sql.parser.AbstractDeleteParser;
import com.alibaba.druid.sql.parser.SQLExprParser;
......@@ -17,7 +18,7 @@ public final class PostgreSQLDeleteParser extends AbstractDeleteParser {
@Override
protected void skipBetweenDeleteAndTable() {
getExprParser().getLexer().skipIfEqual(Token.FROM);
getExprParser().getLexer().skipIfEqual(Token.ONLY);
getExprParser().getLexer().skipIfEqual(DefaultKeyword.FROM);
getExprParser().getLexer().skipIfEqual(PostgreSQLKeyword.ONLY);
}
}
package com.alibaba.druid.sql.dialect.postgresql.parser;
import com.alibaba.druid.sql.lexer.Token;
import com.alibaba.druid.sql.dialect.postgresql.lexer.PostgreSQLKeyword;
import com.alibaba.druid.sql.parser.AbstractUpdateParser;
import com.alibaba.druid.sql.parser.SQLExprParser;
......@@ -17,6 +17,6 @@ public final class PostgreSQLUpdateParser extends AbstractUpdateParser {
@Override
protected void skipBetweenUpdateAndTable() {
getExprParser().getLexer().skipIfEqual(Token.ONLY);
getExprParser().getLexer().skipIfEqual(PostgreSQLKeyword.ONLY);
}
}
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package com.alibaba.druid.sql.dialect.sqlserver.lexer;
import com.alibaba.druid.sql.lexer.Keyword;
/**
* SQLServer关键词.
*
* @author zhangliang
*/
public enum SQLServerKeyword implements Keyword {
TOP,
OFFSET,
ONLY,
IF,
OUTPUT,
AUTO,
BEGIN,
IDENTITY,
BROWSE,
TYPE,
ELEMENTS,
XML,
XSINIL,
XMLSCHEMA,
TYP
}
......@@ -13,15 +13,23 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.sqlserver.parser;
package com.alibaba.druid.sql.dialect.sqlserver.lexer;
import com.alibaba.druid.sql.lexer.DataType;
import com.alibaba.druid.sql.lexer.Dictionary;
import com.alibaba.druid.sql.lexer.Lexer;
import com.alibaba.druid.sql.lexer.Token;
public class SQLServerLexer extends Lexer {
private static Dictionary dictionary = new Dictionary();
static {
dictionary.fill(SQLServerKeyword.values());
}
public SQLServerLexer(final String input) {
super(input, Token.getSqlserverKeywords());
super(input, dictionary);
}
@Override
......@@ -40,12 +48,12 @@ public class SQLServerLexer extends Lexer {
private void scanNChar() {
increaseCurrentPosition();
scanString();
setToken(Token.LITERAL_NCHARS);
setToken(DataType.LITERAL_NCHARS);
scanString();
}
@Override
protected boolean isHint() {
return ('/' == charAt(getCurrentPosition()) && '*' == charAt(getCurrentPosition() + 1) && '!' == charAt(getCurrentPosition() + 2));
return '/' == charAt(getCurrentPosition()) && '*' == charAt(getCurrentPosition() + 1) && '!' == charAt(getCurrentPosition() + 2);
}
}
package com.alibaba.druid.sql.dialect.sqlserver.parser;
import com.alibaba.druid.sql.lexer.Token;
import com.alibaba.druid.sql.lexer.DefaultKeyword;
import com.alibaba.druid.sql.parser.AbstractDeleteParser;
import com.alibaba.druid.sql.parser.SQLExprParser;
......@@ -19,6 +19,6 @@ public final class SQLServerDeleteParser extends AbstractDeleteParser {
protected void skipBetweenDeleteAndTable() {
((SQLServerExprParser) getExprParser()).parseTop();
((SQLServerExprParser) getExprParser()).skipOutput();
getExprParser().getLexer().skipIfEqual(Token.FROM);
getExprParser().getLexer().skipIfEqual(DefaultKeyword.FROM);
}
}
......@@ -19,7 +19,11 @@ package com.alibaba.druid.sql.dialect.sqlserver.parser;
import com.alibaba.druid.sql.context.LimitContext;
import com.alibaba.druid.sql.context.SelectSQLContext;
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop;
import com.alibaba.druid.sql.lexer.Token;
import com.alibaba.druid.sql.dialect.sqlserver.lexer.SQLServerKeyword;
import com.alibaba.druid.sql.dialect.sqlserver.lexer.SQLServerLexer;
import com.alibaba.druid.sql.lexer.DataType;
import com.alibaba.druid.sql.lexer.DefaultKeyword;
import com.alibaba.druid.sql.lexer.Symbol;
import com.alibaba.druid.sql.parser.ParserException;
import com.alibaba.druid.sql.parser.ParserUnsupportedException;
import com.alibaba.druid.sql.parser.SQLExprParser;
......@@ -41,7 +45,7 @@ public class SQLServerExprParser extends SQLExprParser {
// getLexer().nextToken();
//
// boolean paren = false;
// if (getLexer().equalToken(Token.LEFT_PAREN)) {
// if (getLexer().equalToken(Symbol.LEFT_PAREN)) {
// paren = true;
// getLexer().nextToken();
// }
......@@ -49,7 +53,7 @@ public class SQLServerExprParser extends SQLExprParser {
// top.setExpr(primary());
//
// if (paren) {
// getLexer().accept(Token.RIGHT_PAREN);
// getLexer().accept(Symbol.RIGHT_PAREN);
// }
//
// if (getLexer().equalToken(Token.PERCENT)) {
......@@ -63,8 +67,8 @@ public class SQLServerExprParser extends SQLExprParser {
}
protected void skipOutput() {
if (getLexer().equalToken(Token.OUTPUT)) {
throw new ParserUnsupportedException(Token.OUTPUT);
if (getLexer().equalToken(SQLServerKeyword.OUTPUT)) {
throw new ParserUnsupportedException(SQLServerKeyword.OUTPUT);
}
}
......@@ -72,9 +76,9 @@ public class SQLServerExprParser extends SQLExprParser {
getLexer().nextToken();
int offset;
int offsetIndex = -1;
if (getLexer().equalToken(Token.LITERAL_INT)) {
if (getLexer().equalToken(DataType.LITERAL_INT)) {
offset = Integer.parseInt(getLexer().getLiterals());
} else if (getLexer().equalToken(Token.QUESTION)) {
} else if (getLexer().equalToken(Symbol.QUESTION)) {
offsetIndex = getParametersIndex();
offset = (int) getParameters().get(offsetIndex);
setParametersIndex(offsetIndex + 1);
......@@ -83,14 +87,14 @@ public class SQLServerExprParser extends SQLExprParser {
}
getLexer().nextToken();
LimitContext limitContext;
if (getLexer().skipIfEqual(Token.FETCH)) {
if (getLexer().skipIfEqual(DefaultKeyword.FETCH)) {
getLexer().nextToken();
int rowCount;
int rowCountIndex = -1;
getLexer().nextToken();
if (getLexer().equalToken(Token.LITERAL_INT)) {
if (getLexer().equalToken(DataType.LITERAL_INT)) {
rowCount = Integer.parseInt(getLexer().getLiterals());
} else if (getLexer().equalToken(Token.QUESTION)) {
} else if (getLexer().equalToken(Symbol.QUESTION)) {
rowCountIndex = getParametersIndex();
rowCount = (int) getParameters().get(rowCountIndex);
setParametersIndex(rowCountIndex + 1);
......
......@@ -17,7 +17,10 @@
package com.alibaba.druid.sql.dialect.sqlserver.parser;
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock;
import com.alibaba.druid.sql.lexer.Token;
import com.alibaba.druid.sql.dialect.sqlserver.lexer.SQLServerKeyword;
import com.alibaba.druid.sql.lexer.DataType;
import com.alibaba.druid.sql.lexer.DefaultKeyword;
import com.alibaba.druid.sql.lexer.Symbol;
import com.alibaba.druid.sql.parser.AbstractSelectParser;
import com.alibaba.druid.sql.parser.ParserUnsupportedException;
import com.alibaba.druid.sql.parser.SQLExprParser;
......@@ -30,10 +33,10 @@ public class SQLServerSelectParser extends AbstractSelectParser {
@Override
protected void customizedSelect() {
if (getExprParser().getLexer().equalToken(Token.FOR)) {
if (getExprParser().getLexer().equalToken(DefaultKeyword.FOR)) {
parseFor();
}
if (getExprParser().getLexer().equalToken(Token.OFFSET)) {
if (getExprParser().getLexer().equalToken(SQLServerKeyword.OFFSET)) {
((SQLServerExprParser) getExprParser()).parseOffset(getSqlContext());
}
}
......@@ -41,18 +44,18 @@ public class SQLServerSelectParser extends AbstractSelectParser {
@Override
public void query() {
SQLServerSelectQueryBlock queryBlock = new SQLServerSelectQueryBlock();
if (getExprParser().getLexer().equalToken(Token.SELECT)) {
if (getExprParser().getLexer().equalToken(DefaultKeyword.SELECT)) {
getExprParser().getLexer().nextToken();
if (getExprParser().getLexer().equalToken(Token.COMMENT)) {
if (getExprParser().getLexer().equalToken(DataType.COMMENT)) {
getExprParser().getLexer().nextToken();
}
parseDistinct();
if (getExprParser().getLexer().equalToken(Token.TOP)) {
if (getExprParser().getLexer().equalToken(SQLServerKeyword.TOP)) {
queryBlock.setTop(((SQLServerExprParser) getExprParser()).parseTop());
}
parseSelectList();
}
if (getExprParser().getLexer().equalToken(Token.INTO)) {
if (getExprParser().getLexer().equalToken(DefaultKeyword.INTO)) {
throw new ParserUnsupportedException(getExprParser().getLexer().getToken());
}
parseFrom();
......@@ -63,7 +66,7 @@ public class SQLServerSelectParser extends AbstractSelectParser {
@Override
protected void parseJoinTable() {
if (getExprParser().getLexer().skipIfEqual(Token.WITH)) {
if (getExprParser().getLexer().skipIfEqual(DefaultKeyword.WITH)) {
getExprParser().getLexer().skipParentheses();
}
super.parseJoinTable();
......@@ -71,18 +74,18 @@ public class SQLServerSelectParser extends AbstractSelectParser {
private void parseFor() {
getExprParser().getLexer().nextToken();
if (getExprParser().getLexer().equalToken(Token.BROWSE)) {
if (getExprParser().getLexer().equalToken(SQLServerKeyword.BROWSE)) {
getExprParser().getLexer().nextToken();
} else if (getExprParser().getLexer().skipIfEqual(Token.XML)) {
} else if (getExprParser().getLexer().skipIfEqual(SQLServerKeyword.XML)) {
while (true) {
if (getExprParser().getLexer().equalToken(Token.AUTO, Token.TYPE, Token.XMLSCHEMA)) {
if (getExprParser().getLexer().equalToken(SQLServerKeyword.AUTO, SQLServerKeyword.TYPE, SQLServerKeyword.XMLSCHEMA)) {
getExprParser().getLexer().nextToken();
} else if (getExprParser().getLexer().skipIfEqual(Token.ELEMENTS)) {
getExprParser().getLexer().skipIfEqual(Token.XSINIL);
} else if (getExprParser().getLexer().skipIfEqual(SQLServerKeyword.ELEMENTS)) {
getExprParser().getLexer().skipIfEqual(SQLServerKeyword.XSINIL);
} else {
break;
}
if (getExprParser().getLexer().equalToken(Token.COMMA)) {
if (getExprParser().getLexer().equalToken(Symbol.COMMA)) {
getExprParser().getLexer().nextToken();
} else {
break;
......
package com.alibaba.druid.sql.lexer;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
/**
* 数据类型标记.
*
* @author zhangliang
*/
@RequiredArgsConstructor
@Getter
public enum DataType implements Token {
IDENTIFIER,
LITERAL_INT,
LITERAL_FLOAT,
LITERAL_HEX,
LITERAL_CHARS,
LITERAL_NCHARS,
LITERAL_ALIAS,
BINARY_FLOAT,
BINARY_DOUBLE,
VARIANT,
HINT,
COMMENT,
LINE_COMMENT,
MULTI_LINE_COMMENT,
ERROR,
EOF,
}
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License,Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package com.alibaba.druid.sql.lexer;
/**
* 默认关键词.
*
* @author zhangliang
*/
public enum DefaultKeyword implements Keyword {
SELECT,
DELETE,
INSERT,
UPDATE,
CREATE,
ALTER,
DROP,
TRUNCATE,
REPLACE,
DECLARE,
GRANT,
REVOKE,
AS,
DISTINCT,
DISTINCTROW,
MAX,
MIN,
SUM,
AVG,
COUNT,
FROM,
WHERE,
ORDER,
ASC,
DESC,
GROUP,
BY,
HAVING,
INTO,
VALUES,
COLUMN,
TABLE,
TABLESPACE,
SET,
PRIMARY,
KEY,
INDEX,
CONSTRAINT,
CHECK,
UNIQUE,
FOREIGN,
REFERENCES,
INNER,
LEFT,
RIGHT,
FULL,
OUTER,
CROSS,
JOIN,
STRAIGHT_JOIN,
APPLY,
ON,
IS,
IN,
BETWEEN,
LIKE,
AND,
OR,
XOR,
NULL,
NOT,
FOR,
ALL,
UNION,
CAST,
USE,
USING,
TO,
CASE,
WHEN,
THEN,
ELSE,
END,
EXISTS,
NEW,
ESCAPE,
INTERVAL,
LOCK,
SOME,
ANY,
WHILE,
DO,
LEAVE,
ITERATE,
REPEAT,
UNTIL,
OPEN,
CLOSE,
OUT,
INOUT,
OVER,
FETCH,
WITH,
CURSOR,
ADVISE,
SIBLINGS,
LOOP,
ENABLE,
DISABLE,
EXPLAIN,
SCHEMA,
DATABASE,
VIEW,
SEQUENCE,
TRIGGER,
PROCEDURE,
FUNCTION,
DEFAULT,
EXCEPT,
INTERSECT,
MINUS,
USER,
PASSWORD,
CONNECT_BY_ROOT
}
package com.alibaba.druid.sql.lexer;
import lombok.Getter;
import java.util.HashMap;
import java.util.Map;
/**
* 词法解析字典.
*
* @author zhangliang
*/
@Getter
public class Dictionary {
private final Map<String, Token> tokens = new HashMap<>(1024);
/**
* 填充字典.
*
* @param dialectKeywords 方言关键词
*/
public void fill(final Keyword... dialectKeywords) {
for (Symbol each : Symbol.values()) {
tokens.put(each.getLiterals(), each);
}
for (DataType each : DataType.values()) {
tokens.put(each.name(), each);
}
for (DefaultKeyword each : DefaultKeyword.values()) {
tokens.put(each.name(), each);
}
for (Keyword each : dialectKeywords) {
tokens.put(each.toString(), each);
}
}
}
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License,Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package com.alibaba.druid.sql.lexer;
/**
* 关键词接口.
*
* @author zhangliang
*/
public interface Keyword extends Token {
}
......@@ -22,7 +22,6 @@ import com.google.common.collect.Sets;
import lombok.Getter;
import lombok.Setter;
import java.util.Map;
import java.util.Set;
/**
......@@ -50,7 +49,7 @@ public class Lexer {
@Setter
private String literals;
public Lexer(final String input, final Map<String, Token> dictionary) {
public Lexer(final String input, final Dictionary dictionary) {
this.input = input;
term = new Term(input, dictionary);
}
......@@ -107,9 +106,9 @@ public class Lexer {
return;
}
if (isEOF()) {
token = Token.EOF;
token = DataType.EOF;
} else {
token = Token.ERROR;
token = DataType.ERROR;
}
literals = "";
}
......@@ -130,11 +129,11 @@ public class Lexer {
protected void scanVariable() {
char nextChar = charAt(currentPosition + 1);
if ('{' == nextChar) {
term.scanContentUntil(currentPosition, '}', Token.VARIANT, false);
term.scanContentUntil(currentPosition, '}', DataType.VARIANT, false);
} else if ('`' == nextChar) {
term.scanContentUntil(currentPosition, '`', Token.VARIANT, false);
term.scanContentUntil(currentPosition, '`', DataType.VARIANT, false);
} else if ('"' == nextChar) {
term.scanContentUntil(currentPosition, '"', Token.VARIANT, false);
term.scanContentUntil(currentPosition, '"', DataType.VARIANT, false);
} else {
term.scanVariable(currentPosition);
}
......@@ -147,7 +146,7 @@ public class Lexer {
protected void scanIdentifier() {
if ('`' == charAt(currentPosition)) {
term.scanContentUntil(currentPosition, '`', Token.IDENTIFIER, false);
term.scanContentUntil(currentPosition, '`', DataType.IDENTIFIER, false);
} else {
term.scanIdentifier(currentPosition);
}
......@@ -251,7 +250,7 @@ public class Lexer {
}
private void scanAlias() {
term.scanContentUntil(currentPosition, '\"', Token.LITERAL_ALIAS, true);
term.scanContentUntil(currentPosition, '\"', DataType.LITERAL_ALIAS, true);
setTermResult();
}
......@@ -320,7 +319,7 @@ public class Lexer {
*/
public final void skipUntil(final Token... tokens) {
Set<Token> tokenSet = Sets.newHashSet(tokens);
tokenSet.add(Token.EOF);
tokenSet.add(DataType.EOF);
while (!tokenSet.contains(token)) {
nextToken();
}
......@@ -334,17 +333,17 @@ public class Lexer {
public final String skipParentheses() {
StringBuilder result = new StringBuilder("");
int count = 0;
if (Token.LEFT_PAREN == token) {
if (Symbol.LEFT_PAREN == token) {
int beginPosition = currentPosition;
result.append(Token.LEFT_PAREN.getName());
result.append(Symbol.LEFT_PAREN.getLiterals());
nextToken();
while (true) {
if (Token.EOF == token || (Token.RIGHT_PAREN == token && 0 == count)) {
if (DataType.EOF == token || (Symbol.RIGHT_PAREN == token && 0 == count)) {
break;
}
if (Token.LEFT_PAREN == token) {
if (Symbol.LEFT_PAREN == token) {
count++;
} else if (Token.RIGHT_PAREN == token) {
} else if (Symbol.RIGHT_PAREN == token) {
count--;
}
nextToken();
......
package com.alibaba.druid.sql.lexer;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
/**
* 符号标记.
*
* @author zhangliang
*/
@RequiredArgsConstructor
@Getter
public enum Symbol implements Token {
LEFT_PAREN("("),
RIGHT_PAREN(")"),
LEFT_BRACE("{"),
RIGHT_BRACE("}"),
LEFT_BRACKET("["),
RIGHT_BRACKET("]"),
SEMI(";"),
COMMA(","),
DOT("."),
DOUBLE_DOT(".."),
PLUS("+"),
SUB("-"),
STAR("*"),
SLASH("/"),
QUESTION("?"),
EQ("="),
GT(">"),
LT("<"),
BANG("!"),
TILDE("~"),
CARET("^"),
PERCENT("%"),
COLON(":"),
DOUBLE_COLON("::"),
COLON_EQ(":="),
LT_EQ("<="),
GT_EQ(">="),
LT_EQ_GT("<=>"),
LT_GT("<>"),
BANG_EQ("!="),
BANG_GT("!>"),
BANG_LT("!<"),
AMP("&"),
BAR("|"),
DOUBLE_AMP("&&"),
DOUBLE_BAR("||"),
DOUBLE_LT("<<"),
DOUBLE_GT(">>"),
MONKEYS_AT("@"),
POUND("#");
private final String literals;
}
......@@ -21,8 +21,6 @@ import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import java.util.Map;
/**
* 处理词.
*
......@@ -33,7 +31,7 @@ public final class Term {
private final String input;
private final Map<String, Token> dictionary;
private final Dictionary dictionary;
@Getter(AccessLevel.PACKAGE)
private int offset;
......@@ -67,7 +65,7 @@ public final class Term {
}
length++;
literals = contentOnly ? input.substring(offset + 1, offset + length - 1) : input.substring(offset, offset + length);
token = null == dictionary.get(literals.toUpperCase()) ? defaultToken : dictionary.get(literals.toUpperCase());
token = null == dictionary.getTokens().get(literals.toUpperCase()) ? defaultToken : dictionary.getTokens().get(literals.toUpperCase());
}
void scanVariable(final int currentPosition) {
......@@ -82,7 +80,7 @@ public final class Term {
length++;
}
literals = input.substring(offset, offset + length);
token = Token.VARIANT;
token = DataType.VARIANT;
}
void scanIdentifier(final int currentPosition) {
......@@ -94,18 +92,18 @@ public final class Term {
}
literals = input.substring(offset, offset + length);
String upperCaseLiterals = literals.toUpperCase();
if (Token.ORDER.getName().equals(upperCaseLiterals) || Token.GROUP.getName().equals(upperCaseLiterals)) {
if (DefaultKeyword.ORDER.toString().equals(upperCaseLiterals) || DefaultKeyword.GROUP.toString().equals(upperCaseLiterals)) {
int i = 0;
while (CharTypes.isWhitespace(charAt(position + i))) {
i++;
}
if (Token.BY.getName().equalsIgnoreCase(String.valueOf(new char[] {charAt(position + i), charAt(position + i + 1)}))) {
token = dictionary.get(literals.toUpperCase());
if (DefaultKeyword.BY.toString().equalsIgnoreCase(String.valueOf(new char[] {charAt(position + i), charAt(position + i + 1)}))) {
token = dictionary.getTokens().get(literals.toUpperCase());
} else {
token = Token.IDENTIFIER;
token = DataType.IDENTIFIER;
}
} else {
token = dictionary.containsKey(upperCaseLiterals) ? dictionary.get(upperCaseLiterals) : Token.IDENTIFIER;
token = dictionary.getTokens().containsKey(upperCaseLiterals) ? dictionary.getTokens().get(upperCaseLiterals) : DataType.IDENTIFIER;
}
}
......@@ -125,7 +123,7 @@ public final class Term {
length++;
}
literals = input.substring(offset, offset + length);
token = Token.LITERAL_HEX;
token = DataType.LITERAL_HEX;
}
private boolean isHex(final char ch) {
......@@ -150,7 +148,7 @@ public final class Term {
if ('.' == charAt(position + 1)) {
length++;
literals = input.substring(offset, offset + length);
token = Token.LITERAL_INT;
token = DataType.LITERAL_INT;
return;
}
isFloat = true;
......@@ -182,17 +180,17 @@ public final class Term {
if ('f' == charAt(position) || 'F' == charAt(position)) {
length++;
literals = input.substring(offset, offset + length);
token = Token.BINARY_FLOAT;
token = DataType.BINARY_FLOAT;
return;
}
if ('d' == charAt(position) || 'D' == charAt(position)) {
length++;
literals = input.substring(offset, offset + length);
token = Token.BINARY_DOUBLE;
token = DataType.BINARY_DOUBLE;
return;
}
literals = input.substring(offset, offset + length);
token = isFloat ? Token.LITERAL_FLOAT : Token.LITERAL_INT;
token = isFloat ? DataType.LITERAL_FLOAT : DataType.LITERAL_INT;
}
private boolean isDigital(final char ch) {
......@@ -216,7 +214,7 @@ public final class Term {
}
length++;
literals = input.substring(offset + 1, offset + length - 1);
token = Token.LITERAL_CHARS;
token = DataType.LITERAL_CHARS;
}
private boolean hasEscapeChar(final int position) {
......@@ -236,7 +234,7 @@ public final class Term {
}
length += 2;
literals = input.substring(offset + 4, offset + length - 2);
token = Token.HINT;
token = DataType.HINT;
}
void scanSingleLineComment(final int currentPosition, final int commentFlagLength) {
......@@ -248,7 +246,7 @@ public final class Term {
length++;
}
literals = input.substring(offset, offset + length);
token = Token.LINE_COMMENT;
token = DataType.LINE_COMMENT;
}
void scanMultiLineComment(final int currentPosition) {
......@@ -264,7 +262,7 @@ public final class Term {
}
length += 2;
literals = input.substring(offset, offset + length);
token = Token.MULTI_LINE_COMMENT;
token = DataType.MULTI_LINE_COMMENT;
}
void scanSymbol(final int currentPosition, final int charLength) {
......@@ -277,7 +275,7 @@ public final class Term {
length++;
}
literals = String.valueOf(symbolChars);
token = dictionary.get(literals);
token = dictionary.getTokens().get(literals);
}
private char charAt(final int index) {
......
package com.alibaba.druid.sql.parser;
import com.alibaba.druid.sql.context.DeleteSQLContext;
import com.alibaba.druid.sql.lexer.Token;
import com.alibaba.druid.sql.lexer.DefaultKeyword;
import com.dangdang.ddframe.rdb.sharding.parser.result.router.ConditionContext;
import com.google.common.base.Optional;
import lombok.AccessLevel;
......@@ -35,7 +35,7 @@ public abstract class AbstractDeleteParser {
exprParser.getLexer().nextToken();
skipBetweenDeleteAndTable();
exprParser.parseSingleTable(sqlContext);
exprParser.getLexer().skipUntil(Token.WHERE);
exprParser.getLexer().skipUntil(DefaultKeyword.WHERE);
Optional<ConditionContext> conditionContext = exprParser.parseWhere(sqlContext);
if (conditionContext.isPresent()) {
sqlContext.getConditionContexts().add(conditionContext.get());
......
......@@ -7,7 +7,10 @@ import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
import com.alibaba.druid.sql.context.InsertSQLContext;
import com.alibaba.druid.sql.context.ItemsToken;
import com.alibaba.druid.sql.context.TableContext;
import com.alibaba.druid.sql.lexer.Token;
import com.alibaba.druid.sql.lexer.DataType;
import com.alibaba.druid.sql.lexer.DefaultKeyword;
import com.alibaba.druid.sql.lexer.Keyword;
import com.alibaba.druid.sql.lexer.Symbol;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition;
import com.dangdang.ddframe.rdb.sharding.parser.visitor.ParseContext;
......@@ -54,10 +57,10 @@ public abstract class AbstractInsertParser {
exprParser.getLexer().nextToken();
parseInto();
Collection<Condition.Column> columns = parseColumns();
if (exprParser.getLexer().equalToken(Token.SELECT, Token.LEFT_PAREN)) {
if (exprParser.getLexer().equalToken(DefaultKeyword.SELECT, Symbol.LEFT_PAREN)) {
throw new UnsupportedOperationException("Cannot support subquery");
}
if (getValuesTokens().contains(exprParser.getLexer().getToken())) {
if (getValuesKeywords().contains(exprParser.getLexer().getToken())) {
parseValues(columns);
} else if (getCustomizedInsertTokens().contains(exprParser.getLexer().getToken())) {
parseCustomizedInsert();
......@@ -65,16 +68,16 @@ public abstract class AbstractInsertParser {
return sqlContext;
}
protected Set<Token> getUnsupportedTokens() {
protected Set<Keyword> getUnsupportedKeywords() {
return Collections.emptySet();
}
private void parseInto() {
exprParser.getLexer().skipIfEqual(Token.HINT);
if (getUnsupportedTokens().contains(exprParser.getLexer().getToken())) {
exprParser.getLexer().skipIfEqual(DataType.HINT);
if (getUnsupportedKeywords().contains(exprParser.getLexer().getToken())) {
throw new ParserUnsupportedException(exprParser.getLexer().getToken());
}
exprParser.getLexer().skipUntil(Token.INTO);
exprParser.getLexer().skipUntil(DefaultKeyword.INTO);
exprParser.getLexer().nextToken();
exprParser.parseSingleTable(sqlContext);
skipBetweenTableAndValues();
......@@ -83,25 +86,25 @@ public abstract class AbstractInsertParser {
private void skipBetweenTableAndValues() {
while (getSkippedTokensBetweenTableAndValues().contains(exprParser.getLexer().getToken())) {
exprParser.getLexer().nextToken();
if (exprParser.getLexer().equalToken(Token.LEFT_PAREN)) {
if (exprParser.getLexer().equalToken(Symbol.LEFT_PAREN)) {
exprParser.getLexer().skipParentheses();
}
}
}
protected Set<Token> getSkippedTokensBetweenTableAndValues() {
protected Set<Keyword> getSkippedTokensBetweenTableAndValues() {
return Collections.emptySet();
}
private Collection<Condition.Column> parseColumns() {
Collection<Condition.Column> result = new LinkedList<>();
Collection<String> autoIncrementColumns = shardingRule.getAutoIncrementColumns(sqlContext.getTables().get(0).getName());
if (exprParser.getLexer().equalToken(Token.LEFT_PAREN)) {
if (exprParser.getLexer().equalToken(Symbol.LEFT_PAREN)) {
do {
exprParser.getLexer().nextToken();
result.add(getColumn(autoIncrementColumns));
exprParser.getLexer().nextToken();
} while (!exprParser.getLexer().equalToken(Token.RIGHT_PAREN) && !exprParser.getLexer().equalToken(Token.EOF));
} while (!exprParser.getLexer().equalToken(Symbol.RIGHT_PAREN) && !exprParser.getLexer().equalToken(DataType.EOF));
ItemsToken itemsToken = new ItemsToken(exprParser.getLexer().getCurrentPosition() - exprParser.getLexer().getLiterals().length());
for (String each : autoIncrementColumns) {
itemsToken.getItems().add(each);
......@@ -123,8 +126,8 @@ public abstract class AbstractInsertParser {
return new Condition.Column(columnName, sqlContext.getTables().get(0).getName());
}
protected Set<Token> getValuesTokens() {
return Sets.newHashSet(Token.VALUES);
protected Set<Keyword> getValuesKeywords() {
return Sets.<Keyword>newHashSet(DefaultKeyword.VALUES);
}
private void parseValues(final Collection<Condition.Column> columns) {
......@@ -135,21 +138,11 @@ public abstract class AbstractInsertParser {
throw new UnsupportedOperationException("Cannot support multiple insert");
}
exprParser.getLexer().nextToken();
exprParser.getLexer().accept(Token.LEFT_PAREN);
// List<SQLExpr> sqlExprs = getExprParser().exprList(new SQLIdentifierExpr(""));
exprParser.getLexer().accept(Symbol.LEFT_PAREN);
List<SQLExpr> sqlExprs = new LinkedList<>();
do {
sqlExprs.add(exprParser.parseExpr());
} while (exprParser.getLexer().skipIfEqual(Token.COMMA));
} while (exprParser.getLexer().skipIfEqual(Symbol.COMMA));
ItemsToken itemsToken = new ItemsToken(exprParser.getLexer().getCurrentPosition() - exprParser.getLexer().getLiterals().length());
int count = 0;
int parameterCount = 0;
......@@ -181,10 +174,10 @@ public abstract class AbstractInsertParser {
if (!itemsToken.getItems().isEmpty()) {
sqlContext.getSqlTokens().add(itemsToken);
}
exprParser.getLexer().accept(Token.RIGHT_PAREN);
exprParser.getLexer().accept(Symbol.RIGHT_PAREN);
parsed = true;
}
while (exprParser.getLexer().equalToken(Token.COMMA));
while (exprParser.getLexer().equalToken(Symbol.COMMA));
sqlContext.getConditionContexts().add(parseContext.getCurrentConditionContext());
}
......@@ -197,7 +190,7 @@ public abstract class AbstractInsertParser {
return result;
}
protected Set<Token> getCustomizedInsertTokens() {
protected Set<Keyword> getCustomizedInsertTokens() {
return Collections.emptySet();
}
......
......@@ -25,7 +25,9 @@ import com.alibaba.druid.sql.context.SelectItemContext;
import com.alibaba.druid.sql.context.SelectSQLContext;
import com.alibaba.druid.sql.context.TableContext;
import com.alibaba.druid.sql.context.TableToken;
import com.alibaba.druid.sql.lexer.Token;
import com.alibaba.druid.sql.lexer.DataType;
import com.alibaba.druid.sql.lexer.DefaultKeyword;
import com.alibaba.druid.sql.lexer.Symbol;
import com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn;
import com.dangdang.ddframe.rdb.sharding.parser.result.router.ConditionContext;
import com.dangdang.ddframe.rdb.sharding.util.SQLUtil;
......@@ -67,8 +69,8 @@ public abstract class AbstractSelectParser {
}
protected void query() {
getExprParser().getLexer().accept(Token.SELECT);
getExprParser().getLexer().skipIfEqual(Token.COMMENT);
getExprParser().getLexer().accept(DefaultKeyword.SELECT);
getExprParser().getLexer().skipIfEqual(DataType.COMMENT);
parseDistinct();
parseSelectList();
parseFrom();
......@@ -78,14 +80,14 @@ public abstract class AbstractSelectParser {
}
protected final void parseDistinct() {
if (getExprParser().getLexer().equalToken(Token.DISTINCT, Token.DISTINCTROW, Token.UNION)) {
if (getExprParser().getLexer().equalToken(DefaultKeyword.DISTINCT, DefaultKeyword.DISTINCTROW, DefaultKeyword.UNION)) {
sqlContext.setDistinct(true);
getExprParser().getLexer().nextToken();
if (hasDistinctOn() && getExprParser().getLexer().equalToken(Token.ON)) {
if (hasDistinctOn() && getExprParser().getLexer().equalToken(DefaultKeyword.ON)) {
getExprParser().getLexer().nextToken();
getExprParser().getLexer().skipParentheses();
}
} else if (getExprParser().getLexer().equalToken(Token.ALL)) {
} else if (getExprParser().getLexer().equalToken(DefaultKeyword.ALL)) {
getExprParser().getLexer().nextToken();
}
}
......@@ -103,12 +105,12 @@ public abstract class AbstractSelectParser {
sqlContext.setContainStar(true);
}
index++;
} while (getExprParser().getLexer().skipIfEqual(Token.COMMA));
} while (getExprParser().getLexer().skipIfEqual(Symbol.COMMA));
sqlContext.setSelectListLastPosition(getExprParser().getLexer().getCurrentPosition() - getExprParser().getLexer().getLiterals().length());
}
protected void queryRest() {
if (getExprParser().getLexer().equalToken(Token.UNION, Token.EXCEPT, Token.INTERSECT, Token.MINUS)) {
if (getExprParser().getLexer().equalToken(DefaultKeyword.UNION, DefaultKeyword.EXCEPT, DefaultKeyword.INTERSECT, DefaultKeyword.MINUS)) {
throw new ParserUnsupportedException(getExprParser().getLexer().getToken());
}
}
......@@ -125,31 +127,31 @@ public abstract class AbstractSelectParser {
}
protected void parseGroupBy() {
if (getExprParser().getLexer().skipIfEqual(Token.GROUP)) {
getExprParser().getLexer().accept(Token.BY);
if (getExprParser().getLexer().skipIfEqual(DefaultKeyword.GROUP)) {
getExprParser().getLexer().accept(DefaultKeyword.BY);
while (true) {
addGroupByItem(exprParser.parseExpr(sqlContext));
if (!getExprParser().getLexer().equalToken(Token.COMMA)) {
if (!getExprParser().getLexer().equalToken(Symbol.COMMA)) {
break;
}
getExprParser().getLexer().nextToken();
}
while (getExprParser().getLexer().equalToken(Token.WITH) || getExprParser().getLexer().getLiterals().equalsIgnoreCase("ROLLUP")) {
while (getExprParser().getLexer().equalToken(DefaultKeyword.WITH) || getExprParser().getLexer().getLiterals().equalsIgnoreCase("ROLLUP")) {
getExprParser().getLexer().nextToken();
}
if (getExprParser().getLexer().skipIfEqual(Token.HAVING)) {
if (getExprParser().getLexer().skipIfEqual(DefaultKeyword.HAVING)) {
exprParser.parseExpr(sqlContext);
}
} else if (getExprParser().getLexer().skipIfEqual(Token.HAVING)) {
} else if (getExprParser().getLexer().skipIfEqual(DefaultKeyword.HAVING)) {
exprParser.parseExpr(sqlContext);
}
}
protected final void addGroupByItem(final SQLExpr sqlExpr) {
OrderByColumn.OrderByType orderByType = OrderByColumn.OrderByType.ASC;
if (getExprParser().getLexer().equalToken(Token.ASC)) {
if (getExprParser().getLexer().equalToken(DefaultKeyword.ASC)) {
getExprParser().getLexer().nextToken();
} else if (getExprParser().getLexer().skipIfEqual(Token.DESC)) {
} else if (getExprParser().getLexer().skipIfEqual(DefaultKeyword.DESC)) {
orderByType = OrderByColumn.OrderByType.DESC;
}
if (sqlExpr instanceof SQLPropertyExpr) {
......@@ -162,13 +164,13 @@ public abstract class AbstractSelectParser {
}
public final void parseFrom() {
if (getExprParser().getLexer().skipIfEqual(Token.FROM)) {
if (getExprParser().getLexer().skipIfEqual(DefaultKeyword.FROM)) {
parseTable();
}
}
public List<TableContext> parseTable() {
if (getExprParser().getLexer().equalToken(Token.LEFT_PAREN)) {
if (getExprParser().getLexer().equalToken(Symbol.LEFT_PAREN)) {
throw new UnsupportedOperationException("Cannot support subquery");
}
parseTableFactor();
......@@ -180,7 +182,7 @@ public abstract class AbstractSelectParser {
int beginPosition = getExprParser().getLexer().getCurrentPosition() - getExprParser().getLexer().getLiterals().length();
String literals = getExprParser().getLexer().getLiterals();
getExprParser().getLexer().nextToken();
if (getExprParser().getLexer().skipIfEqual(Token.DOT)) {
if (getExprParser().getLexer().skipIfEqual(Symbol.DOT)) {
getExprParser().getLexer().nextToken();
getExprParser().as();
return;
......@@ -191,16 +193,16 @@ public abstract class AbstractSelectParser {
}
protected void parseJoinTable() {
getExprParser().getLexer().skipIfEqual(Token.HINT);
getExprParser().getLexer().skipIfEqual(DataType.HINT);
if (getExprParser().isJoin()) {
parseTable();
if (getExprParser().getLexer().skipIfEqual(Token.ON)) {
if (getExprParser().getLexer().skipIfEqual(DefaultKeyword.ON)) {
do {
parseTableCondition(getExprParser().getLexer().getCurrentPosition());
getExprParser().getLexer().accept(Token.EQ);
getExprParser().getLexer().accept(Symbol.EQ);
parseTableCondition(getExprParser().getLexer().getCurrentPosition() - getExprParser().getLexer().getLiterals().length());
} while (getExprParser().getLexer().skipIfEqual(Token.AND));
} else if (getExprParser().getLexer().skipIfEqual(Token.USING)) {
} while (getExprParser().getLexer().skipIfEqual(DefaultKeyword.AND));
} else if (getExprParser().getLexer().skipIfEqual(DefaultKeyword.USING)) {
getExprParser().getLexer().skipParentheses();
}
parseJoinTable();
......
......@@ -2,7 +2,8 @@ package com.alibaba.druid.sql.parser;
import com.alibaba.druid.sql.context.TableToken;
import com.alibaba.druid.sql.context.UpdateSQLContext;
import com.alibaba.druid.sql.lexer.Token;
import com.alibaba.druid.sql.lexer.DefaultKeyword;
import com.alibaba.druid.sql.lexer.Symbol;
import com.dangdang.ddframe.rdb.sharding.parser.result.router.ConditionContext;
import com.dangdang.ddframe.rdb.sharding.util.SQLUtil;
import com.google.common.base.Optional;
......@@ -38,7 +39,7 @@ public abstract class AbstractUpdateParser {
skipBetweenUpdateAndTable();
exprParser.parseSingleTable(sqlContext);
parseSetItems();
exprParser.getLexer().skipUntil(Token.WHERE);
exprParser.getLexer().skipUntil(DefaultKeyword.WHERE);
exprParser.setParametersIndex(parametersIndex);
Optional<ConditionContext> conditionContext = exprParser.parseWhere(sqlContext);
if (conditionContext.isPresent()) {
......@@ -50,28 +51,28 @@ public abstract class AbstractUpdateParser {
protected abstract void skipBetweenUpdateAndTable();
private void parseSetItems() {
exprParser.getLexer().accept(Token.SET);
exprParser.getLexer().accept(DefaultKeyword.SET);
do {
parseSetItem();
} while (exprParser.getLexer().skipIfEqual(Token.COMMA));
} while (exprParser.getLexer().skipIfEqual(Symbol.COMMA));
}
private void parseSetItem() {
if (exprParser.getLexer().equalToken(Token.LEFT_PAREN)) {
if (exprParser.getLexer().equalToken(Symbol.LEFT_PAREN)) {
exprParser.getLexer().skipParentheses();
} else {
int beginPosition = exprParser.getLexer().getCurrentPosition();
String literals = exprParser.getLexer().getLiterals();
exprParser.getLexer().nextToken();
String tableName = sqlContext.getTables().get(0).getName();
if (exprParser.getLexer().skipIfEqual(Token.DOT)) {
if (exprParser.getLexer().skipIfEqual(Symbol.DOT)) {
if (tableName.equalsIgnoreCase(SQLUtil.getExactlyValue(literals))) {
sqlContext.getSqlTokens().add(new TableToken(beginPosition - literals.length(), literals, tableName));
}
exprParser.getLexer().nextToken();
}
}
exprParser.getLexer().skipIfEqual(Token.EQ, Token.COLON_EQ);
exprParser.getLexer().skipIfEqual(Symbol.EQ, Symbol.COLON_EQ);
exprParser.parseExpr(sqlContext);
parametersIndex = exprParser.getParametersIndex();
}
......
......@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.parser;
import com.alibaba.druid.sql.lexer.Token;
......@@ -24,6 +25,6 @@ public class ParserUnsupportedException extends RuntimeException {
private static final String MESSAGE = "Not supported token '%s'.";
public ParserUnsupportedException(final Token token) {
super(String.format(MESSAGE, token.getName()));
super(String.format(MESSAGE, token.toString()));
}
}
......@@ -35,8 +35,10 @@ import com.alibaba.druid.sql.context.SelectItemContext;
import com.alibaba.druid.sql.context.SelectSQLContext;
import com.alibaba.druid.sql.context.TableContext;
import com.alibaba.druid.sql.context.TableToken;
import com.alibaba.druid.sql.lexer.DataType;
import com.alibaba.druid.sql.lexer.DefaultKeyword;
import com.alibaba.druid.sql.lexer.Lexer;
import com.alibaba.druid.sql.lexer.Token;
import com.alibaba.druid.sql.lexer.Symbol;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn;
import com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn;
......@@ -79,9 +81,9 @@ public class SQLExprParser {
}
protected Optional<String> as() {
if (lexer.skipIfEqual(Token.AS)) {
if (lexer.skipIfEqual(DefaultKeyword.AS)) {
// TODO 判断Literals是符号则返回null, 目前仅判断为LEFT_PAREN
if (lexer.equalToken(Token.LEFT_PAREN)) {
if (lexer.equalToken(Symbol.LEFT_PAREN)) {
return Optional.absent();
}
String result = SQLUtil.getExactlyValue(lexer.getLiterals());
......@@ -89,7 +91,8 @@ public class SQLExprParser {
return Optional.of(result);
}
// TODO 增加哪些数据库识别哪些关键字作为别名的配置
if (lexer.equalToken(Token.IDENTIFIER, Token.LITERAL_ALIAS, Token.LITERAL_CHARS, Token.USER, Token.END, Token.CASE, Token.KEY, Token.INTERVAL, Token.CONSTRAINT)) {
if (lexer.equalToken(DataType.IDENTIFIER, DataType.LITERAL_ALIAS, DataType.LITERAL_CHARS,
DefaultKeyword.USER, DefaultKeyword.END, DefaultKeyword.CASE, DefaultKeyword.KEY, DefaultKeyword.INTERVAL, DefaultKeyword.CONSTRAINT)) {
String result = SQLUtil.getExactlyValue(lexer.getLiterals());
lexer.nextToken();
return Optional.of(result);
......@@ -98,17 +101,17 @@ public class SQLExprParser {
}
public List<OrderByContext> parseOrderBy(final SQLContext sqlContext) {
if (!getLexer().skipIfEqual(Token.ORDER)) {
if (!getLexer().skipIfEqual(DefaultKeyword.ORDER)) {
return Collections.emptyList();
}
List<OrderByContext> result = new LinkedList<>();
getLexer().skipIfEqual(Token.SIBLINGS);
getLexer().accept(Token.BY);
getLexer().skipIfEqual(DefaultKeyword.SIBLINGS);
getLexer().accept(DefaultKeyword.BY);
OrderByContext orderByContext = parseSelectOrderByItem(sqlContext);
if (null != orderByContext) {
result.add(orderByContext);
}
while (getLexer().equalToken(Token.COMMA)) {
while (getLexer().equalToken(Symbol.COMMA)) {
getLexer().nextToken();
orderByContext = parseSelectOrderByItem(sqlContext);
if (null != orderByContext) {
......@@ -121,9 +124,9 @@ public class SQLExprParser {
public OrderByContext parseSelectOrderByItem(final SQLContext sqlContext) {
SQLExpr expr = parseExpr(sqlContext);
OrderByColumn.OrderByType orderByType = OrderByColumn.OrderByType.ASC;
if (getLexer().equalToken(Token.ASC)) {
if (getLexer().equalToken(DefaultKeyword.ASC)) {
getLexer().nextToken();
} else if (getLexer().equalToken(Token.DESC)) {
} else if (getLexer().equalToken(DefaultKeyword.DESC)) {
getLexer().nextToken();
orderByType = OrderByColumn.OrderByType.DESC;
}
......@@ -142,8 +145,8 @@ public class SQLExprParser {
protected final void parseSingleTable(final SQLContext sqlContext) {
boolean hasParentheses = false;
if (getLexer().skipIfEqual(Token.LEFT_PAREN)) {
if (getLexer().equalToken(Token.SELECT)) {
if (getLexer().skipIfEqual(Symbol.LEFT_PAREN)) {
if (getLexer().equalToken(DefaultKeyword.SELECT)) {
throw new UnsupportedOperationException("Cannot support subquery");
}
hasParentheses = true;
......@@ -152,16 +155,16 @@ public class SQLExprParser {
int beginPosition = getLexer().getCurrentPosition() - getLexer().getLiterals().length();
String literals = getLexer().getLiterals();
getLexer().nextToken();
if (getLexer().skipIfEqual(Token.DOT)) {
if (getLexer().skipIfEqual(Symbol.DOT)) {
String tableName = getLexer().getLiterals();
getLexer().nextToken();
if (hasParentheses) {
getLexer().accept(Token.RIGHT_PAREN);
getLexer().accept(Symbol.RIGHT_PAREN);
}
tableContext = new TableContext(tableName, SQLUtil.getExactlyValue(literals), as());
} else {
if (hasParentheses) {
getLexer().accept(Token.RIGHT_PAREN);
getLexer().accept(Symbol.RIGHT_PAREN);
}
tableContext = new TableContext(literals, SQLUtil.getExactlyValue(literals), as());
}
......@@ -173,21 +176,21 @@ public class SQLExprParser {
}
public final boolean isJoin() {
if (getLexer().skipIfEqual(Token.LEFT, Token.RIGHT, Token.FULL)) {
getLexer().skipIfEqual(Token.OUTER);
getLexer().accept(Token.JOIN);
if (getLexer().skipIfEqual(DefaultKeyword.LEFT, DefaultKeyword.RIGHT, DefaultKeyword.FULL)) {
getLexer().skipIfEqual(DefaultKeyword.OUTER);
getLexer().accept(DefaultKeyword.JOIN);
return true;
} else if (getLexer().skipIfEqual(Token.INNER)) {
getLexer().accept(Token.JOIN);
} else if (getLexer().skipIfEqual(DefaultKeyword.INNER)) {
getLexer().accept(DefaultKeyword.JOIN);
return true;
} else if (getLexer().skipIfEqual(Token.JOIN, Token.COMMA, Token.STRAIGHT_JOIN)) {
} else if (getLexer().skipIfEqual(DefaultKeyword.JOIN, Symbol.COMMA, DefaultKeyword.STRAIGHT_JOIN)) {
return true;
} else if (getLexer().skipIfEqual(Token.CROSS)) {
if (getLexer().skipIfEqual(Token.JOIN, Token.APPLY)) {
} else if (getLexer().skipIfEqual(DefaultKeyword.CROSS)) {
if (getLexer().skipIfEqual(DefaultKeyword.JOIN, DefaultKeyword.APPLY)) {
return true;
}
} else if (getLexer().skipIfEqual(Token.OUTER)) {
if (getLexer().skipIfEqual(Token.APPLY)) {
} else if (getLexer().skipIfEqual(DefaultKeyword.OUTER)) {
if (getLexer().skipIfEqual(DefaultKeyword.APPLY)) {
return true;
}
}
......@@ -195,24 +198,24 @@ public class SQLExprParser {
}
public final SelectItemContext parseSelectItem(final int index, final SelectSQLContext sqlContext) {
getLexer().skipIfEqual(Token.CONNECT_BY_ROOT);
getLexer().skipIfEqual(DefaultKeyword.CONNECT_BY_ROOT);
String literals = getLexer().getLiterals();
if (getLexer().equalToken(Token.STAR) || Token.STAR.getName().equals(SQLUtil.getExactlyValue(literals))) {
if (getLexer().equalToken(Symbol.STAR) || Symbol.STAR.getLiterals().equals(SQLUtil.getExactlyValue(literals))) {
getLexer().nextToken();
return new CommonSelectItemContext(Token.STAR.getName(), as(), index, true);
return new CommonSelectItemContext(Symbol.STAR.getLiterals(), as(), index, true);
}
if (getLexer().skipIfEqual(Token.MAX, Token.MIN, Token.SUM, Token.AVG, Token.COUNT)) {
if (getLexer().skipIfEqual(DefaultKeyword.MAX, DefaultKeyword.MIN, DefaultKeyword.SUM, DefaultKeyword.AVG, DefaultKeyword.COUNT)) {
return new AggregationSelectItemContext(getLexer().skipParentheses(), as(), index, AggregationColumn.AggregationType.valueOf(literals.toUpperCase()));
}
StringBuilder expression = new StringBuilder();
// FIXME 无as的alias解析, 应该做成倒数第二个token不是运算符,倒数第一个token是Identifier或char,则为别名, 不过CommonSelectItemContext类型并不关注expression和alias
// FIXME 解析xxx.*
while (!getLexer().equalToken(Token.AS) && !getLexer().equalToken(Token.COMMA) && !getLexer().equalToken(Token.FROM) && !getLexer().equalToken(Token.EOF)) {
while (!getLexer().equalToken(DefaultKeyword.AS) && !getLexer().equalToken(Symbol.COMMA) && !getLexer().equalToken(DefaultKeyword.FROM) && !getLexer().equalToken(DataType.EOF)) {
String value = getLexer().getLiterals();
int position = getLexer().getCurrentPosition() - value.length();
expression.append(value);
getLexer().nextToken();
if (getLexer().equalToken(Token.DOT)) {
if (getLexer().equalToken(Symbol.DOT)) {
sqlContext.getSqlTokens().add(new TableToken(position, value, SQLUtil.getExactlyValue(value)));
}
}
......@@ -220,7 +223,7 @@ public class SQLExprParser {
}
public Optional<ConditionContext> parseWhere(final SQLContext sqlContext) {
if (lexer.skipIfEqual(Token.WHERE)) {
if (lexer.skipIfEqual(DefaultKeyword.WHERE)) {
ParseContext parseContext = getParseContext(sqlContext);
parseConditions(sqlContext, parseContext);
return Optional.of(parseContext.getCurrentConditionContext());
......@@ -240,27 +243,27 @@ public class SQLExprParser {
private void parseConditions(final SQLContext sqlContext, final ParseContext parseContext) {
do {
parseComparisonCondition(sqlContext, parseContext);
} while (lexer.skipIfEqual(Token.AND));
if (lexer.equalToken(Token.OR)) {
} while (lexer.skipIfEqual(DefaultKeyword.AND));
if (lexer.equalToken(DefaultKeyword.OR)) {
throw new ParserUnsupportedException(lexer.getToken());
}
}
// TODO 解析组合expr
public void parseComparisonCondition(final SQLContext sqlContext, final ParseContext parseContext) {
getLexer().skipIfEqual(Token.LEFT_PAREN);
getLexer().skipIfEqual(Symbol.LEFT_PAREN);
SQLExpr left = parseExpr(sqlContext);
if (lexer.equalToken(Token.EQ)) {
if (lexer.equalToken(Symbol.EQ)) {
parseEqualCondition(sqlContext, parseContext, left);
} else if (lexer.equalToken(Token.IN)) {
} else if (lexer.equalToken(DefaultKeyword.IN)) {
parseInCondition(sqlContext, parseContext, left);
} else if (lexer.equalToken(Token.BETWEEN)) {
} else if (lexer.equalToken(DefaultKeyword.BETWEEN)) {
parseBetweenCondition(sqlContext, parseContext, left);
} else if (lexer.equalToken(Token.LT) || lexer.equalToken(Token.GT)
|| lexer.equalToken(Token.LT_EQ) || lexer.equalToken(Token.GT_EQ)) {
} else if (lexer.equalToken(Symbol.LT) || lexer.equalToken(Symbol.GT)
|| lexer.equalToken(Symbol.LT_EQ) || lexer.equalToken(Symbol.GT_EQ)) {
parserOtherCondition(sqlContext);
}
getLexer().skipIfEqual(Token.LEFT_PAREN);
getLexer().skipIfEqual(Symbol.LEFT_PAREN);
}
private void parseEqualCondition(final SQLContext sqlContext, final ParseContext parseContext, final SQLExpr left) {
......@@ -274,14 +277,14 @@ public class SQLExprParser {
private void parseInCondition(final SQLContext sqlContext, final ParseContext parseContext, final SQLExpr left) {
lexer.nextToken();
lexer.accept(Token.LEFT_PAREN);
lexer.accept(Symbol.LEFT_PAREN);
List<SQLExpr> rights = new LinkedList<>();
do {
if (lexer.equalToken(Token.COMMA)) {
if (lexer.equalToken(Symbol.COMMA)) {
lexer.nextToken();
}
rights.add(parseExpr(sqlContext));
} while (!lexer.equalToken(Token.RIGHT_PAREN));
} while (!lexer.equalToken(Symbol.RIGHT_PAREN));
parseContext.addCondition(left, Condition.BinaryOperator.IN, rights, parameters);
lexer.nextToken();
}
......@@ -290,7 +293,7 @@ public class SQLExprParser {
lexer.nextToken();
List<SQLExpr> rights = new LinkedList<>();
rights.add(parseExpr(sqlContext));
lexer.accept(Token.AND);
lexer.accept(DefaultKeyword.AND);
rights.add(parseExpr(sqlContext));
parseContext.addCondition(left, Condition.BinaryOperator.BETWEEN, rights, parameters);
}
......@@ -315,24 +318,24 @@ public class SQLExprParser {
public SQLExpr parseExpr() {
String literals = lexer.getLiterals();
if (lexer.equalToken(Token.IDENTIFIER)) {
if (lexer.equalToken(DataType.IDENTIFIER)) {
SQLExpr result = getSQLExpr(SQLUtil.getExactlyValue(literals));
getLexer().nextToken();
if (lexer.skipIfEqual(Token.DOT)) {
if (lexer.skipIfEqual(Symbol.DOT)) {
String property = lexer.getLiterals();
getLexer().nextToken();
if (!lexer.equalToken(Token.PLUS, Token.MINUS, Token.STAR, Token.SLASH)) {
if (!lexer.equalToken(Symbol.PLUS, Symbol.SUB, Symbol.STAR, Symbol.SLASH)) {
return new SQLPropertyExpr(new SQLIdentifierExpr(literals), property);
}
skipRest();
return new SQLIgnoreExpr();
}
if (lexer.equalToken(Token.LEFT_PAREN)) {
if (lexer.equalToken(Symbol.LEFT_PAREN)) {
getLexer().skipParentheses();
skipRest();
return new SQLIgnoreExpr();
}
if (!lexer.equalToken(Token.PLUS, Token.MINUS, Token.STAR, Token.SLASH)) {
if (!lexer.equalToken(Symbol.PLUS, Symbol.SUB, Symbol.STAR, Symbol.SLASH)) {
return result;
}
skipRest();
......@@ -340,7 +343,7 @@ public class SQLExprParser {
}
SQLExpr result = getSQLExpr(literals);
getLexer().nextToken();
if (!lexer.equalToken(Token.PLUS, Token.MINUS, Token.STAR, Token.SLASH)) {
if (!lexer.equalToken(Symbol.PLUS, Symbol.SUB, Symbol.STAR, Symbol.SLASH)) {
return result;
}
getLexer().skipParentheses();
......@@ -349,12 +352,12 @@ public class SQLExprParser {
}
private void skipRest() {
while (lexer.skipIfEqual(Token.PLUS, Token.MINUS, Token.STAR, Token.SLASH)) {
if (getLexer().equalToken(Token.QUESTION)) {
while (lexer.skipIfEqual(Symbol.PLUS, Symbol.SUB, Symbol.STAR, Symbol.SLASH)) {
if (getLexer().equalToken(Symbol.QUESTION)) {
++parametersIndex;
}
getLexer().nextToken();
if (lexer.skipIfEqual(Token.DOT)) {
if (lexer.skipIfEqual(Symbol.DOT)) {
getLexer().nextToken();
}
getLexer().skipParentheses();
......@@ -362,29 +365,29 @@ public class SQLExprParser {
}
private SQLExpr getSQLExpr(final String literals) {
if (lexer.equalToken(Token.VARIANT) || lexer.equalToken(Token.QUESTION)) {
if (lexer.equalToken(DataType.VARIANT) || lexer.equalToken(Symbol.QUESTION)) {
SQLVariantRefExpr result = new SQLVariantRefExpr("?");
result.setIndex(++parametersIndex);
result.getAttributes().put(SQLEvalConstants.EVAL_VALUE, parameters.get(parametersIndex - 1));
result.getAttributes().put(SQLEvalConstants.EVAL_VAR_INDEX, parametersIndex - 1);
return result;
}
if (lexer.equalToken(Token.LITERAL_CHARS)) {
if (lexer.equalToken(DataType.LITERAL_CHARS)) {
return new SQLCharExpr(literals);
}
if (lexer.equalToken(Token.LITERAL_NCHARS)) {
if (lexer.equalToken(DataType.LITERAL_NCHARS)) {
return new SQLNCharExpr(literals);
}
if (lexer.equalToken(Token.LITERAL_INT)) {
if (lexer.equalToken(DataType.LITERAL_INT)) {
return new SQLIntegerExpr(Integer.parseInt(literals));
}
if (lexer.equalToken(Token.LITERAL_FLOAT)) {
if (lexer.equalToken(DataType.LITERAL_FLOAT)) {
return new SQLNumberExpr(Double.parseDouble(literals));
}
if (lexer.equalToken(Token.LITERAL_HEX)) {
if (lexer.equalToken(DataType.LITERAL_HEX)) {
return new SQLNumberExpr(Integer.parseInt(literals, 16));
}
if (lexer.equalToken(Token.IDENTIFIER)) {
if (lexer.equalToken(DataType.IDENTIFIER)) {
return new SQLIdentifierExpr(literals);
}
return new SQLIgnoreExpr();
......
......@@ -17,7 +17,9 @@
package com.alibaba.druid.sql.parser;
import com.alibaba.druid.sql.context.SQLContext;
import com.alibaba.druid.sql.lexer.Token;
import com.alibaba.druid.sql.lexer.DataType;
import com.alibaba.druid.sql.lexer.DefaultKeyword;
import com.alibaba.druid.sql.lexer.Symbol;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.constants.DatabaseType;
......@@ -51,22 +53,22 @@ public final class SQLStatementParser {
* @return SQL解析对象
*/
public SQLContext parseStatement() {
if (exprParser.getLexer().equalToken(Token.SEMI)) {
if (exprParser.getLexer().equalToken(Symbol.SEMI)) {
exprParser.getLexer().nextToken();
}
if (exprParser.getLexer().equalToken(Token.WITH)) {
if (exprParser.getLexer().equalToken(DefaultKeyword.WITH)) {
parseWith();
}
if (exprParser.getLexer().equalToken(Token.SELECT)) {
if (exprParser.getLexer().equalToken(DefaultKeyword.SELECT)) {
return SQLSelectParserFactory.newInstance(exprParser, dbType).parse();
}
if (exprParser.getLexer().equalToken(Token.INSERT)) {
if (exprParser.getLexer().equalToken(DefaultKeyword.INSERT)) {
return SQLInsertParserFactory.newInstance(shardingRule, parameters, exprParser, dbType).parse();
}
if (exprParser.getLexer().equalToken(Token.UPDATE)) {
if (exprParser.getLexer().equalToken(DefaultKeyword.UPDATE)) {
return SQLUpdateParserFactory.newInstance(exprParser, dbType).parse();
}
if (exprParser.getLexer().equalToken(Token.DELETE)) {
if (exprParser.getLexer().equalToken(DefaultKeyword.DELETE)) {
return SQLDeleteParserFactory.newInstance(exprParser, dbType).parse();
}
throw new ParserUnsupportedException(exprParser.getLexer().getToken());
......@@ -76,27 +78,27 @@ public final class SQLStatementParser {
exprParser.getLexer().nextToken();
do {
parseWithQuery();
if (exprParser.getLexer().equalToken(Token.EOF)) {
if (exprParser.getLexer().equalToken(DataType.EOF)) {
return;
}
} while (exprParser.getLexer().equalToken(Token.COMMA));
} while (exprParser.getLexer().equalToken(Symbol.COMMA));
}
private void parseWithQuery() {
while (!exprParser.getLexer().equalToken(Token.AS)) {
while (!exprParser.getLexer().equalToken(DefaultKeyword.AS)) {
exprParser.getLexer().nextToken();
if (exprParser.getLexer().equalToken(Token.EOF)) {
if (exprParser.getLexer().equalToken(DataType.EOF)) {
return;
}
}
exprParser.getLexer().accept(Token.AS);
exprParser.getLexer().accept(Token.LEFT_PAREN);
while (!exprParser.getLexer().equalToken(Token.RIGHT_PAREN)) {
exprParser.getLexer().accept(DefaultKeyword.AS);
exprParser.getLexer().accept(Symbol.LEFT_PAREN);
while (!exprParser.getLexer().equalToken(Symbol.RIGHT_PAREN)) {
exprParser.getLexer().nextToken();
if (exprParser.getLexer().equalToken(Token.EOF)) {
if (exprParser.getLexer().equalToken(DataType.EOF)) {
return;
}
}
exprParser.getLexer().accept(Token.RIGHT_PAREN);
exprParser.getLexer().accept(Symbol.RIGHT_PAREN);
}
}
package com.alibaba.druid.sql.dialect.mysql.parser;
import com.alibaba.druid.sql.dialect.mysql.lexer.MySqlLexer;
import com.alibaba.druid.sql.lexer.DataType;
import com.alibaba.druid.sql.lexer.DefaultKeyword;
import com.alibaba.druid.sql.lexer.Symbol;
import com.alibaba.druid.sql.lexer.Token;
import org.junit.Test;
......@@ -14,95 +18,95 @@ public final class MySqlLexerTest {
public void assertNextTokenForComments() {
lexer = new MySqlLexer("SELECT * FROM TABLE_XXX # xxx ");
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.SELECT));
assertThat(lexer.getToken(), is((Token) DefaultKeyword.SELECT));
assertThat(lexer.getLiterals(), is("SELECT"));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.STAR));
assertThat(lexer.getToken(), is((Token) Symbol.STAR));
assertThat(lexer.getLiterals(), is("*"));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.FROM));
assertThat(lexer.getToken(), is((Token) DefaultKeyword.FROM));
assertThat(lexer.getLiterals(), is("FROM"));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.IDENTIFIER));
assertThat(lexer.getToken(), is((Token) DataType.IDENTIFIER));
assertThat(lexer.getLiterals(), is("TABLE_XXX"));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.LINE_COMMENT));
assertThat(lexer.getToken(), is((Token) DataType.LINE_COMMENT));
assertThat(lexer.getLiterals(), is(" # xxx "));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.EOF));
assertThat(lexer.getToken(), is((Token) DataType.EOF));
}
@Test
public void assertNextTokenForCommentsAndMultplieLines() {
lexer = new MySqlLexer("SELECT * FROM TABLE_XXX # comment 1 \n #comment 2 \r\n WHERE XXX=1");
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.SELECT));
assertThat(lexer.getToken(), is((Token) DefaultKeyword.SELECT));
assertThat(lexer.getLiterals(), is("SELECT"));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.STAR));
assertThat(lexer.getToken(), is((Token) Symbol.STAR));
assertThat(lexer.getLiterals(), is("*"));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.FROM));
assertThat(lexer.getToken(), is((Token) DefaultKeyword.FROM));
assertThat(lexer.getLiterals(), is("FROM"));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.IDENTIFIER));
assertThat(lexer.getToken(), is((Token) DataType.IDENTIFIER));
assertThat(lexer.getLiterals(), is("TABLE_XXX"));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.LINE_COMMENT));
assertThat(lexer.getToken(), is((Token) DataType.LINE_COMMENT));
assertThat(lexer.getLiterals(), is(" # comment 1 "));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.LINE_COMMENT));
assertThat(lexer.getToken(), is((Token) DataType.LINE_COMMENT));
assertThat(lexer.getLiterals(), is(" #comment 2 \r"));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.WHERE));
assertThat(lexer.getToken(), is((Token) DefaultKeyword.WHERE));
assertThat(lexer.getLiterals(), is("WHERE"));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.IDENTIFIER));
assertThat(lexer.getToken(), is((Token) DataType.IDENTIFIER));
assertThat(lexer.getLiterals(), is("XXX"));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.EQ));
assertThat(lexer.getToken(), is((Token) Symbol.EQ));
assertThat(lexer.getLiterals(), is("="));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.LITERAL_INT));
assertThat(lexer.getToken(), is((Token) DataType.LITERAL_INT));
assertThat(lexer.getLiterals(), is("1"));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.EOF));
assertThat(lexer.getToken(), is((Token) DataType.EOF));
}
@Test
public void assertNextTokenForHint() {
lexer = new MySqlLexer("SELECT * FROM TABLE_XXX /*! hint 1 \n xxx */ WHERE XXX=1 /*!hint 2*/");
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.SELECT));
assertThat(lexer.getToken(), is((Token) DefaultKeyword.SELECT));
assertThat(lexer.getLiterals(), is("SELECT"));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.STAR));
assertThat(lexer.getToken(), is((Token) Symbol.STAR));
assertThat(lexer.getLiterals(), is("*"));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.FROM));
assertThat(lexer.getToken(), is((Token) DefaultKeyword.FROM));
assertThat(lexer.getLiterals(), is("FROM"));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.IDENTIFIER));
assertThat(lexer.getToken(), is((Token) DataType.IDENTIFIER));
assertThat(lexer.getLiterals(), is("TABLE_XXX"));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.HINT));
assertThat(lexer.getToken(), is((Token) DataType.HINT));
assertThat(lexer.getLiterals(), is(" hint 1 \n xxx "));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.WHERE));
assertThat(lexer.getToken(), is((Token) DefaultKeyword.WHERE));
assertThat(lexer.getLiterals(), is("WHERE"));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.IDENTIFIER));
assertThat(lexer.getToken(), is((Token) DataType.IDENTIFIER));
assertThat(lexer.getLiterals(), is("XXX"));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.EQ));
assertThat(lexer.getToken(), is((Token) Symbol.EQ));
assertThat(lexer.getLiterals(), is("="));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.LITERAL_INT));
assertThat(lexer.getToken(), is((Token) DataType.LITERAL_INT));
assertThat(lexer.getLiterals(), is("1"));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.HINT));
assertThat(lexer.getToken(), is((Token) DataType.HINT));
assertThat(lexer.getLiterals(), is("hint 2"));
lexer.nextToken();
assertThat(lexer.getToken(), is(Token.EOF));
assertThat(lexer.getToken(), is((Token) DataType.EOF));
}
}
\ No newline at end of file
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册