Search in sources :

Example 51 with SQLSelectQueryBlock

use of com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock in project druid by alibaba.

the class OrderByResolve method visit.

public boolean visit(SQLSelect x) {
    SQLSelectQueryBlock queryBlock = x.getQueryBlock();
    if (queryBlock == null) {
        return super.visit(x);
    }
    if (x.getOrderBy() != null && queryBlock.isForUpdate() && queryBlock.getOrderBy() == null) {
        queryBlock.setOrderBy(x.getOrderBy());
        x.setOrderBy(null);
    }
    SQLOrderBy orderBy = queryBlock.getOrderBy();
    if (orderBy == null) {
        return super.visit(x);
    }
    if (!queryBlock.selectItemHasAllColumn(false)) {
        List<SQLSelectOrderByItem> notContainsOrderBy = new ArrayList<SQLSelectOrderByItem>();
        for (SQLSelectOrderByItem orderByItem : orderBy.getItems()) {
            SQLExpr orderByExpr = orderByItem.getExpr();
            if (orderByExpr instanceof SQLName) {
                if (((SQLName) orderByExpr).hashCode64() == DBMS_RANDOM_VALUE) {
                    continue;
                }
                long hashCode64 = ((SQLName) orderByExpr).nameHashCode64();
                SQLSelectItem selectItem = queryBlock.findSelectItem(hashCode64);
                if (selectItem == null) {
                    queryBlock.addSelectItem(orderByExpr.clone());
                }
            }
        }
        if (notContainsOrderBy.size() > 0) {
            for (SQLSelectOrderByItem orderByItem : notContainsOrderBy) {
                queryBlock.addSelectItem(orderByItem.getExpr());
            }
            OracleSelectQueryBlock queryBlock1 = new OracleSelectQueryBlock();
            queryBlock1.setFrom(queryBlock, "x");
            x.setQuery(queryBlock1);
        }
    }
    return super.visit(x);
}
Also used : SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) OracleSelectQueryBlock(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock) ArrayList(java.util.ArrayList) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 52 with SQLSelectQueryBlock

use of com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock in project druid by alibaba.

the class NameResolveVisitor method visit.

@Override
public boolean visit(SQLPropertyExpr x) {
    String ownerName = x.getOwnernName();
    if (ownerName == null) {
        return super.visit(x);
    }
    for (SQLObject parent = x.getParent(); parent != null; parent = parent.getParent()) {
        if (parent instanceof SQLSelectQueryBlock) {
            SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) parent;
            SQLTableSource tableSource = queryBlock.findTableSource(ownerName);
            if (tableSource == null) {
                continue;
            }
            String alias = tableSource.computeAlias();
            if (ownerName.equalsIgnoreCase(alias) && !ownerName.equals(alias)) {
                x.setOwner(alias);
            }
            break;
        }
    }
    return super.visit(x);
}
Also used : SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource)

Example 53 with SQLSelectQueryBlock

use of com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock in project druid by alibaba.

the class ParameterizedOutputVisitorUtils method parameterizeHash.

public static long parameterizeHash(String sql, DbType dbType, SQLSelectListCache selectListCache, List<Object> outParameters, VisitorFeature... visitorFeatures) {
    final SQLParserFeature[] features = outParameters == null ? defaultFeatures2 : defaultFeatures;
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType, features);
    if (selectListCache != null) {
        parser.setSelectListCache(selectListCache);
    }
    List<SQLStatement> statementList = parser.parseStatementList();
    final int stmtSize = statementList.size();
    if (stmtSize == 0) {
        return 0L;
    }
    StringBuilder out = new StringBuilder(sql.length());
    ParameterizedVisitor visitor = createParameterizedOutputVisitor(out, dbType);
    if (outParameters != null) {
        visitor.setOutputParameters(outParameters);
    }
    configVisitorFeatures(visitor, visitorFeatures);
    if (stmtSize == 1) {
        SQLStatement stmt = statementList.get(0);
        if (stmt.getClass() == SQLSelectStatement.class) {
            SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
            if (selectListCache != null) {
                SQLSelectQueryBlock queryBlock = selectStmt.getSelect().getQueryBlock();
                if (queryBlock != null) {
                    String cachedSelectList = queryBlock.getCachedSelectList();
                    long cachedSelectListHash = queryBlock.getCachedSelectListHash();
                    if (cachedSelectList != null) {
                        visitor.config(VisitorFeature.OutputSkipSelectListCacheString, true);
                    }
                    visitor.visit(selectStmt);
                    return FnvHash.fnv1a_64_lower(cachedSelectListHash, out);
                }
            }
            visitor.visit(selectStmt);
        } else if (stmt.getClass() == MySqlInsertStatement.class) {
            MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
            String columnsString = insertStmt.getColumnsString();
            if (columnsString != null) {
                long columnsStringHash = insertStmt.getColumnsStringHash();
                visitor.config(VisitorFeature.OutputSkipInsertColumnsString, true);
                ((MySqlASTVisitor) visitor).visit(insertStmt);
                return FnvHash.fnv1a_64_lower(columnsStringHash, out);
            }
        } else {
            stmt.accept(visitor);
        }
        return FnvHash.fnv1a_64_lower(out);
    }
    for (int i = 0; i < statementList.size(); i++) {
        if (i > 0) {
            out.append(";\n");
        }
        SQLStatement stmt = statementList.get(i);
        if (stmt.hasBeforeComment()) {
            stmt.getBeforeCommentsDirect().clear();
        }
        Class<?> stmtClass = stmt.getClass();
        if (stmtClass == SQLSelectStatement.class) {
            // only for performance
            SQLSelectStatement selectStatement = (SQLSelectStatement) stmt;
            visitor.visit(selectStatement);
            visitor.postVisit(selectStatement);
        } else {
            stmt.accept(visitor);
        }
    }
    return FnvHash.fnv1a_64_lower(out);
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) OracleASTParameterizedVisitor(com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTParameterizedVisitor) MySqlParameterizedVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlParameterizedVisitor) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLParserFeature(com.alibaba.druid.sql.parser.SQLParserFeature) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)

