Search in sources :

Example 1 with SQLExistsExpr

use of com.alibaba.druid.sql.ast.expr.SQLExistsExpr in project druid by alibaba.

the class NameResolveVisitor method visit.

@Override
public boolean visit(SQLIdentifierExpr x) {
    SQLObject parent = x.getParent();
    if (parent instanceof SQLBinaryOpExpr && x.getResolvedColumn() == null) {
        SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) parent;
        boolean isJoinCondition = binaryOpExpr.getLeft() instanceof SQLName && binaryOpExpr.getRight() instanceof SQLName;
        if (isJoinCondition) {
            return false;
        }
    }
    String name = x.getName();
    if ("ROWNUM".equalsIgnoreCase(name)) {
        return false;
    }
    long hash = x.nameHashCode64();
    SQLTableSource tableSource = null;
    if (hash == FnvHash.Constants.LEVEL || hash == FnvHash.Constants.CONNECT_BY_ISCYCLE || hash == FnvHash.Constants.SYSTIMESTAMP) {
        return false;
    }
    if (parent instanceof SQLPropertyExpr) {
        return false;
    }
    for (; parent != null; parent = parent.getParent()) {
        if (parent instanceof SQLTableSource) {
            return false;
        }
        if (parent instanceof SQLSelectQueryBlock) {
            SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) parent;
            if (queryBlock.getInto() != null) {
                return false;
            }
            if (queryBlock.getParent() instanceof SQLSelect) {
                SQLObject pp = queryBlock.getParent().getParent();
                if (pp instanceof SQLInSubQueryExpr || pp instanceof SQLExistsExpr) {
                    return false;
                }
            }
            SQLTableSource from = queryBlock.getFrom();
            if (from instanceof SQLExprTableSource || from instanceof SQLSubqueryTableSource) {
                String alias = from.getAlias();
                if (alias != null) {
                    // rownum 不加 alias
                    boolean isRowNumColumn = isRowNumColumn(x, queryBlock);
                    // 别名 不加 alias
                    boolean isAliasColumn = isAliasColumn(x, queryBlock);
                    if (!isRowNumColumn && !isAliasColumn) {
                        SQLUtils.replaceInParent(x, new SQLPropertyExpr(alias, name));
                    }
                }
            }
            return false;
        }
    }
    return true;
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLInSubQueryExpr(com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr) SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLExistsExpr(com.alibaba.druid.sql.ast.expr.SQLExistsExpr)

Example 2 with SQLExistsExpr

use of com.alibaba.druid.sql.ast.expr.SQLExistsExpr in project druid by alibaba.

the class EqualTest_exists method test_exits.

public void test_exits() throws Exception {
    String sql = "exists (select 1)";
    String sql_c = "not exists (select 1)";
    SQLExistsExpr exprA, exprB, exprC;
    {
        OracleExprParser parser = new OracleExprParser(sql);
        exprA = (SQLExistsExpr) parser.expr();
    }
    {
        OracleExprParser parser = new OracleExprParser(sql);
        exprB = (SQLExistsExpr) parser.expr();
    }
    {
        OracleExprParser parser = new OracleExprParser(sql_c);
        exprC = (SQLExistsExpr) parser.expr();
    }
    Assert.assertEquals(exprA, exprB);
    Assert.assertNotEquals(exprA, exprC);
    Assert.assertTrue(exprA.equals(exprA));
    Assert.assertFalse(exprA.equals(new Object()));
    Assert.assertEquals(exprA.hashCode(), exprB.hashCode());
    Assert.assertEquals(new SQLExistsExpr(), new SQLExistsExpr());
    Assert.assertEquals(new SQLExistsExpr().hashCode(), new SQLExistsExpr().hashCode());
}
Also used : OracleExprParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleExprParser) SQLExistsExpr(com.alibaba.druid.sql.ast.expr.SQLExistsExpr)

Example 3 with SQLExistsExpr

use of com.alibaba.druid.sql.ast.expr.SQLExistsExpr in project druid by alibaba.

the class WallVisitorUtils method isFirstSelectTableSource.

private static boolean isFirstSelectTableSource(SQLObject x) {
    for (; ; ) {
        if (x instanceof SQLExpr) {
            x = x.getParent();
        } else {
            break;
        }
    }
    if (!(x instanceof SQLExprTableSource)) {
        return false;
    }
    SQLSelectQueryBlock queryBlock = null;
    SQLObject parent = x.getParent();
    while (parent != null) {
        if (parent instanceof SQLSelectQueryBlock) {
            queryBlock = (SQLSelectQueryBlock) parent;
            break;
        }
        x = parent;
        parent = x.getParent();
    }
    if (queryBlock == null) {
        return false;
    }
    boolean isWhereQueryExpr = false;
    boolean isSelectItem = false;
    do {
        x = parent;
        parent = parent.getParent();
        if (parent instanceof SQLUnionQuery) {
            SQLUnionQuery union = (SQLUnionQuery) parent;
            if (union.getRight() == x && hasTableSource(union.getLeft())) {
                return false;
            }
        } else if (parent instanceof SQLQueryExpr || parent instanceof SQLInSubQueryExpr || parent instanceof SQLExistsExpr) {
            isWhereQueryExpr = isWhereOrHaving(parent);
        } else if (parent instanceof SQLSelectItem) {
            isSelectItem = true;
        } else if ((isWhereQueryExpr || isSelectItem) && parent instanceof SQLSelectQueryBlock) {
            if (hasTableSource((SQLSelectQueryBlock) parent)) {
                return false;
            }
        }
    } while (parent != null);
    return true;
}
Also used : SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr) SQLExistsExpr(com.alibaba.druid.sql.ast.expr.SQLExistsExpr) SQLInSubQueryExpr(com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLExistsExpr (com.alibaba.druid.sql.ast.expr.SQLExistsExpr)3 SQLObject (com.alibaba.druid.sql.ast.SQLObject)2 SQLInSubQueryExpr (com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)1 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)1 SQLQueryExpr (com.alibaba.druid.sql.ast.expr.SQLQueryExpr)1 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)1 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)1 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)1 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)1 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)1 OracleExprParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleExprParser)1