Search in sources :

Example 21 with RouteResultset

use of io.mycat.route.RouteResultset in project Mycat-Server by MyCATApache.

the class DruidMysqlRouteStrategyTest method testRouteInsertShort.

//	public void testAlias() throws Exception {
//		String sql = "SELECT  UM.UserId , UM.MenuId ,SM.ParentId ,SM.FullName , SM.Description , SM.Img , SM.NavigateUrl ,SM.FormName ,SM.Target ,SM.IsUnfold FROM    Lever_SysMenu SM INNER JOIN ( SELECT UR.UserId AS UserId ,  RM.MenuId AS MenuId FROM   Lever_RoleMenu RM  INNER JOIN Lever_UserRole UR ON RM.RoleId = UR.RoleId  UNION SELECT UserId ,   MenuId FROM   Lever_UserMenu   UNION SELECT U.UserId ,      RM.MenuId   FROM   Lever_User U     LEFT JOIN Lever_RoleMenu RM ON U.RoleId = RM.RoleId    WHERE  U.UserId = '8d28533f-1762-4e79-b71f-64eb1a50cb8b' ) UM ON SM.MenuId = UM.MenuId   WHERE   UM.UserId = '8d28533f-1762-4e79-b71f-64eb1a50cb8b'  AND SM.Enabled = 1  ORDER BY SM.SortCode";
//		SchemaConfig schema = schemaMap.get("wdw");
//		RouteResultset rrs = routeStrategy.route(new SystemConfig(),schema, -1, sql, null,
//				null, cachePool);
//	}
public void testRouteInsertShort() throws Exception {
    String sql = "inSErt into offer_detail (`offer_id`, gmt) values (123,now())";
    SchemaConfig schema = schemaMap.get("cndb");
    RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals("detail_dn15", rrs.getNodes()[0].getName());
    Assert.assertEquals("inSErt into offer_detail (`offer_id`, gmt) values (123,now())", rrs.getNodes()[0].getStatement());
    sql = "inSErt into offer_detail ( gmt) values (now())";
    schema = schemaMap.get("cndb");
    try {
        rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    } catch (Exception e) {
        String msg = "bad insert sql (sharding column:";
        Assert.assertTrue(e.getMessage().contains(msg));
    }
    sql = "inSErt into offer_detail (offer_id, gmt) values (123,now())";
    schema = schemaMap.get("cndb");
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals("detail_dn15", rrs.getNodes()[0].getName());
    Assert.assertEquals("inSErt into offer_detail (offer_id, gmt) values (123,now())", rrs.getNodes()[0].getStatement());
    sql = "insert into offer(group_id,offer_id,member_id)values(234,123,'abc')";
    schema = schemaMap.get("cndb");
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals("offer_dn12", rrs.getNodes()[0].getName());
    Assert.assertEquals("insert into offer(group_id,offer_id,member_id)values(234,123,'abc')", rrs.getNodes()[0].getStatement());
    sql = "\n" + "  INSERT INTO \n" + "`offer` \n" + "(`asf`,member_id) \n" + "VALUES \n" + "(' the articles sfroms user selection ','abc')";
    schema = schemaMap.get("cndb");
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) SQLNonTransientException(java.sql.SQLNonTransientException) NoSuchElementException(java.util.NoSuchElementException) RouteResultset(io.mycat.route.RouteResultset)

Example 22 with RouteResultset

use of io.mycat.route.RouteResultset in project Mycat-Server by MyCATApache.

the class DruidMysqlRouteStrategyTest method testGlobalTableSingleNodeLimit.

public void testGlobalTableSingleNodeLimit() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    String sql = "select * from globalsn";
    RouteResultset rrs = null;
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
    Assert.assertEquals(100L, rrs.getLimitSize());
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultset(io.mycat.route.RouteResultset)

Example 23 with RouteResultset

use of io.mycat.route.RouteResultset in project Mycat-Server by MyCATApache.

the class DruidMysqlRouteStrategyTest method testAlias.

/**
     * 测试别名路由
     *
     * @throws Exception
     */
public void testAlias() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    RouteResultset rrs = null;
    //不支持childtable 批量插入
    //update 全局表
    String sql = "update company a set name = '' where a.id = 1;";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(3, rrs.getNodes().length);
    //update带别名时的路由
    sql = "update travelrecord a set name = '' where a.id = 1;";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
    //别名大小写路由
    sql = "select * from travelrecord A where a.id = 1;";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultset(io.mycat.route.RouteResultset)

Example 24 with RouteResultset

use of io.mycat.route.RouteResultset in project Mycat-Server by MyCATApache.

the class DruidMysqlRouteStrategyTest method testNonPartitionSQL.

