Search in sources :

Example 1 with SQLIdentifierExpr

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

the class MySqlSelectParser method parseIndexHint.

private void parseIndexHint(MySqlIndexHintImpl hint) {
    if (lexer.token() == Token.INDEX) {
        lexer.nextToken();
    } else {
        accept(Token.KEY);
    }
    if (lexer.token() == Token.FOR) {
        lexer.nextToken();
        if (lexer.token() == Token.JOIN) {
            lexer.nextToken();
            hint.setOption(MySqlIndexHint.Option.JOIN);
        } else if (lexer.token() == Token.ORDER) {
            lexer.nextToken();
            accept(Token.BY);
            hint.setOption(MySqlIndexHint.Option.ORDER_BY);
        } else {
            accept(Token.GROUP);
            accept(Token.BY);
            hint.setOption(MySqlIndexHint.Option.GROUP_BY);
        }
    }
    accept(Token.LPAREN);
    if (lexer.token() == Token.PRIMARY) {
        lexer.nextToken();
        hint.getIndexList().add(new SQLIdentifierExpr("PRIMARY"));
    } else {
        this.exprParser.names(hint.getIndexList());
    }
    accept(Token.RPAREN);
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)

Example 2 with SQLIdentifierExpr

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

the class MySqlStatementParser method parseAlterTable.

