Search in sources :

Example 36 with MySqlSelectQueryBlock

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

the class MySqlSelectTest_10 method test_0.

public void test_0() throws Exception {
    String sql = "SELECT * FROM t_department  WHERE name IN ('0000','4444') ORDER BY name ASC";
    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.assertNotNull(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(1, visitor.getOrderByColumns().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_department")));
}
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)

Example 37 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 38 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 39 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 40 with MySqlSelectQueryBlock

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

the class ShareRowOutPutDataHandler method route.

public void route(SystemConfig sysConfig, SchemaConfig schema, int sqlType, String realSQL, String charset, ServerConnection sc, LayerCachePool cachePool) {
    int rs = ServerParse.parse(realSQL);
    this.sqltype = rs & 0xff;
    this.sysConfig = sysConfig;
    this.schema = schema;
    this.charset = charset;
    this.sc = sc;
    this.cachePool = cachePool;
    try {
        // RouteStrategy routes=RouteStrategyFactory.getRouteStrategy();
        // rrs =RouteStrategyFactory.getRouteStrategy().route(sysConfig, schema, sqlType2, realSQL,charset, sc, cachePool);
        MySqlStatementParser parser = new MySqlStatementParser(realSQL);
        SQLStatement statement = parser.parseStatement();
        if (statement instanceof SQLSelectStatement) {
            SQLSelectStatement st = (SQLSelectStatement) statement;
            SQLSelectQuery sqlSelectQuery = st.getSelect().getQuery();
            if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
                MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock) st.getSelect().getQuery();
                joinParser = new JoinParser(mysqlSelectQuery, realSQL);
                joinParser.parser();
            }
        }
    /*	
		   if (routes instanceof DruidMysqlRouteStrategy) {
			   SQLSelectStatement st=((DruidMysqlRouteStrategy) routes).getSQLStatement();
			   SQLSelectQuery sqlSelectQuery =st.getSelect().getQuery();
				if(sqlSelectQuery instanceof MySqlSelectQueryBlock) {
					MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock)st.getSelect().getQuery();
					joinParser=new JoinParser(mysqlSelectQuery,realSQL);
					joinParser.parser();
				}
		   }
		   */
    } catch (Exception e) {
    }
}
Also used : SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) 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)82 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)41 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)34 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)31 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)28 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)25 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)23 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)19 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)13 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)11 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)10 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)9 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)8 SQLQueryExpr (com.alibaba.druid.sql.ast.expr.SQLQueryExpr)8 HashMap (java.util.HashMap)8 Item (com.actiontech.dble.plan.common.item.Item)7 SQLNonTransientException (java.sql.SQLNonTransientException)7 Test (org.junit.Test)7 SQLInSubQueryExpr (com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr)6 SQLNullExpr (com.alibaba.druid.sql.ast.expr.SQLNullExpr)6