use of io.mycat.route.parser.druid.RouteCalculateUnit in project Mycat-Server by MyCATApache.
the class DruidSelectParser method tryRoute.
private void tryRoute(SchemaConfig schema, RouteResultset rrs, LayerCachePool cachePool) throws SQLNonTransientException {
if (rrs.isFinishedRoute()) {
//避免重复路由
return;
}
//无表的select语句直接路由带任一节点
if ((ctx.getTables() == null || ctx.getTables().size() == 0) && (ctx.getTableAliasMap() == null || ctx.getTableAliasMap().isEmpty())) {
rrs = RouterUtil.routeToSingleNode(rrs, schema.getRandomDataNode(), ctx.getSql());
rrs.setFinishedRoute(true);
return;
}
// RouterUtil.tryRouteForTables(schema, ctx, rrs, true, cachePool);
SortedSet<RouteResultsetNode> nodeSet = new TreeSet<RouteResultsetNode>();
boolean isAllGlobalTable = RouterUtil.isAllGlobalTable(ctx, schema);
for (RouteCalculateUnit unit : ctx.getRouteCalculateUnits()) {
RouteResultset rrsTmp = RouterUtil.tryRouteForTables(schema, ctx, unit, rrs, true, cachePool);
if (rrsTmp != null && rrsTmp.getNodes() != null) {
for (RouteResultsetNode node : rrsTmp.getNodes()) {
nodeSet.add(node);
}
}
if (isAllGlobalTable) {
//都是全局表时只计算一遍路由
break;
}
}
if (nodeSet.size() == 0) {
Collection<String> stringCollection = ctx.getTableAliasMap().values();
for (String table : stringCollection) {
if (table != null && table.toLowerCase().contains("information_schema.")) {
rrs = RouterUtil.routeToSingleNode(rrs, schema.getRandomDataNode(), ctx.getSql());
rrs.setFinishedRoute(true);
return;
}
}
String msg = " find no Route:" + ctx.getSql();
LOGGER.warn(msg);
throw new SQLNonTransientException(msg);
}
RouteResultsetNode[] nodes = new RouteResultsetNode[nodeSet.size()];
int i = 0;
for (Iterator<RouteResultsetNode> iterator = nodeSet.iterator(); iterator.hasNext(); ) {
nodes[i] = (RouteResultsetNode) iterator.next();
i++;
}
rrs.setNodes(nodes);
rrs.setFinishedRoute(true);
}
use of io.mycat.route.parser.druid.RouteCalculateUnit in project Mycat-Server by MyCATApache.
the class DQLRouteTest method test.
@Test
public void test() throws Exception {
String stmt = "select * from `offer` where id = 100";
SchemaConfig schema = schemaMap.get("mysqldb");
RouteResultset rrs = new RouteResultset(stmt, 7);
SQLStatementParser parser = null;
if (schema.isNeedSupportMultiDBType()) {
parser = new MycatStatementParser(stmt);
} else {
parser = new MySqlStatementParser(stmt);
}
SQLStatement statement;
MycatSchemaStatVisitor visitor = null;
try {
statement = parser.parseStatement();
visitor = new MycatSchemaStatVisitor();
} catch (Exception t) {
throw new SQLSyntaxErrorException(t);
}
ctx = new DruidShardingParseInfo();
ctx.setSql(stmt);
List<RouteCalculateUnit> taskList = visitorParse(rrs, statement, visitor);
Assert.assertEquals(true, !taskList.get(0).getTablesAndConditions().isEmpty());
}
Aggregations