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);
}
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);
}
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;
}
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);
}
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);
}
Aggregations