use of com.alibaba.druid.stat.TableStat.Condition in project dble by actiontech.
the class ServerSchemaStatVisitor method visit.
@Override
public boolean visit(SQLBetweenExpr x) {
if (x.isNot()) {
return true;
}
String begin;
if (x.beginExpr instanceof SQLCharExpr) {
begin = (String) ((SQLCharExpr) x.beginExpr).getValue();
} else {
Object value = ActionSQLEvalVisitorUtils.eval(this.getDbType(), x.beginExpr, this.getParameters(), false);
begin = value.toString();
}
String end;
if (x.endExpr instanceof SQLCharExpr) {
end = (String) ((SQLCharExpr) x.endExpr).getValue();
} else {
Object value = ActionSQLEvalVisitorUtils.eval(this.getDbType(), x.endExpr, this.getParameters(), false);
end = value.toString();
}
Column column = getColumn(x);
if (column == null) {
return true;
}
Condition condition = null;
for (Condition item : this.getConditions()) {
if (item.getColumn().equals(column) && item.getOperator().equals("between")) {
condition = item;
break;
}
}
if (condition == null) {
condition = new Condition();
condition.setColumn(column);
condition.setOperator("between");
this.conditions.add(condition);
}
condition.getValues().add(begin);
condition.getValues().add(end);
return true;
}
use of com.alibaba.druid.stat.TableStat.Condition in project dble by actiontech.
the class ServerSchemaStatVisitor method handleCondition.
@Override
protected void handleCondition(SQLExpr expr, String operator, SQLExpr... valueExprs) {
if (expr instanceof SQLCastExpr) {
expr = ((SQLCastExpr) expr).getExpr();
}
Column column = this.getColumn(expr);
if (column != null) {
Condition condition = null;
Iterator var6 = this.getConditions().iterator();
while (var6.hasNext()) {
Condition item = (Condition) var6.next();
if (item.getColumn().equals(column) && item.getOperator().equals(operator)) {
condition = item;
break;
}
}
if (condition == null) {
condition = new Condition();
condition.setColumn(column);
condition.setOperator(operator);
this.conditions.add(condition);
}
SQLExpr[] var12 = valueExprs;
int var13 = valueExprs.length;
for (int var8 = 0; var8 < var13; ++var8) {
SQLExpr item = var12[var8];
Column valueColumn = this.getColumn(item);
if (valueColumn == null) {
Object value = ActionSQLEvalVisitorUtils.eval(this.getDbType(), item, this.getParameters(), false);
condition.getValues().add(value);
}
}
}
}
use of com.alibaba.druid.stat.TableStat.Condition in project dble by actiontech.
the class DQLRouteTest method visitorParse.
@SuppressWarnings("unchecked")
private List<RouteCalculateUnit> visitorParse(RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor) throws Exception {
stmt.accept(visitor);
List<List<Condition>> mergedConditionList = new ArrayList<List<Condition>>();
if (visitor.hasOrCondition()) {
// contains or
mergedConditionList = visitor.splitConditions();
} else {
mergedConditionList.add(visitor.getConditions());
}
if (visitor.getAliasMap() != null) {
for (Map.Entry<String, String> entry : visitor.getAliasMap().entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (key != null && key.indexOf("`") >= 0) {
key = key.replaceAll("`", "");
}
if (value != null && value.indexOf("`") >= 0) {
value = value.replaceAll("`", "");
}
// remove the database of table
if (key != null) {
int pos = key.indexOf(".");
if (pos > 0) {
key = key.substring(pos + 1);
}
}
if (key.equals(value)) {
ctx.addTable(key.toUpperCase());
}
// else {
// tableAliasMap.put(key, value);
// }
tableAliasMap.put(key.toUpperCase(), value);
}
visitor.getAliasMap().putAll(tableAliasMap);
ctx.setTableAliasMap(tableAliasMap);
}
Class<?> clazz = Class.forName("com.actiontech.dble.route.parser.druid.impl.DefaultDruidParser");
Method buildRouteCalculateUnits = clazz.getDeclaredMethod("buildRouteCalculateUnits", new Class[] { SchemaStatVisitor.class, List.class });
// System.out.println("buildRouteCalculateUnits:\t" + buildRouteCalculateUnits);
Object newInstance = clazz.newInstance();
buildRouteCalculateUnits.setAccessible(true);
Object returnValue = buildRouteCalculateUnits.invoke(newInstance, new Object[] { visitor, mergedConditionList });
List<RouteCalculateUnit> retList = new ArrayList<RouteCalculateUnit>();
if (returnValue instanceof ArrayList<?>) {
retList.add(((ArrayList<RouteCalculateUnit>) returnValue).get(0));
// retList = (ArrayList<RouteCalculateUnit>)returnValue;
// System.out.println(taskList.get(0).getTablesAndConditions().values());
}
return retList;
}
use of com.alibaba.druid.stat.TableStat.Condition in project dble by actiontech.
the class ServerSchemaStatVisitorTest method getConditionList.
private List<List<Condition>> getConditionList(String sql) {
SQLStatementParser parser = null;
parser = new MySqlStatementParser(sql);
ServerSchemaStatVisitor visitor = null;
SQLStatement statement = null;
// throw exception
try {
statement = parser.parseStatement();
visitor = new ServerSchemaStatVisitor();
} catch (Exception e) {
e.printStackTrace();
}
statement.accept(visitor);
List<List<Condition>> mergedConditionList = new ArrayList<List<Condition>>();
if (visitor.hasOrCondition()) {
// contains OR
mergedConditionList = visitor.splitConditions();
} else {
mergedConditionList.add(visitor.getConditions());
}
return mergedConditionList;
}
use of com.alibaba.druid.stat.TableStat.Condition in project Mycat-Server by MyCATApache.
the class MycatSchemaStatVisitor method mergeSqlConditionList.
/**
* 合并两个条件列表的元素为一个条件列表
*
* @author SvenAugustus
* @param list1 条件列表1
* @param list2 条件列表2
* @return
*/
private List<Condition> mergeSqlConditionList(List<Condition> list1, List<Condition> list2) {
if (list1 == null) {
list1 = new ArrayList();
}
if (list2 == null) {
list2 = new ArrayList();
}
List<Condition> retList = new ArrayList<Condition>();
if (!list1.isEmpty() && !(list1.get(0) instanceof Condition)) {
return retList;
}
if (!list2.isEmpty() && !(list2.get(0) instanceof Condition)) {
return retList;
}
retList.addAll(list1);
for (int j = 0; j < list2.size(); j++) {
boolean exists = false;
for (int i = 0; i < list1.size(); i++) {
if (sqlConditionEquals(list2.get(j), list1.get(i))) {
exists = true;
break;
}
}
if (!exists) {
retList.add(list2.get(j));
}
}
return retList;
}
Aggregations