protected SQLStatement parseAlterTable(boolean ignore) {
    lexer.nextToken();
    SQLAlterTableStatement stmt = new SQLAlterTableStatement(getDbType());
    stmt.setIgnore(ignore);
    stmt.setName(this.exprParser.name());
    for (; ; ) {
        if (lexer.token() == Token.DROP) {
            parseAlterDrop(stmt);
        } else if (lexer.token() == Token.TRUNCATE) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableTruncatePartition item = new SQLAlterTableTruncatePartition();
            if (lexer.token() == Token.ALL) {
                item.getPartitions().add(new SQLIdentifierExpr("ALL"));
                lexer.nextToken();
            } else {
                this.exprParser.names(item.getPartitions(), item);
            }
            stmt.addItem(item);
        } else if (identifierEquals("ADD")) {
            lexer.nextToken();
            if (lexer.token() == Token.COLUMN) {
                lexer.nextToken();
                parseAlterTableAddColumn(stmt);
            } else if (lexer.token() == Token.INDEX) {
                SQLAlterTableAddIndex item = parseAlterTableAddIndex();
                item.setParent(stmt);
                stmt.addItem(item);
            } else if (lexer.token() == Token.UNIQUE) {
                SQLAlterTableAddIndex item = parseAlterTableAddIndex();
                item.setParent(stmt);
                stmt.addItem(item);
            } else if (lexer.token() == Token.PRIMARY) {
                SQLPrimaryKey primaryKey = this.exprParser.parsePrimaryKey();
                SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(primaryKey);
                stmt.addItem(item);
            } else if (lexer.token() == Token.KEY) {
                // throw new ParserException("TODO " + lexer.token() +
                // " " + lexer.stringVal());
                SQLAlterTableAddIndex item = parseAlterTableAddIndex();
                item.setParent(stmt);
                stmt.addItem(item);
            } else if (lexer.token() == Token.CONSTRAINT) {
                lexer.nextToken();
                SQLName constraintName = this.exprParser.name();
                if (lexer.token() == Token.PRIMARY) {
                    SQLPrimaryKey primaryKey = ((MySqlExprParser) this.exprParser).parsePrimaryKey();
                    primaryKey.setName(constraintName);
                    SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(primaryKey);
                    item.setParent(stmt);
                    stmt.addItem(item);
                } else if (lexer.token() == Token.FOREIGN) {
                    MysqlForeignKey fk = this.getExprParser().parseForeignKey();
                    fk.setName(constraintName);
                    fk.setHasConstraint(true);
                    SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(fk);
                    stmt.addItem(item);
                } else if (lexer.token() == Token.UNIQUE) {
                    SQLUnique unique = this.exprParser.parseUnique();
                    SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(unique);
                    stmt.addItem(item);
                } else {
                    throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
                }
            } else if (lexer.token() == Token.PARTITION) {
                lexer.nextToken();
                SQLAlterTableAddPartition item = new SQLAlterTableAddPartition();
                if (identifierEquals("PARTITIONS")) {
                    lexer.nextToken();
                    item.setPartitionCount(this.exprParser.integerExpr());
                }
                if (lexer.token() == Token.LPAREN) {
                    lexer.nextToken();
                    SQLPartition partition = this.getExprParser().parsePartition();
                    accept(Token.RPAREN);
                    item.addPartition(partition);
                }
                stmt.addItem(item);
            } else if (identifierEquals(FULLTEXT)) {
                throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
            } else if (identifierEquals(SPATIAL)) {
                throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
            } else {
                parseAlterTableAddColumn(stmt);
            }
        } else if (lexer.token() == Token.ALTER) {
            lexer.nextToken();
            if (lexer.token() == Token.COLUMN) {
                lexer.nextToken();
            }
            MySqlAlterTableAlterColumn alterColumn = new MySqlAlterTableAlterColumn();
            alterColumn.setColumn(this.exprParser.name());
            if (lexer.token() == Token.SET) {
                lexer.nextToken();
                accept(Token.DEFAULT);
                alterColumn.setDefaultExpr(this.exprParser.expr());
            } else {
                accept(Token.DROP);
                accept(Token.DEFAULT);
                alterColumn.setDropDefault(true);
            }
            stmt.addItem(alterColumn);
        } else if (identifierEquals("CHANGE")) {
            lexer.nextToken();
            if (lexer.token() == Token.COLUMN) {
                lexer.nextToken();
            }
            MySqlAlterTableChangeColumn item = new MySqlAlterTableChangeColumn();
            item.setColumnName(this.exprParser.name());
            item.setNewColumnDefinition(this.exprParser.parseColumn());
            if (identifierEquals("AFTER")) {
                lexer.nextToken();
                item.setAfterColumn(this.exprParser.name());
            } else if (identifierEquals("FIRST")) {
                lexer.nextToken();
                if (lexer.token() == Token.IDENTIFIER) {
                    item.setFirstColumn(this.exprParser.name());
                } else {
                    item.setFirst(true);
                }
            }
            stmt.addItem(item);
        } else if (identifierEquals("MODIFY")) {
            lexer.nextToken();
            if (lexer.token() == Token.COLUMN) {
                lexer.nextToken();
            }
            boolean paren = false;
            if (lexer.token() == Token.LPAREN) {
                paren = true;
                lexer.nextToken();
            }
            for (; ; ) {
                MySqlAlterTableModifyColumn item = new MySqlAlterTableModifyColumn();
                item.setNewColumnDefinition(this.exprParser.parseColumn());
                if (identifierEquals("AFTER")) {
                    lexer.nextToken();
                    item.setAfterColumn(this.exprParser.name());
                } else if (identifierEquals("FIRST")) {
                    lexer.nextToken();
                    if (lexer.token() == Token.IDENTIFIER) {
                        item.setFirstColumn(this.exprParser.name());
                    } else {
                        item.setFirst(true);
                    }
                }
                stmt.addItem(item);
                if (paren && lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                    continue;
                }
                break;
            }
            if (paren) {
                accept(Token.RPAREN);
            }
        } else if (lexer.token() == Token.DISABLE) {
            lexer.nextToken();
            if (lexer.token() == Token.CONSTRAINT) {
                lexer.nextToken();
                SQLAlterTableDisableConstraint item = new SQLAlterTableDisableConstraint();
                item.setConstraintName(this.exprParser.name());
                stmt.addItem(item);
            } else {
                acceptIdentifier("KEYS");
                SQLAlterTableDisableKeys item = new SQLAlterTableDisableKeys();
                stmt.addItem(item);
            }
        } else if (lexer.token() == Token.ENABLE) {
            lexer.nextToken();
            if (lexer.token() == Token.CONSTRAINT) {
                lexer.nextToken();
                SQLAlterTableEnableConstraint item = new SQLAlterTableEnableConstraint();
                item.setConstraintName(this.exprParser.name());
                stmt.addItem(item);
            } else {
                acceptIdentifier("KEYS");
                SQLAlterTableEnableKeys item = new SQLAlterTableEnableKeys();
                stmt.addItem(item);
            }
        } else if (identifierEquals("RENAME")) {
            lexer.nextToken();
            if (lexer.token() == Token.TO || lexer.token() == Token.AS) {
                lexer.nextToken();
            }
            MySqlRenameTableStatement renameStmt = new MySqlRenameTableStatement();
            MySqlRenameTableStatement.Item item = new MySqlRenameTableStatement.Item();
            item.setName(stmt.getTableSource().getExpr());
            item.setTo(this.exprParser.name());
            renameStmt.addItem(item);
            return renameStmt;
        } else if (lexer.token() == Token.ORDER) {
            throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
        } else if (identifierEquals("CONVERT")) {
            lexer.nextToken();
            accept(Token.TO);
            acceptIdentifier("CHARACTER");
            accept(Token.SET);
            SQLAlterTableConvertCharSet item = new SQLAlterTableConvertCharSet();
            SQLExpr charset = this.exprParser.primary();
            item.setCharset(charset);
            if (identifierEquals("COLLATE")) {
                lexer.nextToken();
                SQLExpr collate = this.exprParser.primary();
                item.setCollate(collate);
            }
            stmt.addItem(item);
        } else if (lexer.token() == Token.DEFAULT) {
            throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
        } else if (identifierEquals("DISCARD")) {
            lexer.nextToken();
            if (lexer.token() == Token.PARTITION) {
                lexer.nextToken();
                SQLAlterTableDiscardPartition item = new SQLAlterTableDiscardPartition();
                if (lexer.token() == Token.ALL) {
                    lexer.nextToken();
                    item.getPartitions().add(new SQLIdentifierExpr("ALL"));
                } else {
                    this.exprParser.names(item.getPartitions(), item);
                }
                stmt.addItem(item);
            } else {
                accept(Token.TABLESPACE);
                MySqlAlterTableDiscardTablespace item = new MySqlAlterTableDiscardTablespace();
                stmt.addItem(item);
            }
        } else if (lexer.token() == Token.CHECK) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableCheckPartition item = new SQLAlterTableCheckPartition();
            if (lexer.token() == Token.ALL) {
                lexer.nextToken();
                item.getPartitions().add(new SQLIdentifierExpr("ALL"));
            } else {
                this.exprParser.names(item.getPartitions(), item);
            }
            stmt.addItem(item);
        } else if (identifierEquals("IMPORT")) {
            lexer.nextToken();
            if (lexer.token() == Token.PARTITION) {
                lexer.nextToken();
                SQLAlterTableImportPartition item = new SQLAlterTableImportPartition();
                if (lexer.token() == Token.ALL) {
                    lexer.nextToken();
                    item.getPartitions().add(new SQLIdentifierExpr("ALL"));
                } else {
                    this.exprParser.names(item.getPartitions(), item);
                }
                stmt.addItem(item);
            } else {
                accept(Token.TABLESPACE);
                MySqlAlterTableImportTablespace item = new MySqlAlterTableImportTablespace();
                stmt.addItem(item);
            }
        } else if (lexer.token() == Token.ANALYZE) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableAnalyzePartition item = new SQLAlterTableAnalyzePartition();
            if (lexer.token() == Token.ALL) {
                lexer.nextToken();
                item.getPartitions().add(new SQLIdentifierExpr("ALL"));
            } else {
                this.exprParser.names(item.getPartitions(), item);
            }
            stmt.addItem(item);
        } else if (identifierEquals("FORCE")) {
            throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
        } else if (identifierEquals("COALESCE")) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableCoalescePartition item = new SQLAlterTableCoalescePartition();
            SQLIntegerExpr countExpr = this.exprParser.integerExpr();
            item.setCount(countExpr);
            stmt.addItem(item);
        } else if (identifierEquals("REORGANIZE")) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableReOrganizePartition item = new SQLAlterTableReOrganizePartition();
            this.exprParser.names(item.getNames(), item);
            accept(Token.INTO);
            accept(Token.LPAREN);
            for (; ; ) {
                SQLPartition partition = this.getExprParser().parsePartition();
                item.addPartition(partition);
                if (lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                    continue;
                } else {
                    break;
                }
            }
            accept(Token.RPAREN);
            stmt.addItem(item);
        } else if (identifierEquals("EXCHANGE")) {
            throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
        } else if (lexer.token() == Token.OPTIMIZE) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableOptimizePartition item = new SQLAlterTableOptimizePartition();
            if (lexer.token() == Token.ALL) {
                lexer.nextToken();
                item.getPartitions().add(new SQLIdentifierExpr("ALL"));
            } else {
                this.exprParser.names(item.getPartitions(), item);
            }
            stmt.addItem(item);
        } else if (identifierEquals("REBUILD")) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableRebuildPartition item = new SQLAlterTableRebuildPartition();
            if (lexer.token() == Token.ALL) {
                lexer.nextToken();
                item.getPartitions().add(new SQLIdentifierExpr("ALL"));
            } else {
                this.exprParser.names(item.getPartitions(), item);
            }
            stmt.addItem(item);
        } else if (identifierEquals("REPAIR")) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableRepairPartition item = new SQLAlterTableRepairPartition();
            if (lexer.token() == Token.ALL) {
                lexer.nextToken();
                item.getPartitions().add(new SQLIdentifierExpr("ALL"));
            } else {
                this.exprParser.names(item.getPartitions(), item);
            }
            stmt.addItem(item);
        } else if (identifierEquals("REMOVE")) {
            lexer.nextToken();
            acceptIdentifier("PARTITIONING");
            stmt.setRemovePatiting(true);
        } else if (identifierEquals("UPGRADE")) {
            lexer.nextToken();
            acceptIdentifier("PARTITIONING");
            stmt.setUpgradePatiting(true);
        } else if (identifierEquals("ALGORITHM")) {
            lexer.nextToken();
            accept(Token.EQ);
            stmt.addItem(new MySqlAlterTableOption("ALGORITHM", lexer.stringVal()));
            lexer.nextToken();
        } else if (identifierEquals(ENGINE)) {
            lexer.nextToken();
            accept(Token.EQ);
            stmt.addItem(new MySqlAlterTableOption(ENGINE, lexer.stringVal()));
            lexer.nextToken();
        } else if (identifierEquals(AUTO_INCREMENT)) {
            lexer.nextToken();
            accept(Token.EQ);
            stmt.addItem(new MySqlAlterTableOption(AUTO_INCREMENT, lexer.integerValue()));
            lexer.nextToken();
        } else if (identifierEquals(COLLATE2)) {
            lexer.nextToken();
            accept(Token.EQ);
            stmt.addItem(new MySqlAlterTableOption(COLLATE2, lexer.stringVal()));
            lexer.nextToken();
        } else if (identifierEquals("PACK_KEYS")) {
            lexer.nextToken();
            accept(Token.EQ);
            if (identifierEquals("PACK")) {
                lexer.nextToken();
                accept(Token.ALL);
                stmt.addItem(new MySqlAlterTableOption("PACK_KEYS", "PACK ALL"));
            } else {
                stmt.addItem(new MySqlAlterTableOption("PACK_KEYS", lexer.stringVal()));
                lexer.nextToken();
            }
        } else if (identifierEquals(CHARACTER)) {
            lexer.nextToken();
            accept(Token.SET);
            accept(Token.EQ);
            MySqlAlterTableCharacter item = new MySqlAlterTableCharacter();
            item.setCharacterSet(this.exprParser.primary());
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                acceptIdentifier(COLLATE2);
                accept(Token.EQ);
                item.setCollate(this.exprParser.primary());
            }
            stmt.addItem(item);
        } else if (lexer.token() == Token.COMMENT) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                accept(Token.EQ);
            }
            stmt.addItem(new MySqlAlterTableOption("COMMENT", '\'' + lexer.stringVal() + '\''));
            lexer.nextToken();
        } else if (lexer.token() == Token.UNION) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            accept(Token.LPAREN);
            SQLTableSource tableSrc = this.createSQLSelectParser().parseTableSource();
            stmt.getTableOptions().put("UNION", tableSrc);
            accept(Token.RPAREN);
        } else if (identifierEquals("ROW_FORMAT")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            if (lexer.token() == Token.DEFAULT || lexer.token() == Token.IDENTIFIER) {
                SQLIdentifierExpr rowFormat = new SQLIdentifierExpr(lexer.stringVal());
                lexer.nextToken();
                stmt.getTableOptions().put("ROW_FORMAT", rowFormat);
            } else {
                throw new ParserException("illegal syntax.");
            }
        } else {
            break;
        }
        if (lexer.token() == Token.COMMA) {
            lexer.nextToken();
            continue;
        } else {
            break;
        }
    }
    return stmt;
}
Also used : MySqlAlterTableModifyColumn(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn) MySqlAlterTableCharacter(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableCharacter) MySqlAlterTableAlterColumn(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableAlterColumn) SQLDeclareItem(com.alibaba.druid.sql.ast.SQLDeclareItem) MySqlAlterTableDiscardTablespace(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableDiscardTablespace) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) MySqlRenameTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLName(com.alibaba.druid.sql.ast.SQLName) MySqlAlterTableOption(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableOption) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) MysqlForeignKey(com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey) SQLPartition(com.alibaba.druid.sql.ast.SQLPartition) MySqlAlterTableImportTablespace(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableImportTablespace) MySqlAlterTableChangeColumn(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn)

