com.alibaba.druid.stat.TableStat Java Examples

The following examples show how to use com.alibaba.druid.stat.TableStat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SQLStatementHolder.java    From DataLink with Apache License 2.0 6 votes vote down vote up
private boolean checkIfTableCreate() {
    if (shouldIgnore()) {
        return false;
    }

    boolean flag = false;
    Map<TableStat.Name, TableStat> tables = schemaStatVisitor.getTables();
    for (Map.Entry<TableStat.Name, TableStat> entry : tables.entrySet()) {
        String tableName = entry.getKey().getName();
        TableStat tStat = entry.getValue();

        if (tStat.getCreateCount() > 0) {
            buildSqlCheckItem(tableName, SqlType.CreateTable);
            flag = true;
        }
    }

    return flag;
}
 
Example #2
Source File: OrVisitorTest.java    From baymax with Apache License 2.0 6 votes vote down vote up
private List<List<TableStat.Condition>> test(String sql){
    MySqlStatementParser parser = new MySqlStatementParser(sql);

    SQLStatement statemen = parser.parseStatement();

    List<Object> parameters = new ArrayList<Object>();

    parameters.add(10);

    OrVisitor visitor = new OrVisitor();
    OrVisitor.OrEntity orEntity = new OrVisitor.OrEntity(visitor, statemen);
    List<List<TableStat.Condition>> conditions = orEntity.getOrConditions();
    System.out.println();
    System.out.println(sql);
    System.out.println(conditions);
    System.out.println();
    return conditions;
}
 
Example #3
Source File: OrVisitor.java    From baymax with Apache License 2.0 6 votes vote down vote up
public List<List<TableStat.Condition>> getOrConditions() {
    // 遍历
    orVisitor.accept(x);
    // 获取Or节点
    List<OrEntity> entitys = orVisitor.orEntity;
    // 设置条件
    setConditions(orVisitor.getConditions(), entitys);
    //
    orVisitor.reset();

    if (entitys != null && entitys.size() != 0){
        // 笛卡尔乘积
        return dikerMerge(entitys);
    }else {
        List<List<TableStat.Condition>> conditions = new ArrayList<List<TableStat.Condition>>(1);
        conditions.add(this.conditions);
        return conditions;
    }
}
 
Example #4
Source File: SqlVisitor.java    From baymax with Apache License 2.0 6 votes vote down vote up
@Override
public boolean visit(MySqlDeleteStatement x) {
    setAliasMap();

    setMode(x, Mode.Delete);

    accept(x.getFrom());
    accept(x.getUsing());
    x.getTableSource().accept(this);

    if (x.getTableSource() instanceof SQLExprTableSource) {
        SQLName tableName = (SQLName) ((SQLExprTableSource) x.getTableSource()).getExpr();
        String ident = tableName.toString();
        setCurrentTable(x, ident);
        // 和父类只有这行不同
        TableStat stat = this.getTableStat(ident,ident);
        stat.incrementDeleteCount();
    }

    accept(x.getWhere());

    accept(x.getOrderBy());
    accept(x.getLimit());

    return false;
}
 
Example #5
Source File: ConditionUtil.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
private static void conditionToRouteUnit(RouteCalculateUnit routeCalculateUnit, List<TableStat.Condition> andConditionList) {
    for (TableStat.Condition condition : andConditionList) {
        List<Object> values = condition.getValues();
        String columnName = condition.getColumn().getName();
        String tableFullName = condition.getColumn().getTable();
        String operator = condition.getOperator();
        String[] tableInfo = tableFullName.split("\\.");
        Pair<String, String> table = new Pair<>(tableInfo[0], tableInfo[1]);
        //execute only between ,in and =
        if (operator.equalsIgnoreCase("between")) {
            RangeValue rv = new RangeValue(values.get(0), values.get(1));
            routeCalculateUnit.addShardingExpr(table, columnName, rv);
        } else if (operator.equals("=")) {
            routeCalculateUnit.addShardingExpr(table, columnName, values.get(0));
        } else if (operator.equalsIgnoreCase("in")) {
            routeCalculateUnit.addShardingExpr(table, columnName, values.toArray());
        } else if (operator.equalsIgnoreCase("IS")) {
            IsValue isValue = new IsValue(values.get(0));
            routeCalculateUnit.addShardingExpr(table, columnName, isValue);
        }
    }
}
 
