use of com.alibaba.druid.stat.TableStat.Condition in project Mycat_plus by coderczp.
the class MycatSchemaStatVisitorTest method getConditionList.
private List<List<Condition>> getConditionList(String sql) {
SQLStatementParser parser = null;
parser = new MySqlStatementParser(sql);
MycatSchemaStatVisitor visitor = null;
SQLStatement statement = null;
// 解析出现问题统一抛SQL语法错误
try {
statement = parser.parseStatement();
visitor = new MycatSchemaStatVisitor();
} catch (Exception e) {
e.printStackTrace();
}
statement.accept(visitor);
List<List<Condition>> mergedConditionList = new ArrayList<List<Condition>>();
if (visitor.hasOrCondition()) {
// 包含or语句
// TODO
// 根据or拆分
mergedConditionList = visitor.splitConditions();
} else {
// 不包含OR语句
mergedConditionList.add(visitor.getConditions());
}
return mergedConditionList;
}
use of com.alibaba.druid.stat.TableStat.Condition in project Mycat_plus by coderczp.
the class MycatSchemaStatVisitor method getMergedConditionList.
/**
* 条件合并:多个WhereUnit中的条件组合
* @return
*/
private List<List<Condition>> getMergedConditionList(List<WhereUnit> whereUnitList) {
List<List<Condition>> mergedConditionList = new ArrayList<List<Condition>>();
if (whereUnitList.size() == 0) {
return mergedConditionList;
}
mergedConditionList.addAll(whereUnitList.get(0).getConditionList());
for (int i = 1; i < whereUnitList.size(); i++) {
mergedConditionList = merge(mergedConditionList, whereUnitList.get(i).getConditionList());
}
return mergedConditionList;
}
use of com.alibaba.druid.stat.TableStat.Condition in project Mycat_plus by coderczp.
the class MycatSchemaStatVisitor method visit.
@Override
public boolean visit(SQLBetweenExpr x) {
String begin = null;
if (x.beginExpr instanceof SQLCharExpr) {
begin = (String) ((SQLCharExpr) x.beginExpr).getValue();
} else {
begin = x.beginExpr.toString();
}
String end = null;
if (x.endExpr instanceof SQLCharExpr) {
end = (String) ((SQLCharExpr) x.endExpr).getValue();
} else {
end = x.endExpr.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 Mycat_plus by coderczp.
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;
}
use of com.alibaba.druid.stat.TableStat.Condition in project Mycat_plus by coderczp.
the class DefaultDruidParser method buildRouteCalculateUnits.
private List<RouteCalculateUnit> buildRouteCalculateUnits(SchemaStatVisitor visitor, List<List<Condition>> conditionList) {
List<RouteCalculateUnit> retList = new ArrayList<RouteCalculateUnit>();
// 遍历condition ,找分片字段
for (int i = 0; i < conditionList.size(); i++) {
RouteCalculateUnit routeCalculateUnit = new RouteCalculateUnit();
for (Condition condition : conditionList.get(i)) {
List<Object> values = condition.getValues();
if (values.size() == 0) {
continue;
}
if (checkConditionValues(values)) {
String columnName = StringUtil.removeBackquote(condition.getColumn().getName().toUpperCase());
String tableName = StringUtil.removeBackquote(condition.getColumn().getTable().toUpperCase());
if (visitor.getAliasMap() != null && visitor.getAliasMap().get(tableName) != null && !visitor.getAliasMap().get(tableName).equals(tableName)) {
tableName = visitor.getAliasMap().get(tableName);
}
if (visitor.getAliasMap() != null && visitor.getAliasMap().get(StringUtil.removeBackquote(condition.getColumn().getTable().toUpperCase())) == null) {
// 子查询的别名条件忽略掉,不参数路由计算,否则后面找不到表
continue;
}
String operator = condition.getOperator();
// 只处理between ,in和=3中操作符
if (operator.equals("between")) {
RangeValue rv = new RangeValue(values.get(0), values.get(1), RangeValue.EE);
routeCalculateUnit.addShardingExpr(tableName.toUpperCase(), columnName, rv);
} else if (operator.equals("=") || operator.toLowerCase().equals("in")) {
// 只处理=号和in操作符,其他忽略
routeCalculateUnit.addShardingExpr(tableName.toUpperCase(), columnName, values.toArray());
}
}
}
retList.add(routeCalculateUnit);
}
return retList;
}
Aggregations