Search in sources :

Example 21 with MySqlSelectQueryBlock

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project Mycat-Server by MyCATApache.

the class DruidSelectSqlServerParser method statementParse.

@Override
public void statementParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt) {
    SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
    SQLSelectQuery sqlSelectQuery = selectStmt.getSelect().getQuery();
    //从mysql解析过来
    if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
        MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock) selectStmt.getSelect().getQuery();
        MySqlSelectQueryBlock.Limit limit = mysqlSelectQuery.getLimit();
        if (limit == null) {
            sqlserverParse(schema, rrs);
        }
        if (isNeedParseOrderAgg) {
            parseOrderAggGroupMysql(schema, stmt, rrs, mysqlSelectQuery);
            //更改canRunInReadDB属性
            if ((mysqlSelectQuery.isForUpdate() || mysqlSelectQuery.isLockInShareMode()) && rrs.isAutocommit() == false) {
                rrs.setCanRunInReadDB(false);
            }
        }
    }
}
Also used : SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)

Example 22 with MySqlSelectQueryBlock

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project druid by alibaba.

the class SQLSelectBuilderImpl method limit.

@Override
public SQLSelectBuilderImpl limit(int rowCount, int offset) {
    SQLSelectQueryBlock queryBlock = getQueryBlock();
    if (queryBlock instanceof MySqlSelectQueryBlock) {
        MySqlSelectQueryBlock mySqlQueryBlock = (MySqlSelectQueryBlock) queryBlock;
        SQLLimit limit = new SQLLimit();
        limit.setRowCount(new SQLIntegerExpr(rowCount));
        if (offset > 0) {
            limit.setOffset(new SQLIntegerExpr(offset));
        }
        mySqlQueryBlock.setLimit(limit);
        return this;
    }
    if (queryBlock instanceof SQLServerSelectQueryBlock) {
        SQLServerSelectQueryBlock sqlserverQueryBlock = (SQLServerSelectQueryBlock) queryBlock;
        if (offset <= 0) {
            SQLServerTop top = new SQLServerTop();
            top.setExpr(new SQLIntegerExpr(rowCount));
            sqlserverQueryBlock.setTop(top);
        } else {
            throw new UnsupportedOperationException("not support offset");
        }
        return this;
    }
    if (queryBlock instanceof PGSelectQueryBlock) {
        PGSelectQueryBlock pgQueryBlock = (PGSelectQueryBlock) queryBlock;
        SQLLimit limit = new SQLLimit();
        if (offset > 0) {
            limit.setOffset(new SQLIntegerExpr(offset));
        }
        limit.setRowCount(new SQLIntegerExpr(rowCount));
        pgQueryBlock.setLimit(limit);
        return this;
    }
    if (queryBlock instanceof DB2SelectQueryBlock) {
        DB2SelectQueryBlock db2QueryBlock = (DB2SelectQueryBlock) queryBlock;
        if (offset <= 0) {
            SQLExpr rowCountExpr = new SQLIntegerExpr(rowCount);
            db2QueryBlock.setFirst(rowCountExpr);
        } else {
            throw new UnsupportedOperationException("not support offset");
        }
        return this;
    }
    if (queryBlock instanceof OracleSelectQueryBlock) {
        OracleSelectQueryBlock oracleQueryBlock = (OracleSelectQueryBlock) queryBlock;
        if (offset <= 0) {
            SQLExpr rowCountExpr = new SQLIntegerExpr(rowCount);
            SQLExpr newCondition = SQLUtils.buildCondition(SQLBinaryOperator.BooleanAnd, rowCountExpr, false, oracleQueryBlock.getWhere());
            queryBlock.setWhere(newCondition);
        } else {
            throw new UnsupportedOperationException("not support offset");
        }
        return this;
    }
    if (queryBlock instanceof OdpsSelectQueryBlock) {
        OdpsSelectQueryBlock odpsQueryBlock = (OdpsSelectQueryBlock) queryBlock;
        if (offset > 0) {
            throw new UnsupportedOperationException("not support offset");
        }
        odpsQueryBlock.setLimit(new SQLLimit(new SQLIntegerExpr(rowCount)));
        return this;
    }
    throw new UnsupportedOperationException();
}
Also used : SQLServerTop(com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop) OracleSelectQueryBlock(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock) SQLServerSelectQueryBlock(com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock) SQLLimit(com.alibaba.druid.sql.ast.SQLLimit) DB2SelectQueryBlock(com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) OdpsSelectQueryBlock(com.alibaba.druid.sql.dialect.odps.ast.OdpsSelectQueryBlock) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) PGSelectQueryBlock(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 23 with MySqlSelectQueryBlock

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project druid by alibaba.

the class MySqlSelectIntoParser method query.

@Override
public SQLSelectQuery query() {
    if (lexer.token() == (Token.LPAREN)) {
        lexer.nextToken();
        SQLSelectQuery select = query();
        accept(Token.RPAREN);
        return queryRest(select);
    }
    MySqlSelectQueryBlock queryBlock = new MySqlSelectQueryBlock();
    if (lexer.token() == Token.SELECT) {
        lexer.nextToken();
        if (lexer.token() == Token.HINT) {
            this.exprParser.parseHints(queryBlock.getHints());
        }
        if (lexer.token() == Token.COMMENT) {
            lexer.nextToken();
        }
        if (lexer.token() == (Token.DISTINCT)) {
            queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT);
            lexer.nextToken();
        } else if (identifierEquals("DISTINCTROW")) {
            queryBlock.setDistionOption(SQLSetQuantifier.DISTINCTROW);
            lexer.nextToken();
        } else if (lexer.token() == (Token.ALL)) {
            queryBlock.setDistionOption(SQLSetQuantifier.ALL);
            lexer.nextToken();
        }
        if (identifierEquals("HIGH_PRIORITY")) {
            queryBlock.setHignPriority(true);
            lexer.nextToken();
        }
        if (identifierEquals("STRAIGHT_JOIN")) {
            queryBlock.setStraightJoin(true);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_SMALL_RESULT")) {
            queryBlock.setSmallResult(true);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_BIG_RESULT")) {
            queryBlock.setBigResult(true);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_BUFFER_RESULT")) {
            queryBlock.setBufferResult(true);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_CACHE")) {
            queryBlock.setCache(true);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_NO_CACHE")) {
            queryBlock.setCache(false);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_CALC_FOUND_ROWS")) {
            queryBlock.setCalcFoundRows(true);
            lexer.nextToken();
        }
        parseSelectList(queryBlock);
        argsList = parseIntoArgs();
    }
    parseFrom(queryBlock);
    parseWhere(queryBlock);
    parseGroupBy(queryBlock);
    queryBlock.setOrderBy(this.exprParser.parseOrderBy());
    if (lexer.token() == Token.LIMIT) {
        queryBlock.setLimit(this.exprParser.parseLimit());
    }
    if (lexer.token() == Token.PROCEDURE) {
        lexer.nextToken();
        throw new ParserException("TODO");
    }
    parseInto(queryBlock);
    if (lexer.token() == Token.FOR) {
        lexer.nextToken();
        accept(Token.UPDATE);
        queryBlock.setForUpdate(true);
    }
    if (lexer.token() == Token.LOCK) {
        lexer.nextToken();
        accept(Token.IN);
        acceptIdentifier("SHARE");
        acceptIdentifier("MODE");
        queryBlock.setLockInShareMode(true);
    }
    return queryRest(queryBlock);
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)

