Search in sources :

Example 51 with SystemConfig

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

the class DDLRouteTest method testTableMetaRead.

@Test
public void testTableMetaRead() throws Exception {
    final SchemaConfig schema = schemaMap.get("cndb");
    String sql = "desc offer";
    RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.DESCRIBE, sql, null, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    // random return one node
    // Assert.assertEquals("offer_dn[0]", rrs.getNodes()[0].getName());
    Assert.assertEquals("desc offer", rrs.getNodes()[0].getStatement());
    sql = " desc cndb.offer";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.DESCRIBE, sql, null, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    // random return one node
    // Assert.assertEquals("offer_dn[0]", rrs.getNodes()[0].getName());
    Assert.assertEquals("desc offer", rrs.getNodes()[0].getStatement());
    sql = " desc cndb.offer col1";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.DESCRIBE, sql, null, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    // random return one node
    // Assert.assertEquals("offer_dn[0]", rrs.getNodes()[0].getName());
    Assert.assertEquals("desc offer col1", rrs.getNodes()[0].getStatement());
    sql = "SHOW FULL COLUMNS FROM  offer  IN db_name WHERE true";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SHOW, sql, null, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    // random return one node
    // Assert.assertEquals("offer_dn[0]", rrs.getNodes()[0].getName());
    Assert.assertEquals("SHOW FULL COLUMNS FROM offer WHERE true", rrs.getNodes()[0].getStatement());
    sql = "SHOW FULL COLUMNS FROM  db.offer  IN db_name WHERE true";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SHOW, sql, null, null, cachePool);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(1, rrs.getNodes().length);
    // random return one node
    // Assert.assertEquals("offer_dn[0]", rrs.getNodes()[0].getName());
    Assert.assertEquals("SHOW FULL COLUMNS FROM offer WHERE true", rrs.getNodes()[0].getStatement());
    sql = "SHOW FULL TABLES FROM `TESTDB` WHERE Table_type != 'VIEW'";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SHOW, sql, null, null, cachePool);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals("SHOW FULL TABLES WHERE Table_type != 'VIEW'", rrs.getNodes()[0].getStatement());
    sql = "SHOW INDEX  IN offer FROM  db_name";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SHOW, sql, null, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    // random return one node
    // Assert.assertEquals("offer_dn[0]", rrs.getNodes()[0].getName());
    Assert.assertEquals("SHOW INDEX  FROM offer", rrs.getNodes()[0].getStatement());
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultset(io.mycat.route.RouteResultset) Test(org.junit.Test)

Example 52 with SystemConfig

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

the class DDLRouteTest method testDDLDefaultNode.

@Test
public void testDDLDefaultNode() throws Exception {
    SchemaConfig schema = schemaMap.get("solo1");
    CacheService cacheService = new CacheService();
    RouteService routerService = new RouteService(cacheService);
    // create table/view/function/..
    String sql = " create table company(idd int)";
    sql = RouterUtil.getFixedSql(sql);
    String upsql = sql.toUpperCase();
    // TODO:modify by zhuam 小写表名,转为大写比较
    String tablename = RouterUtil.getTableName(sql, RouterUtil.getCreateTablePos(upsql, 0));
    tablename = tablename.toUpperCase();
    List<String> dataNodes = new ArrayList<>();
    Map<String, TableConfig> tables = schema.getTables();
    TableConfig tc;
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    int nodeSize = dataNodes.size();
    if (nodeSize == 0 && schema.getDataNode() != null) {
        nodeSize = 1;
    }
    int rs = ServerParse.parse(sql);
    int sqlType = rs & 0xff;
    RouteResultset rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
    // drop table test
    sql = " drop table COMPANY";
    sql = RouterUtil.getFixedSql(sql);
    upsql = sql.toUpperCase();
    tablename = RouterUtil.getTableName(sql, RouterUtil.getDropTablePos(upsql, 0));
    tables = schema.getTables();
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    nodeSize = dataNodes.size();
    if (nodeSize == 0 && schema.getDataNode() != null) {
        nodeSize = 1;
    }
    rs = ServerParse.parse(sql);
    sqlType = rs & 0xff;
    rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
    // drop table test
    sql = " drop table if exists COMPANY";
    sql = RouterUtil.getFixedSql(sql);
    upsql = sql.toUpperCase();
    tablename = RouterUtil.getTableName(sql, RouterUtil.getDropTablePos(upsql, 0));
    tables = schema.getTables();
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    nodeSize = dataNodes.size();
    if (nodeSize == 0 && schema.getDataNode() != null) {
        nodeSize = 1;
    }
    rs = ServerParse.parse(sql);
    sqlType = rs & 0xff;
    rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
    // alter table
    sql = "   alter table COMPANY add COLUMN name int ;";
    sql = RouterUtil.getFixedSql(sql);
    upsql = sql.toUpperCase();
    tablename = RouterUtil.getTableName(sql, RouterUtil.getAlterTablePos(upsql, 0));
    tables = schema.getTables();
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    nodeSize = dataNodes.size();
    if (nodeSize == 0 && schema.getDataNode() != null) {
        nodeSize = 1;
    }
    rs = ServerParse.parse(sql);
    sqlType = rs & 0xff;
    rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
    // truncate table;
    sql = " truncate table COMPANY";
    sql = RouterUtil.getFixedSql(sql);
    upsql = sql.toUpperCase();
    tablename = RouterUtil.getTableName(sql, RouterUtil.getTruncateTablePos(upsql, 0));
    tables = schema.getTables();
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    nodeSize = dataNodes.size();
    if (nodeSize == 0 && schema.getDataNode() != null) {
        nodeSize = 1;
    }
    rs = ServerParse.parse(sql);
    sqlType = rs & 0xff;
    rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) ArrayList(java.util.ArrayList) TableConfig(io.mycat.config.model.TableConfig) RouteService(io.mycat.route.RouteService) CacheService(io.mycat.cache.CacheService) RouteResultset(io.mycat.route.RouteResultset) Test(org.junit.Test)

