Search in sources :

Example 1 with ExpressionAdapter

use of com.alibaba.cobar.route.function.ExpressionAdapter 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)

Aggregations

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 MySQLFunctionManager (com.alibaba.cobar.parser.recognizer.mysql.MySQLFunctionManager)1 MySQLLexer (com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer)1 MySQLExprParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLExprParser)1 ExpressionAdapter (com.alibaba.cobar.route.function.ExpressionAdapter)1