use of com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLExprParser in project cobar by alibaba.
the class RouteRuleInitializer method initRouteRule.
public static void initRouteRule(SchemaLoader loader) throws SQLSyntaxErrorException {
Map<String, RuleAlgorithm> functions = loader.getFunctions();
MySQLFunctionManager functionManager = new MySQLFunctionManager(true);
buildFuncManager(functionManager, functions);
for (RuleConfig conf : loader.listRuleConfig()) {
String algorithmString = conf.getAlgorithm();
MySQLLexer lexer = new MySQLLexer(algorithmString);
MySQLExprParser parser = new MySQLExprParser(lexer, functionManager, false, MySQLParser.DEFAULT_CHARSET);
Expression expression = parser.expression();
if (lexer.token() != MySQLToken.EOF) {
throw new ConfigException("route algorithm not end with EOF: " + algorithmString);
}
RuleAlgorithm algorithm;
if (expression instanceof RuleAlgorithm) {
algorithm = (RuleAlgorithm) expression;
} else {
algorithm = new ExpressionAdapter(expression);
}
conf.setRuleAlgorithm(algorithm);
}
}
use of com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLExprParser in project cobar by alibaba.
the class LiteralHexadecimalTest method testUtf8.
public void testUtf8() throws Exception {
String sql = "x'E982B1E7A195275C73'";
LiteralHexadecimal hex = (LiteralHexadecimal) new MySQLExprParser(new MySQLLexer(sql), "utf-8").expression();
Assert.assertEquals("邱硕'\\s", hex.evaluation(null));
sql = "x'd0A'";
hex = (LiteralHexadecimal) new MySQLExprParser(new MySQLLexer(sql), "utf-8").expression();
Assert.assertEquals("\r\n", hex.evaluation(null));
}
use of com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLExprParser in project cobar by alibaba.
the class SQLParserDelegate method parse.
public static SQLStatement parse(String sql, MySQLLexer lexer, String charset) throws SQLSyntaxErrorException {
try {
SQLStatement stmt = null;
boolean isEOF = true;
MySQLExprParser exprParser = new MySQLExprParser(lexer, charset);
stmtSwitch: switch(lexer.token()) {
case KW_DESC:
case KW_DESCRIBE:
stmt = new MySQLDALParser(lexer, exprParser).desc();
break stmtSwitch;
case KW_SELECT:
case PUNC_LEFT_PAREN:
stmt = new MySQLDMLSelectParser(lexer, exprParser).selectUnion();
break stmtSwitch;
case KW_DELETE:
stmt = new MySQLDMLDeleteParser(lexer, exprParser).delete();
break stmtSwitch;
case KW_INSERT:
stmt = new MySQLDMLInsertParser(lexer, exprParser).insert();
break stmtSwitch;
case KW_REPLACE:
stmt = new MySQLDMLReplaceParser(lexer, exprParser).replace();
break stmtSwitch;
case KW_UPDATE:
stmt = new MySQLDMLUpdateParser(lexer, exprParser).update();
break stmtSwitch;
case KW_CALL:
stmt = new MySQLDMLCallParser(lexer, exprParser).call();
break stmtSwitch;
case KW_SET:
stmt = new MySQLDALParser(lexer, exprParser).set();
break stmtSwitch;
case KW_SHOW:
stmt = new MySQLDALParser(lexer, exprParser).show();
break stmtSwitch;
case KW_ALTER:
case KW_CREATE:
case KW_DROP:
case KW_RENAME:
stmt = new MySQLDDLParser(lexer, exprParser).ddlStmt();
isEOF = isEOFedDDL(stmt);
break stmtSwitch;
case KW_RELEASE:
stmt = new MySQLMTSParser(lexer).release();
break stmtSwitch;
case IDENTIFIER:
SpecialIdentifier si = null;
if ((si = specialIdentifiers.get(lexer.stringValueUppercase())) != null) {
switch(si) {
case TRUNCATE:
stmt = new MySQLDDLParser(lexer, exprParser).truncate();
break stmtSwitch;
case SAVEPOINT:
stmt = new MySQLMTSParser(lexer).savepoint();
break stmtSwitch;
case ROLLBACK:
stmt = new MySQLMTSParser(lexer).rollback();
break stmtSwitch;
}
}
default:
throw new SQLSyntaxErrorException("sql is not a supported statement");
}
if (isEOF) {
while (lexer.token() == MySQLToken.PUNC_SEMICOLON) {
lexer.nextToken();
}
if (lexer.token() != MySQLToken.EOF) {
throw new SQLSyntaxErrorException("SQL syntax error!");
}
}
return stmt;
} catch (Exception e) {
throw new SQLSyntaxErrorException(buildErrorMsg(e, lexer, sql), e);
}
}
Aggregations