Search in sources :

Example 71 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 72 with SQLExpr

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

the class OracleSchemaStatVisitor method visit.

public boolean visit(OracleSelectTableReference x) {
    SQLExpr expr = x.getExpr();
    if (expr instanceof SQLMethodInvokeExpr) {
        SQLMethodInvokeExpr methodInvoke = (SQLMethodInvokeExpr) expr;
        if ("TABLE".equalsIgnoreCase(methodInvoke.getMethodName()) && methodInvoke.getParameters().size() == 1) {
            expr = methodInvoke.getParameters().get(0);
        }
    }
    Map<String, String> aliasMap = getAliasMap();
    if (expr instanceof SQLName) {
        String ident;
        if (expr instanceof SQLPropertyExpr) {
            String owner = ((SQLPropertyExpr) expr).getOwner().toString();
            String name = ((SQLPropertyExpr) expr).getName();
            if (aliasMap.containsKey(owner)) {
                owner = aliasMap.get(owner);
            }
            ident = owner + "." + name;
        } else {
            ident = expr.toString();
        }
        if (containsSubQuery(ident)) {
            return false;
        }
        if ("DUAL".equalsIgnoreCase(ident)) {
            return false;
        }
        x.putAttribute(ATTR_TABLE, ident);
        TableStat stat = getTableStat(ident);
        Mode mode = getMode();
        switch(mode) {
            case Delete:
                stat.incrementDeleteCount();
                break;
            case Insert:
                stat.incrementInsertCount();
                break;
            case Update:
                stat.incrementUpdateCount();
                break;
            case Select:
                stat.incrementSelectCount();
                break;
            case Merge:
                stat.incrementMergeCount();
                break;
            default:
                break;
        }
        putAliasMap(aliasMap, x.getAlias(), ident);
        putAliasMap(aliasMap, ident, ident);
        return false;
    }
    accept(x.getExpr());
    return false;
}
Also used : SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) Mode(com.alibaba.druid.stat.TableStat.Mode) SQLName(com.alibaba.druid.sql.ast.SQLName) TableStat(com.alibaba.druid.stat.TableStat) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 73 with SQLExpr

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

the class OracleUpdateParser method parseReturn.

private void parseReturn(OracleUpdateStatement update) {
    if (identifierEquals("RETURN") || lexer.token() == Token.RETURNING) {
        lexer.nextToken();
        for (; ; ) {
            SQLExpr item = this.exprParser.expr();
            update.getReturning().add(item);
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                continue;
            }
            break;
        }
        accept(Token.INTO);
        for (; ; ) {
            SQLExpr item = this.exprParser.expr();
            update.getReturningInto().add(item);
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                continue;
            }
            break;
        }
    }
}
Also used : SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 74 with SQLExpr

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

the class SQLServerStatementParser method parseExecParameter.

/**
     * SQLServer parse Parameter statement support out type
     * @author zz [455910092@qq.com]
     */
public void parseExecParameter(Collection<SQLServerParameter> exprCol, SQLObject parent) {
    if (lexer.token() == Token.RPAREN || lexer.token() == Token.RBRACKET) {
        return;
    }
    if (lexer.token() == Token.EOF) {
        return;
    }
    SQLServerParameter param = new SQLServerParameter();
    SQLExpr expr = this.exprParser.expr();
    expr.setParent(parent);
    param.setExpr(expr);
    if (lexer.token() == Token.OUT) {
        param.setType(true);
        accept(Token.OUT);
    }
    exprCol.add(param);
    while (lexer.token() == Token.COMMA) {
        lexer.nextToken();
        param = new SQLServerParameter();
        expr = this.exprParser.expr();
        expr.setParent(parent);
        param.setExpr(expr);
        if (lexer.token() == Token.OUT) {
            param.setType(true);
            accept(Token.OUT);
        }
        exprCol.add(param);
    }
}
Also used : SQLServerParameter(com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerExecStatement.SQLServerParameter) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 75 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)

Aggregations

SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)225 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)42 SQLName (com.alibaba.druid.sql.ast.SQLName)33 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)30 ParserException (com.alibaba.druid.sql.parser.ParserException)23 SQLObject (com.alibaba.druid.sql.ast.SQLObject)22 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)17 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)16 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)13 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)13 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)12 ArrayList (java.util.ArrayList)12 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)11 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)10 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)10 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)10 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)8 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)8 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)7 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)7