Search in sources :

Example 1 with SQLAllColumnExpr

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

the class SchemaStatVisitor method handleSubQueryColumn.

protected Column handleSubQueryColumn(String alias, SQLObject query) {
    if (query instanceof SQLSelect) {
        query = ((SQLSelect) query).getQuery();
    }
    SQLSelectQueryBlock queryBlock = null;
    List<SQLSelectItem> selectList = null;
    if (query instanceof SQLSelectQueryBlock) {
        queryBlock = (SQLSelectQueryBlock) query;
        if (queryBlock.getGroupBy() != null) {
            return null;
        }
        selectList = queryBlock.getSelectList();
    }
    if (selectList != null) {
        boolean allColumn = false;
        String allColumnOwner = null;
        for (SQLSelectItem item : selectList) {
            if (!item.getClass().equals(SQLSelectItem.class)) {
                continue;
            }
            String itemAlias = item.getAlias();
            SQLExpr itemExpr = item.getExpr();
            String ident = itemAlias;
            if (itemAlias == null) {
                if (itemExpr instanceof SQLIdentifierExpr) {
                    ident = itemAlias = itemExpr.toString();
                } else if (itemExpr instanceof SQLPropertyExpr) {
                    itemAlias = ((SQLPropertyExpr) itemExpr).getName();
                    ident = itemExpr.toString();
                }
            }
            if (alias.equalsIgnoreCase(itemAlias)) {
                Column column = (Column) itemExpr.getAttribute(ATTR_COLUMN);
                if (column != null) {
                    return column;
                } else {
                    SQLTableSource from = queryBlock.getFrom();
                    if (from instanceof SQLSubqueryTableSource) {
                        SQLSelect select = ((SQLSubqueryTableSource) from).getSelect();
                        Column subQueryColumn = handleSubQueryColumn(ident, select);
                        return subQueryColumn;
                    }
                }
            }
            if (itemExpr instanceof SQLAllColumnExpr) {
                allColumn = true;
            } else if (itemExpr instanceof SQLPropertyExpr) {
                SQLPropertyExpr propertyExpr = (SQLPropertyExpr) itemExpr;
                if (propertyExpr.getName().equals("*")) {
                    SQLExpr owner = propertyExpr.getOwner();
                    if (owner instanceof SQLIdentifierExpr) {
                        allColumnOwner = ((SQLIdentifierExpr) owner).getName();
                        allColumn = true;
                    }
                }
            }
        }
        if (allColumn) {
            SQLTableSource from = queryBlock.getFrom();
            String tableName = getTable(from, allColumnOwner);
            if (tableName != null) {
                return new Column(tableName, alias);
            }
        }
    }
    return null;
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) Column(com.alibaba.druid.stat.TableStat.Column)

Example 2 with SQLAllColumnExpr

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

the class WallVisitorUtils method check.

public static void check(WallVisitor visitor, SQLSelectItem x) {
    SQLExpr expr = x.getExpr();
    if (expr instanceof SQLVariantRefExpr) {
        if (!isTopSelectItem(expr) && "@".equals(((SQLVariantRefExpr) expr).getName())) {
            addViolation(visitor, ErrorCode.EVIL_NAME, "@ not allow", x);
        }
    }
    if (visitor.getConfig().isSelectAllColumnAllow()) {
        return;
    }
    if (//
    expr instanceof SQLAllColumnExpr && x.getParent() instanceof SQLSelectQueryBlock) {
        SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) x.getParent();
        SQLTableSource from = queryBlock.getFrom();
        if (from instanceof SQLExprTableSource) {
            addViolation(visitor, ErrorCode.SELECT_NOT_ALLOW, "'SELECT *' not allow", x);
        }
    }
}
Also used : SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 3 with SQLAllColumnExpr

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

the class PagerUtils method createCountItem.

private static SQLSelectItem createCountItem(String dbType) {
    SQLAggregateExpr countExpr = new SQLAggregateExpr("COUNT");
    countExpr.addArgument(new SQLAllColumnExpr());
    SQLSelectItem countItem = new SQLSelectItem(countExpr);
    return countItem;
}
Also used : SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)

Example 4 with SQLAllColumnExpr

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

the class PagerUtils method limitDB2.

private static String limitDB2(SQLSelect select, String dbType, int offset, int count) {
    SQLSelectQuery query = select.getQuery();
    SQLBinaryOpExpr gt = new //
    SQLBinaryOpExpr(//
    new SQLIdentifierExpr("ROWNUM"), //
    SQLBinaryOperator.GreaterThan, //
    new SQLNumberExpr(offset), JdbcConstants.DB2);
    SQLBinaryOpExpr lteq = new //
    SQLBinaryOpExpr(//
    new SQLIdentifierExpr("ROWNUM"), //
    SQLBinaryOperator.LessThanOrEqual, //
    new SQLNumberExpr(count + offset), JdbcConstants.DB2);
    SQLBinaryOpExpr pageCondition = new SQLBinaryOpExpr(gt, SQLBinaryOperator.BooleanAnd, lteq, JdbcConstants.DB2);
    if (query instanceof SQLSelectQueryBlock) {
        DB2SelectQueryBlock queryBlock = (DB2SelectQueryBlock) query;
        if (offset <= 0) {
            queryBlock.setFirst(new SQLNumberExpr(count));
            return SQLUtils.toSQLString(select, dbType);
        }
        SQLAggregateExpr aggregateExpr = new SQLAggregateExpr("ROW_NUMBER");
        SQLOrderBy orderBy = select.getOrderBy();
        if (orderBy == null && select.getQuery() instanceof SQLSelectQueryBlock) {
            SQLSelectQueryBlock selectQueryBlcok = (SQLSelectQueryBlock) select.getQuery();
            orderBy = selectQueryBlcok.getOrderBy();
            selectQueryBlcok.setOrderBy(null);
        } else {
            select.setOrderBy(null);
        }
        aggregateExpr.setOver(new SQLOver(orderBy));
        queryBlock.getSelectList().add(new SQLSelectItem(aggregateExpr, "ROWNUM"));
        DB2SelectQueryBlock countQueryBlock = new DB2SelectQueryBlock();
        countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
        countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
        countQueryBlock.setWhere(pageCondition);
        return SQLUtils.toSQLString(countQueryBlock, dbType);
    }
    DB2SelectQueryBlock countQueryBlock = new DB2SelectQueryBlock();
    countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLPropertyExpr(new SQLIdentifierExpr("XX"), "*")));
    SQLAggregateExpr aggregateExpr = new SQLAggregateExpr("ROW_NUMBER");
    SQLOrderBy orderBy = select.getOrderBy();
    aggregateExpr.setOver(new SQLOver(orderBy));
    select.setOrderBy(null);
    countQueryBlock.getSelectList().add(new SQLSelectItem(aggregateExpr, "ROWNUM"));
    countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
    if (offset <= 0) {
        return SQLUtils.toSQLString(countQueryBlock, dbType);
    }
    DB2SelectQueryBlock offsetQueryBlock = new DB2SelectQueryBlock();
    offsetQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
    offsetQueryBlock.setFrom(new SQLSubqueryTableSource(new SQLSelect(countQueryBlock), "XXX"));
    offsetQueryBlock.setWhere(pageCondition);
    return SQLUtils.toSQLString(offsetQueryBlock, dbType);
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) SQLOver(com.alibaba.druid.sql.ast.SQLOver) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) DB2SelectQueryBlock(com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)

Example 5 with SQLAllColumnExpr

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

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