Example #6
Source File: ConditionUtil.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
private static List<RouteCalculateUnit> conditionsToRouteUnits(List<List<TableStat.Condition>> orConditionList) {
    List<RouteCalculateUnit> retList = new ArrayList<>();
    //find partition column in condition
    for (List<TableStat.Condition> andConditionList : orConditionList) {
        RouteCalculateUnit routeCalculateUnit = new RouteCalculateUnit();
        conditionToRouteUnit(routeCalculateUnit, andConditionList);
        retList.add(routeCalculateUnit);
    }
    if (LOGGER.isTraceEnabled()) {
        StringBuilder sb = new StringBuilder();
        int i = 0;
        for (RouteCalculateUnit routeUnit : retList) {
            i++;
            sb.append("{ RouteCalculateUnit ").append(i).append(" :");
            sb.append(routeUnit.toString());
            sb.append("}");
        }
        LOGGER.trace(sb.toString());
    }
    return retList;
}
 
Example #7
Source File: ConditionUtil.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
private static void pruningAndConditions(Map<String, String> tableAliasMap, String defaultSchema, ListIterator<TableStat.Condition> iteratorConditions) {
    while (iteratorConditions.hasNext()) {
        TableStat.Condition condition = iteratorConditions.next();
        List<Object> values = condition.getValues();
        if (values.size() == 0 || !checkConditionValues(values)) {
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("condition [" + condition + "] will be pruned for empty values");
            }
            iteratorConditions.remove(); //AND CONDITION can be pruned
        } else {
            TableStat.Condition newCondition = getUsefulCondition(condition, tableAliasMap, defaultSchema);
            if (newCondition == null) {
                iteratorConditions.remove(); //AND CONDITION can be pruned
            } else {
                iteratorConditions.set(newCondition); //replace table name and column name
            }
        }
    }
}
 
Example #8
Source File: SQLStatementHolder.java    From DataLink with Apache License 2.0 6 votes vote down vote up
private boolean checkIfTableSelect() {
    if (shouldIgnore()) {
        return false;
    }

    boolean flag = false;
    Map<TableStat.Name, TableStat> tables = schemaStatVisitor.getTables();
    for (Map.Entry<TableStat.Name, TableStat> entry : tables.entrySet()) {
        String tableName = entry.getKey().getName();
        TableStat tStat = entry.getValue();

        if (tStat.getSelectCount() > 0) {
            buildSqlCheckItem(tableName, SqlType.SelectTable);
            flag = true;
        }
    }

    return flag;
}
 
Example #9
Source File: SQLStatementHolder.java    From DataLink with Apache License 2.0 6 votes vote down vote up
private boolean checkIfTableDrop() {
    if (shouldIgnore()) {
        return false;
    }

    // drop语句支持一个sql对多个表进行操作
    boolean flag = false;
    Map<TableStat.Name, TableStat> tables = schemaStatVisitor.getTables();
    for (Map.Entry<TableStat.Name, TableStat> entry : tables.entrySet()) {
        String tableName = entry.getKey().getName();
        TableStat tStat = entry.getValue();

        if (tStat.getDropCount() > 0) {
            buildSqlCheckItem(tableName, SqlType.DropTable);
            flag = true;
        }
    }

    return flag;
}
 
