use of com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr in project Mycat_plus by coderczp.
the class BinaryOpResultHandler method dohandler.
@Override
public String dohandler(SQLStatement statement, SQLSelect sqlselect, SQLObject parent, List param) {
SQLBinaryOpExpr pp = (SQLBinaryOpExpr) parent;
if (pp.getLeft() instanceof SQLQueryExpr) {
SQLQueryExpr left = (SQLQueryExpr) pp.getLeft();
if (left.getSubQuery().equals(sqlselect)) {
SQLExprImpl listExpr = null;
if (null == param || param.isEmpty()) {
listExpr = new SQLNullExpr();
} else {
listExpr = new SQLListExpr();
listExpr.setParent(left.getParent());
((SQLListExpr) listExpr).getItems().addAll(param);
}
pp.setLeft(listExpr);
}
} else if (pp.getRight() instanceof SQLQueryExpr) {
SQLQueryExpr right = (SQLQueryExpr) pp.getRight();
if (right.getSubQuery().equals(sqlselect)) {
SQLExprImpl listExpr = null;
if (null == param || param.isEmpty()) {
listExpr = new SQLNullExpr();
} else {
listExpr = new SQLListExpr();
listExpr.setParent(right.getParent());
((SQLListExpr) listExpr).getItems().addAll(param);
}
pp.setRight(listExpr);
}
} else if (pp.getLeft() instanceof SQLInSubQueryExpr) {
SQLInSubQueryExpr left = (SQLInSubQueryExpr) pp.getLeft();
if (left.getSubQuery().equals(sqlselect)) {
SQLExprImpl inlistExpr = null;
if (null == param || param.isEmpty()) {
inlistExpr = new SQLNullExpr();
} else {
inlistExpr = new SQLInListExpr();
((SQLInListExpr) inlistExpr).setTargetList(param);
((SQLInListExpr) inlistExpr).setExpr(pp.getRight());
((SQLInListExpr) inlistExpr).setNot(left.isNot());
((SQLInListExpr) inlistExpr).setParent(left.getParent());
}
pp.setLeft(inlistExpr);
}
} else if (pp.getRight() instanceof SQLInSubQueryExpr) {
SQLInSubQueryExpr right = (SQLInSubQueryExpr) pp.getRight();
if (right.getSubQuery().equals(sqlselect)) {
SQLExprImpl listExpr = null;
if (null == param || param.isEmpty()) {
listExpr = new SQLNullExpr();
} else {
listExpr = new SQLListExpr();
((SQLListExpr) listExpr).getItems().addAll(param);
}
pp.setRight(listExpr);
}
}
return statement.toString();
}
use of com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr in project Mycat_plus by coderczp.
the class MycatSchemaStatVisitor method visit.
/*
* 遇到 all 将子查询改写成 SELECT MAX(name) FROM subtest1
* 例如:
* select * from subtest where id > all (select name from subtest1);
* >/>= all ----> >/>= max
* </<= all ----> </<= min
* <> all ----> not in
* = all ----> id = 1 and id = 2
* other 不改写
*/
@Override
public boolean visit(SQLAllExpr 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("MAX");
saexpr.getArguments().add(sexpr);
saexpr.setParent(itemlist.get(0));
itemlist.get(0).setExpr(saexpr);
}
SQLQueryExpr maxSubQuery = new SQLQueryExpr(x.getSubQuery());
x.getSubQuery().setParent(x.getParent());
// 生成新的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("MIN");
saexpr.getArguments().add(sexpr);
saexpr.setParent(itemlist.get(0));
itemlist.get(0).setExpr(saexpr);
x.subQuery.setParent(x.getParent());
}
// 生成新的SQLQueryExpr 替换当前 SQLAllExpr 节点
SQLQueryExpr minSubQuery = new SQLQueryExpr(x.getSubQuery());
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);
default:
break;
}
}
addSubQuerys(x.getSubQuery());
return super.visit(x);
}
use of com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr in project Mycat_plus by coderczp.
the class GlobalTableUtil method convertUpdateSQL.
/**
* UPDATE [LOW_PRIORITY] [IGNORE] table_reference
* SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
* [WHERE where_condition]
* [ORDER BY ...]
* [LIMIT row_count]
*
* Multiple-table syntax:
*
* UPDATE [LOW_PRIORITY] [IGNORE] table_references
* SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
* [WHERE where_condition]
*
* update user, tuser set user.name='dddd',tuser.pwd='aaa'
* where user.id=2 and tuser.id=0;
* @param sql update tuser set pwd='aaa', name='digdee' where id=0;
* @return
*/
public static String convertUpdateSQL(String sql) {
try {
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement stmt = parser.parseStatement();
MySqlUpdateStatement update = (MySqlUpdateStatement) stmt;
SQLTableSource ts = update.getTableSource();
if (ts != null && ts.toString().contains(",")) {
System.out.println(ts.toString());
LOGGER.warn("Do not support Multiple-table udpate syntax...");
return sql;
}
String tableName = StringUtil.removeBackquote(update.getTableName().getSimpleName());
if (!isGlobalTable(tableName))
return sql;
if (!isInnerColExist(tableName))
// 没有内部列
return sql;
StringBuilder sb = new StringBuilder(150);
SQLExpr se = update.getWhere();
// where中有子查询: update company set name='com' where id in (select id from xxx where ...)
if (se instanceof SQLInSubQueryExpr) {
// return sql;
int idx = sql.toUpperCase().indexOf(" SET ") + 5;
sb.append(sql.substring(0, idx)).append(GLOBAL_TABLE_MYCAT_COLUMN).append("=").append(operationTimestamp).append(",").append(sql.substring(idx));
return sb.toString();
}
String where = null;
if (update.getWhere() != null)
where = update.getWhere().toString();
SQLOrderBy orderBy = update.getOrderBy();
Limit limit = update.getLimit();
sb.append("update ").append(tableName).append(" set ");
List<SQLUpdateSetItem> items = update.getItems();
boolean flag = false;
for (int i = 0; i < items.size(); i++) {
SQLUpdateSetItem item = items.get(i);
String col = item.getColumn().toString();
String val = item.getValue().toString();
if (StringUtil.removeBackquote(col).equalsIgnoreCase(GLOBAL_TABLE_MYCAT_COLUMN)) {
flag = true;
sb.append(col).append("=");
if (i != items.size() - 1)
sb.append(operationTimestamp).append(",");
else
sb.append(operationTimestamp);
} else {
sb.append(col).append("=");
if (i != items.size() - 1)
sb.append(val).append(",");
else
sb.append(val);
}
}
if (!flag) {
sb.append(",").append(GLOBAL_TABLE_MYCAT_COLUMN).append("=").append(operationTimestamp);
}
sb.append(" where ").append(where);
if (orderBy != null && orderBy.getItems() != null && orderBy.getItems().size() > 0) {
sb.append(" order by ");
for (int i = 0; i < orderBy.getItems().size(); i++) {
SQLSelectOrderByItem item = orderBy.getItems().get(i);
SQLOrderingSpecification os = item.getType();
sb.append(item.getExpr().toString());
if (i < orderBy.getItems().size() - 1) {
if (os != null)
sb.append(" ").append(os.toString());
sb.append(",");
} else {
if (os != null)
sb.append(" ").append(os.toString());
}
}
}
if (limit != null) {
// 分为两种情况: limit 10; limit 10,10;
sb.append(" limit ");
if (limit.getOffset() != null)
sb.append(limit.getOffset().toString()).append(",");
sb.append(limit.getRowCount().toString());
}
return sb.toString();
} catch (Exception e) {
LOGGER.warn(e.getMessage());
return sql;
}
}
use of com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr in project Mycat-Server by MyCATApache.
the class MycatSchemaStatVisitor method visit.
/*
* 遇到 all 将子查询改写成 SELECT MAX(name) FROM subtest1
* 例如:
* select * from subtest where id > all (select name from subtest1);
* >/>= all ----> >/>= max
* </<= all ----> </<= min
* <> all ----> not in
* = all ----> id = 1 and id = 2
* other 不改写
*/
@Override
public boolean visit(SQLAllExpr 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("MAX");
saexpr.getArguments().add(sexpr);
saexpr.setParent(itemlist.get(0));
itemlist.get(0).setExpr(saexpr);
}
SQLQueryExpr maxSubQuery = new SQLQueryExpr(x.getSubQuery());
x.getSubQuery().setParent(x.getParent());
// 生成新的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("MIN");
saexpr.getArguments().add(sexpr);
saexpr.setParent(itemlist.get(0));
itemlist.get(0).setExpr(saexpr);
x.subQuery.setParent(x.getParent());
}
// 生成新的SQLQueryExpr 替换当前 SQLAllExpr 节点
SQLQueryExpr minSubQuery = new SQLQueryExpr(x.getSubQuery());
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);
default:
break;
}
}
addSubQuerys(x.getSubQuery());
return super.visit(x);
}
use of com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr in project Mycat-Server by MyCATApache.
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);
}
Aggregations