use of com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop in project druid by alibaba.
the class SQLServerOutputVisitor method visit.
public boolean visit(SQLServerSelectQueryBlock x) {
print0(ucase ? "SELECT " : "select ");
if (SQLSetQuantifier.ALL == x.getDistionOption()) {
print0(ucase ? "ALL " : "all ");
} else if (SQLSetQuantifier.DISTINCT == x.getDistionOption()) {
print0(ucase ? "DISTINCT " : "distinct ");
} else if (SQLSetQuantifier.UNIQUE == x.getDistionOption()) {
print0(ucase ? "UNIQUE " : "unique ");
}
SQLServerTop top = x.getTop();
if (top != null) {
visit(top);
print(' ');
}
printSelectList(x.getSelectList());
SQLExprTableSource into = x.getInto();
if (into != null) {
println();
print0(ucase ? "INTO " : "into ");
printTableSource(into);
}
SQLTableSource from = x.getFrom();
if (from != null) {
println();
print0(ucase ? "FROM " : "from ");
printTableSource(from);
}
SQLExpr where = x.getWhere();
if (where != null) {
println();
print0(ucase ? "WHERE " : "where ");
printExpr(where);
}
SQLSelectGroupByClause groupBy = x.getGroupBy();
if (groupBy != null) {
println();
visit(groupBy);
}
SQLOrderBy orderBy = x.getOrderBy();
if (orderBy != null) {
println();
visit(orderBy);
}
printFetchFirst(x);
return false;
}
use of com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop in project Mycat-Server by MyCATApache.
the class DruidSelectSqlServerParser method parseSqlServerPageSql.
private void parseSqlServerPageSql(SQLStatement stmt, RouteResultset rrs, SQLServerSelectQueryBlock sqlserverSelectQuery, SchemaConfig schema) {
// 第一层子查询
SQLExpr where = sqlserverSelectQuery.getWhere();
SQLTableSource from = sqlserverSelectQuery.getFrom();
if (sqlserverSelectQuery.getTop() != null) {
SQLServerTop top = sqlserverSelectQuery.getTop();
SQLExpr sqlExpr = top.getExpr();
if (sqlExpr instanceof SQLIntegerExpr) {
int topValue = ((SQLIntegerExpr) sqlExpr).getNumber().intValue();
rrs.setLimitStart(0);
rrs.setLimitSize(topValue);
}
} else if (where instanceof SQLBinaryOpExpr && from instanceof SQLSubqueryTableSource) {
SQLBinaryOpExpr one = (SQLBinaryOpExpr) where;
SQLExpr left = one.getLeft();
SQLBinaryOperator operator = one.getOperator();
SQLSelectQuery subSelect = ((SQLSubqueryTableSource) from).getSelect().getQuery();
SQLOrderBy orderBy = null;
if (subSelect instanceof SQLServerSelectQueryBlock) {
boolean hasRowNumber = false;
boolean hasSubTop = false;
int subTop = 0;
SQLServerSelectQueryBlock subSelectOracle = (SQLServerSelectQueryBlock) subSelect;
List<SQLSelectItem> sqlSelectItems = subSelectOracle.getSelectList();
for (SQLSelectItem sqlSelectItem : sqlSelectItems) {
SQLExpr sqlExpr = sqlSelectItem.getExpr();
if (sqlExpr instanceof SQLAggregateExpr) {
SQLAggregateExpr agg = (SQLAggregateExpr) sqlExpr;
if ("row_number".equalsIgnoreCase(agg.getMethodName()) && agg.getOver() != null) {
hasRowNumber = true;
orderBy = agg.getOver().getOrderBy();
}
}
}
if (subSelectOracle.getFrom() instanceof SQLSubqueryTableSource) {
SQLSubqueryTableSource subFrom = (SQLSubqueryTableSource) subSelectOracle.getFrom();
if (subFrom.getSelect().getQuery() instanceof SQLServerSelectQueryBlock) {
SQLServerSelectQueryBlock sqlSelectQuery = (SQLServerSelectQueryBlock) subFrom.getSelect().getQuery();
if (sqlSelectQuery.getTop() != null) {
SQLExpr sqlExpr = sqlSelectQuery.getTop().getExpr();
if (sqlExpr instanceof SQLIntegerExpr) {
hasSubTop = true;
subTop = ((SQLIntegerExpr) sqlExpr).getNumber().intValue();
orderBy = subFrom.getSelect().getOrderBy();
}
}
}
}
if (hasRowNumber) {
if (hasSubTop && (operator == SQLBinaryOperator.GreaterThan || operator == SQLBinaryOperator.GreaterThanOrEqual) && one.getRight() instanceof SQLIntegerExpr) {
SQLIntegerExpr right = (SQLIntegerExpr) one.getRight();
int firstrownum = right.getNumber().intValue();
if (operator == SQLBinaryOperator.GreaterThanOrEqual && firstrownum != 0) {
firstrownum = firstrownum - 1;
}
int lastrownum = subTop;
setLimitIFChange(stmt, rrs, schema, one, firstrownum, lastrownum);
if (orderBy != null) {
SQLServerSelect oracleSelect = (SQLServerSelect) subSelect.getParent();
oracleSelect.setOrderBy(orderBy);
}
parseOrderAggGroupSqlServer(schema, stmt, rrs, (SQLServerSelectQueryBlock) subSelect);
isNeedParseOrderAgg = false;
} else if ((operator == SQLBinaryOperator.LessThan || operator == SQLBinaryOperator.LessThanOrEqual) && one.getRight() instanceof SQLIntegerExpr) {
SQLIntegerExpr right = (SQLIntegerExpr) one.getRight();
int firstrownum = right.getNumber().intValue();
if (operator == SQLBinaryOperator.LessThan && firstrownum != 0) {
firstrownum = firstrownum - 1;
}
if (subSelect instanceof SQLServerSelectQueryBlock) {
rrs.setLimitStart(0);
rrs.setLimitSize(firstrownum);
// 为了继续解出order by 等
sqlserverSelectQuery = (SQLServerSelectQueryBlock) subSelect;
if (orderBy != null) {
SQLServerSelect oracleSelect = (SQLServerSelect) subSelect.getParent();
oracleSelect.setOrderBy(orderBy);
}
parseOrderAggGroupSqlServer(schema, stmt, rrs, sqlserverSelectQuery);
isNeedParseOrderAgg = false;
}
} else if (operator == SQLBinaryOperator.BooleanAnd && left instanceof SQLBinaryOpExpr && one.getRight() instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr leftE = (SQLBinaryOpExpr) left;
SQLBinaryOpExpr rightE = (SQLBinaryOpExpr) one.getRight();
SQLBinaryOpExpr small = null;
SQLBinaryOpExpr larger = null;
int firstrownum = 0;
int lastrownum = 0;
if (leftE.getRight() instanceof SQLIntegerExpr && (leftE.getOperator() == SQLBinaryOperator.GreaterThan || leftE.getOperator() == SQLBinaryOperator.GreaterThanOrEqual)) {
small = leftE;
firstrownum = ((SQLIntegerExpr) leftE.getRight()).getNumber().intValue();
if (leftE.getOperator() == SQLBinaryOperator.GreaterThanOrEqual && firstrownum != 0) {
firstrownum = firstrownum - 1;
}
} else if (leftE.getRight() instanceof SQLIntegerExpr && (leftE.getOperator() == SQLBinaryOperator.LessThan || leftE.getOperator() == SQLBinaryOperator.LessThanOrEqual)) {
larger = leftE;
lastrownum = ((SQLIntegerExpr) leftE.getRight()).getNumber().intValue();
if (leftE.getOperator() == SQLBinaryOperator.LessThan && lastrownum != 0) {
lastrownum = lastrownum - 1;
}
}
if (rightE.getRight() instanceof SQLIntegerExpr && (rightE.getOperator() == SQLBinaryOperator.GreaterThan || rightE.getOperator() == SQLBinaryOperator.GreaterThanOrEqual)) {
small = rightE;
firstrownum = ((SQLIntegerExpr) rightE.getRight()).getNumber().intValue();
if (rightE.getOperator() == SQLBinaryOperator.GreaterThanOrEqual && firstrownum != 0) {
firstrownum = firstrownum - 1;
}
} else if (rightE.getRight() instanceof SQLIntegerExpr && (rightE.getOperator() == SQLBinaryOperator.LessThan || rightE.getOperator() == SQLBinaryOperator.LessThanOrEqual)) {
larger = rightE;
lastrownum = ((SQLIntegerExpr) rightE.getRight()).getNumber().intValue();
if (rightE.getOperator() == SQLBinaryOperator.LessThan && lastrownum != 0) {
lastrownum = lastrownum - 1;
}
}
if (small != null && larger != null) {
setLimitIFChange(stmt, rrs, schema, small, firstrownum, lastrownum);
if (orderBy != null) {
SQLServerSelect oracleSelect = (SQLServerSelect) subSelect.getParent();
oracleSelect.setOrderBy(orderBy);
}
parseOrderAggGroupSqlServer(schema, stmt, rrs, (SQLServerSelectQueryBlock) subSelect);
isNeedParseOrderAgg = false;
}
}
}
}
}
}
Aggregations