Example 3 with SQLIdentifierExpr

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

the class MySqlShowIndexesStatement method setTable.

public void setTable(SQLName table) {
    if (table instanceof SQLPropertyExpr) {
        SQLPropertyExpr propExpr = (SQLPropertyExpr) table;
        this.setDatabase((SQLName) propExpr.getOwner());
        this.table = new SQLIdentifierExpr(propExpr.getName());
        return;
    }
    this.table = table;
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)

Example 4 with SQLIdentifierExpr

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

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

the class MySqlExprParser method primaryRest.

public final SQLExpr primaryRest(SQLExpr expr) {
    if (expr == null) {
        throw new IllegalArgumentException("expr");
    }
    if (lexer.token() == Token.LITERAL_CHARS) {
        if (expr instanceof SQLIdentifierExpr) {
            SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
            String ident = identExpr.getName();
            if (ident.equalsIgnoreCase("x")) {
                String charValue = lexer.stringVal();
                lexer.nextToken();
                expr = new SQLHexExpr(charValue);
                return primaryRest(expr);
            } else if (ident.equalsIgnoreCase("b")) {
                String charValue = lexer.stringVal();
                lexer.nextToken();
                expr = new SQLBinaryExpr(charValue);
                return primaryRest(expr);
            } else if (ident.startsWith("_")) {
                String charValue = lexer.stringVal();
                lexer.nextToken();
                MySqlCharExpr mysqlCharExpr = new MySqlCharExpr(charValue);
                mysqlCharExpr.setCharset(identExpr.getName());
                if (identifierEquals("COLLATE")) {
                    lexer.nextToken();
                    String collate = lexer.stringVal();
                    mysqlCharExpr.setCollate(collate);
                    accept(Token.IDENTIFIER);
                }
                expr = mysqlCharExpr;
                return primaryRest(expr);
            }
        } else if (expr instanceof SQLCharExpr) {
            SQLMethodInvokeExpr concat = new SQLMethodInvokeExpr("CONCAT");
            concat.addParameter(expr);
            do {
                String chars = lexer.stringVal();
                concat.addParameter(new SQLCharExpr(chars));
                lexer.nextToken();
            } while (lexer.token() == Token.LITERAL_CHARS || lexer.token() == Token.LITERAL_ALIAS);
            expr = concat;
        }
    } else if (lexer.token() == Token.IDENTIFIER) {
        if (expr instanceof SQLHexExpr) {
            if ("USING".equalsIgnoreCase(lexer.stringVal())) {
                lexer.nextToken();
                if (lexer.token() != Token.IDENTIFIER) {
                    throw new ParserException("syntax error, illegal hex");
                }
                String charSet = lexer.stringVal();
                lexer.nextToken();
                expr.getAttributes().put("USING", charSet);
                return primaryRest(expr);
            }
        } else if ("COLLATE".equalsIgnoreCase(lexer.stringVal())) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            if (lexer.token() != Token.IDENTIFIER) {
                throw new ParserException("syntax error");
            }
            String collate = lexer.stringVal();
            lexer.nextToken();
            SQLBinaryOpExpr binaryExpr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.COLLATE, new SQLIdentifierExpr(collate), JdbcConstants.MYSQL);
            expr = binaryExpr;
            return primaryRest(expr);
        } else if (expr instanceof SQLVariantRefExpr) {
            if ("COLLATE".equalsIgnoreCase(lexer.stringVal())) {
                lexer.nextToken();
                if (lexer.token() != Token.IDENTIFIER) {
                    throw new ParserException("syntax error");
                }
                String collate = lexer.stringVal();
                lexer.nextToken();
                expr.putAttribute("COLLATE", collate);
                return primaryRest(expr);
            }
        } else if (expr instanceof SQLIntegerExpr) {
            SQLIntegerExpr intExpr = (SQLIntegerExpr) expr;
            String binaryString = lexer.stringVal();
            if (intExpr.getNumber().intValue() == 0 && binaryString.startsWith("b")) {
                lexer.nextToken();
                expr = new SQLBinaryExpr(binaryString.substring(1));
                return primaryRest(expr);
            }
        }
    }
    if (lexer.token() == Token.LPAREN && expr instanceof SQLIdentifierExpr) {
        SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
        String ident = identExpr.getName();
        if ("EXTRACT".equalsIgnoreCase(ident)) {
            lexer.nextToken();
            if (lexer.token() != Token.IDENTIFIER) {
                throw new ParserException("syntax error");
            }
            String unitVal = lexer.stringVal();
            MySqlIntervalUnit unit = MySqlIntervalUnit.valueOf(unitVal.toUpperCase());
            lexer.nextToken();
            accept(Token.FROM);
            SQLExpr value = expr();
            MySqlExtractExpr extract = new MySqlExtractExpr();
            extract.setValue(value);
            extract.setUnit(unit);
            accept(Token.RPAREN);
            expr = extract;
            return primaryRest(expr);
        } else if ("SUBSTRING".equalsIgnoreCase(ident)) {
            lexer.nextToken();
            SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(ident);
            for (; ; ) {
                SQLExpr param = expr();
                methodInvokeExpr.addParameter(param);
                if (lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                    continue;
                } else if (lexer.token() == Token.FROM) {
                    lexer.nextToken();
                    SQLExpr from = expr();
                    methodInvokeExpr.addParameter(from);
                    if (lexer.token() == Token.FOR) {
                        lexer.nextToken();
                        SQLExpr forExpr = expr();
                        methodInvokeExpr.addParameter(forExpr);
                    }
                    break;
                } else if (lexer.token() == Token.RPAREN) {
                    break;
                } else {
                    throw new ParserException("syntax error");
                }
            }
            accept(Token.RPAREN);
            expr = methodInvokeExpr;
            return primaryRest(expr);
        } else if ("TRIM".equalsIgnoreCase(ident)) {
            lexer.nextToken();
            SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(ident);
            if (lexer.token() == Token.IDENTIFIER) {
                String flagVal = lexer.stringVal();
                if ("LEADING".equalsIgnoreCase(flagVal)) {
                    lexer.nextToken();
                    methodInvokeExpr.getAttributes().put("TRIM_TYPE", "LEADING");
                } else if ("BOTH".equalsIgnoreCase(flagVal)) {
                    lexer.nextToken();
                    methodInvokeExpr.getAttributes().put("TRIM_TYPE", "BOTH");
                } else if ("TRAILING".equalsIgnoreCase(flagVal)) {
                    lexer.nextToken();
                    methodInvokeExpr.putAttribute("TRIM_TYPE", "TRAILING");
                }
            }
            SQLExpr param = expr();
            methodInvokeExpr.addParameter(param);
            if (lexer.token() == Token.FROM) {
                lexer.nextToken();
                SQLExpr from = expr();
                methodInvokeExpr.putAttribute("FROM", from);
            }
            accept(Token.RPAREN);
            expr = methodInvokeExpr;
            return primaryRest(expr);
        } else if ("MATCH".equalsIgnoreCase(ident)) {
            lexer.nextToken();
            MySqlMatchAgainstExpr matchAgainstExpr = new MySqlMatchAgainstExpr();
            if (lexer.token() == Token.RPAREN) {
                lexer.nextToken();
            } else {
                exprList(matchAgainstExpr.getColumns(), matchAgainstExpr);
                accept(Token.RPAREN);
            }
            acceptIdentifier("AGAINST");
            accept(Token.LPAREN);
            SQLExpr against = primary();
            matchAgainstExpr.setAgainst(against);
            if (lexer.token() == Token.IN) {
                lexer.nextToken();
                if (identifierEquals("NATURAL")) {
                    lexer.nextToken();
                    acceptIdentifier("LANGUAGE");
                    acceptIdentifier("MODE");
                    if (lexer.token() == Token.WITH) {
                        lexer.nextToken();
                        acceptIdentifier("QUERY");
                        acceptIdentifier("EXPANSION");
                        matchAgainstExpr.setSearchModifier(SearchModifier.IN_NATURAL_LANGUAGE_MODE_WITH_QUERY_EXPANSION);
                    } else {
                        matchAgainstExpr.setSearchModifier(SearchModifier.IN_NATURAL_LANGUAGE_MODE);
                    }
                } else if (identifierEquals("BOOLEAN")) {
                    lexer.nextToken();
                    acceptIdentifier("MODE");
                    matchAgainstExpr.setSearchModifier(SearchModifier.IN_BOOLEAN_MODE);
                } else {
                    throw new ParserException("TODO");
                }
            } else if (lexer.token() == Token.WITH) {
                throw new ParserException("TODO");
            }
            accept(Token.RPAREN);
            expr = matchAgainstExpr;
            return primaryRest(expr);
        } else if (("CONVERT".equalsIgnoreCase(ident)) || ("CHAR".equalsIgnoreCase(ident))) {
            lexer.nextToken();
            SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(ident);
            if (lexer.token() != Token.RPAREN) {
                exprList(methodInvokeExpr.getParameters(), methodInvokeExpr);
            }
            if (identifierEquals("USING")) {
                lexer.nextToken();
                if (lexer.token() != Token.IDENTIFIER) {
                    throw new ParserException("syntax error");
                }
                String charset = lexer.stringVal();
                lexer.nextToken();
                methodInvokeExpr.putAttribute("USING", charset);
            }
            accept(Token.RPAREN);
            expr = methodInvokeExpr;
            return primaryRest(expr);
        } else if ("POSITION".equalsIgnoreCase(ident)) {
            accept(Token.LPAREN);
            SQLExpr subStr = this.primary();
            accept(Token.IN);
            SQLExpr str = this.expr();
            accept(Token.RPAREN);
            SQLMethodInvokeExpr locate = new SQLMethodInvokeExpr("LOCATE");
            locate.addParameter(subStr);
            locate.addParameter(str);
            expr = locate;
            return primaryRest(expr);
        }
    }
    if (lexer.token() == Token.VARIANT && "@".equals(lexer.stringVal())) {
        lexer.nextToken();
        MySqlUserName userName = new MySqlUserName();
        if (expr instanceof SQLCharExpr) {
            userName.setUserName(((SQLCharExpr) expr).toString());
        } else {
            userName.setUserName(((SQLIdentifierExpr) expr).getName());
        }
        if (lexer.token() == Token.LITERAL_CHARS) {
            userName.setHost("'" + lexer.stringVal() + "'");
        } else {
            userName.setHost(lexer.stringVal());
        }
        lexer.nextToken();
        return userName;
    }
    if (lexer.token() == Token.ERROR) {
        throw new ParserException("syntax error, token: " + lexer.token() + " " + lexer.stringVal() + ", pos : " + lexer.pos());
    }
    return super.primaryRest(expr);
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLBinaryExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryExpr) SQLHexExpr(com.alibaba.druid.sql.ast.expr.SQLHexExpr) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) MySqlMatchAgainstExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlMatchAgainstExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) MySqlIntervalUnit(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalUnit) MySqlCharExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) MySqlExtractExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExtractExpr) MySqlUserName(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlUserName)

Aggregations

SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)152 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)68 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)45 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)19 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)18 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)17 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)16 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)16 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)15 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)15 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)14 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)13 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)11 SQLAssignItem (com.alibaba.druid.sql.ast.statement.SQLAssignItem)11 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)10 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)10 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)10 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)9 ArrayList (java.util.ArrayList)9 SQLName (com.alibaba.druid.sql.ast.SQLName)8