Search in sources :

Example 6 with SQLPropertyExpr

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

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

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

the class WallVisitorUtils method checkSchema.

private static boolean checkSchema(WallVisitor visitor, SQLExpr x) {
    final WallTopStatementContext topStatementContext = wallTopStatementContextLocal.get();
    if (topStatementContext != null && (topStatementContext.fromSysSchema || topStatementContext.fromSysTable)) {
        return true;
    }
    if (x instanceof SQLName) {
        String owner = ((SQLName) x).getSimpleName();
        owner = WallVisitorUtils.form(owner);
        if (isInTableSource(x) && !visitor.getProvider().checkDenySchema(owner)) {
            if (!isTopStatementWithTableSource(x) && !isFirstSelectTableSource(x) && !isFirstInSubQuery(x)) {
                SQLObject parent = x.getParent();
                while (parent != null && !(parent instanceof SQLStatement)) {
                    parent = parent.getParent();
                }
                boolean sameToTopSelectSchema = false;
                if (parent instanceof SQLSelectStatement) {
                    SQLSelectStatement selectStmt = (SQLSelectStatement) parent;
                    SQLSelectQuery query = selectStmt.getSelect().getQuery();
                    if (query instanceof SQLSelectQueryBlock) {
                        SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
                        SQLTableSource from = queryBlock.getFrom();
                        while (from instanceof SQLJoinTableSource) {
                            from = ((SQLJoinTableSource) from).getLeft();
                        }
                        if (from instanceof SQLExprTableSource) {
                            SQLExpr expr = ((SQLExprTableSource) from).getExpr();
                            if (expr instanceof SQLPropertyExpr) {
                                SQLExpr schemaExpr = ((SQLPropertyExpr) expr).getOwner();
                                if (schemaExpr instanceof SQLIdentifierExpr) {
                                    String schema = ((SQLIdentifierExpr) schemaExpr).getName();
                                    schema = form(schema);
                                    if (schema.equalsIgnoreCase(owner)) {
                                        sameToTopSelectSchema = true;
                                    }
                                }
                            }
                        }
                    }
                }
                if (!sameToTopSelectSchema) {
                    addViolation(visitor, ErrorCode.SCHEMA_DENY, "deny schema : " + owner, x);
                }
            } else {
                if (topStatementContext != null) {
                    topStatementContext.setFromSysSchema(Boolean.TRUE);
                    clearViolation(visitor);
                }
            }
            return true;
        }
        if (visitor.getConfig().isDenyObjects(owner)) {
            addViolation(visitor, ErrorCode.OBJECT_DENY, "deny object : " + owner, x);
            return true;
        }
    }
    // if (ownerExpr instanceof SQLPropertyExpr) {
    if (x instanceof SQLPropertyExpr) {
        return checkSchema(visitor, ((SQLPropertyExpr) x).getOwner());
    }
    return true;
}
Also used : SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 9 with SQLPropertyExpr

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

the class WallVisitorUtils method checkSelectForMultiTenant.

private static void checkSelectForMultiTenant(WallVisitor visitor, SQLSelectQueryBlock x) {
    TenantCallBack tenantCallBack = visitor.getConfig().getTenantCallBack();
    String tenantTablePattern = visitor.getConfig().getTenantTablePattern();
    if (tenantCallBack == null && (tenantTablePattern == null || tenantTablePattern.length() == 0)) {
        return;
    }
    if (x == null) {
        throw new IllegalStateException("x is null");
    }
    if (!isSelectStatmentForMultiTenant(x)) {
        return;
    }
    SQLTableSource tableSource = x.getFrom();
    String alias = null;
    String matchTableName = null;
    String tenantColumn = null;
    if (tableSource instanceof SQLExprTableSource) {
        SQLExpr tableExpr = ((SQLExprTableSource) tableSource).getExpr();
        if (tableExpr instanceof SQLIdentifierExpr) {
            String tableName = ((SQLIdentifierExpr) tableExpr).getName();
            if (tenantCallBack != null) {
                tenantColumn = tenantCallBack.getTenantColumn(StatementType.SELECT, tableName);
            }
            if (StringUtils.isEmpty(tenantColumn) && ServletPathMatcher.getInstance().matches(tenantTablePattern, tableName)) {
                tenantColumn = visitor.getConfig().getTenantColumn();
            }
            if (!StringUtils.isEmpty(tenantColumn)) {
                matchTableName = tableName;
                alias = tableSource.getAlias();
            }
        }
    } else if (tableSource instanceof SQLJoinTableSource) {
        SQLJoinTableSource join = (SQLJoinTableSource) tableSource;
        if (join.getLeft() instanceof SQLExprTableSource) {
            SQLExpr tableExpr = ((SQLExprTableSource) join.getLeft()).getExpr();
            if (tableExpr instanceof SQLIdentifierExpr) {
                String tableName = ((SQLIdentifierExpr) tableExpr).getName();
                if (tenantCallBack != null) {
                    tenantColumn = tenantCallBack.getTenantColumn(StatementType.SELECT, tableName);
                }
                if (StringUtils.isEmpty(tenantColumn) && ServletPathMatcher.getInstance().matches(tenantTablePattern, tableName)) {
                    tenantColumn = visitor.getConfig().getTenantColumn();
                }
                if (!StringUtils.isEmpty(tenantColumn)) {
                    matchTableName = tableName;
                    alias = join.getLeft().getAlias();
                    if (alias == null) {
                        alias = tableName;
                    }
                }
            }
            checkJoinSelectForMultiTenant(visitor, join, x);
        } else {
            checkJoinSelectForMultiTenant(visitor, join, x);
        }
    }
    if (matchTableName == null) {
        return;
    }
    SQLExpr item = null;
    if (alias != null) {
        item = new SQLPropertyExpr(new SQLIdentifierExpr(alias), tenantColumn);
    } else {
        item = new SQLIdentifierExpr(tenantColumn);
    }
    SQLSelectItem selectItem = new SQLSelectItem(item);
    x.getSelectList().add(selectItem);
    visitor.setSqlModified(true);
}
Also used : TenantCallBack(com.alibaba.druid.wall.WallConfig.TenantCallBack) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 10 with SQLPropertyExpr

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