Example 24 with MySqlSelectQueryBlock

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project druid by alibaba.

the class MySqlSelectParser method query.

@Override
public SQLSelectQuery query() {
    if (lexer.token() == (Token.LPAREN)) {
        lexer.nextToken();
        SQLSelectQuery select = query();
        accept(Token.RPAREN);
        return queryRest(select);
    }
    MySqlSelectQueryBlock queryBlock = new MySqlSelectQueryBlock();
    if (lexer.token() == Token.SELECT) {
        lexer.nextToken();
        if (lexer.token() == Token.HINT) {
            this.exprParser.parseHints(queryBlock.getHints());
        }
        if (lexer.token() == Token.COMMENT) {
            lexer.nextToken();
        }
        if (lexer.token() == (Token.DISTINCT)) {
            queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT);
            lexer.nextToken();
        } else if (identifierEquals("DISTINCTROW")) {
            queryBlock.setDistionOption(SQLSetQuantifier.DISTINCTROW);
            lexer.nextToken();
        } else if (lexer.token() == (Token.ALL)) {
            queryBlock.setDistionOption(SQLSetQuantifier.ALL);
            lexer.nextToken();
        }
        if (identifierEquals("HIGH_PRIORITY")) {
            queryBlock.setHignPriority(true);
            lexer.nextToken();
        }
        if (identifierEquals("STRAIGHT_JOIN")) {
            queryBlock.setStraightJoin(true);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_SMALL_RESULT")) {
            queryBlock.setSmallResult(true);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_BIG_RESULT")) {
            queryBlock.setBigResult(true);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_BUFFER_RESULT")) {
            queryBlock.setBufferResult(true);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_CACHE")) {
            queryBlock.setCache(true);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_NO_CACHE")) {
            queryBlock.setCache(false);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_CALC_FOUND_ROWS")) {
            queryBlock.setCalcFoundRows(true);
            lexer.nextToken();
        }
        parseSelectList(queryBlock);
        parseInto(queryBlock);
    }
    parseFrom(queryBlock);
    parseWhere(queryBlock);
    parseGroupBy(queryBlock);
    queryBlock.setOrderBy(this.exprParser.parseOrderBy());
    if (lexer.token() == Token.LIMIT) {
        queryBlock.setLimit(this.exprParser.parseLimit());
    }
    if (lexer.token() == Token.PROCEDURE) {
        lexer.nextToken();
        throw new ParserException("TODO");
    }
    parseInto(queryBlock);
    if (lexer.token() == Token.FOR) {
        lexer.nextToken();
        accept(Token.UPDATE);
        queryBlock.setForUpdate(true);
        if (identifierEquals("NO_WAIT")) {
            lexer.nextToken();
            queryBlock.setNoWait(true);
        } else if (identifierEquals("WAIT")) {
            lexer.nextToken();
            SQLExpr waitTime = this.exprParser.primary();
            queryBlock.setWaitTime(waitTime);
        }
    }
    if (lexer.token() == Token.LOCK) {
        lexer.nextToken();
        accept(Token.IN);
        acceptIdentifier("SHARE");
        acceptIdentifier("MODE");
        queryBlock.setLockInShareMode(true);
    }
    return queryRest(queryBlock);
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 25 with MySqlSelectQueryBlock

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project druid by alibaba.

