Search in sources :

Example 6 with SQLObject

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

the class SchemaStatVisitor method visit.

public boolean visit(SQLIdentifierExpr x) {
    String currentTable = getCurrentTable();
    if (containsSubQuery(currentTable)) {
        return false;
    }
    String ident = x.toString();
    if (variants.containsKey(ident)) {
        return false;
    }
    Column column = null;
    if (currentTable != null) {
        column = addColumn(currentTable, ident);
        if (column != null && isParentGroupBy(x)) {
            this.groupByColumns.add(column);
        }
        x.putAttribute(ATTR_COLUMN, column);
    } else {
        boolean skip = false;
        for (SQLObject parent = x.getParent(); parent != null; parent = parent.getParent()) {
            if (parent instanceof SQLSelectQueryBlock) {
                SQLTableSource from = ((SQLSelectQueryBlock) parent).getFrom();
                if (from instanceof OdpsValuesTableSource) {
                    skip = true;
                    break;
                }
            } else if (parent instanceof SQLSelectQuery) {
                break;
            }
        }
        if (!skip) {
            column = handleUnkownColumn(ident);
        }
        if (column != null) {
            x.putAttribute(ATTR_COLUMN, column);
        }
    }
    if (column != null) {
        SQLObject parent = x.getParent();
        if (parent instanceof SQLPrimaryKey) {
            column.setPrimaryKey(true);
        } else if (parent instanceof SQLUnique) {
            column.setUnique(true);
        }
        setColumn(x, column);
    }
    return false;
}
Also used : SQLObject(com.alibaba.druid.sql.ast.SQLObject) OdpsValuesTableSource(com.alibaba.druid.sql.dialect.odps.ast.OdpsValuesTableSource) Column(com.alibaba.druid.stat.TableStat.Column)

Example 7 with SQLObject

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

the class SQLEvalVisitorUtils method eval.

public static Object eval(String dbType, SQLObject sqlObject, List<Object> parameters, boolean throwError) {
    SQLEvalVisitor visitor = createEvalVisitor(dbType);
    visitor.setParameters(parameters);
    sqlObject.accept(visitor);
    Object value = getValue(sqlObject);
    if (value == null) {
        if (throwError && !sqlObject.getAttributes().containsKey(EVAL_VALUE)) {
            throw new DruidRuntimeException("eval error : " + SQLUtils.toSQLString(sqlObject, dbType));
        }
    }
    return value;
}
Also used : SQLObject(com.alibaba.druid.sql.ast.SQLObject) DruidRuntimeException(com.alibaba.druid.DruidRuntimeException)

Example 8 with SQLObject

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

the class SchemaStatVisitor method setColumn.

private void setColumn(SQLExpr x, Column column) {
    SQLObject current = x;
    for (; ; ) {
        SQLObject parent = current.getParent();
        if (parent == null) {
            break;
        }
        if (parent instanceof SQLSelectQueryBlock) {
            SQLSelectQueryBlock query = (SQLSelectQueryBlock) parent;
            if (query.getWhere() == current) {
                column.setWhere(true);
            }
            break;
        }
        if (parent instanceof SQLSelectGroupByClause) {
            SQLSelectGroupByClause groupBy = (SQLSelectGroupByClause) parent;
            if (current == groupBy.getHaving()) {
                column.setHaving(true);
            } else if (groupBy.getItems().contains(current)) {
                column.setGroupBy(true);
            }
            break;
        }
        if (isParentSelectItem(parent)) {
            column.setSelec(true);
            break;
        }
        if (parent instanceof SQLJoinTableSource) {
            SQLJoinTableSource join = (SQLJoinTableSource) parent;
            if (join.getCondition() == current) {
                column.setJoin(true);
            }
            break;
        }
        current = parent;
    }
}
Also used : SQLObject(com.alibaba.druid.sql.ast.SQLObject)

Example 9 with SQLObject

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

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

the class WallVisitorUtils method isFirst.

public static boolean isFirst(SQLObject x) {
    if (x == null) {
        return true;
    }
    for (; ; ) {
        SQLObject parent = x.getParent();
        if (!(parent instanceof SQLExpr)) {
            return true;
        }
        if (parent instanceof SQLBinaryOpExpr) {
            SQLBinaryOpExpr binaryExpr = (SQLBinaryOpExpr) parent;
            if (x == binaryExpr.getRight()) {
                return false;
            }
        }
        x = parent;
    }
}
Also used : SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLObject (com.alibaba.druid.sql.ast.SQLObject)23 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)8 Column (com.alibaba.druid.stat.TableStat.Column)4 SQLName (com.alibaba.druid.sql.ast.SQLName)3 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)3 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)3 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)2 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)2 SQLInSubQueryExpr (com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr)2 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)2 MySqlForceIndexHint (com.alibaba.druid.sql.dialect.mysql.ast.MySqlForceIndexHint)2 MySqlIgnoreIndexHint (com.alibaba.druid.sql.dialect.mysql.ast.MySqlIgnoreIndexHint)2 MySqlUseIndexHint (com.alibaba.druid.sql.dialect.mysql.ast.MySqlUseIndexHint)2 WallContext (com.alibaba.druid.wall.WallContext)2 Map (java.util.Map)2 DruidRuntimeException (com.alibaba.druid.DruidRuntimeException)1 SQLLimit (com.alibaba.druid.sql.ast.SQLLimit)1 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)1 SQLOrderingSpecification (com.alibaba.druid.sql.ast.SQLOrderingSpecification)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1