Search in sources :

Example 26 with SQLBinaryOpExpr

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

the class PagerUtils method limitSQLServer.

private static String limitSQLServer(SQLSelect select, String dbType, int offset, int count) {
    SQLSelectQuery query = select.getQuery();
    SQLBinaryOpExpr gt = new //
    SQLBinaryOpExpr(//
    new SQLIdentifierExpr("ROWNUM"), //
    SQLBinaryOperator.GreaterThan, //
    new SQLNumberExpr(offset), JdbcConstants.SQL_SERVER);
    SQLBinaryOpExpr lteq = new //
    SQLBinaryOpExpr(//
    new SQLIdentifierExpr("ROWNUM"), //
    SQLBinaryOperator.LessThanOrEqual, //
    new SQLNumberExpr(count + offset), JdbcConstants.SQL_SERVER);
    SQLBinaryOpExpr pageCondition = new SQLBinaryOpExpr(gt, SQLBinaryOperator.BooleanAnd, lteq, JdbcConstants.SQL_SERVER);
    if (query instanceof SQLSelectQueryBlock) {
        SQLServerSelectQueryBlock queryBlock = (SQLServerSelectQueryBlock) query;
        if (offset <= 0) {
            queryBlock.setTop(new SQLServerTop(new SQLNumberExpr(count)));
            return SQLUtils.toSQLString(select, dbType);
        }
        SQLAggregateExpr aggregateExpr = new SQLAggregateExpr("ROW_NUMBER");
        SQLOrderBy orderBy = select.getOrderBy();
        aggregateExpr.setOver(new SQLOver(orderBy));
        select.setOrderBy(null);
        queryBlock.getSelectList().add(new SQLSelectItem(aggregateExpr, "ROWNUM"));
        SQLServerSelectQueryBlock countQueryBlock = new SQLServerSelectQueryBlock();
        countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
        countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
        countQueryBlock.setWhere(pageCondition);
        return SQLUtils.toSQLString(countQueryBlock, dbType);
    }
    SQLServerSelectQueryBlock countQueryBlock = new SQLServerSelectQueryBlock();
    countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLPropertyExpr(new SQLIdentifierExpr("XX"), "*")));
    countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
    if (offset <= 0) {
        countQueryBlock.setTop(new SQLServerTop(new SQLNumberExpr(count)));
        return SQLUtils.toSQLString(countQueryBlock, dbType);
    }
    SQLAggregateExpr aggregateExpr = new SQLAggregateExpr("ROW_NUMBER");
    SQLOrderBy orderBy = select.getOrderBy();
    aggregateExpr.setOver(new SQLOver(orderBy));
    select.setOrderBy(null);
    countQueryBlock.getSelectList().add(new SQLSelectItem(aggregateExpr, "ROWNUM"));
    SQLServerSelectQueryBlock offsetQueryBlock = new SQLServerSelectQueryBlock();
    offsetQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
    offsetQueryBlock.setFrom(new SQLSubqueryTableSource(new SQLSelect(countQueryBlock), "XXX"));
    offsetQueryBlock.setWhere(pageCondition);
    return SQLUtils.toSQLString(offsetQueryBlock, dbType);
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) SQLOver(com.alibaba.druid.sql.ast.SQLOver) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) SQLServerTop(com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop) SQLServerSelectQueryBlock(com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)

Example 27 with SQLBinaryOpExpr

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

the class MySqlExprParser method relationalRest.

public SQLExpr relationalRest(SQLExpr expr) {
    if (identifierEquals("REGEXP")) {
        lexer.nextToken();
        SQLExpr rightExp = equality();
        rightExp = relationalRest(rightExp);
        return new SQLBinaryOpExpr(expr, SQLBinaryOperator.RegExp, rightExp, JdbcConstants.MYSQL);
    }
    return super.relationalRest(expr);
}
Also used : SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 28 with SQLBinaryOpExpr

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

the class MySqlExprParser method orRest.

public SQLExpr orRest(SQLExpr expr) {
    for (; ; ) {
        if (lexer.token() == Token.OR || lexer.token() == Token.BARBAR) {
            lexer.nextToken();
            SQLExpr rightExp = and();
            expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanOr, rightExp, JdbcConstants.MYSQL);
        } else if (lexer.token() == Token.XOR) {
            lexer.nextToken();
            SQLExpr rightExp = and();
            expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanXor, rightExp, JdbcConstants.MYSQL);
        } else {
            break;
        }
    }
    return expr;
}
Also used : SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 29 with SQLBinaryOpExpr

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

the class MySqlExprParser method multiplicativeRest.

public SQLExpr multiplicativeRest(SQLExpr expr) {
    final Token token = lexer.token();
    if (token == Token.IDENTIFIER && "MOD".equalsIgnoreCase(lexer.stringVal())) {
        lexer.nextToken();
        SQLExpr rightExp = primary();
        rightExp = relationalRest(rightExp);
        return new SQLBinaryOpExpr(expr, SQLBinaryOperator.Modulus, rightExp, JdbcConstants.MYSQL);
    } else if (token == Token.DIV) {
        lexer.nextToken();
        SQLExpr rightExp = bitXor();
        expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.DIV, rightExp, getDbType());
        expr = multiplicativeRest(expr);
    }
    return super.multiplicativeRest(expr);
}
Also used : Token(com.alibaba.druid.sql.parser.Token) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 30 with SQLBinaryOpExpr

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

the class SQLUtils method split.

public static List<SQLExpr> split(SQLBinaryOpExpr x) {
    List<SQLExpr> groupList = new ArrayList<SQLExpr>();
    groupList.add(x.getRight());
    SQLExpr left = x.getLeft();
    for (; ; ) {
        if (left instanceof SQLBinaryOpExpr && ((SQLBinaryOpExpr) left).getOperator() == x.getOperator()) {
            SQLBinaryOpExpr binaryLeft = (SQLBinaryOpExpr) left;
            groupList.add(binaryLeft.getRight());
            left = binaryLeft.getLeft();
        } else {
            groupList.add(left);
            break;
        }
    }
    return groupList;
}
Also used : ArrayList(java.util.ArrayList) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)46 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)30 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)10 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)9 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)8 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)8 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)7 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)7 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)6 SQLObject (com.alibaba.druid.sql.ast.SQLObject)5 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)5 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)5 SQLBinaryOperator (com.alibaba.druid.sql.ast.expr.SQLBinaryOperator)4 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)4 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)4 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)4 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)4 ParserException (com.alibaba.druid.sql.parser.ParserException)4 SQLOver (com.alibaba.druid.sql.ast.SQLOver)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3