Search in sources :

Example 76 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr 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 77 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.

the class SchemaStatVisitor method getColumn.

protected Column getColumn(SQLExpr expr) {
    Map<String, String> aliasMap = getAliasMap();
    if (aliasMap == null) {
        return null;
    }
    if (expr instanceof SQLMethodInvokeExpr) {
        SQLMethodInvokeExpr methodInvokeExp = (SQLMethodInvokeExpr) expr;
        if (methodInvokeExp.getParameters().size() == 1) {
            SQLExpr firstExpr = methodInvokeExp.getParameters().get(0);
            return getColumn(firstExpr);
        }
    }
    if (expr instanceof SQLCastExpr) {
        expr = ((SQLCastExpr) expr).getExpr();
    }
    if (expr instanceof SQLPropertyExpr) {
        SQLExpr owner = ((SQLPropertyExpr) expr).getOwner();
        String column = ((SQLPropertyExpr) expr).getName();
        if (owner instanceof SQLIdentifierExpr) {
            String tableName = ((SQLIdentifierExpr) owner).getName();
            String table = tableName;
            String tableNameLower = tableName.toLowerCase();
            if (aliasMap.containsKey(tableNameLower)) {
                table = aliasMap.get(tableNameLower);
            }
            if (containsSubQuery(tableNameLower)) {
                table = null;
            }
            if (variants.containsKey(table)) {
                return null;
            }
            if (table != null) {
                return new Column(table, column);
            }
            return handleSubQueryColumn(tableName, column);
        }
        return null;
    }
    if (expr instanceof SQLIdentifierExpr) {
        Column attrColumn = (Column) expr.getAttribute(ATTR_COLUMN);
        if (attrColumn != null) {
            return attrColumn;
        }
        String column = ((SQLIdentifierExpr) expr).getName();
        String table = getCurrentTable();
        if (table != null && aliasMap.containsKey(table)) {
            table = aliasMap.get(table);
            if (table == null) {
                return null;
            }
        }
        if (table != null) {
            return new Column(table, column);
        }
        if (variants.containsKey(column)) {
            return null;
        }
        return new Column("UNKNOWN", column);
    }
    return null;
}
Also used : SQLCastExpr(com.alibaba.druid.sql.ast.expr.SQLCastExpr) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) Column(com.alibaba.druid.stat.TableStat.Column) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 78 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.

the class SchemaStatVisitor method visit.

public boolean visit(SQLJoinTableSource x) {
    x.getLeft().accept(this);
    x.getRight().accept(this);
    SQLExpr condition = x.getCondition();
    if (condition != null) {
        condition.accept(this);
    }
    return false;
}
Also used : SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 79 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.

the class SQLEvalVisitorUtils method visit.

public static boolean visit(SQLEvalVisitor visitor, SQLQueryExpr x) {
    if (WallVisitorUtils.isSimpleCountTableSource(null, ((SQLQueryExpr) x).getSubQuery())) {
        x.putAttribute(EVAL_VALUE, 1);
        return false;
    }
    if (x.getSubQuery().getQuery() instanceof SQLSelectQueryBlock) {
        SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) x.getSubQuery().getQuery();
        boolean nullFrom = false;
        if (queryBlock.getFrom() == null) {
            nullFrom = true;
        } else if (queryBlock.getFrom() instanceof SQLExprTableSource) {
            SQLExpr expr = ((SQLExprTableSource) queryBlock.getFrom()).getExpr();
            if (expr instanceof SQLIdentifierExpr) {
                if ("dual".equalsIgnoreCase(((SQLIdentifierExpr) expr).getName())) {
                    nullFrom = true;
                }
            }
        }
        if (nullFrom) {
            List<Object> row = new ArrayList<Object>(queryBlock.getSelectList().size());
            for (int i = 0; i < queryBlock.getSelectList().size(); ++i) {
                SQLSelectItem item = queryBlock.getSelectList().get(i);
                item.getExpr().accept(visitor);
                Object cell = item.getExpr().getAttribute(EVAL_VALUE);
                row.add(cell);
            }
            List<List<Object>> rows = new ArrayList<List<Object>>(1);
            rows.add(row);
            Object result = rows;
            queryBlock.putAttribute(EVAL_VALUE, result);
            x.getSubQuery().putAttribute(EVAL_VALUE, result);
            x.putAttribute(EVAL_VALUE, result);
            return false;
        }
    }
    return false;
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) ArrayList(java.util.ArrayList) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLObject(com.alibaba.druid.sql.ast.SQLObject) List(java.util.List) ArrayList(java.util.ArrayList) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 80 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.

the class SQLEvalVisitorUtils method visit.

public static boolean visit(SQLEvalVisitor visitor, SQLInListExpr x) {
    SQLExpr valueExpr = x.getExpr();
    valueExpr.accept(visitor);
    if (!valueExpr.getAttributes().containsKey(EVAL_VALUE)) {
        return false;
    }
    Object value = valueExpr.getAttribute(EVAL_VALUE);
    for (SQLExpr item : x.getTargetList()) {
        item.accept(visitor);
        if (!item.getAttributes().containsKey(EVAL_VALUE)) {
            return false;
        }
        Object itemValue = item.getAttribute(EVAL_VALUE);
        if (eq(value, itemValue)) {
            x.getAttributes().put(EVAL_VALUE, x.isNot() ? false : true);
            return false;
        }
    }
    x.getAttributes().put(EVAL_VALUE, x.isNot() ? true : false);
    return false;
}
Also used : SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)225 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)42 SQLName (com.alibaba.druid.sql.ast.SQLName)33 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)30 ParserException (com.alibaba.druid.sql.parser.ParserException)23 SQLObject (com.alibaba.druid.sql.ast.SQLObject)22 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)17 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)16 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)13 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)13 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)12 ArrayList (java.util.ArrayList)12 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)11 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)10 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)10 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)10 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)8 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)8 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)7 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)7