Search in sources :

Example 6 with SQLAllColumnExpr

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

the class SQLSelect method addWhere.

public boolean addWhere(SQLExpr where) {
    if (where == null) {
        return false;
    }
    if (query instanceof SQLSelectQueryBlock) {
        ((SQLSelectQueryBlock) query).addWhere(where);
        return true;
    }
    if (query instanceof SQLUnionQuery) {
        SQLSelectQueryBlock queryBlock = new SQLSelectQueryBlock(getDbType());
        queryBlock.setFrom(new SQLSelect(query), "u");
        queryBlock.addSelectItem(new SQLAllColumnExpr());
        queryBlock.setParent(queryBlock);
        query = queryBlock;
        return true;
    }
    return false;
}
Also used : SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)

Example 7 with SQLAllColumnExpr

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

the class OracleRowNumToLimit method visit.

@Override
public boolean visit(OracleSelectQueryBlock x) {
    context = new Context(context);
    context.queryBlock = x;
    SQLExpr where = x.getWhere();
    if (where != null) {
        where.accept(this);
    }
    SQLTableSource from = x.getFrom();
    if (from != null) {
        from.accept(this);
    }
    removeSelectListRowNum(x);
    List<SQLSelectItem> selectList = x.getSelectList();
    for (SQLSelectItem selectItem : selectList) {
        selectItem.accept(this);
    }
    SQLExpr startWith = x.getStartWith();
    if (startWith != null) {
        startWith.accept(this);
    }
    boolean allColumn = false;
    if (selectList.size() == 1) {
        SQLExpr expr = selectList.get(0).getExpr();
        if (expr instanceof SQLAllColumnExpr) {
            allColumn = true;
        } else if (expr instanceof SQLPropertyExpr && ((SQLPropertyExpr) expr).getName().equals("*")) {
            allColumn = true;
        }
    }
    if ((!allColumn) && x.getFrom() instanceof SQLSubqueryTableSource && ((SQLSubqueryTableSource) x.getFrom()).getSelect().getQuery() instanceof SQLSelectQueryBlock) {
        SQLSelectQueryBlock subQuery = ((SQLSubqueryTableSource) x.getFrom()).getSelect().getQueryBlock();
        List<SQLSelectItem> subSelectList = subQuery.getSelectList();
        if (subSelectList.size() >= selectList.size()) {
            boolean match = true;
            for (int i = 0; i < selectList.size(); i++) {
                if (!selectList.get(i).equals(subSelectList.get(i))) {
                    match = false;
                    break;
                }
            }
            if (match) {
                allColumn = true;
            }
        }
    }
    if (x.getParent() instanceof SQLSelect && x.getWhere() == null && x.getOrderBy() == null && allColumn && x.getLimit() != null && x.getFrom() instanceof SQLSubqueryTableSource && ((SQLSubqueryTableSource) x.getFrom()).getSelect().getQuery() instanceof SQLSelectQueryBlock) {
        SQLSelect select = (SQLSelect) x.getParent();
        SQLSelectQueryBlock subQuery = ((SQLSubqueryTableSource) x.getFrom()).getSelect().getQueryBlock();
        subQuery.mergeLimit(x.getLimit());
        x.setLimit(null);
        select.setQuery(subQuery);
        context.queryBlock = subQuery;
        context.fixLimit();
        subQuery.accept(this);
    }
    if (x.getParent() instanceof SQLUnionQuery && x.getWhere() == null && x.getOrderBy() == null && allColumn && x.getLimit() != null && x.getFrom() instanceof SQLSubqueryTableSource && ((SQLSubqueryTableSource) x.getFrom()).getSelect().getQuery() instanceof SQLSelectQueryBlock) {
        SQLUnionQuery union = (SQLUnionQuery) x.getParent();
        SQLSelectQueryBlock subQuery = ((SQLSubqueryTableSource) x.getFrom()).getSelect().getQueryBlock();
        subQuery.mergeLimit(x.getLimit());
        x.setLimit(null);
        if (union.getLeft() == x) {
            union.setLeft(subQuery);
        } else {
            union.setRight(subQuery);
        }
        context.queryBlock = subQuery;
        context.fixLimit();
        subQuery.accept(this);
    }
    context = context.parent;
    return false;
}
Also used : SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)