public void testNonPartitionSQL() throws Exception {
    SchemaConfig schema = schemaMap.get("cndb");
    String sql = null;
    RouteResultset rrs = null;
    schema = schemaMap.get("dubbo2");
    sql = "SHOW TABLES from db_name like 'solo'";
    rrs = routeStrategy.route(new SystemConfig(), schema, 9, sql, null, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals("dn1", rrs.getNodes()[0].getName());
    Assert.assertEquals("SHOW TABLES like 'solo'", rrs.getNodes()[0].getStatement());
    schema = schemaMap.get("dubbo");
    sql = "SHOW TABLES from db_name like 'solo'";
    rrs = routeStrategy.route(new SystemConfig(), schema, 9, sql, null, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals("dubbo_dn", rrs.getNodes()[0].getName());
    Assert.assertEquals("SHOW TABLES like 'solo'", rrs.getNodes()[0].getStatement());
    sql = "desc cndb.offer";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals("dubbo_dn", rrs.getNodes()[0].getName());
    Assert.assertEquals("desc cndb.offer", rrs.getNodes()[0].getStatement());
    schema = schemaMap.get("cndb");
    sql = "SHOW fulL TaBLES from db_name like 'solo'";
    rrs = routeStrategy.route(new SystemConfig(), schema, 9, sql, null, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 3);
    NodeNameAsserter nameAsserter = new NodeNameAsserter("detail_dn0", "offer_dn0", "independent_dn0");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    SimpleSQLAsserter sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SHOW FULL TABLES like 'solo'").addExpectSQL(1, "SHOW FULL TABLES like 'solo'").addExpectSQL(2, "SHOW FULL TABLES like 'solo'").addExpectSQL(3, "SHOW FULL TABLES like 'solo'");
    RouteNodeAsserter asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultsetNode(io.mycat.route.RouteResultsetNode) RouteResultset(io.mycat.route.RouteResultset)

Example 25 with RouteResultset

use of io.mycat.route.RouteResultset in project Mycat-Server by MyCATApache.

the class MultiNodeQueryHandler method outputMergeResult.

/**
	 * 将汇聚结果集数据真正的发送给Mycat客户端
	 * @param source
	 * @param eof
	 * @param
	 */
public void outputMergeResult(final ServerConnection source, final byte[] eof, Iterator<UnsafeRow> iter) {
    try {
        lock.lock();
        ByteBuffer buffer = session.getSource().allocate();
        final RouteResultset rrs = this.dataMergeSvr.getRrs();
        /**
			 * 处理limit语句的start 和 end位置,将正确的结果发送给
			 * Mycat 客户端
			 */
        int start = rrs.getLimitStart();
        int end = start + rrs.getLimitSize();
        int index = 0;
        if (start < 0)
            start = 0;
        if (rrs.getLimitSize() < 0)
            end = Integer.MAX_VALUE;
        if (prepared) {
            while (iter.hasNext()) {
                UnsafeRow row = iter.next();
                if (index >= start) {
                    row.packetId = ++packetId;
                    BinaryRowDataPacket binRowPacket = new BinaryRowDataPacket();
                    binRowPacket.read(fieldPackets, row);
                    buffer = binRowPacket.write(buffer, source, true);
                }
                index++;
                if (index == end) {
                    break;
                }
            }
        } else {
            while (iter.hasNext()) {
                UnsafeRow row = iter.next();
                if (index >= start) {
                    row.packetId = ++packetId;
                    buffer = row.write(buffer, source, true);
                }
                index++;
                if (index == end) {
                    break;
                }
            }
        }
        eof[3] = ++packetId;
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("last packet id:" + packetId);
        }
        /**
			 * 真正的开始把Writer Buffer的数据写入到channel 中
			 */
        source.write(source.writeToBuffer(eof, buffer));
    } catch (Exception e) {
        handleDataProcessException(e);
    } finally {
        lock.unlock();
        dataMergeSvr.clear();
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) UnsafeRow(io.mycat.memory.unsafe.row.UnsafeRow) RouteResultset(io.mycat.route.RouteResultset)

Aggregations

RouteResultset (io.mycat.route.RouteResultset)52 SchemaConfig (io.mycat.config.model.SchemaConfig)34 SystemConfig (io.mycat.config.model.SystemConfig)32 RouteResultsetNode (io.mycat.route.RouteResultsetNode)15 Test (org.junit.Test)13 SQLNonTransientException (java.sql.SQLNonTransientException)11 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)7 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)6 TableConfig (io.mycat.config.model.TableConfig)6 CacheService (io.mycat.cache.CacheService)4 RouteService (io.mycat.route.RouteService)4 ByteBuffer (java.nio.ByteBuffer)4 ArrayList (java.util.ArrayList)4 NoSuchElementException (java.util.NoSuchElementException)4 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)3 DruidShardingParseInfo (io.mycat.route.parser.druid.DruidShardingParseInfo)3 RouteCalculateUnit (io.mycat.route.parser.druid.RouteCalculateUnit)3 TreeSet (java.util.TreeSet)3 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)2