Search in sources :

Example 26 with SQLIdentifierExpr

use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr 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 27 with SQLIdentifierExpr

use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr 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 28 with SQLIdentifierExpr

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

the class SchemaStatVisitor method visit.

public boolean visit(SQLPropertyExpr x) {
    if (x.getOwner() instanceof SQLIdentifierExpr) {
        String owner = ((SQLIdentifierExpr) x.getOwner()).getName();
        if (containsSubQuery(owner)) {
            return false;
        }
        owner = aliasWrap(owner);
        if (owner != null) {
            Column column = addColumn(owner, x.getName());
            x.putAttribute(ATTR_COLUMN, column);
            if (column != null) {
                if (isParentGroupBy(x)) {
                    this.groupByColumns.add(column);
                }
                if (column != null) {
                    setColumn(x, column);
                }
            }
        }
    }
    return false;
}
Also used : Column(com.alibaba.druid.stat.TableStat.Column) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)

Example 29 with SQLIdentifierExpr

use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr 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 30 with SQLIdentifierExpr

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

the class PGSchemaStatVisitor method visit.

@Override
public boolean visit(PGDeleteStatement x) {
    if (x.getWith() != null) {
        x.getWith().accept(this);
    }
    setAliasMap();
    for (SQLName name : x.getUsing()) {
        String ident = name.toString();
        TableStat stat = getTableStat(ident);
        stat.incrementSelectCount();
        Map<String, String> aliasMap = getAliasMap();
        if (aliasMap != null) {
            aliasMap.put(ident, ident);
        }
    }
    x.putAttribute("_original_use_mode", getMode());
    setMode(x, Mode.Delete);
    String ident = ((SQLIdentifierExpr) x.getTableName()).getName();
    setCurrentTable(ident);
    TableStat stat = getTableStat(ident, x.getAlias());
    stat.incrementDeleteCount();
    accept(x.getWhere());
    return false;
}
Also used : SQLName(com.alibaba.druid.sql.ast.SQLName) TableStat(com.alibaba.druid.stat.TableStat) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)

Aggregations

SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)73 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)42 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)24 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)10 ParserException (com.alibaba.druid.sql.parser.ParserException)10 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)9 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)9 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)9 SQLName (com.alibaba.druid.sql.ast.SQLName)8 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)8 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)7 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)7 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)6 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)6 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)5 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)5 SQLObject (com.alibaba.druid.sql.ast.SQLObject)4 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)4 MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)4 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)4