Search in sources :

Example 11 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 12 with SystemConfig

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

the class PartitionByRangeDateHashTest method testRange.

@Test
public void testRange() throws SQLNonTransientException {
    String sql = "select * from offer1  where col_date between '2014-01-01 00:00:00'  and '2014-01-03 23:59:59'     order by id desc limit 100";
    SchemaConfig schema = schemaMap.get("TESTDB");
    RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    junit.framework.Assert.assertEquals(6, rrs.getNodes().length);
    sql = "select * from offer1  where col_date between '2014-01-01 00:00:00'  and '2014-01-04 00:00:59'      order by id desc limit 100";
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    junit.framework.Assert.assertEquals(12, rrs.getNodes().length);
    sql = "select * from offer1  where col_date between '2014-01-04 00:00:00'  and '2014-01-06 23:59:59'      order by id desc limit 100";
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    junit.framework.Assert.assertEquals(6, rrs.getNodes().length);
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultset(io.mycat.route.RouteResultset) Test(org.junit.Test)

Example 13 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)

Example 14 with SystemConfig

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

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

Aggregations

SystemConfig (io.mycat.config.model.SystemConfig)59 SchemaConfig (io.mycat.config.model.SchemaConfig)54 RouteResultset (io.mycat.route.RouteResultset)32 Test (org.junit.Test)29 RouteResultsetNode (io.mycat.route.RouteResultsetNode)7 CacheService (io.mycat.cache.CacheService)5 SQLNonTransientException (java.sql.SQLNonTransientException)5 NoSuchElementException (java.util.NoSuchElementException)5 RouteService (io.mycat.route.RouteService)4 ArrayList (java.util.ArrayList)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)3 TableConfig (io.mycat.config.model.TableConfig)3 PhysicalDBPool (io.mycat.backend.datasource.PhysicalDBPool)1 DirectByteBufferPool (io.mycat.buffer.DirectByteBufferPool)1 MycatConfig (io.mycat.config.MycatConfig)1 ManagerConnectionFactory (io.mycat.manager.ManagerConnectionFactory)1 MyCatMemory (io.mycat.memory.MyCatMemory)1 AIOAcceptor (io.mycat.net.AIOAcceptor)1 AIOConnector (io.mycat.net.AIOConnector)1