Example 8 with SQLAllColumnExpr

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

the class Schema method findTable.

public SchemaObject findTable(SQLTableSource tableSource, SQLExpr expr) {
    if (expr instanceof SQLAggregateExpr) {
        SQLAggregateExpr aggregateExpr = (SQLAggregateExpr) expr;
        String function = aggregateExpr.getMethodName();
        if ("min".equalsIgnoreCase(function) || "max".equalsIgnoreCase(function)) {
            SQLExpr arg = aggregateExpr.getArguments().get(0);
            return findTable(tableSource, arg);
        }
    }
    if (expr instanceof SQLPropertyExpr) {
        String ownerName = ((SQLPropertyExpr) expr).getOwnernName();
        return findTable(tableSource, ownerName);
    }
    if (expr instanceof SQLAllColumnExpr || expr instanceof SQLIdentifierExpr) {
        if (tableSource instanceof SQLExprTableSource) {
            return findTable(tableSource, tableSource.computeAlias());
        }
        if (tableSource instanceof SQLJoinTableSource) {
            SQLJoinTableSource join = (SQLJoinTableSource) tableSource;
            SchemaObject table = findTable(join.getLeft(), expr);
            if (table == null) {
                table = findTable(join.getRight(), expr);
            }
            return table;
        }
        return null;
    }
    return null;
}
Also used : SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 9 with SQLAllColumnExpr

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

the class TPCDS_ALL_Resolve method test_q01.

public void test_q01() throws Exception {
    for (int q = 1; q <= 99; ++q) {
        System.out.println("tpcds query-" + q);
        System.out.println("-----------------------------------------------------");
        String sql = TPCDS.getQuery(q);
        final List<SQLStatement> statements = SQLUtils.parseStatements(sql, DbType.mysql);
        for (SQLStatement stmt : statements) {
            repository.resolve(stmt);
            final SQLSelect select = ((SQLSelectStatement) stmt).getSelect();
            final SQLSelectQueryBlock firstQueryBlock = select.getFirstQueryBlock();
            if (firstQueryBlock == null) {
                continue;
            }
            final List<SQLSelectItem> selectList = firstQueryBlock.getSelectList();
            for (int i = 0; i < selectList.size(); i++) {
                SQLSelectItem selectItem = selectList.get(i);
                if (selectItem.getExpr() instanceof SQLAllColumnExpr) {
                    continue;
                }
                final SQLDataType dataType = selectItem.computeDataType();
                if (dataType == null) {
                // fail("dataType is null : " + selectItem);
                }
            }
        }
    }
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) SQLDataType(com.alibaba.druid.sql.ast.SQLDataType) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 10 with SQLAllColumnExpr

use of com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr in project Mycat-Server by MyCATApache.

the class JoinParser method parserFields.

private void parserFields(List<SQLSelectItem> mysqlSelectList) {
    // 显示的字段
    String key = "";
    String value = "";
    String exprfield = "";
    for (SQLSelectItem item : mysqlSelectList) {
        if (item.getExpr() instanceof SQLAllColumnExpr) {
            // *解析
            setField(item.toString(), item.toString());
        } else {
            if (item.getExpr() instanceof SQLAggregateExpr) {
                SQLAggregateExpr expr = (SQLAggregateExpr) item.getExpr();
                key = getExprFieldName(expr);
                setField(key, value);
            } else if (item.getExpr() instanceof SQLMethodInvokeExpr) {
                key = getMethodInvokeFieldName(item);
                exprfield = getFieldName(item);
                // value=item.getAlias();
                setField(key, value, exprfield);
            } else {
                key = getFieldName(item);
                value = item.getAlias();
                setField(key, value);
            }
        }
    }
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)

Aggregations

SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)21 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)13 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)10 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)9 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)9 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)8 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)5 SQLDataType (com.alibaba.druid.sql.ast.SQLDataType)4 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)4 SQLDataTypeImpl (com.alibaba.druid.sql.ast.SQLDataTypeImpl)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)3 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)3 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)3 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)3 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)3 SQLCharacterDataType (com.alibaba.druid.sql.ast.statement.SQLCharacterDataType)3 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)3 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)3 DB2SelectQueryBlock (com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock)3