Search in sources :

Example 31 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)

Example 32 with SQLIdentifierExpr

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

the class SchemaStatVisitor method visit.

@Override
public boolean visit(SQLCreateIndexStatement x) {
    setMode(x, Mode.CreateIndex);
    SQLName name = (SQLName) ((SQLExprTableSource) x.getTable()).getExpr();
    String table = name.toString();
    setCurrentTable(table);
    TableStat stat = getTableStat(table);
    stat.incrementDropIndexCount();
    Map<String, String> aliasMap = getAliasMap();
    putAliasMap(aliasMap, table, table);
    for (SQLSelectOrderByItem item : x.getItems()) {
        SQLExpr expr = item.getExpr();
        if (expr instanceof SQLIdentifierExpr) {
            SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
            String columnName = identExpr.getName();
            addColumn(table, columnName);
        }
    }
    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) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 33 with SQLIdentifierExpr

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

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

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

the class WallVisitorUtils method checkJoinConditionForMultiTenant.

@Deprecated
public static void checkJoinConditionForMultiTenant(WallVisitor visitor, SQLJoinTableSource join, boolean checkLeft, StatementType statementType) {
    String tenantTablePattern = visitor.getConfig().getTenantTablePattern();
    if (tenantTablePattern == null || tenantTablePattern.length() == 0) {
        return;
    }
    SQLExpr condition = join.getCondition();
    SQLTableSource right = join.getRight();
    if (right instanceof SQLExprTableSource) {
        SQLExpr tableExpr = ((SQLExprTableSource) right).getExpr();
        if (tableExpr instanceof SQLIdentifierExpr) {
            String tableName = ((SQLIdentifierExpr) tableExpr).getName();
            if (ServletPathMatcher.getInstance().matches(tenantTablePattern, tableName)) {
                String alias = right.getAlias();
                if (alias == null) {
                    alias = tableName;
                }
                SQLBinaryOpExpr tenantCondition = createTenantCondition(visitor, alias, statementType, tableName);
                if (condition == null) {
                    condition = tenantCondition;
                } else {
                    condition = new SQLBinaryOpExpr(tenantCondition, SQLBinaryOperator.BooleanAnd, condition);
                }
            }
        }
    }
    if (condition != join.getCondition()) {
        join.setCondition(condition);
        visitor.setSqlModified(true);
    }
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

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