com.alibaba.druid.stat.TableStat.Column Java Examples

The following examples show how to use com.alibaba.druid.stat.TableStat.Column. 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: ServerSchemaStatVisitor.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected Column getColumn(SQLExpr expr) {
    if (aliasMap == null) {
        return null;
    }

    if (expr instanceof SQLPropertyExpr) {
        return getColumnByExpr((SQLPropertyExpr) expr);
    }

    if (expr instanceof SQLIdentifierExpr) {
        return getColumnByExpr((SQLIdentifierExpr) expr);
    }

    if (expr instanceof SQLBetweenExpr) {
        return getColumnByExpr((SQLBetweenExpr) expr);
    }
    return null;
}
 
Example #2
Source File: ServerSchemaStatVisitor.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
private Column getColumnByExpr(SQLBetweenExpr betweenExpr) {
    if (betweenExpr.getTestExpr() != null) {
        String tableName = null;
        String column = null;
        if (betweenExpr.getTestExpr() instanceof SQLPropertyExpr) { //field has alias
            tableName = ((SQLIdentifierExpr) ((SQLPropertyExpr) betweenExpr.getTestExpr()).getOwner()).getName();
            column = ((SQLPropertyExpr) betweenExpr.getTestExpr()).getName();
        } else if (betweenExpr.getTestExpr() instanceof SQLIdentifierExpr) {
            column = ((SQLIdentifierExpr) betweenExpr.getTestExpr()).getName();
            tableName = getOwnerTableName(betweenExpr, column);
        }
        if (tableName != null && !"".equals(tableName)) {
            checkAliasInColumn(tableName, false);
            return new Column(tableName, column);
        }
    }
    return null;
}
 
Example #3
Source File: ServerSchemaStatVisitor.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
private Column getColumnByExpr(SQLPropertyExpr expr) {
    SQLExpr owner = expr.getOwner();
    String column = expr.getName();
    boolean containSchema = false;
    if (owner instanceof SQLIdentifierExpr || owner instanceof SQLPropertyExpr) {
        String tableName;
        if (owner instanceof SQLPropertyExpr) {
            tableName = ((SQLPropertyExpr) owner).getName();
            if (((SQLPropertyExpr) owner).getOwner() instanceof SQLIdentifierExpr) {
                containSchema = true;
                tableName = ((SQLIdentifierExpr) ((SQLPropertyExpr) owner).getOwner()).getName() + "." + tableName;
            }
        } else {
            tableName = ((SQLIdentifierExpr) owner).getName();
        }
        checkAliasInColumn(tableName, containSchema);
        return new Column(tableName, column);
    }

    return null;
}
 
Example #4
Source File: ServerSchemaStatVisitor.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Equal or not equal to relationship transfer
 * E.g.  ntest.id = mtest.id and xtest.id = ntest.id
 * we try to add a derivative relationship "xtest.id = mtest.id"
 * so when there has limit in mtest.id the xtest.id can also get the limit
 * P.S.:Only order insensitive operator can be optimize,like = or <=>
 *
 */
private void relationMerge(Set<Relationship> relationships) {
    HashSet<Relationship> loopReSet = new HashSet<>();
    loopReSet.addAll(relationships);
    for (Relationship re : loopReSet) {
        for (Relationship inv : loopReSet) {
            if (inv.getOperator().equals(re.getOperator()) && (inv.getOperator().equals("=") || inv.getOperator().equals("<=>"))) {
                List<Column> tempSet = new ArrayList<>();
                addAndCheckDuplicate(tempSet, re.getLeft());
                addAndCheckDuplicate(tempSet, re.getRight());
                addAndCheckDuplicate(tempSet, inv.getLeft());
                addAndCheckDuplicate(tempSet, inv.getRight());
                if (tempSet.size() == 2) {
                    Relationship rs1 = new Relationship(tempSet.get(0), tempSet.get(1), inv.getOperator());
                    Relationship rs2 = new Relationship(tempSet.get(1), tempSet.get(0), inv.getOperator());
                    if (!relationships.contains(rs1) && !relationships.contains(rs2)) {
                        relationships.add(rs1);
                    }
                }
            }
        }
    }
}
 
Example #5
Source File: ServerSchemaStatVisitor.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
@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 = new Condition(column, 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) {
                if (item instanceof SQLNullExpr) {
                    condition.getValues().add(item);
                } else {
                    Object value = SQLEvalVisitorUtils.eval(this.getDbType(), item, this.getParameters(), false);
                    condition.getValues().add(value);
                }
            }
        }

    }
}
 
