Search in sources :

Example 21 with SchemaConfig

use of io.mycat.config.model.SchemaConfig in project Mycat-Server by MyCATApache.

the class DruidMysqlRouteStrategyTest method testBatchInsert.

/**
     * 支持insert into ... values (),()...
     * 不支持insert into ... select...
     *
     * @throws Exception
     */
public void testBatchInsert() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    RouteResultset rrs = null;
    //不支持childtable 批量插入
    String sql = "insert into orders (id,name,customer_id) values(1,'testonly',1),(2,'testonly',2000001)";
    try {
        rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    } catch (Exception e) {
        Assert.assertEquals("ChildTable multi insert not provided", e.getMessage());
    }
    sql = "insert into employee (id,name,customer_id) select id,name,customer_id from customer";
    try {
        rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    } catch (Exception e) {
        Assert.assertEquals("TODO:insert into .... select .... not supported!", e.getMessage());
    }
    //分片表批量插入正常 employee
    sql = "insert into employee (id,name,sharding_id) values(1,'testonly',10000),(2,'testonly',10010)";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(2, rrs.getNodes().length);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals("dn1", rrs.getNodes()[0].getName());
    Assert.assertEquals("dn2", rrs.getNodes()[1].getName());
    String node1Sql = formatSql("insert into employee (id,name,sharding_id) values(1,'testonly',10000)");
    String node2Sql = formatSql("insert into employee (id,name,sharding_id) values(2,'testonly',10010)");
    RouteResultsetNode[] nodes = rrs.getNodes();
    Assert.assertEquals(node1Sql, nodes[0].getStatement());
    Assert.assertEquals(node2Sql, nodes[1].getStatement());
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultsetNode(io.mycat.route.RouteResultsetNode) SQLNonTransientException(java.sql.SQLNonTransientException) NoSuchElementException(java.util.NoSuchElementException) RouteResultset(io.mycat.route.RouteResultset)

Example 22 with SchemaConfig

use of io.mycat.config.model.SchemaConfig in project Mycat-Server by MyCATApache.

the class DruidMysqlRouteStrategyTest method testModifySQLLimit.

public void testModifySQLLimit() throws Exception {
    final SchemaConfig schema = schemaMap.get("TESTDB");
    String sql = null;
    RouteResultset rrs = null;
    //SQL span multi datanode 
    sql = "select * from orders limit 2,3";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 2);
    NodeNameAsserter nameAsserter = new NodeNameAsserter("dn2", "dn1");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    Assert.assertEquals(3, rrs.getLimitSize());
    MySqlStatementParser parser = new MySqlStatementParser("SELECT * FROM orders LIMIT 0,5");
    SQLStatement statement = parser.parseStatement();
    Assert.assertEquals(statement.toString(), rrs.getNodes()[0].getStatement());
    //SQL  not span multi datanode
    sql = "select * from customer where id=10000 limit 2,3";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    nodeMap = getNodeMap(rrs, 1);
    nameAsserter = new NodeNameAsserter("dn1");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    Assert.assertEquals(3, rrs.getLimitSize());
    Assert.assertEquals("select * from customer where id=10000 limit 2,3", rrs.getNodes()[0].getStatement());
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultsetNode(io.mycat.route.RouteResultsetNode) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) RouteResultset(io.mycat.route.RouteResultset)

Example 23 with SchemaConfig

use of io.mycat.config.model.SchemaConfig in project Mycat-Server by MyCATApache.

the class DruidMysqlRouteStrategyTest method testInsertOnDuplicateKey.

/**
     * insert ... on duplicate key ... update...
     *
     * @throws Exception
     */
public void testInsertOnDuplicateKey() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    String sql = "insert into employee (id,name,sharding_id) values(1,'testonly',10000) on duplicate key update name='nihao'";
    RouteResultset rrs = null;
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals("dn1", rrs.getNodes()[0].getName());
    //insert ... on duplicate key ... update col1 = VALUES(col1),col2 = VALUES(col2)
    sql = "insert into employee (id,name,sharding_id) values(1,'testonly',10000) " + "on duplicate key update name=VALUES(name),id = VALUES(id)";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals("dn1", rrs.getNodes()[0].getName());
    //insert ... on duplicate key ,sharding key can't be updated
    sql = "insert into employee (id,name,sharding_id) values(1,'testonly',10000) " + "on duplicate key update name=VALUES(name),id = VALUES(id),sharding_id = VALUES(sharding_id)";
    try {
        rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
    } catch (Exception e) {
        Assert.assertEquals("Sharding column can't be updated: EMPLOYEE -> SHARDING_ID", e.getMessage());
    }
}
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 24 with SchemaConfig

use of io.mycat.config.model.SchemaConfig in project Mycat-Server by MyCATApache.

the class DruidMysqlRouteStrategyTest method testroute.

public void testroute() throws Exception {
    SchemaConfig schema = schemaMap.get("cndb");
    String sql = "select * from independent where member='abc'";
    RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 128);
    IndexedNodeNameAsserter nameAsserter = new IndexedNodeNameAsserter("independent_dn", 0, 128);
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    SimpleSQLAsserter sqlAsserter = new SimpleSQLAsserter();
    for (int i = 0; i < 128; ++i) {
        sqlAsserter.addExpectSQL(i, "select * from independent where member='abc'");
    }
    RouteNodeAsserter asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    // include database schema ,should remove
    sql = "select * from cndb.independent A  where a.member='abc'";
    schema = schemaMap.get("cndb");
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    nodeMap = getNodeMap(rrs, 128);
    nameAsserter = new IndexedNodeNameAsserter("independent_dn", 0, 128);
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    for (int i = 0; i < 128; ++i) {
        sqlAsserter.addExpectSQL(i, "select * from independent A  where a.member='abc'");
    }
    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 SchemaConfig

use of io.mycat.config.model.SchemaConfig 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)

Aggregations

SchemaConfig (io.mycat.config.model.SchemaConfig)79 SystemConfig (io.mycat.config.model.SystemConfig)54 RouteResultset (io.mycat.route.RouteResultset)34 Test (org.junit.Test)31 TableConfig (io.mycat.config.model.TableConfig)12 MycatConfig (io.mycat.config.MycatConfig)9 RouteResultsetNode (io.mycat.route.RouteResultsetNode)7 SQLNonTransientException (java.sql.SQLNonTransientException)7 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)6 RowDataPacket (io.mycat.net.mysql.RowDataPacket)6 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)5 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)5 CacheService (io.mycat.cache.CacheService)5 MycatCluster (io.mycat.config.MycatCluster)5 UserConfig (io.mycat.config.model.UserConfig)5 ArrayList (java.util.ArrayList)5 NoSuchElementException (java.util.NoSuchElementException)5 EOFPacket (io.mycat.net.mysql.EOFPacket)4 FieldPacket (io.mycat.net.mysql.FieldPacket)4 RouteService (io.mycat.route.RouteService)4