Search in sources :

Example 1 with SQLAssignItem

use of com.alibaba.druid.sql.ast.statement.SQLAssignItem in project druid by alibaba.

the class MySqlCreateTableParser method partitionClauseRest.

protected void partitionClauseRest(SQLPartitionBy clause) {
    if (identifierEquals("PARTITIONS")) {
        lexer.nextToken();
        SQLIntegerExpr countExpr = this.exprParser.integerExpr();
        clause.setPartitionsCount(countExpr);
    }
    if (lexer.token() == Token.PARTITION) {
        lexer.nextToken();
        if (identifierEquals("NUM")) {
            lexer.nextToken();
        }
        clause.setPartitionsCount(this.exprParser.expr());
        clause.putAttribute("ads.partition", Boolean.TRUE);
    }
    if (identifierEquals("SUBPARTITION")) {
        lexer.nextToken();
        accept(Token.BY);
        SQLSubPartitionBy subPartitionByClause = null;
        boolean linear = false;
        if (identifierEquals("LINEAR")) {
            lexer.nextToken();
            linear = true;
        }
        if (lexer.token() == Token.KEY) {
            MySqlSubPartitionByKey subPartitionKey = new MySqlSubPartitionByKey();
            lexer.nextToken();
            if (linear) {
                clause.setLinear(true);
            }
            accept(Token.LPAREN);
            for (; ; ) {
                subPartitionKey.addColumn(this.exprParser.name());
                if (lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                    continue;
                }
                break;
            }
            accept(Token.RPAREN);
            subPartitionByClause = subPartitionKey;
        } else if (identifierEquals("HASH")) {
            lexer.nextToken();
            SQLSubPartitionByHash subPartitionHash = new SQLSubPartitionByHash();
            if (linear) {
                clause.setLinear(true);
            }
            if (lexer.token() == Token.KEY) {
                lexer.nextToken();
                subPartitionHash.setKey(true);
            }
            accept(Token.LPAREN);
            subPartitionHash.setExpr(this.exprParser.expr());
            accept(Token.RPAREN);
            subPartitionByClause = subPartitionHash;
        } else if (identifierEquals("LIST")) {
            lexer.nextToken();
            MySqlSubPartitionByList subPartitionList = new MySqlSubPartitionByList();
            if (lexer.token() == Token.LPAREN) {
                lexer.nextToken();
                SQLExpr expr = this.exprParser.expr();
                if (expr instanceof SQLIdentifierExpr && (identifierEquals("bigint") || identifierEquals("long"))) {
                    String dataType = lexer.stringVal();
                    lexer.nextToken();
                    SQLColumnDefinition column = this.exprParser.createColumnDefinition();
                    column.setName((SQLIdentifierExpr) expr);
                    column.setDataType(new SQLDataTypeImpl(dataType));
                    subPartitionList.addColumn(column);
                    subPartitionList.putAttribute("ads.subPartitionList", Boolean.TRUE);
                } else {
                    subPartitionList.setExpr(expr);
                }
                accept(Token.RPAREN);
            } else {
                acceptIdentifier("COLUMNS");
                accept(Token.LPAREN);
                for (; ; ) {
                    subPartitionList.addColumn(this.exprParser.parseColumn());
                    if (lexer.token() == Token.COMMA) {
                        lexer.nextToken();
                        continue;
                    }
                    break;
                }
                accept(Token.RPAREN);
            }
            subPartitionByClause = subPartitionList;
        }
        if (identifierEquals("SUBPARTITION")) {
            lexer.nextToken();
            acceptIdentifier("OPTIONS");
            accept(Token.LPAREN);
            SQLAssignItem option = this.exprParser.parseAssignItem();
            accept(Token.RPAREN);
            option.setParent(subPartitionByClause);
            subPartitionByClause.getOptions().add(option);
        }
        if (identifierEquals("SUBPARTITIONS")) {
            lexer.nextToken();
            Number intValue = lexer.integerValue();
            SQLNumberExpr numExpr = new SQLNumberExpr(intValue);
            subPartitionByClause.setSubPartitionsCount(numExpr);
            lexer.nextToken();
        }
        if (subPartitionByClause != null) {
            subPartitionByClause.setLinear(linear);
            clause.setSubPartitionBy(subPartitionByClause);
        }
    }
}
Also used : MySqlSubPartitionByList(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSubPartitionByList) SQLSubPartitionByHash(com.alibaba.druid.sql.ast.SQLSubPartitionByHash) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLAssignItem(com.alibaba.druid.sql.ast.statement.SQLAssignItem) MySqlSubPartitionByKey(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSubPartitionByKey) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLDataTypeImpl(com.alibaba.druid.sql.ast.SQLDataTypeImpl) SQLSubPartitionBy(com.alibaba.druid.sql.ast.SQLSubPartitionBy)

