Search in sources :

Example 1 with EmptySQLASTVisitor

use of com.alibaba.cobar.parser.visitor.EmptySQLASTVisitor in project cobar by alibaba.

the class MySQLDataNode method setHeartbeat.

private void setHeartbeat(String heartbeat) {
    if (heartbeat == null) {
        heartbeatAST = null;
        placeHolderToStringer = null;
        return;
    }
    try {
        final Set<PlaceHolder> plist = new HashSet<PlaceHolder>(1, 1);
        SQLStatement ast = SQLParserDelegate.parse(heartbeat);
        ast.accept(new EmptySQLASTVisitor() {

            @Override
            public void visit(PlaceHolder node) {
                plist.add(node);
            }
        });
        if (plist.isEmpty()) {
            heartbeatAST = null;
            placeHolderToStringer = null;
            return;
        }
        Map<PlaceHolder, Object> phm = new HashMap<PlaceHolder, Object>(plist.size(), 1);
        for (PlaceHolder ph : plist) {
            final String content = ph.getName();
            final int low = Integer.parseInt(content.substring(content.indexOf('(') + 1, content.indexOf(',')).trim());
            final int high = Integer.parseInt(content.substring(content.indexOf(',') + 1, content.indexOf(')')).trim());
            phm.put(ph, new Object() {

                private Random rnd = new Random();

                @Override
                public String toString() {
                    return String.valueOf(rnd.nextInt(high - low + 1) + low);
                }
            });
        }
        heartbeatAST = ast;
        placeHolderToStringer = phm;
    } catch (SQLSyntaxErrorException e) {
        throw new ConfigException("heartbeat syntax err: " + heartbeat, e);
    }
}
Also used : EmptySQLASTVisitor(com.alibaba.cobar.parser.visitor.EmptySQLASTVisitor) PlaceHolder(com.alibaba.cobar.parser.ast.expression.primary.PlaceHolder) HashMap(java.util.HashMap) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) ConfigException(com.alibaba.cobar.config.util.ConfigException) SQLStatement(com.alibaba.cobar.parser.ast.stmt.SQLStatement) Random(java.util.Random) HashSet(java.util.HashSet)

Aggregations

ConfigException (com.alibaba.cobar.config.util.ConfigException)1 PlaceHolder (com.alibaba.cobar.parser.ast.expression.primary.PlaceHolder)1 SQLStatement (com.alibaba.cobar.parser.ast.stmt.SQLStatement)1 EmptySQLASTVisitor (com.alibaba.cobar.parser.visitor.EmptySQLASTVisitor)1 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1