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);
}
}
Aggregations