the class MySqlSelectTest_11 method test_0.

public void test_0() throws Exception {
    String sql = "select * from users where uid = :uid";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
    SQLSelect select = selectStmt.getSelect();
    Assert.assertNotNull(select.getQuery());
    MySqlSelectQueryBlock queryBlock = (MySqlSelectQueryBlock) select.getQuery();
    Assert.assertNull(queryBlock.getOrderBy());
    //        print(statementList);
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(2, visitor.getColumns().size());
    Assert.assertEquals(1, visitor.getConditions().size());
    Assert.assertEquals(0, visitor.getOrderByColumns().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("users")));
    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(//
    "SELECT *" + //
    "\nFROM users" + //
    "\nWHERE uid = :uid", output);
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)

Aggregations

MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)31 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)23 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)18 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)18 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)15 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)15 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)9 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)8 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)3 PGSelectQueryBlock (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock)3 SQLLimit (com.alibaba.druid.sql.ast.SQLLimit)2 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)2 Limit (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit)2 OdpsSelectQueryBlock (com.alibaba.druid.sql.dialect.odps.ast.OdpsSelectQueryBlock)2 OracleSelectQueryBlock (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock)2 OracleStatementParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)2 SQLServerSelectQueryBlock (com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock)2 ParserException (com.alibaba.druid.sql.parser.ParserException)2 HashMap (java.util.HashMap)2 List (java.util.List)2