the class WallVisitorUtils method checkInsertForMultiTenant.

private static void checkInsertForMultiTenant(WallVisitor visitor, SQLInsertInto x) {
    TenantCallBack tenantCallBack = visitor.getConfig().getTenantCallBack();
    String tenantTablePattern = visitor.getConfig().getTenantTablePattern();
    if (tenantCallBack == null && (tenantTablePattern == null || tenantTablePattern.length() == 0)) {
        return;
    }
    if (x == null) {
        throw new IllegalStateException("x is null");
    }
    SQLExprTableSource tableSource = x.getTableSource();
    String alias = null;
    String matchTableName = null;
    String tenantColumn = null;
    SQLExpr tableExpr = tableSource.getExpr();
    if (tableExpr instanceof SQLIdentifierExpr) {
        String tableName = ((SQLIdentifierExpr) tableExpr).getName();
        if (tenantCallBack != null) {
            tenantColumn = tenantCallBack.getTenantColumn(StatementType.INSERT, tableName);
        }
        if (StringUtils.isEmpty(tenantColumn) && ServletPathMatcher.getInstance().matches(tenantTablePattern, tableName)) {
            tenantColumn = visitor.getConfig().getTenantColumn();
        }
        if (!StringUtils.isEmpty(tenantColumn)) {
            matchTableName = tableName;
            alias = tableSource.getAlias();
        }
    }
    if (matchTableName == null) {
        return;
    }
    SQLExpr item = null;
    if (alias != null) {
        item = new SQLPropertyExpr(new SQLIdentifierExpr(alias), tenantColumn);
    } else {
        item = new SQLIdentifierExpr(tenantColumn);
    }
    SQLExpr value = generateTenantValue(visitor, alias, StatementType.INSERT, matchTableName);
    // add insert item and value
    x.getColumns().add(item);
    List<ValuesClause> valuesClauses = null;
    ValuesClause valuesClause = null;
    if (x instanceof MySqlInsertStatement) {
        valuesClauses = ((MySqlInsertStatement) x).getValuesList();
    } else if (x instanceof SQLServerInsertStatement) {
        valuesClauses = ((MySqlInsertStatement) x).getValuesList();
    } else {
        valuesClause = x.getValues();
    }
    if (valuesClauses != null && valuesClauses.size() > 0) {
        for (ValuesClause clause : valuesClauses) {
            clause.addValue(value);
        }
    }
    if (valuesClause != null) {
        valuesClause.addValue(value);
    }
    // insert .. select
    SQLSelect select = x.getQuery();
    if (select != null) {
        List<SQLSelectQueryBlock> queryBlocks = splitSQLSelectQuery(select.getQuery());
        for (SQLSelectQueryBlock queryBlock : queryBlocks) {
            queryBlock.getSelectList().add(new SQLSelectItem(value));
        }
    }
    visitor.setSqlModified(true);
}
Also used : TenantCallBack(com.alibaba.druid.wall.WallConfig.TenantCallBack) SQLServerInsertStatement(com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) ValuesClause(com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)

Aggregations

SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)27 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)24 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)17 SQLName (com.alibaba.druid.sql.ast.SQLName)6 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)5 SQLObject (com.alibaba.druid.sql.ast.SQLObject)4 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)4 TenantCallBack (com.alibaba.druid.wall.WallConfig.TenantCallBack)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)3 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)3 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)3 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)3 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)3 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)3 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)3 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)3 Column (com.alibaba.druid.stat.TableStat.Column)3 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)2 SQLOver (com.alibaba.druid.sql.ast.SQLOver)2