use of com.alibaba.druid.sql.ast.statement.SQLSelectItem in project Mycat_plus by coderczp.
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;
}
}
}
}
}
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectItem in project Mycat_plus by coderczp.
the class MycatSchemaStatVisitor method visit.
/*
* 遇到 some 将子查询改写成 SELECT MIN(name) FROM subtest1
* 例如:
* select * from subtest where id > some (select name from subtest1);
* >/>= some ----> >/>= min
* </<= some ----> </<= max
* <> some ----> not in
* = some ----> in
* other 不改写
*/
@Override
public boolean visit(SQLSomeExpr x) {
setSubQueryRelationOrFlag(x);
List<SQLSelectItem> itemlist = ((SQLSelectQueryBlock) (x.getSubQuery().getQuery())).getSelectList();
SQLExpr sexpr = itemlist.get(0).getExpr();
if (x.getParent() instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr parentExpr = (SQLBinaryOpExpr) x.getParent();
SQLAggregateExpr saexpr = null;
switch(parentExpr.getOperator()) {
case GreaterThan:
case GreaterThanOrEqual:
case NotLessThan:
this.hasChange = true;
if (sexpr instanceof SQLIdentifierExpr || (sexpr instanceof SQLPropertyExpr && ((SQLPropertyExpr) sexpr).getOwner() instanceof SQLIdentifierExpr)) {
saexpr = new SQLAggregateExpr("MIN");
saexpr.getArguments().add(sexpr);
saexpr.setParent(itemlist.get(0));
itemlist.get(0).setExpr(saexpr);
}
SQLQueryExpr maxSubQuery = new SQLQueryExpr(x.getSubQuery());
x.getSubQuery().setParent(maxSubQuery);
// 生成新的SQLQueryExpr 替换当前 SQLAllExpr 节点
if (x.getParent() instanceof SQLBinaryOpExpr) {
if (((SQLBinaryOpExpr) x.getParent()).getLeft().equals(x)) {
((SQLBinaryOpExpr) x.getParent()).setLeft(maxSubQuery);
} else if (((SQLBinaryOpExpr) x.getParent()).getRight().equals(x)) {
((SQLBinaryOpExpr) x.getParent()).setRight(maxSubQuery);
}
}
addSubQuerys(x.getSubQuery());
return super.visit(x.getSubQuery());
case LessThan:
case LessThanOrEqual:
case NotGreaterThan:
this.hasChange = true;
if (sexpr instanceof SQLIdentifierExpr || (sexpr instanceof SQLPropertyExpr && ((SQLPropertyExpr) sexpr).getOwner() instanceof SQLIdentifierExpr)) {
saexpr = new SQLAggregateExpr("MAX");
saexpr.getArguments().add(sexpr);
saexpr.setParent(itemlist.get(0));
itemlist.get(0).setExpr(saexpr);
}
// 生成新的SQLQueryExpr 替换当前 SQLAllExpr 节点
SQLQueryExpr minSubQuery = new SQLQueryExpr(x.getSubQuery());
x.getSubQuery().setParent(minSubQuery);
if (x.getParent() instanceof SQLBinaryOpExpr) {
if (((SQLBinaryOpExpr) x.getParent()).getLeft().equals(x)) {
((SQLBinaryOpExpr) x.getParent()).setLeft(minSubQuery);
} else if (((SQLBinaryOpExpr) x.getParent()).getRight().equals(x)) {
((SQLBinaryOpExpr) x.getParent()).setRight(minSubQuery);
}
}
addSubQuerys(x.getSubQuery());
return super.visit(x.getSubQuery());
case LessThanOrGreater:
case NotEqual:
this.hasChange = true;
SQLInSubQueryExpr notInSubQueryExpr = new SQLInSubQueryExpr(x.getSubQuery());
x.getSubQuery().setParent(notInSubQueryExpr);
notInSubQueryExpr.setNot(true);
// 生成新的SQLQueryExpr 替换当前 SQLAllExpr 节点
if (x.getParent() instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr xp = (SQLBinaryOpExpr) x.getParent();
if (xp.getLeft().equals(x)) {
notInSubQueryExpr.setExpr(xp.getRight());
} else if (xp.getRight().equals(x)) {
notInSubQueryExpr.setExpr(xp.getLeft());
}
if (xp.getParent() instanceof MySqlSelectQueryBlock) {
((MySqlSelectQueryBlock) xp.getParent()).setWhere(notInSubQueryExpr);
} else if (xp.getParent() instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr pp = ((SQLBinaryOpExpr) xp.getParent());
if (pp.getLeft().equals(xp)) {
pp.setLeft(notInSubQueryExpr);
} else if (pp.getRight().equals(xp)) {
pp.setRight(notInSubQueryExpr);
}
}
}
addSubQuerys(x.getSubQuery());
return super.visit(notInSubQueryExpr);
case Equality:
this.hasChange = true;
SQLInSubQueryExpr inSubQueryExpr = new SQLInSubQueryExpr(x.getSubQuery());
x.getSubQuery().setParent(inSubQueryExpr);
inSubQueryExpr.setNot(false);
// 生成新的SQLQueryExpr 替换当前 SQLAllExpr 节点
if (x.getParent() instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr xp = (SQLBinaryOpExpr) x.getParent();
if (xp.getLeft().equals(x)) {
inSubQueryExpr.setExpr(xp.getRight());
} else if (xp.getRight().equals(x)) {
inSubQueryExpr.setExpr(xp.getLeft());
}
if (xp.getParent() instanceof MySqlSelectQueryBlock) {
((MySqlSelectQueryBlock) xp.getParent()).setWhere(inSubQueryExpr);
} else if (xp.getParent() instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr pp = ((SQLBinaryOpExpr) xp.getParent());
if (pp.getLeft().equals(xp)) {
pp.setLeft(inSubQueryExpr);
} else if (pp.getRight().equals(xp)) {
pp.setRight(inSubQueryExpr);
}
}
}
addSubQuerys(x.getSubQuery());
return super.visit(inSubQueryExpr);
default:
break;
}
}
addSubQuerys(x.getSubQuery());
return super.visit(x);
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectItem in project Mycat_plus by coderczp.
the class MycatSchemaStatVisitor method visit.
/*
* 遇到 any 将子查询改写成 SELECT MIN(name) FROM subtest1
* 例如:
* select * from subtest where id oper any (select name from subtest1);
* >/>= any ----> >/>= min
* </<= any ----> </<= max
* <> any ----> not in
* = some ----> in
* other 不改写
*/
@Override
public boolean visit(SQLAnyExpr x) {
setSubQueryRelationOrFlag(x);
List<SQLSelectItem> itemlist = ((SQLSelectQueryBlock) (x.getSubQuery().getQuery())).getSelectList();
SQLExpr sexpr = itemlist.get(0).getExpr();
if (x.getParent() instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr parentExpr = (SQLBinaryOpExpr) x.getParent();
SQLAggregateExpr saexpr = null;
switch(parentExpr.getOperator()) {
case GreaterThan:
case GreaterThanOrEqual:
case NotLessThan:
this.hasChange = true;
if (sexpr instanceof SQLIdentifierExpr || (sexpr instanceof SQLPropertyExpr && ((SQLPropertyExpr) sexpr).getOwner() instanceof SQLIdentifierExpr)) {
saexpr = new SQLAggregateExpr("MIN");
saexpr.getArguments().add(sexpr);
saexpr.setParent(itemlist.get(0));
itemlist.get(0).setExpr(saexpr);
}
SQLQueryExpr maxSubQuery = new SQLQueryExpr(x.getSubQuery());
x.getSubQuery().setParent(maxSubQuery);
// 生成新的SQLQueryExpr 替换当前 SQLAllExpr 节点
if (x.getParent() instanceof SQLBinaryOpExpr) {
if (((SQLBinaryOpExpr) x.getParent()).getLeft().equals(x)) {
((SQLBinaryOpExpr) x.getParent()).setLeft(maxSubQuery);
} else if (((SQLBinaryOpExpr) x.getParent()).getRight().equals(x)) {
((SQLBinaryOpExpr) x.getParent()).setRight(maxSubQuery);
}
}
addSubQuerys(x.getSubQuery());
return super.visit(x.getSubQuery());
case LessThan:
case LessThanOrEqual:
case NotGreaterThan:
this.hasChange = true;
if (sexpr instanceof SQLIdentifierExpr || (sexpr instanceof SQLPropertyExpr && ((SQLPropertyExpr) sexpr).getOwner() instanceof SQLIdentifierExpr)) {
saexpr = new SQLAggregateExpr("MAX");
saexpr.getArguments().add(sexpr);
saexpr.setParent(itemlist.get(0));
itemlist.get(0).setExpr(saexpr);
}
// 生成新的SQLQueryExpr 替换当前 SQLAllExpr 节点
SQLQueryExpr minSubQuery = new SQLQueryExpr(x.getSubQuery());
x.subQuery.setParent(minSubQuery);
if (x.getParent() instanceof SQLBinaryOpExpr) {
if (((SQLBinaryOpExpr) x.getParent()).getLeft().equals(x)) {
((SQLBinaryOpExpr) x.getParent()).setLeft(minSubQuery);
} else if (((SQLBinaryOpExpr) x.getParent()).getRight().equals(x)) {
((SQLBinaryOpExpr) x.getParent()).setRight(minSubQuery);
}
}
addSubQuerys(x.getSubQuery());
return super.visit(x.getSubQuery());
case LessThanOrGreater:
case NotEqual:
this.hasChange = true;
SQLInSubQueryExpr notInSubQueryExpr = new SQLInSubQueryExpr(x.getSubQuery());
x.getSubQuery().setParent(notInSubQueryExpr);
notInSubQueryExpr.setNot(true);
// 生成新的SQLQueryExpr 替换当前 SQLAllExpr 节点
if (x.getParent() instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr xp = (SQLBinaryOpExpr) x.getParent();
if (xp.getLeft().equals(x)) {
notInSubQueryExpr.setExpr(xp.getRight());
} else if (xp.getRight().equals(x)) {
notInSubQueryExpr.setExpr(xp.getLeft());
}
if (xp.getParent() instanceof MySqlSelectQueryBlock) {
((MySqlSelectQueryBlock) xp.getParent()).setWhere(notInSubQueryExpr);
} else if (xp.getParent() instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr pp = ((SQLBinaryOpExpr) xp.getParent());
if (pp.getLeft().equals(xp)) {
pp.setLeft(notInSubQueryExpr);
} else if (pp.getRight().equals(xp)) {
pp.setRight(notInSubQueryExpr);
}
}
}
addSubQuerys(x.getSubQuery());
return super.visit(notInSubQueryExpr);
case Equality:
this.hasChange = true;
SQLInSubQueryExpr inSubQueryExpr = new SQLInSubQueryExpr(x.getSubQuery());
x.getSubQuery().setParent(inSubQueryExpr);
inSubQueryExpr.setNot(false);
// 生成新的SQLQueryExpr 替换当前 SQLAllExpr 节点
if (x.getParent() instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr xp = (SQLBinaryOpExpr) x.getParent();
if (xp.getLeft().equals(x)) {
inSubQueryExpr.setExpr(xp.getRight());
} else if (xp.getRight().equals(x)) {
inSubQueryExpr.setExpr(xp.getLeft());
}
if (xp.getParent() instanceof MySqlSelectQueryBlock) {
((MySqlSelectQueryBlock) xp.getParent()).setWhere(inSubQueryExpr);
} else if (xp.getParent() instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr pp = ((SQLBinaryOpExpr) xp.getParent());
if (pp.getLeft().equals(xp)) {
pp.setLeft(inSubQueryExpr);
} else if (pp.getRight().equals(xp)) {
pp.setRight(inSubQueryExpr);
}
}
}
addSubQuerys(x.getSubQuery());
return super.visit(inSubQueryExpr);
default:
break;
}
}
addSubQuerys(x.getSubQuery());
return super.visit(x);
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectItem in project Mycat_plus by coderczp.
the class SQLQueryResultHandler method dohandler.
@Override
public String dohandler(SQLStatement statement, SQLSelect sqlselect, SQLObject parent, List param) {
if (parent.getParent() instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr pp = (SQLBinaryOpExpr) parent.getParent();
SQLExprImpl listExpr = null;
if (null == param || param.isEmpty()) {
listExpr = new SQLNullExpr();
} else {
listExpr = new SQLListExpr();
((SQLListExpr) listExpr).getItems().addAll(param);
}
if (pp.getLeft().equals(parent)) {
pp.setLeft(listExpr);
} else if (pp.getRight().equals(parent)) {
pp.setRight(listExpr);
}
} else if (parent.getParent() instanceof SQLSelectItem) {
SQLSelectItem pp = (SQLSelectItem) parent.getParent();
SQLExprImpl listExpr = null;
if (null == param || param.isEmpty()) {
listExpr = new SQLNullExpr();
} else {
listExpr = new SQLListExpr();
((SQLListExpr) listExpr).getItems().addAll(param);
}
pp.setExpr(listExpr);
} else if (parent.getParent() instanceof SQLSelectGroupByClause) {
SQLSelectGroupByClause pp = (SQLSelectGroupByClause) parent.getParent();
List<SQLExpr> items = pp.getItems();
for (int i = 0; i < items.size(); i++) {
SQLExpr expr = items.get(i);
if (expr instanceof SQLQueryExpr && ((SQLQueryExpr) expr).getSubQuery().equals(sqlselect)) {
SQLExprImpl listExpr = null;
if (null == param || param.isEmpty()) {
listExpr = new SQLNullExpr();
} else {
listExpr = new SQLListExpr();
((SQLListExpr) listExpr).getItems().addAll(param);
}
items.set(i, listExpr);
}
}
} else if (parent.getParent() instanceof SQLSelectOrderByItem) {
SQLSelectOrderByItem orderItem = (SQLSelectOrderByItem) parent.getParent();
SQLExprImpl listExpr = null;
if (null == param || param.isEmpty()) {
listExpr = new SQLNullExpr();
} else {
listExpr = new SQLListExpr();
((SQLListExpr) listExpr).getItems().addAll(param);
}
listExpr.setParent(orderItem);
orderItem.setExpr(listExpr);
} else if (parent.getParent() instanceof MySqlSelectQueryBlock) {
MySqlSelectQueryBlock query = (MySqlSelectQueryBlock) parent.getParent();
// select * from subtest1 a where (select 1 from subtest3); 这种情况会进入到当前分支.
// 改写为 select * from subtest1 a where (1); 或 select * from subtest1 a where (null);
SQLExprImpl listExpr = null;
if (null == param || param.isEmpty()) {
listExpr = new SQLNullExpr();
} else {
listExpr = new SQLListExpr();
((SQLListExpr) listExpr).getItems().addAll(param);
}
listExpr.setParent(query);
query.setWhere(listExpr);
}
return statement.toString();
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectItem in project dble by actiontech.
the class SelectHandler method isSupportSelect.
private static boolean isSupportSelect(String stmt) {
SQLStatementParser parser = new MySqlStatementParser(stmt);
SQLStatement statement = parser.parseStatement();
if (!(statement instanceof SQLSelectStatement)) {
return false;
}
SQLSelectQuery sqlSelectQuery = ((SQLSelectStatement) statement).getSelect().getQuery();
if (!(sqlSelectQuery instanceof MySqlSelectQueryBlock)) {
return false;
}
MySqlSelectQueryBlock selectQueryBlock = (MySqlSelectQueryBlock) sqlSelectQuery;
SQLTableSource mysqlFrom = selectQueryBlock.getFrom();
if (mysqlFrom != null) {
return false;
}
for (SQLSelectItem item : selectQueryBlock.getSelectList()) {
SQLExpr selectItem = item.getExpr();
if (!isVariantRef(selectItem)) {
return false;
}
}
return true;
}
Aggregations