use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project Mycat_plus by coderczp.
the class DefaultDruidParser method visitorParse.
/**
* 子类可覆盖(如果该方法解析得不到表名、字段等信息的,就覆盖该方法,覆盖成空方法,然后通过statementPparse去解析)
* 通过visitor解析:有些类型的Statement通过visitor解析得不到表名、
* @param stmt
*/
@Override
public void visitorParse(RouteResultset rrs, SQLStatement stmt, MycatSchemaStatVisitor visitor) throws SQLNonTransientException {
stmt.accept(visitor);
ctx.setVisitor(visitor);
if (stmt instanceof SQLSelectStatement) {
SQLSelectQuery query = ((SQLSelectStatement) stmt).getSelect().getQuery();
if (query instanceof MySqlSelectQueryBlock) {
if (((MySqlSelectQueryBlock) query).isForUpdate()) {
rrs.setSelectForUpdate(true);
}
}
}
List<List<Condition>> mergedConditionList = new ArrayList<List<Condition>>();
if (visitor.hasOrCondition()) {
// 包含or语句
// TODO
// 根据or拆分
mergedConditionList = visitor.splitConditions();
} else {
// 不包含OR语句
mergedConditionList.add(visitor.getConditions());
}
if (visitor.isHasChange()) {
// 在解析的过程中子查询被改写了.需要更新ctx.
ctx.setSql(stmt.toString());
rrs.setStatement(ctx.getSql());
}
if (visitor.getAliasMap() != null) {
for (Map.Entry<String, String> entry : visitor.getAliasMap().entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (key != null && key.indexOf("`") >= 0) {
key = key.replaceAll("`", "");
}
if (value != null && value.indexOf("`") >= 0) {
value = value.replaceAll("`", "");
}
// 表名前面带database的,去掉
if (key != null) {
int pos = key.indexOf(".");
if (pos > 0) {
key = key.substring(pos + 1);
}
tableAliasMap.put(key.toUpperCase(), value);
}
// else {
// tableAliasMap.put(key, value);
// }
}
ctx.addTables(visitor.getTables());
visitor.getAliasMap().putAll(tableAliasMap);
ctx.setTableAliasMap(tableAliasMap);
}
ctx.setRouteCalculateUnits(this.buildRouteCalculateUnits(visitor, mergedConditionList));
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project Mycat_plus by coderczp.
the class DruidMycatRouteStrategy method routeNormalSqlWithAST.
@Override
public RouteResultset routeNormalSqlWithAST(SchemaConfig schema, String stmt, RouteResultset rrs, String charset, LayerCachePool cachePool, int sqlType, ServerConnection sc) throws SQLNonTransientException {
/**
* 只有mysql时只支持mysql语法
*/
SQLStatementParser parser = null;
if (schema.isNeedSupportMultiDBType()) {
parser = new MycatStatementParser(stmt);
} else {
parser = new MySqlStatementParser(stmt);
}
MycatSchemaStatVisitor visitor = null;
SQLStatement statement;
/**
* 解析出现问题统一抛SQL语法错误
*/
try {
statement = parser.parseStatement();
visitor = new MycatSchemaStatVisitor();
} catch (Exception t) {
LOGGER.error("DruidMycatRouteStrategyError", t);
throw new SQLSyntaxErrorException(t);
}
/**
* 检验unsupported statement
*/
checkUnSupportedStatement(statement);
DruidParser druidParser = DruidParserFactory.create(schema, statement, visitor);
druidParser.parser(schema, rrs, statement, stmt, cachePool, visitor);
DruidShardingParseInfo ctx = druidParser.getCtx();
rrs.setTables(ctx.getTables());
if (visitor.isSubqueryRelationOr()) {
String err = "In subQuery,the or condition is not supported.";
LOGGER.error(err);
throw new SQLSyntaxErrorException(err);
}
/* 按照以下情况路由
1.2.1 可以直接路由.
1.2.2 两个表夸库join的sql.调用calat
1.2.3 需要先执行subquery 的sql.把subquery拆分出来.获取结果后,与outerquery
*/
// add huangyiming 分片规则不一样的且表中带查询条件的则走Catlet
List<String> tables = ctx.getTables();
SchemaConfig schemaConf = MycatServer.getInstance().getConfig().getSchemas().get(schema.getName());
int index = 0;
RuleConfig firstRule = null;
boolean directRoute = true;
Set<String> firstDataNodes = new HashSet<String>();
Map<String, TableConfig> tconfigs = schemaConf == null ? null : schemaConf.getTables();
Map<String, RuleConfig> rulemap = new HashMap<>();
if (tconfigs != null) {
for (String tableName : tables) {
TableConfig tc = tconfigs.get(tableName);
if (tc == null) {
// add 别名中取
Map<String, String> tableAliasMap = ctx.getTableAliasMap();
if (tableAliasMap != null && tableAliasMap.get(tableName) != null) {
tc = schemaConf.getTables().get(tableAliasMap.get(tableName));
}
}
if (index == 0) {
if (tc != null) {
firstRule = tc.getRule();
// 没有指定分片规则时,不做处理
if (firstRule == null) {
continue;
}
firstDataNodes.addAll(tc.getDataNodes());
rulemap.put(tc.getName(), firstRule);
}
} else {
if (tc != null) {
// ER关系表的时候是可能存在字表中没有tablerule的情况,所以加上判断
RuleConfig ruleCfg = tc.getRule();
if (ruleCfg == null) {
// 没有指定分片规则时,不做处理
continue;
}
Set<String> dataNodes = new HashSet<String>();
dataNodes.addAll(tc.getDataNodes());
rulemap.put(tc.getName(), ruleCfg);
// 如果匹配规则不相同或者分片的datanode不相同则需要走子查询处理
if (firstRule != null && ((ruleCfg != null && !ruleCfg.getRuleAlgorithm().equals(firstRule.getRuleAlgorithm())) || (!dataNodes.equals(firstDataNodes)))) {
directRoute = false;
break;
}
}
}
index++;
}
}
RouteResultset rrsResult = rrs;
if (directRoute) {
// 直接路由
if (!RouterUtil.isAllGlobalTable(ctx, schemaConf)) {
if (rulemap.size() > 1 && !checkRuleField(rulemap, visitor)) {
String err = "In case of slice table,there is no rule field in the relationship condition!";
LOGGER.error(err);
throw new SQLSyntaxErrorException(err);
}
}
rrsResult = directRoute(rrs, ctx, schema, druidParser, statement, cachePool);
} else {
int subQuerySize = visitor.getSubQuerys().size();
if (subQuerySize == 0 && ctx.getTables().size() == 2) {
// 两表关联,考虑使用catlet
if (!visitor.getRelationships().isEmpty()) {
rrs.setCacheAble(false);
rrs.setFinishedRoute(true);
rrsResult = catletRoute(schema, ctx.getSql(), charset, sc);
} else {
rrsResult = directRoute(rrs, ctx, schema, druidParser, statement, cachePool);
}
} else if (subQuerySize == 1) {
// 只涉及一张表的子查询,使用 MiddlerResultHandler 获取中间结果后,改写原有 sql 继续执行 TODO 后期可能会考虑多个子查询的情况.
SQLSelect sqlselect = visitor.getSubQuerys().iterator().next();
if (!visitor.getRelationships().isEmpty()) {
// 当 inner query 和 outer query 有关联条件时,暂不支持
String err = "In case of slice table,sql have different rules,the relationship condition is not supported.";
LOGGER.error(err);
throw new SQLSyntaxErrorException(err);
} else {
SQLSelectQuery sqlSelectQuery = sqlselect.getQuery();
if (((MySqlSelectQueryBlock) sqlSelectQuery).getFrom() instanceof SQLExprTableSource) {
rrs.setCacheAble(false);
rrs.setFinishedRoute(true);
rrsResult = middlerResultRoute(schema, charset, sqlselect, sqlType, statement, sc);
}
}
} else if (subQuerySize >= 2) {
String err = "In case of slice table,sql has different rules,currently only one subQuery is supported.";
LOGGER.error(err);
throw new SQLSyntaxErrorException(err);
}
}
return rrsResult;
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock 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.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project Mycat_plus by coderczp.
the class SQLExistsResultHandler method dohandler.
@Override
public String dohandler(SQLStatement statement, SQLSelect sqlselect, SQLObject parent, List param) {
SQLExpr se = null;
if (param == null || param.isEmpty()) {
se = new SQLNullExpr();
} else {
se = (SQLExpr) param.get(0);
}
if (parent.getParent() instanceof MySqlSelectQueryBlock) {
MySqlSelectQueryBlock msqb = (MySqlSelectQueryBlock) parent.getParent();
msqb.setWhere(se);
} else if (parent.getParent() instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr sbqe = (SQLBinaryOpExpr) parent.getParent();
if (sbqe.getLeft().equals(parent)) {
sbqe.setLeft(se);
} else {
sbqe.setRight(se);
}
}
return statement.toString();
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project Mycat-Server by MyCATApache.
the class DruidSelectParser method statementParse.
@Override
public void statementParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt) {
SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
SQLSelectQuery sqlSelectQuery = selectStmt.getSelect().getQuery();
if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock) selectStmt.getSelect().getQuery();
parseOrderAggGroupMysql(schema, stmt, rrs, mysqlSelectQuery);
// 更改canRunInReadDB属性
if ((mysqlSelectQuery.isForUpdate() || mysqlSelectQuery.isLockInShareMode()) && rrs.isAutocommit() == false) {
rrs.setCanRunInReadDB(false);
}
} else if (sqlSelectQuery instanceof MySqlUnionQuery) {
// MySqlUnionQuery unionQuery = (MySqlUnionQuery)sqlSelectQuery;
// MySqlSelectQueryBlock left = (MySqlSelectQueryBlock)unionQuery.getLeft();
// MySqlSelectQueryBlock right = (MySqlSelectQueryBlock)unionQuery.getLeft();
// System.out.println();
}
}
Aggregations