Search in sources :

Example 36 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.

the class OracleSchemaStatVisitor method visit.

@Override
public boolean visit(OracleSelectJoin x) {
    super.visit(x);
    for (SQLExpr item : x.getUsing()) {
        if (item instanceof SQLIdentifierExpr) {
            String columnName = ((SQLIdentifierExpr) item).getName();
            String leftTable = (String) x.getLeft().getAttribute(ATTR_TABLE);
            String rightTable = (String) x.getRight().getAttribute(ATTR_TABLE);
            if (leftTable != null && rightTable != null) {
                Relationship relationship = new Relationship();
                relationship.setLeft(new Column(leftTable, columnName));
                relationship.setRight(new Column(rightTable, columnName));
                relationship.setOperator("USING");
                relationships.add(relationship);
            }
            if (leftTable != null) {
                addColumn(leftTable, columnName);
            }
            if (rightTable != null) {
                addColumn(rightTable, columnName);
            }
        }
    }
    return false;
}
Also used : ModelColumn(com.alibaba.druid.sql.dialect.oracle.ast.clause.ModelClause.ModelColumn) Column(com.alibaba.druid.stat.TableStat.Column) Relationship(com.alibaba.druid.stat.TableStat.Relationship) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 37 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.

the class ParameterizedOutputVisitorUtils method merge.

public static SQLBinaryOpExpr merge(ParameterizedVisitor v, SQLBinaryOpExpr x) {
    SQLExpr left = x.getLeft();
    SQLExpr right = x.getRight();
    SQLObject parent = x.getParent();
    if (left instanceof SQLLiteralExpr && right instanceof SQLLiteralExpr) {
        if (//
        x.getOperator() == SQLBinaryOperator.Equality || x.getOperator() == SQLBinaryOperator.NotEqual) {
            if ((left instanceof SQLIntegerExpr) && (right instanceof SQLIntegerExpr)) {
                if (((SQLIntegerExpr) left).getNumber().intValue() < 100) {
                    left.putAttribute(ATTR_PARAMS_SKIP, true);
                }
                if (((SQLIntegerExpr) right).getNumber().intValue() < 100) {
                    right.putAttribute(ATTR_PARAMS_SKIP, true);
                }
            } else {
                left.putAttribute(ATTR_PARAMS_SKIP, true);
                right.putAttribute(ATTR_PARAMS_SKIP, true);
            }
        }
        return x;
    }
    for (; ; ) {
        if (x.getRight() instanceof SQLBinaryOpExpr) {
            if (x.getLeft() instanceof SQLBinaryOpExpr) {
                SQLBinaryOpExpr leftBinaryExpr = (SQLBinaryOpExpr) x.getLeft();
                if (leftBinaryExpr.getRight().equals(x.getRight())) {
                    x = leftBinaryExpr;
                    v.incrementReplaceCunt();
                    continue;
                }
            }
            SQLExpr mergedRight = merge(v, (SQLBinaryOpExpr) x.getRight());
            if (mergedRight != x.getRight()) {
                x = new SQLBinaryOpExpr(x.getLeft(), x.getOperator(), mergedRight);
                v.incrementReplaceCunt();
            }
            x.setParent(parent);
        }
        break;
    }
    if (x.getLeft() instanceof SQLBinaryOpExpr) {
        SQLExpr mergedLeft = merge(v, (SQLBinaryOpExpr) x.getLeft());
        if (mergedLeft != x.getLeft()) {
            SQLBinaryOpExpr tmp = new SQLBinaryOpExpr(mergedLeft, x.getOperator(), x.getRight());
            tmp.setParent(parent);
            x = tmp;
            v.incrementReplaceCunt();
        }
    }
    // ID = ? OR ID = ? => ID = ?
    if (x.getOperator() == SQLBinaryOperator.BooleanOr) {
        if ((x.getLeft() instanceof SQLBinaryOpExpr) && (x.getRight() instanceof SQLBinaryOpExpr)) {
            SQLBinaryOpExpr leftBinary = (SQLBinaryOpExpr) x.getLeft();
            SQLBinaryOpExpr rightBinary = (SQLBinaryOpExpr) x.getRight();
            if (mergeEqual(leftBinary, rightBinary)) {
                v.incrementReplaceCunt();
                leftBinary.setParent(x.getParent());
                putMergedArribute(leftBinary, rightBinary);
                return leftBinary;
            }
            if (//
            isLiteralExpr(leftBinary.getLeft()) && leftBinary.getOperator() == SQLBinaryOperator.BooleanOr) {
                if (mergeEqual(leftBinary.getRight(), x.getRight())) {
                    v.incrementReplaceCunt();
                    putMergedArribute(leftBinary, rightBinary);
                    return leftBinary;
                }
            }
        }
    }
    return x;
}
Also used : SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 38 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.