Example 2 with SQLAssignItem

use of com.alibaba.druid.sql.ast.statement.SQLAssignItem in project druid by alibaba.

the class MySqlExprParser method parseAssignItem.

public SQLAssignItem parseAssignItem() {
    SQLAssignItem item = new SQLAssignItem();
    SQLExpr var = primary();
    String ident = null;
    if (var instanceof SQLIdentifierExpr) {
        ident = ((SQLIdentifierExpr) var).getName();
        if ("GLOBAL".equalsIgnoreCase(ident)) {
            ident = lexer.stringVal();
            lexer.nextToken();
            var = new SQLVariantRefExpr(ident, true);
        } else if ("SESSION".equalsIgnoreCase(ident)) {
            ident = lexer.stringVal();
            lexer.nextToken();
            var = new SQLVariantRefExpr(ident, false);
        } else {
            var = new SQLVariantRefExpr(ident);
        }
    }
    if ("NAMES".equalsIgnoreCase(ident)) {
    // skip
    } else if ("CHARACTER".equalsIgnoreCase(ident)) {
        var = new SQLIdentifierExpr("CHARACTER SET");
        accept(Token.SET);
        if (lexer.token() == Token.EQ) {
            lexer.nextToken();
        }
    } else {
        if (lexer.token() == Token.COLONEQ) {
            lexer.nextToken();
        } else {
            accept(Token.EQ);
        }
    }
    if (lexer.token() == Token.ON) {
        lexer.nextToken();
        item.setValue(new SQLIdentifierExpr("ON"));
    } else {
        item.setValue(this.expr());
    }
    item.setTarget(var);
    return item;
}
Also used : SQLAssignItem(com.alibaba.druid.sql.ast.statement.SQLAssignItem) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 3 with SQLAssignItem

use of com.alibaba.druid.sql.ast.statement.SQLAssignItem in project druid by alibaba.

the class SQLExprParser method parseAssignItem.

public SQLAssignItem parseAssignItem() {
    SQLAssignItem item = new SQLAssignItem();
    SQLExpr var = primary();
    if (var instanceof SQLIdentifierExpr) {
        var = new SQLVariantRefExpr(((SQLIdentifierExpr) var).getName());
    }
    item.setTarget(var);
    if (lexer.token() == Token.COLONEQ) {
        lexer.nextToken();
    } else {
        accept(Token.EQ);
    }
    if (lexer.token() == Token.ON) {
        item.setValue(new SQLIdentifierExpr(lexer.stringVal()));
        lexer.nextToken();
    } else {
        if (lexer.token() == Token.ALL) {
            item.setValue(new SQLIdentifierExpr(lexer.stringVal()));
            lexer.nextToken();
        } else {
            SQLExpr expr = expr();
            if (lexer.token() == Token.COMMA && JdbcConstants.POSTGRESQL.equals(dbType)) {
                SQLListExpr listExpr = new SQLListExpr();
                listExpr.addItem(expr);
                expr.setParent(listExpr);
                do {
                    lexer.nextToken();
                    SQLExpr listItem = this.expr();
                    listItem.setParent(listExpr);
                    listExpr.addItem(listItem);
                } while (lexer.token() == Token.COMMA);
                item.setValue(listExpr);
            } else {
                item.setValue(expr);
            }
        }
    }
    return item;
}
Also used : SQLAssignItem(com.alibaba.druid.sql.ast.statement.SQLAssignItem)

Example 4 with SQLAssignItem

use of com.alibaba.druid.sql.ast.statement.SQLAssignItem in project druid by alibaba.

the class OdpsStatementParser method parseOdpsInsert.

