Search in sources :

Example 1 with MySQLExprParser

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);
    }
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) RuleAlgorithm(com.alibaba.cobar.config.model.rule.RuleAlgorithm) MySQLExprParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLExprParser) FunctionExpression(com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression) Expression(com.alibaba.cobar.parser.ast.expression.Expression) MySQLFunctionManager(com.alibaba.cobar.parser.recognizer.mysql.MySQLFunctionManager) ConfigException(com.alibaba.cobar.config.util.ConfigException) RuleConfig(com.alibaba.cobar.config.model.rule.RuleConfig) ExpressionAdapter(com.alibaba.cobar.route.function.ExpressionAdapter)

Example 2 with MySQLExprParser

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));
}
Also used : LiteralHexadecimal(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralHexadecimal) MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) MySQLExprParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLExprParser)

Example 3 with MySQLExprParser

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);
    }
}
Also used : MySQLDMLUpdateParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLUpdateParser) MySQLDMLSelectParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLSelectParser) MySQLDDLParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDDLParser) MySQLDALParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDALParser) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) MySQLDMLCallParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLCallParser) SQLStatement(com.alibaba.cobar.parser.ast.stmt.SQLStatement) MySQLDMLDeleteParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLDeleteParser) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) MySQLExprParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLExprParser) MySQLDMLReplaceParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLReplaceParser) MySQLMTSParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLMTSParser) MySQLDMLInsertParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLInsertParser)

Aggregations

MySQLExprParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLExprParser)3 MySQLLexer (com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer)2 RuleAlgorithm (com.alibaba.cobar.config.model.rule.RuleAlgorithm)1 RuleConfig (com.alibaba.cobar.config.model.rule.RuleConfig)1 ConfigException (com.alibaba.cobar.config.util.ConfigException)1 Expression (com.alibaba.cobar.parser.ast.expression.Expression)1 FunctionExpression (com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression)1 LiteralHexadecimal (com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralHexadecimal)1 SQLStatement (com.alibaba.cobar.parser.ast.stmt.SQLStatement)1 MySQLFunctionManager (com.alibaba.cobar.parser.recognizer.mysql.MySQLFunctionManager)1 MySQLDALParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDALParser)1 MySQLDDLParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDDLParser)1 MySQLDMLCallParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLCallParser)1 MySQLDMLDeleteParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLDeleteParser)1 MySQLDMLInsertParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLInsertParser)1 MySQLDMLReplaceParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLReplaceParser)1 MySQLDMLSelectParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLSelectParser)1 MySQLDMLUpdateParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDMLUpdateParser)1 MySQLMTSParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLMTSParser)1 ExpressionAdapter (com.alibaba.cobar.route.function.ExpressionAdapter)1