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;
}
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());
}
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;
}
Aggregations