Search in sources :

Example 1 with Condition

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;
}
Also used : Condition(com.alibaba.druid.stat.TableStat.Condition) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) MycatSchemaStatVisitor(io.mycat.route.parser.druid.MycatSchemaStatVisitor) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 2 with Condition

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;
}
Also used : Condition(com.alibaba.druid.stat.TableStat.Condition) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint)

Example 3 with Condition

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;
}
Also used : Condition(com.alibaba.druid.stat.TableStat.Condition) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) Column(com.alibaba.druid.stat.TableStat.Column)

Example 4 with Condition

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;
}
Also used : Condition(com.alibaba.druid.stat.TableStat.Condition) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint)

Example 5 with Condition

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;
}
Also used : RouteCalculateUnit(io.mycat.route.parser.druid.RouteCalculateUnit) Condition(com.alibaba.druid.stat.TableStat.Condition) ArrayList(java.util.ArrayList) RangeValue(io.mycat.sqlengine.mpp.RangeValue)

Aggregations

Condition (com.alibaba.druid.stat.TableStat.Condition)28 ArrayList (java.util.ArrayList)22 List (java.util.List)16 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)9 Column (com.alibaba.druid.stat.TableStat.Column)7 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)6 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 RouteCalculateUnit (io.mycat.route.parser.druid.RouteCalculateUnit)4 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)3 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)3 Method (java.lang.reflect.Method)3 RouteCalculateUnit (com.actiontech.dble.route.parser.druid.RouteCalculateUnit)2 SQLObject (com.alibaba.druid.sql.ast.SQLObject)2 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)2 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)2 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)2 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)2