Example #10
Source File: MycatSchemaStatVisitor.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
public boolean visit(MySqlDeleteStatement x) {
    setAliasMap();

    setMode(x, Mode.Delete);

    accept(x.getFrom());
    accept(x.getUsing());
    x.getTableSource().accept(this);

    if (x.getTableSource() instanceof SQLExprTableSource) {
        SQLName tableName = (SQLName) ((SQLExprTableSource) x.getTableSource()).getExpr();
        String ident = tableName.toString();
        setCurrentTable(x, ident);

        TableStat stat = this.getTableStat(ident,ident);
        stat.incrementDeleteCount();
    }

    accept(x.getWhere());

    accept(x.getOrderBy());
    accept(x.getLimit());

    return false;
}
 
Example #11
Source File: DruidShardingParseInfo.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
public void addTables(Map<Name, TableStat> map) {
	
	int dotIndex;
	for(Name _name : map.keySet()){
		
		String _tableName = _name.getName().toString().toUpperCase();
		//系统表直接跳过,路由到默认datanode
		if(RouterUtil.isSystemSchema(_tableName)){
			continue;
		}
		if((dotIndex = _tableName.indexOf('.')) != -1){
			_tableName = _tableName.substring(dotIndex + 1);
		}
		addTable(_tableName);
	}
}
 
Example #12
Source File: ServerSchemaStatVisitor.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
private static void mergeOuterRelations(WhereUnit whereUnit) {
    if (whereUnit.getSubWhereUnit().size() > 0) {
        for (WhereUnit sub : whereUnit.getSubWhereUnit()) {
            mergeOuterRelations(sub);
            if (whereUnit.getOutRelationships().size() > 0) {
                for (List<TableStat.Condition> subConditionList : sub.getOrConditionList()) {
                    ConditionUtil.extendConditionsFromRelations(subConditionList, whereUnit.getOutRelationships());
                }
            }
        }
    }
}
 
Example #13
Source File: OrVisitor.java    From baymax with Apache License 2.0 5 votes vote down vote up
/**
 * 笛卡尔乘
 * @param c1
 * @param c2
 * @return
 */
public List<List<TableStat.Condition>> dikerMerge(List<List<TableStat.Condition>> c1, List<List<TableStat.Condition>> c2){
    if (c1 == null && c2 == null){
        return null;
    }
    if (c1 == null){
        return c2;
    }
    if (c2 == null){
        return c1;
    }
    List<List<TableStat.Condition>> result = new ArrayList<List<TableStat.Condition>>();
    for (Iterator<List<TableStat.Condition>> iteratorc1 = c1.iterator(); iteratorc1.hasNext(); ) {
        List<TableStat.Condition> inc1 = iteratorc1.next();
        if (inc1 == null){
            continue;
        }
        for (int i = 0; i < c2.size(); i++) {
            List<TableStat.Condition> inc2 = c2.get(i);
            if (inc2 == null){
                continue;
            }
            List<TableStat.Condition> newList = new ArrayList<TableStat.Condition>(inc1);
            newList.addAll(inc2);
            // or true
            result.add(newList);
        }
        iteratorc1.remove();
    }
    return result;
}
 
Example #14
Source File: OrVisitor.java    From baymax with Apache License 2.0 5 votes vote down vote up
public List<List<TableStat.Condition>> dikerMerge(List<OrEntity> entities){
    List<List<TableStat.Condition>> result = entities.get(0).mergeConditions();
    if (entities.size() == 1){
        return result;
    }
    // 做笛卡尔乘
    for (int i = 1; i < entities.size(); i++) {
        List<List<TableStat.Condition>> merge =  entities.get(i).mergeConditions();
        result = dikerMerge(result, merge);
    }
    return result;
}
 
Example #15
Source File: OrVisitor.java    From baymax with Apache License 2.0 5 votes vote down vote up
public void setConditions(List<TableStat.Condition> conditions0, List<OrEntity> entitys){
    // copy
    if (conditions0 != null && conditions0.size() != 0){
        this.conditions = new ArrayList<TableStat.Condition>(conditions0.size());
        for (TableStat.Condition con : conditions0){
            this.conditions.add(con);
        }
    }
    if (entitys != null){
        for (OrEntity entity : entitys){
            entity.conditions = this.conditions;
        }
    }
}
 