public OdpsInsert parseOdpsInsert() {
    OdpsInsert insert = new OdpsInsert();
    if (lexer.isKeepComments() && lexer.hasComment()) {
        insert.addBeforeComment(lexer.readAndResetComments());
    }
    SQLSelectParser selectParser = createSQLSelectParser();
    accept(Token.INSERT);
    if (lexer.token() == Token.INTO) {
        lexer.nextToken();
    } else {
        accept(Token.OVERWRITE);
        insert.setOverwrite(true);
    }
    accept(Token.TABLE);
    insert.setTableSource(this.exprParser.name());
    if (lexer.token() == Token.PARTITION) {
        lexer.nextToken();
        accept(Token.LPAREN);
        for (; ; ) {
            SQLAssignItem ptExpr = new SQLAssignItem();
            ptExpr.setTarget(this.exprParser.name());
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
                SQLExpr ptValue = this.exprParser.expr();
                ptExpr.setValue(ptValue);
            }
            insert.addPartition(ptExpr);
            if (!(lexer.token() == (Token.COMMA))) {
                break;
            } else {
                lexer.nextToken();
            }
        }
        accept(Token.RPAREN);
    }
    SQLSelect query = selectParser.select();
    insert.setQuery(query);
    return insert;
}
Also used : SQLAssignItem(com.alibaba.druid.sql.ast.statement.SQLAssignItem) SQLSelectParser(com.alibaba.druid.sql.parser.SQLSelectParser) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) OdpsInsert(com.alibaba.druid.sql.dialect.odps.ast.OdpsInsert) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 5 with SQLAssignItem

use of com.alibaba.druid.sql.ast.statement.SQLAssignItem in project druid by alibaba.

the class MySqlWallVisitor method visit.

public boolean visit(SQLPropertyExpr x) {
    if (x.getOwner() instanceof SQLVariantRefExpr) {
        SQLVariantRefExpr varExpr = (SQLVariantRefExpr) x.getOwner();
        SQLObject parent = x.getParent();
        String varName = varExpr.getName();
        if (varName.equalsIgnoreCase("@@session") || varName.equalsIgnoreCase("@@global")) {
            if (!(parent instanceof SQLSelectItem) && !(parent instanceof SQLAssignItem)) {
                violations.add(new IllegalSQLObjectViolation(ErrorCode.VARIANT_DENY, "variable in condition not allow", toSQL(x)));
                return false;
            }
            if (!checkVar(x.getParent(), x.getName())) {
                boolean isTop = WallVisitorUtils.isTopNoneFromSelect(this, x);
                if (!isTop) {
                    boolean allow = true;
                    if (isDeny(varName) && (WallVisitorUtils.isWhereOrHaving(x) || WallVisitorUtils.checkSqlExpr(varExpr))) {
                        allow = false;
                    }
                    if (!allow) {
                        violations.add(new IllegalSQLObjectViolation(ErrorCode.VARIANT_DENY, "variable not allow : " + x.getName(), toSQL(x)));
                    }
                }
            }
            return false;
        }
    }
    WallVisitorUtils.check(this, x);
    return true;
}
Also used : SQLAssignItem(com.alibaba.druid.sql.ast.statement.SQLAssignItem) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) IllegalSQLObjectViolation(com.alibaba.druid.wall.violation.IllegalSQLObjectViolation) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)

Aggregations

SQLAssignItem (com.alibaba.druid.sql.ast.statement.SQLAssignItem)5 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)3 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)2 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)2 SQLDataTypeImpl (com.alibaba.druid.sql.ast.SQLDataTypeImpl)1 SQLObject (com.alibaba.druid.sql.ast.SQLObject)1 SQLSubPartitionBy (com.alibaba.druid.sql.ast.SQLSubPartitionBy)1 SQLSubPartitionByHash (com.alibaba.druid.sql.ast.SQLSubPartitionByHash)1 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)1 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)1 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)1 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)1 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)1 MySqlSubPartitionByKey (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSubPartitionByKey)1 MySqlSubPartitionByList (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSubPartitionByList)1 OdpsInsert (com.alibaba.druid.sql.dialect.odps.ast.OdpsInsert)1 SQLSelectParser (com.alibaba.druid.sql.parser.SQLSelectParser)1 IllegalSQLObjectViolation (com.alibaba.druid.wall.violation.IllegalSQLObjectViolation)1