Search in sources :

Example 6 with RouteCalculateUnit

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);
}
Also used : RouteCalculateUnit(io.mycat.route.parser.druid.RouteCalculateUnit) SQLNonTransientException(java.sql.SQLNonTransientException) TreeSet(java.util.TreeSet) RouteResultsetNode(io.mycat.route.RouteResultsetNode) RouteResultset(io.mycat.route.RouteResultset)

Example 7 with RouteCalculateUnit

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());
}
Also used : DruidShardingParseInfo(io.mycat.route.parser.druid.DruidShardingParseInfo) RouteCalculateUnit(io.mycat.route.parser.druid.RouteCalculateUnit) SchemaConfig(io.mycat.config.model.SchemaConfig) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) MycatStatementParser(io.mycat.route.parser.druid.MycatStatementParser) MycatSchemaStatVisitor(io.mycat.route.parser.druid.MycatSchemaStatVisitor) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) Test(org.junit.Test)

Aggregations

RouteCalculateUnit (io.mycat.route.parser.druid.RouteCalculateUnit)7 SQLNonTransientException (java.sql.SQLNonTransientException)4 RouteResultset (io.mycat.route.RouteResultset)3 RouteResultsetNode (io.mycat.route.RouteResultsetNode)3 DruidShardingParseInfo (io.mycat.route.parser.druid.DruidShardingParseInfo)3 ArrayList (java.util.ArrayList)3 TreeSet (java.util.TreeSet)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)2 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)2 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)2 Condition (com.alibaba.druid.stat.TableStat.Condition)2 MycatSchemaStatVisitor (io.mycat.route.parser.druid.MycatSchemaStatVisitor)2 MycatStatementParser (io.mycat.route.parser.druid.MycatStatementParser)2 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)1 MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)1 SchemaConfig (io.mycat.config.model.SchemaConfig)1 RuleConfig (io.mycat.config.model.rule.RuleConfig)1 SlotFunction (io.mycat.route.function.SlotFunction)1