Example #16
Source File: SqlVisitor.java    From baymax with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visit(SQLUpdateStatement x) {
    setAliasMap();

    setMode(x, Mode.Update);

    SQLName identName = x.getTableName();
    if (identName != null) {
        String ident = identName.toString();
        //
        String alias = x.getTableSource().getAlias();
        setCurrentTable(ident);

        TableStat stat = getTableStat(ident);
        stat.incrementUpdateCount();

        Map<String, String> aliasMap = getAliasMap();
        
        aliasMap.put(ident, ident);
        //
        if(alias != null) {
        	aliasMap.put(alias, ident);
        }
    } else {
        x.getTableSource().accept(this);
    }

    accept(x.getItems());
    accept(x.getWhere());

    return false;
}
 
Example #17
Source File: MycatSchemaStatVisitor.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean visit(SQLAlterTableStatement x) {
    String tableName = x.getName().toString();
    TableStat stat = getTableStat(tableName,tableName);
    stat.incrementAlterCount();

    setCurrentTable(x, tableName);

    for (SQLAlterTableItem item : x.getItems()) {
        item.setParent(x);
        item.accept(this);
    }

    return false;
}
 
Example #18
Source File: ConditionUtil.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
private static TableStat.Condition genNewCondition(String tableName, String columnName, String operator, List<Object> values) {
    TableStat.Column column = new TableStat.Column(tableName, columnName);
    TableStat.Condition condition = new TableStat.Condition(column, operator);
    for (Object value : values) {
        condition.addValue(value);
    }
    return condition;
}
 
Example #19
Source File: MycatSchemaStatVisitor.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
public boolean visit(SQLUpdateStatement x) {
    setAliasMap();

    setMode(x, Mode.Update);

    SQLName identName = x.getTableName();
    if (identName != null) {
        String ident = identName.toString();
        String alias = x.getTableSource().getAlias();
        setCurrentTable(ident);

        TableStat stat = getTableStat(ident);
        stat.incrementUpdateCount();

        Map<String, String> aliasMap = getAliasMap();
        
        aliasMap.put(ident, ident);
        if(alias != null) {
        	aliasMap.put(alias, ident);
        }
    } else {
        x.getTableSource().accept(this);
    }

    accept(x.getItems());
    accept(x.getWhere());

    return false;
}
 
Example #20
Source File: ConditionUtil.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
private static void pruningConditions(List<WhereUnit> whereUnits, Map<String, String> tableAliasMap, String defaultSchema) {
    Iterator<WhereUnit> whereUnitIterator = whereUnits.listIterator();
    while (whereUnitIterator.hasNext()) {
        WhereUnit whereUnit = whereUnitIterator.next();
        String whereUnitContent = "empty";
        if (LOGGER.isTraceEnabled()) {
            whereUnitContent = whereUnit.toString();
        }
        final int subWhereSize = whereUnit.getSubWhereUnit().size();
        pruningConditions(whereUnit.getSubWhereUnit(), tableAliasMap, defaultSchema);
        final int subWhereSizeAfter = whereUnit.getSubWhereUnit().size();
        boolean orContainsEmpty = false;
        final int orSize = whereUnit.getOrConditionList().size();
        for (List<TableStat.Condition> conditions : whereUnit.getOrConditionList()) {
            pruningAndConditions(tableAliasMap, defaultSchema, conditions.listIterator());
            if (conditions.size() == 0) {
                orContainsEmpty = true;
                break;
            }
        }
        if (orContainsEmpty) {
            whereUnit.getOrConditionList().clear();
        }
        final int orSizeAfter = whereUnit.getOrConditionList().size();
        List<TableStat.Condition> outConditions = whereUnit.getOutAndConditions(); //outConditions item operator with AND
        ListIterator<TableStat.Condition> iteratorOutConditions = outConditions.listIterator();
        pruningAndConditions(tableAliasMap, defaultSchema, iteratorOutConditions);
        if (outConditions.size() == 0 && (subWhereSize != 0 && subWhereSizeAfter == 0) || (orSize != 0 && orSizeAfter == 0) || (subWhereSize == 0 && orSize == 0)) {
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("whereUnit [" + whereUnitContent + "] will be pruned for contains useless or condition");
            }
            whereUnitIterator.remove();
        }
    }

}
 
