Search in sources :

Example 16 with SQLPropertyExpr

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

the class PagerUtils method limitOracle.

private static String limitOracle(SQLSelect select, String dbType, int offset, int count) {
    SQLSelectQuery query = select.getQuery();
    if (query instanceof SQLSelectQueryBlock) {
        OracleSelectQueryBlock queryBlock = (OracleSelectQueryBlock) query;
        if (queryBlock.getGroupBy() == null && select.getOrderBy() == null && offset <= 0) {
            SQLExpr condition = new //
            SQLBinaryOpExpr(//
            new SQLIdentifierExpr("ROWNUM"), //
            SQLBinaryOperator.LessThanOrEqual, //
            new SQLNumberExpr(count), JdbcConstants.ORACLE);
            if (queryBlock.getWhere() == null) {
                queryBlock.setWhere(condition);
            } else {
                queryBlock.setWhere(new //
                SQLBinaryOpExpr(//
                queryBlock.getWhere(), //
                SQLBinaryOperator.BooleanAnd, //
                condition, JdbcConstants.ORACLE));
            }
            return SQLUtils.toSQLString(select, dbType);
        }
    }
    OracleSelectQueryBlock countQueryBlock = new OracleSelectQueryBlock();
    countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLPropertyExpr(new SQLIdentifierExpr("XX"), "*")));
    countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLIdentifierExpr("ROWNUM"), "RN"));
    countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
    countQueryBlock.setWhere(new //
    SQLBinaryOpExpr(//
    new SQLIdentifierExpr("ROWNUM"), //
    SQLBinaryOperator.LessThanOrEqual, //
    new SQLNumberExpr(count + offset), JdbcConstants.ORACLE));
    if (offset <= 0) {
        return SQLUtils.toSQLString(countQueryBlock, dbType);
    }
    OracleSelectQueryBlock offsetQueryBlock = new OracleSelectQueryBlock();
    offsetQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
    offsetQueryBlock.setFrom(new SQLSubqueryTableSource(new SQLSelect(countQueryBlock), "XXX"));
    offsetQueryBlock.setWhere(new //
    SQLBinaryOpExpr(//
    new SQLIdentifierExpr("RN"), //
    SQLBinaryOperator.GreaterThan, //
    new SQLNumberExpr(offset), JdbcConstants.ORACLE));
    return SQLUtils.toSQLString(offsetQueryBlock, dbType);
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) 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) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) OracleSelectQueryBlock(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)

Example 17 with SQLPropertyExpr

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

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

the class MySqlShowKeysStatement 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 19 with SQLPropertyExpr

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

the class SQLServerExprParser method nameRest.

public SQLName nameRest(SQLName expr) {
    if (lexer.token() == Token.DOTDOT) {
        lexer.nextToken();
        boolean backet = false;
        if (lexer.token() == Token.LBRACKET) {
            lexer.nextToken();
            backet = true;
        }
        String text = lexer.stringVal();
        lexer.nextToken();
        if (backet) {
            accept(Token.RBRACKET);
        }
        SQLServerObjectReferenceExpr owner = new SQLServerObjectReferenceExpr(expr);
        expr = new SQLPropertyExpr(owner, text);
    }
    return super.nameRest(expr);
}
Also used : SQLServerObjectReferenceExpr(com.alibaba.druid.sql.dialect.sqlserver.ast.expr.SQLServerObjectReferenceExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)

Example 20 with SQLPropertyExpr

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

the class OracleExprParser method dotRest.

protected SQLExpr dotRest(SQLExpr expr) {
    if (lexer.token() == Token.LITERAL_ALIAS) {
        String name = '"' + lexer.stringVal() + '"';
        lexer.nextToken();
        expr = new SQLPropertyExpr(expr, name);
        if (lexer.token() == Token.DOT) {
            lexer.nextToken();
            expr = dotRest(expr);
        }
        return expr;
    }
    if (identifierEquals("NEXTVAL")) {
        if (expr instanceof SQLIdentifierExpr) {
            SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
            SQLSequenceExpr seqExpr = new SQLSequenceExpr(identExpr, SQLSequenceExpr.Function.NextVal);
            lexer.nextToken();
            return seqExpr;
        }
    } else if (identifierEquals("CURRVAL")) {
        if (expr instanceof SQLIdentifierExpr) {
            SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
            SQLSequenceExpr seqExpr = new SQLSequenceExpr(identExpr, SQLSequenceExpr.Function.CurrVal);
            lexer.nextToken();
            return seqExpr;
        }
    }
    return super.dotRest(expr);
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLSequenceExpr(com.alibaba.druid.sql.ast.expr.SQLSequenceExpr)

Aggregations

SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)27 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)24 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)17 SQLName (com.alibaba.druid.sql.ast.SQLName)6 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)5 SQLObject (com.alibaba.druid.sql.ast.SQLObject)4 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)4 TenantCallBack (com.alibaba.druid.wall.WallConfig.TenantCallBack)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)3 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)3 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)3 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)3 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)3 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)3 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)3 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)3 Column (com.alibaba.druid.stat.TableStat.Column)3 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)2 SQLOver (com.alibaba.druid.sql.ast.SQLOver)2