the class SchemaStatVisitor method handleSubQueryColumn.

protected Column handleSubQueryColumn(String alias, SQLObject query) {
    if (query instanceof SQLSelect) {
        query = ((SQLSelect) query).getQuery();
    }
    SQLSelectQueryBlock queryBlock = null;
    List<SQLSelectItem> selectList = null;
    if (query instanceof SQLSelectQueryBlock) {
        queryBlock = (SQLSelectQueryBlock) query;
        if (queryBlock.getGroupBy() != null) {
            return null;
        }
        selectList = queryBlock.getSelectList();
    }
    if (selectList != null) {
        boolean allColumn = false;
        String allColumnOwner = null;
        for (SQLSelectItem item : selectList) {
            if (!item.getClass().equals(SQLSelectItem.class)) {
                continue;
            }
            String itemAlias = item.getAlias();
            SQLExpr itemExpr = item.getExpr();
            String ident = itemAlias;
            if (itemAlias == null) {
                if (itemExpr instanceof SQLIdentifierExpr) {
                    ident = itemAlias = itemExpr.toString();
                } else if (itemExpr instanceof SQLPropertyExpr) {
                    itemAlias = ((SQLPropertyExpr) itemExpr).getName();
                    ident = itemExpr.toString();
                }
            }
            if (alias.equalsIgnoreCase(itemAlias)) {
                Column column = (Column) itemExpr.getAttribute(ATTR_COLUMN);
                if (column != null) {
                    return column;
                } else {
                    SQLTableSource from = queryBlock.getFrom();
                    if (from instanceof SQLSubqueryTableSource) {
                        SQLSelect select = ((SQLSubqueryTableSource) from).getSelect();
                        Column subQueryColumn = handleSubQueryColumn(ident, select);
                        return subQueryColumn;
                    }
                }
            }
            if (itemExpr instanceof SQLAllColumnExpr) {
                allColumn = true;
            } else if (itemExpr instanceof SQLPropertyExpr) {
                SQLPropertyExpr propertyExpr = (SQLPropertyExpr) itemExpr;
                if (propertyExpr.getName().equals("*")) {
                    SQLExpr owner = propertyExpr.getOwner();
                    if (owner instanceof SQLIdentifierExpr) {
                        allColumnOwner = ((SQLIdentifierExpr) owner).getName();
                        allColumn = true;
                    }
                }
            }
        }
        if (allColumn) {
            SQLTableSource from = queryBlock.getFrom();
            String tableName = getTable(from, allColumnOwner);
            if (tableName != null) {
                return new Column(tableName, alias);
            }
        }
    }
    return null;
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) Column(com.alibaba.druid.stat.TableStat.Column)

Example 39 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.

the class SchemaStatVisitor method visit.

public boolean visit(SQLJoinTableSource x) {
    x.getLeft().accept(this);
    x.getRight().accept(this);
    SQLExpr condition = x.getCondition();
    if (condition != null) {
        condition.accept(this);
    }
    return false;
}
Also used : SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 40 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.

the class WallVisitorUtils method checkSelelct.

public static void checkSelelct(WallVisitor visitor, SQLSelectQueryBlock x) {
    for (SQLSelectItem item : x.getSelectList()) {
        item.setParent(x);
    }
    if (x.getInto() != null) {
        checkReadOnly(visitor, x.getInto());
    }
    if (!visitor.getConfig().isSelectIntoAllow() && x.getInto() != null) {
        addViolation(visitor, ErrorCode.SELECT_INTO_NOT_ALLOW, "select into not allow", x);
        return;
    }
    if (x.getFrom() != null) {
        x.getFrom().setParent(x);
    }
    SQLExpr where = x.getWhere();
    if (where != null) {
        where.setParent(x);
        checkCondition(visitor, x.getWhere());
        Object whereValue = getConditionValue(visitor, where, visitor.getConfig().isSelectWhereAlwayTrueCheck());
        if (Boolean.TRUE == whereValue) {
            if (visitor.getConfig().isSelectWhereAlwayTrueCheck() && visitor.isSqlEndOfComment() && !isSimpleConstExpr(where)) {
                // 简单表达式
                addViolation(visitor, ErrorCode.ALWAYS_TRUE, "select alway true condition not allow", x);
            }
        }
    }
    checkSelectForMultiTenant(visitor, x);
// checkConditionForMultiTenant(visitor, x.getWhere(), x);
}
Also used : SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)422 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)71 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)68 SQLName (com.alibaba.druid.sql.ast.SQLName)47 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)33 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)32 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)30 ArrayList (java.util.ArrayList)30 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)28 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)25 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)25 ParserException (com.alibaba.druid.sql.parser.ParserException)25 SQLObject (com.alibaba.druid.sql.ast.SQLObject)24 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)23 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)20 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)17 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)16 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)15 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)15 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)15