Example #21
Source File: DruidUpdateParser.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 获取更新的表数
 * @author lian
 * @date 2016年11月2日
 * @return
 */
private int getUpdateTableCount(){
	Map<Name, TableStat> tableMap = this.ctx.getVisitor().getTables();
	int updateTableCount = 0;
	for(Name _name : tableMap.keySet()){
		
		TableStat ts = tableMap.get(_name);
		updateTableCount += ts.getUpdateCount();
	}
	return updateTableCount;
}
 
Example #22
Source File: CalculateUnitUtil.java    From baymax with Apache License 2.0 4 votes vote down vote up
/**
 * 前题:
 * 1. 只有一个表的Contidion
 * 2. Condition已经分割好
 * @param tableAliasMap
 * @param conditionList 这里的conditionList应该是已经用or切分好的计算单元
 * @return
 */
public static List<CalculateUnit> buildCalculateUnits(Map<String, String> tableAliasMap, List<List<TableStat.Condition>> conditionList) {
    List<CalculateUnit> retList = new ArrayList<CalculateUnit>();
    //遍历or
    for(List<TableStat.Condition> conditions : conditionList) {
        if (conditions == null){
            continue;
        }
        // 构建一个or
        CalculateUnit calculateUnit = new CalculateUnit();
        for(TableStat.Condition condition : conditions) {
            //遍历and
            List<Object> values = condition.getValues();
            if(values.size() == 0) {
                break;
            }
            if(checkConditionValues(values)) {
                String columnName = StringUtil.removeBackquote(condition.getColumn().getName());
                String tableName = StringUtil.removeBackquote(condition.getColumn().getTable());

                // 获取真实表名
                tableName = getRealTableName(tableAliasMap, tableName);

                if(tableAliasMap != null && tableAliasMap.get(tableName) == null) {
                    // 子查询的别名条件忽略掉,不参数路由计算,否则后面找不到表
                    // 虚拟表直接忽略 select x from (select xxxx) u;
                    // 忽略u
                    continue;
                }

                String operator = condition.getOperator();

                //只处理between ,in和=3中操作符
//                    if(operator.equals("between")) {
//                        RangeValue rv = new RangeValue(values.get(0), values.get(1), RangeValue.EE);
//                        CalculateUnit.addShardingExpr(tableName.toUpperCase(), columnName, rv);
//                    } else
                //|| operator.toLowerCase().equals("in")

                // between暂时不支持 需要枚举出between之间的值
                if(operator.equals("=")){
                    //只处理=号和in操作符,其他忽略
                    calculateUnit.addCondition(ConditionUnit.buildConditionUnit(tableName, columnName, values.toArray(), ConditionUnitOperator.EQUAL));
                }else if (operator.equalsIgnoreCase("in")){
                    // 对In的支持
                    calculateUnit.addCondition(ConditionUnit.buildConditionUnit(tableName, columnName, values.toArray(), ConditionUnitOperator.IN));
                }
            }
        }
        retList.add(calculateUnit);
    }
    return retList;
}
 
