Search in sources :

Example 51 with SQLName

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

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

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

the class WallVisitorUtils method getValue.

public static Object getValue(WallVisitor visitor, SQLExpr x) {
    if (x != null && x.getAttributes().containsKey(EVAL_VALUE)) {
        return getValueFromAttributes(visitor, x);
    }
    if (x instanceof SQLBinaryOpExpr) {
        return getValue(visitor, (SQLBinaryOpExpr) x);
    }
    if (x instanceof SQLBooleanExpr) {
        return ((SQLBooleanExpr) x).getValue();
    }
    if (x instanceof SQLNumericLiteralExpr) {
        return ((SQLNumericLiteralExpr) x).getNumber();
    }
    if (x instanceof SQLCharExpr) {
        return ((SQLCharExpr) x).getText();
    }
    if (x instanceof SQLNCharExpr) {
        return ((SQLNCharExpr) x).getText();
    }
    if (x instanceof SQLNotExpr) {
        Object result = getValue(visitor, ((SQLNotExpr) x).getExpr());
        if (result instanceof Boolean) {
            return !((Boolean) result).booleanValue();
        }
    }
    if (x instanceof SQLQueryExpr) {
        if (isSimpleCountTableSource(visitor, ((SQLQueryExpr) x).getSubQuery())) {
            return Integer.valueOf(1);
        }
        if (isSimpleCaseTableSource(visitor, ((SQLQueryExpr) x).getSubQuery())) {
            SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) ((SQLQueryExpr) x).getSubQuery().getQuery();
            SQLCaseExpr caseExpr = (SQLCaseExpr) queryBlock.getSelectList().get(0).getExpr();
            Object result = getValue(caseExpr);
            if (visitor != null && !visitor.getConfig().isCaseConditionConstAllow()) {
                boolean leftIsName = false;
                if (x.getParent() instanceof SQLBinaryOpExpr) {
                    SQLExpr left = ((SQLBinaryOpExpr) x.getParent()).getLeft();
                    if (left instanceof SQLName) {
                        leftIsName = true;
                    }
                }
                if (!leftIsName && result != null) {
                    addViolation(visitor, ErrorCode.CONST_CASE_CONDITION, "const case condition", caseExpr);
                }
            }
            return result;
        }
    }
    String dbType = null;
    if (visitor != null) {
        dbType = visitor.getDbType();
    }
    if (//
    x instanceof SQLMethodInvokeExpr || //
    x instanceof SQLBetweenExpr || //
    x instanceof SQLInListExpr || //
    x instanceof SQLUnaryExpr) {
        return eval(visitor, dbType, x, Collections.emptyList());
    }
    if (x instanceof SQLCaseExpr) {
        if (visitor != null && !visitor.getConfig().isCaseConditionConstAllow()) {
            SQLCaseExpr caseExpr = (SQLCaseExpr) x;
            boolean leftIsName = false;
            if (caseExpr.getParent() instanceof SQLBinaryOpExpr) {
                SQLExpr left = ((SQLBinaryOpExpr) caseExpr.getParent()).getLeft();
                if (left instanceof SQLName) {
                    leftIsName = true;
                }
            }
            if (!leftIsName && caseExpr.getValueExpr() == null && caseExpr.getItems().size() > 0) {
                SQLCaseExpr.Item item = caseExpr.getItems().get(0);
                Object conditionVal = getValue(visitor, item.getConditionExpr());
                Object itemVal = getValue(visitor, item.getValueExpr());
                if (conditionVal instanceof Boolean && itemVal != null) {
                    addViolation(visitor, ErrorCode.CONST_CASE_CONDITION, "const case condition", caseExpr);
                }
            }
        }
        return eval(visitor, dbType, x, Collections.emptyList());
    }
    return null;
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) Item(com.alibaba.druid.sql.ast.expr.SQLCaseExpr.Item) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLNCharExpr(com.alibaba.druid.sql.ast.expr.SQLNCharExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLBooleanExpr(com.alibaba.druid.sql.ast.expr.SQLBooleanExpr) SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLInListExpr(com.alibaba.druid.sql.ast.expr.SQLInListExpr) SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr) SQLNotExpr(com.alibaba.druid.sql.ast.expr.SQLNotExpr) SQLBetweenExpr(com.alibaba.druid.sql.ast.expr.SQLBetweenExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLUnaryExpr(com.alibaba.druid.sql.ast.expr.SQLUnaryExpr) SQLCaseExpr(com.alibaba.druid.sql.ast.expr.SQLCaseExpr)

Example 54 with SQLName

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

the class MySqlExprParser method parseForeignKey.

public MysqlForeignKey parseForeignKey() {
    accept(Token.FOREIGN);
    accept(Token.KEY);
    MysqlForeignKey fk = new MysqlForeignKey();
    if (lexer.token() != Token.LPAREN) {
        SQLName indexName = name();
        fk.setIndexName(indexName);
    }
    accept(Token.LPAREN);
    this.names(fk.getReferencingColumns());
    accept(Token.RPAREN);
    accept(Token.REFERENCES);
    fk.setReferencedTableName(this.name());
    accept(Token.LPAREN);
    this.names(fk.getReferencedColumns());
    accept(Token.RPAREN);
    if (identifierEquals("MATCH")) {
        if (identifierEquals("FULL")) {
            fk.setReferenceMatch(Match.FULL);
        } else if (identifierEquals("PARTIAL")) {
            fk.setReferenceMatch(Match.PARTIAL);
        } else if (identifierEquals("SIMPLE")) {
            fk.setReferenceMatch(Match.SIMPLE);
        }
    }
    while (lexer.token() == Token.ON) {
        lexer.nextToken();
        if (lexer.token() == Token.DELETE) {
            lexer.nextToken();
            Option option = parseReferenceOption();
            fk.setOnDelete(option);
        } else if (lexer.token() == Token.UPDATE) {
            lexer.nextToken();
            Option option = parseReferenceOption();
            fk.setOnUpdate(option);
        } else {
            throw new ParserException("syntax error, expect DELETE or UPDATE, actual " + lexer.token() + " " + lexer.stringVal());
        }
    }
    return fk;
}
Also used : MysqlForeignKey(com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLName(com.alibaba.druid.sql.ast.SQLName) Option(com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey.Option)

Example 55 with SQLName

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

the class DB2ExprParser method primaryRest.

public SQLExpr primaryRest(SQLExpr expr) {
    if (identifierEquals("VALUE")) {
        if (expr instanceof SQLIdentifierExpr) {
            SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
            String ident = identExpr.getName();
            if (ident.equalsIgnoreCase("NEXT")) {
                lexer.nextToken();
                accept(Token.FOR);
                SQLName seqName = this.name();
                SQLSequenceExpr seqExpr = new SQLSequenceExpr(seqName, SQLSequenceExpr.Function.NextVal);
                return seqExpr;
            } else if (ident.equalsIgnoreCase("PREVIOUS")) {
                lexer.nextToken();
                accept(Token.FOR);
                SQLName seqName = this.name();
                SQLSequenceExpr seqExpr = new SQLSequenceExpr(seqName, SQLSequenceExpr.Function.PrevVal);
                return seqExpr;
            }
        }
    } else if (identifierEquals("DATE")) {
        if (expr instanceof SQLIdentifierExpr) {
            SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
            String ident = identExpr.getName();
            if (ident.equalsIgnoreCase("CURRENT")) {
                lexer.nextToken();
                expr = new SQLIdentifierExpr("CURRENT DATE");
            }
        }
    } else if (identifierEquals("TIMESTAMP")) {
        if (expr instanceof SQLIdentifierExpr) {
            SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
            String ident = identExpr.getName();
            if (ident.equalsIgnoreCase("CURRENT")) {
                lexer.nextToken();
                expr = new SQLIdentifierExpr("CURRENT DATE");
            }
        }
    }
    return super.primaryRest(expr);
}
Also used : SQLName(com.alibaba.druid.sql.ast.SQLName) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLSequenceExpr(com.alibaba.druid.sql.ast.expr.SQLSequenceExpr)

Aggregations

SQLName (com.alibaba.druid.sql.ast.SQLName)102 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)33 TableStat (com.alibaba.druid.stat.TableStat)20 ParserException (com.alibaba.druid.sql.parser.ParserException)17 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)8 SQLObject (com.alibaba.druid.sql.ast.SQLObject)6 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)6 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)6 WallContext (com.alibaba.druid.wall.WallContext)6 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)5 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)5 WallSqlTableStat (com.alibaba.druid.wall.WallSqlTableStat)5 SQLPartition (com.alibaba.druid.sql.ast.SQLPartition)4 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)4 SQLQueryExpr (com.alibaba.druid.sql.ast.expr.SQLQueryExpr)4 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)4 SQLTableElement (com.alibaba.druid.sql.ast.statement.SQLTableElement)4 SQLSubPartition (com.alibaba.druid.sql.ast.SQLSubPartition)3 SQLLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLLiteralExpr)3 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)3