Example #6
Source File: ServerSchemaStatVisitor.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
private Column getColumnByExpr(SQLIdentifierExpr expr) {
    String column = expr.getName();
    String table = currentTable;
    if (table != null) {
        return new Column(table, column);
    }

    return null;
}
 
Example #7
Source File: ServerSchemaStatVisitor.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
/**
 * get table name of field in between expr
 *
 */
private String getOwnerTableName(SQLBetweenExpr betweenExpr, String column) {
    if (aliasMap.size() == 1) { //only has 1 table
        return aliasMap.keySet().iterator().next();
    } else if (aliasMap.size() == 0) { //no table
        return "";
    } else { // multi tables
        for (Column col : columns.values()) {
            if (col.getName().equals(column)) {
                return col.getTable();
            }
        }

        //parser from parent
        SQLObject parent = betweenExpr.getParent();
        if (parent instanceof SQLBinaryOpExpr) {
            parent = parent.getParent();
        }

        if (parent instanceof MySqlSelectQueryBlock) {
            MySqlSelectQueryBlock select = (MySqlSelectQueryBlock) parent;
            if (select.getFrom() instanceof SQLJoinTableSource) {
                SQLJoinTableSource joinTableSource = (SQLJoinTableSource) select.getFrom();
                //FIXME :left as driven table
                return joinTableSource.getLeft().toString();
            } else if (select.getFrom() instanceof SQLExprTableSource) {
                return select.getFrom().toString();
            }
        } else if (parent instanceof SQLUpdateStatement) {
            SQLUpdateStatement update = (SQLUpdateStatement) parent;
            return update.getTableName().getSimpleName();
        } else if (parent instanceof SQLDeleteStatement) {
            SQLDeleteStatement delete = (SQLDeleteStatement) parent;
            return delete.getTableName().getSimpleName();
        }
    }
    return "";
}
 
Example #8
Source File: MycatSchemaStatVisitor.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
/**
     * 从between语句中获取字段所属的表名。
     * 对于容易出现ambiguous的(字段不知道到底属于哪个表),实际应用中必须使用别名来避免歧义
     * @param betweenExpr
     * @param column
     * @return
     */
    private String getOwnerTableName(SQLBetweenExpr betweenExpr,String column) {
        if(tableStats.size() == 1) {//只有一个表,直接返回这一个表名
            return tableStats.keySet().iterator().next().getName();
        } else if(tableStats.size() == 0) {//一个表都没有,返回空串
            return "";
        } else {//多个表名
            for (Column col : columns.keySet())
            {
                if(col.getName().equals(column)) {
                    return col.getTable();
                }
            }
//            for(Column col : columns) {//从columns中找表名
//                if(col.getName().equals(column)) {
//                    return col.getTable();
//                }
//            }

            //前面没找到表名的,自己从parent中解析

            SQLObject parent = betweenExpr.getParent();
            if(parent instanceof SQLBinaryOpExpr)
            {
                parent=parent.getParent();
            }

            if(parent instanceof MySqlSelectQueryBlock) {
                MySqlSelectQueryBlock select = (MySqlSelectQueryBlock) parent;
                if(select.getFrom() instanceof SQLJoinTableSource) {//多表连接
                    SQLJoinTableSource joinTableSource = (SQLJoinTableSource)select.getFrom();
                    return joinTableSource.getLeft().toString();//将left作为主表,此处有不严谨处,但也是实在没有办法,如果要准确,字段前带表名或者表的别名即可
                } else if(select.getFrom() instanceof SQLExprTableSource) {//单表
                    return select.getFrom().toString();
                }
            }
            else if(parent instanceof SQLUpdateStatement) {
                SQLUpdateStatement update = (SQLUpdateStatement) parent;
                return update.getTableName().getSimpleName();
            } else if(parent instanceof SQLDeleteStatement) {
                SQLDeleteStatement delete = (SQLDeleteStatement) parent;
                return delete.getTableName().getSimpleName();
            } else {
                
            }
        }
        return "";
    }
 
Example #9
Source File: ServerSchemaStatVisitor.java    From dble with GNU General Public License v2.0 3 votes vote down vote up
/**
 * find how mach non-repeating column in 2 relationships
 * E.g. A = B and B = C the result size is 2
 * A = B and C = D the result size is 4
 * A = B and B = A the result size is 0
 * when the result size is 2,we can know that there is 3 columns in  2 relationships
 * and the derivative relationship may be needed
 *
 */
private void addAndCheckDuplicate(List<Column> tempSet, Column tmp) {
    if (tempSet.contains(tmp)) {
        tempSet.remove(tmp);
    } else {
        tempSet.add(tmp);
    }
}