Example #23
Source File: OrVisitor.java    From baymax with Apache License 2.0 4 votes vote down vote up
private List<List<TableStat.Condition>> mergeConditions(){

            List<List<TableStat.Condition>> orConditions = null;

            if (x instanceof SQLBinaryOpExpr){
                SQLExpr leftExpr = ((SQLBinaryOpExpr) x).getLeft();
                SQLExpr rightExpr = ((SQLBinaryOpExpr) x).getRight();


                List<List<TableStat.Condition>> left = null;
                List<List<TableStat.Condition>> right = null;

                if (!BooleanUtil.isConditionAlwaysFalse(leftExpr)){
                    left = new OrEntity(orVisitor, leftExpr).getOrConditions();
                }

                if (!BooleanUtil.isConditionAlwaysFalse(rightExpr)){
                    right = new OrEntity(orVisitor, (rightExpr)).getOrConditions();
                }

                orConditions = new ArrayList<List<TableStat.Condition>>();
                if (conditions != null && conditions.size() > 0){
                    // 做笛卡尔乘
                    ArrayList<List<TableStat.Condition>> conditionList = new ArrayList<List<TableStat.Condition>>();
                    conditionList.add(conditions);
                    if (left != null){
                        left = dikerMerge(left, conditionList);
                    }
                    if (right != null){
                        right = dikerMerge(right, conditionList);
                    }
                }
                if (left != null){
                    orConditions.addAll(left);
                }
                if (right != null){
                    orConditions.addAll(right);
                }
            }

            return orConditions;
        }
 
Example #24
Source File: ConditionUtil.java    From dble with GNU General Public License v2.0 4 votes vote down vote up
private static TableStat.Condition getUsefulCondition(TableStat.Condition condition, Map<String, String> tableAliasMap, String defaultSchema) {
    String tableFullName = condition.getColumn().getTable();
    if (DbleServer.getInstance().getSystemVariables().isLowerCaseTableNames()) {
        tableFullName = tableFullName.toLowerCase();
    }
    if (tableAliasMap != null && tableAliasMap.get(tableFullName) == null) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("condition [" + condition + "] will be pruned for can't find table " + tableFullName);
        }
        //ignore subQuery's alias
        return null;
    }

    Pair<String, String> table = getTableInfo(tableAliasMap, tableFullName, defaultSchema);

    String schemaName = table.getKey();
    String tableName = table.getValue();
    tableFullName = schemaName + "." + tableName;
    if (SchemaUtil.MYSQL_SYS_SCHEMA.contains(schemaName.toUpperCase())) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("condition [" + condition + "] will be pruned for schema name " + schemaName.toUpperCase());
        }
        return null;
    }
    TableConfig tableConfig = DbleServer.getInstance().getConfig().getSchemas().get(schemaName).getTables().get(tableName);
    if (tableConfig == null) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("condition [" + condition + "] will be pruned for table is not config " + tableName);
        }
        return null;
    }

    String operator = condition.getOperator();
    //execute only between ,in and = is
    if (!operator.equalsIgnoreCase("between") && !operator.equals("=") && !operator.equalsIgnoreCase("in") && !operator.equalsIgnoreCase("IS")) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("condition [" + condition + "] will be pruned for operator is not [between,=,in,IS]");
        }
        return null;
    }
    String partitionCol = tableConfig.getPartitionColumn();

    String columnName = StringUtil.removeBackQuote(condition.getColumn().getName().toUpperCase());
    if (columnName.equals(partitionCol)) {
        return genNewCondition(tableFullName, columnName, operator, condition.getValues());
    }

    String joinKey = tableConfig.getJoinKey();
    if (joinKey != null && columnName.equals(joinKey)) {
        return genNewCondition(tableFullName, columnName, operator, condition.getValues());
    }
    String catchKey = tableConfig.getCacheKey();
    if (catchKey != null && columnName.equals(catchKey)) {
        return genNewCondition(tableFullName, columnName, operator, condition.getValues());
    }
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("condition [" + condition + "] will be pruned for columnName is not shardingcolumn/joinkey/cachekey");
    }
    return null;
}