Search in sources :

Example 6 with Condition

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

Example 7 with Condition

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);
            }
        }
    }
}
Also used : Condition(com.alibaba.druid.stat.TableStat.Condition) Column(com.alibaba.druid.stat.TableStat.Column) Iterator(java.util.Iterator) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 8 with Condition

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;
}
Also used : Condition(com.alibaba.druid.stat.TableStat.Condition) RouteCalculateUnit(com.actiontech.dble.route.parser.druid.RouteCalculateUnit) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with Condition

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;
}
Also used : Condition(com.alibaba.druid.stat.TableStat.Condition) ServerSchemaStatVisitor(com.actiontech.dble.route.parser.druid.ServerSchemaStatVisitor) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) 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 10 with Condition

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

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