Search in sources :

Example 1 with SQLIntegerExpr

use of com.alibaba.druid.sql.ast.expr.SQLIntegerExpr in project Mycat-Server by MyCATApache.

the class DruidSelectOracleParser method parseThreeLevelPageSql.

private void parseThreeLevelPageSql(SQLStatement stmt, RouteResultset rrs, SchemaConfig schema, SQLSubqueryTableSource from, SQLBinaryOpExpr one, SQLBinaryOperator operator) {
    SQLIntegerExpr right = (SQLIntegerExpr) one.getRight();
    int firstrownum = right.getNumber().intValue();
    if (operator == SQLBinaryOperator.GreaterThanOrEqual && firstrownum != 0) {
        firstrownum = firstrownum - 1;
    }
    SQLSelectQuery subSelect = from.getSelect().getQuery();
    if (subSelect instanceof OracleSelectQueryBlock) {
        //第二层子查询
        OracleSelectQueryBlock twoSubSelect = (OracleSelectQueryBlock) subSelect;
        if (twoSubSelect.getWhere() instanceof SQLBinaryOpExpr && twoSubSelect.getFrom() instanceof SQLSubqueryTableSource) {
            SQLBinaryOpExpr twoWhere = (SQLBinaryOpExpr) twoSubSelect.getWhere();
            boolean isRowNum = "rownum".equalsIgnoreCase(twoWhere.getLeft().toString());
            boolean isLess = twoWhere.getOperator() == SQLBinaryOperator.LessThanOrEqual || twoWhere.getOperator() == SQLBinaryOperator.LessThan;
            if (isRowNum && twoWhere.getRight() instanceof SQLIntegerExpr && isLess) {
                int lastrownum = ((SQLIntegerExpr) twoWhere.getRight()).getNumber().intValue();
                if (operator == SQLBinaryOperator.LessThan && lastrownum != 0) {
                    lastrownum = lastrownum - 1;
                }
                SQLSelectQuery finalQuery = ((SQLSubqueryTableSource) twoSubSelect.getFrom()).getSelect().getQuery();
                if (finalQuery instanceof OracleSelectQueryBlock) {
                    setLimitIFChange(stmt, rrs, schema, one, firstrownum, lastrownum);
                    parseOrderAggGroupOracle(stmt, rrs, (OracleSelectQueryBlock) finalQuery, schema);
                    isNeedParseOrderAgg = false;
                }
            }
        }
    }
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) OracleSelectQueryBlock(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)

Example 2 with SQLIntegerExpr

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

the class MySqlMockExecuteHandlerImpl method executeQueryFromDual.

public ResultSet executeQueryFromDual(MockStatementBase statement, SQLSelectQueryBlock query) throws SQLException {
    MockResultSet rs = statement.getConnection().getDriver().createMockResultSet(statement);
    MockResultSetMetaData metaData = rs.getMockMetaData();
    Object[] row = new Object[query.getSelectList().size()];
    for (int i = 0, size = query.getSelectList().size(); i < size; ++i) {
        ColumnMetaData column = new ColumnMetaData();
        SQLSelectItem item = query.getSelectList().get(i);
        SQLExpr expr = item.getExpr();
        if (expr instanceof SQLIntegerExpr) {
            row[i] = ((SQLNumericLiteralExpr) expr).getNumber();
            column.setColumnType(Types.INTEGER);
        } else if (expr instanceof SQLNumberExpr) {
            row[i] = ((SQLNumericLiteralExpr) expr).getNumber();
            column.setColumnType(Types.DECIMAL);
        } else if (expr instanceof SQLCharExpr) {
            row[i] = ((SQLCharExpr) expr).getText();
            column.setColumnType(Types.VARCHAR);
        } else if (expr instanceof SQLNCharExpr) {
            row[i] = ((SQLNCharExpr) expr).getText();
            column.setColumnType(Types.NVARCHAR);
        } else if (expr instanceof SQLBooleanExpr) {
            row[i] = ((SQLBooleanExpr) expr).getValue();
            column.setColumnType(Types.NVARCHAR);
        } else if (expr instanceof SQLNullExpr) {
            row[i] = null;
        } else if (expr instanceof SQLMethodInvokeExpr) {
            SQLMethodInvokeExpr methodInvokeExpr = (SQLMethodInvokeExpr) expr;
            if ("NOW".equalsIgnoreCase(methodInvokeExpr.getMethodName())) {
                row[i] = new Timestamp(System.currentTimeMillis());
            } else {
                throw new SQLException("TODO");
            }
        } else if (expr instanceof SQLVariantRefExpr) {
            SQLVariantRefExpr varExpr = (SQLVariantRefExpr) expr;
            int varIndex = varExpr.getIndex();
            if (statement instanceof MockPreparedStatement) {
                MockPreparedStatement mockPstmt = (MockPreparedStatement) statement;
                row[i] = mockPstmt.getParameters().get(varIndex);
            } else {
                row[i] = null;
            }
        } else {
            row[i] = null;
        }
        metaData.getColumns().add(column);
    }
    rs.getRows().add(row);
    return rs;
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) MockResultSetMetaData(com.alibaba.druid.mock.MockResultSetMetaData) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLException(java.sql.SQLException) SQLNCharExpr(com.alibaba.druid.sql.ast.expr.SQLNCharExpr) MockResultSet(com.alibaba.druid.mock.MockResultSet) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) SQLNullExpr(com.alibaba.druid.sql.ast.expr.SQLNullExpr) Timestamp(java.sql.Timestamp) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLBooleanExpr(com.alibaba.druid.sql.ast.expr.SQLBooleanExpr) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) ColumnMetaData(com.alibaba.druid.util.jdbc.ResultSetMetaDataBase.ColumnMetaData)

Example 3 with SQLIntegerExpr

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

the class PagerUtils method limitPostgreSQLQueryBlock.

private static String limitPostgreSQLQueryBlock(PGSelectQueryBlock queryBlock, String dbType, int offset, int count) {
    if (queryBlock.getLimit() != null) {
        throw new IllegalArgumentException("limit already exists.");
    }
    SQLLimit limit = new SQLLimit();
    if (offset > 0) {
        limit.setOffset(new SQLIntegerExpr(offset));
    }
    limit.setRowCount(new SQLIntegerExpr(count));
    queryBlock.setLimit(limit);
    return SQLUtils.toSQLString(queryBlock, dbType);
}
Also used : SQLLimit(com.alibaba.druid.sql.ast.SQLLimit) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)

Example 4 with SQLIntegerExpr

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

use of com.alibaba.druid.sql.ast.expr.SQLIntegerExpr 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)

Aggregations

SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)24 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)16 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)9 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)9 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)5 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)5 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)5 SQLBinaryOperator (com.alibaba.druid.sql.ast.expr.SQLBinaryOperator)4 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)4 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)4 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)4 ParserException (com.alibaba.druid.sql.parser.ParserException)4 List (java.util.List)4 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)3 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)3 SQLNCharExpr (com.alibaba.druid.sql.ast.expr.SQLNCharExpr)3 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)3 ValuesClause (com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)3 OracleSelectQueryBlock (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock)3 SQLLimit (com.alibaba.druid.sql.ast.SQLLimit)2