Example 54 with SQLSelectQueryBlock

use of com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock in project druid by alibaba.

the class SQLRefactorVisitor method visit.

public boolean visit(SQLIdentifierExpr x) {
    TableMapping mapping = null;
    if (groupByLevel > 0 || havingLevel > 0) {
        SQLSelectQueryBlock queryBlock = null;
        for (SQLObject parent = x.getParent(); parent != null; parent = parent.getParent()) {
            if (parent instanceof SQLSelectQueryBlock) {
                queryBlock = (SQLSelectQueryBlock) parent;
                break;
            }
        }
        boolean matchAlias = false;
        if (queryBlock != null) {
            for (SQLSelectItem item : queryBlock.getSelectList()) {
                if (item.alias_hash() == x.hashCode64()) {
                    matchAlias = true;
                    break;
                }
            }
        }
        if (matchAlias) {
            SQLObject parent = x.getParent();
            if (parent instanceof SQLOrderBy || parent instanceof SQLSelectGroupByClause) {
                return false;
            }
            if (havingLevel > 0) {
                boolean agg = false;
                for (; parent != null; parent = parent.getParent()) {
                    if (parent instanceof SQLSelectQueryBlock) {
                        break;
                    }
                    if (parent instanceof SQLAggregateExpr) {
                        agg = true;
                        break;
                    }
                }
                if (!agg) {
                    return false;
                }
            }
        }
    }
    SQLObject ownerObject = x.getResolvedOwnerObject();
    if (ownerObject instanceof SQLExprTableSource) {
        mapping = findMapping((SQLExprTableSource) ownerObject);
    }
    if (mapping == null) {
        return false;
    }
    String srcName = x.getName();
    String mappingColumn = mapping.getMappingColumn(srcName);
    if (mappingColumn != null) {
        x.setName(quote(mappingColumn));
    }
    SQLObject parent = x.getParent();
    if (parent instanceof SQLSelectItem && ((SQLSelectItem) parent).getAlias() == null) {
        ((SQLSelectItem) parent).setAlias(srcName);
    }
    return false;
}
Also used : SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLSelectGroupByClause(com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)

Example 55 with SQLSelectQueryBlock

use of com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock in project druid by alibaba.

the class Issue1865 method test_for_select_group.

public void test_for_select_group() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "select * from t where id = 2 and name = 'wenshao'";
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType, SQLParserFeature.EnableSQLBinaryOpExprGroup);
    SQLSelectStatement stmt = (SQLSelectStatement) parser.parseStatement();
    SQLSelectQueryBlock queryBlock = stmt.getSelect().getQueryBlock();
    assertTrue(queryBlock.removeCondition("name = 'wenshao'"));
    assertEquals("SELECT *\n" + "FROM t\n" + "WHERE id = 2", stmt.toString());
    assertTrue(queryBlock.removeCondition("id = 2"));
    assertEquals("SELECT *\n" + "FROM t", stmt.toString());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) DbType(com.alibaba.druid.DbType)

Aggregations

SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)63 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)24 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)23 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)20 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)15 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)15 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)14 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)14 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)12 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)12 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)11 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)9 SQLInSubQueryExpr (com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr)7 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)7 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)7 SQLQueryExpr (com.alibaba.druid.sql.ast.expr.SQLQueryExpr)6 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)5 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)5 SQLUnionQuery (com.alibaba.druid.sql.ast.statement.SQLUnionQuery)5 SQLObject (com.alibaba.druid.sql.ast.SQLObject)4