Example 53 with SystemConfig

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

the class DDLRouteTest method testSpecialCharDDL.

@Test
public void testSpecialCharDDL() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    CacheService cacheService = new CacheService();
    RouteService routerService = new RouteService(cacheService);
    // alter table test
    String sql = " ALTER TABLE COMPANY\r\nADD COLUMN TEST  VARCHAR(255) NULL AFTER CREATE_DATE,\r\n CHARACTER SET = UTF8";
    sql = RouterUtil.getFixedSql(sql);
    List<String> dataNodes = new ArrayList<>();
    String tablename = RouterUtil.getTableName(sql, RouterUtil.getAlterTablePos(sql, 0));
    Map<String, TableConfig> tables = schema.getTables();
    TableConfig tc;
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    int nodeSize = dataNodes.size();
    int rs = ServerParse.parse(sql);
    int sqlType = rs & 0xff;
    RouteResultset rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) ArrayList(java.util.ArrayList) TableConfig(io.mycat.config.model.TableConfig) RouteService(io.mycat.route.RouteService) CacheService(io.mycat.cache.CacheService) RouteResultset(io.mycat.route.RouteResultset) Test(org.junit.Test)

Example 54 with SystemConfig

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

the class DruidMysqlRouteStrategyTest method testGroupLimit.

public void testGroupLimit() throws Exception {
    final SchemaConfig schema = schemaMap.get("cndb");
    String sql = null;
    RouteResultset rrs = null;
    sql = "select count(*) from (select * from(select * from offer_detail where offer_id='123' or offer_id='234' limit 88)offer  where offer.member_id='abc' limit 60) w " + " where w.member_id ='pavarotti17' limit 99";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    // Assert.assertEquals(88L, rrs.getLimitSize());
    // Assert.assertEquals(RouteResultset.SUM_FLAG, rrs.getFlag());
    Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 2);
    NodeNameAsserter nameAsserter = new NodeNameAsserter("detail_dn29", "detail_dn15");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sql = "select count(*) from (select * from(select max(id) from offer_detail where offer_id='123' or offer_id='234' limit 88)offer  where offer.member_id='abc' limit 60) w " + " where w.member_id ='pavarotti17' limit 99";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("detail_dn29", "detail_dn15");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sql = "select * from (select * from(select max(id) from offer_detail where offer_id='123' or offer_id='234' limit 88)offer  where offer.member_id='abc' limit 60) w " + " where w.member_id ='pavarotti17' limit 99";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("detail_dn29", "detail_dn15");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sql = "select * from (select count(*) from(select * from offer_detail where offer_id='123' or offer_id='234' limit 88)offer  where offer.member_id='abc' limit 60) w " + " where w.member_id ='pavarotti17' limit 99";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    // Assert.assertEquals(88L, rrs.getLimitSize());
    // Assert.assertEquals(RouteResultset.SUM_FLAG, rrs.getFlag());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("detail_dn29", "detail_dn15");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
}
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 55 with SystemConfig

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

Aggregations

SystemConfig (io.mycat.config.model.SystemConfig)120 SchemaConfig (io.mycat.config.model.SchemaConfig)110 RouteResultset (io.mycat.route.RouteResultset)64 Test (org.junit.Test)60 RouteResultsetNode (io.mycat.route.RouteResultsetNode)14 CacheService (io.mycat.cache.CacheService)10 SQLNonTransientException (java.sql.SQLNonTransientException)10 NoSuchElementException (java.util.NoSuchElementException)10 RouteService (io.mycat.route.RouteService)8 ArrayList (java.util.ArrayList)8 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)6 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)6 TableConfig (io.mycat.config.model.TableConfig)6 PhysicalDBPool (io.mycat.backend.datasource.PhysicalDBPool)2 DirectByteBufferPool (io.mycat.buffer.DirectByteBufferPool)2 NettyBufferPool (io.mycat.buffer.NettyBufferPool)2 MycatConfig (io.mycat.config.MycatConfig)2 ManagerConnectionFactory (io.mycat.manager.ManagerConnectionFactory)2 MyCatMemory (io.mycat.memory.MyCatMemory)2 AIOAcceptor (io.mycat.net.AIOAcceptor)2