Search in sources :

Example 56 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat_plus by coderczp.

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 57 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat_plus by coderczp.

the class DDLRouteTest method testDDL.

/**
 * ddl deal test
 *
 * @throws Exception
 */
@Test
public void testDDL() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    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();
    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();
    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();
    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();
    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 58 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat_plus by coderczp.

the class RuleFunctionSuitTableTest method testPartitionByPrefixPattern.

@Test
public void testPartitionByPrefixPattern() {
    PartitionByPrefixPattern partition = new PartitionByPrefixPattern();
    partition.setMapFile("partition_prefix_pattern.txt");
    partition.init();
    /*
		 * partition_prefix_pattern.txt
		 * 1-4=0
         * 5-8=1
         * 9-12=2
         * 13-16=3
         * 17-20=4
	     * 21-24=5
         * 25-28=6
         * 29-32=7
         * 0-0=7
		 */
    Assert.assertEquals(8, partition.getPartitionNum());
    RuleConfig rule = new RuleConfig("id", "partition-prefix-pattern");
    rule.setRuleAlgorithm(partition);
    TableConfig tableConf = new TableConfig("test", "id", true, false, -1, "dn1,dn2", null, rule, true, null, false, null, null, null);
    int suit1 = partition.suitableFor(tableConf);
    Assert.assertEquals(-1, suit1);
    tableConf.getDataNodes().clear();
    String[] dataNodes = SplitUtil.split("dn$1-8", ',', '$', '-');
    tableConf.getDataNodes().addAll(Arrays.asList(dataNodes));
    int suit2 = partition.suitableFor(tableConf);
    Assert.assertEquals(0, suit2);
    tableConf.getDataNodes().clear();
    dataNodes = SplitUtil.split("dn$1-10", ',', '$', '-');
    tableConf.getDataNodes().addAll(Arrays.asList(dataNodes));
    int suit3 = partition.suitableFor(tableConf);
    Assert.assertEquals(1, suit3);
}
Also used : TableConfig(io.mycat.config.model.TableConfig) RuleConfig(io.mycat.config.model.rule.RuleConfig) Test(org.junit.Test)

Example 59 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat_plus by coderczp.

the class RuleFunctionSuitTableTest method testAutoPartitionByLong.

@Test
public void testAutoPartitionByLong() {
    AutoPartitionByLong autoPartition = new AutoPartitionByLong();
    autoPartition.setMapFile("autopartition-long.txt");
    // partition = 3
    autoPartition.init();
    Assert.assertEquals(3, autoPartition.getPartitionNum());
    RuleConfig rule = new RuleConfig("id", "auto-partition-long");
    rule.setRuleAlgorithm(autoPartition);
    TableConfig tableConf = new TableConfig("test", "id", true, false, -1, "dn1,dn2", null, rule, true, null, false, null, null, null);
    int suit1 = autoPartition.suitableFor(tableConf);
    Assert.assertEquals(-1, suit1);
    tableConf.getDataNodes().clear();
    tableConf.getDataNodes().addAll(Arrays.asList("dn1", "dn2", "dn3"));
    int suit2 = autoPartition.suitableFor(tableConf);
    Assert.assertEquals(0, suit2);
    tableConf.getDataNodes().clear();
    tableConf.getDataNodes().addAll(Arrays.asList("dn1", "dn2", "dn3", "dn4"));
    int suit3 = autoPartition.suitableFor(tableConf);
    Assert.assertEquals(1, suit3);
    /* 
		 * autopartition-long-dupl.txt
		 * 0-1000=0
		 * 1001-2000=1
		 * 2001-3000=0
		 * 3001-4000=1
		*/
    AutoPartitionByLong autoPartition2 = new AutoPartitionByLong();
    autoPartition2.setMapFile("autopartition-long-dupl.txt");
    autoPartition2.init();
    Assert.assertEquals(2, autoPartition2.getPartitionNum());
    RuleConfig rule2 = new RuleConfig("id", "auto-partition-long-dupl");
    rule2.setRuleAlgorithm(autoPartition2);
    TableConfig tableConf2 = new TableConfig("test2", "id", true, false, -1, "dn1,dn2", null, rule, true, null, false, null, null, null);
    Assert.assertEquals(0, autoPartition2.suitableFor(tableConf2));
    Assert.assertEquals(0, autoPartition2.calculate("500").intValue());
    Assert.assertEquals(1, autoPartition2.calculate("1500").intValue());
    Assert.assertEquals(1, autoPartition2.calculate("2000").intValue());
    Assert.assertEquals(0, autoPartition2.calculate("3000").intValue());
    Assert.assertEquals(1, autoPartition2.calculate("3001").intValue());
}
Also used : TableConfig(io.mycat.config.model.TableConfig) RuleConfig(io.mycat.config.model.rule.RuleConfig) Test(org.junit.Test)

Example 60 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat_plus by coderczp.

the class RuleFunctionSuitTableTest method testPartitionByDate.

@Test
public void testPartitionByDate() {
    PartitionByDate partition = new PartitionByDate();
    partition.setDateFormat("yyyy-MM-dd");
    partition.setsBeginDate("2014-01-01");
    partition.setsEndDate("2014-01-31");
    partition.setsPartionDay("10");
    // partition = 4
    partition.init();
    Assert.assertEquals(4, partition.getPartitionNum());
    RuleConfig rule = new RuleConfig("col_date", "partition-date");
    rule.setRuleAlgorithm(partition);
    TableConfig tableConf = new TableConfig("test", "id", true, false, -1, "dn1,dn2,dn3", null, rule, true, null, false, null, null, null);
    int suit1 = partition.suitableFor(tableConf);
    Assert.assertEquals(-1, suit1);
    tableConf.getDataNodes().clear();
    tableConf.getDataNodes().addAll(Arrays.asList("dn1", "dn2", "dn3", "dn4"));
    int suit2 = partition.suitableFor(tableConf);
    Assert.assertEquals(0, suit2);
    tableConf.getDataNodes().clear();
    tableConf.getDataNodes().addAll(Arrays.asList("dn1", "dn2", "dn3", "dn4", "dn5"));
    int suit3 = partition.suitableFor(tableConf);
    Assert.assertEquals(1, suit3);
    PartitionByDate partition1 = new PartitionByDate();
    partition.setDateFormat("yyyy-MM-dd");
    partition.setsBeginDate("2014-01-01");
    partition.setsPartionDay("10");
    // partition no limit
    partition.init();
    int suit4 = partition1.suitableFor(tableConf);
    Assert.assertEquals(0, suit4);
}
Also used : TableConfig(io.mycat.config.model.TableConfig) RuleConfig(io.mycat.config.model.rule.RuleConfig) Test(org.junit.Test)

Aggregations

TableConfig (io.mycat.config.model.TableConfig)84 SchemaConfig (io.mycat.config.model.SchemaConfig)26 SQLNonTransientException (java.sql.SQLNonTransientException)26 Test (org.junit.Test)18 RuleConfig (io.mycat.config.model.rule.RuleConfig)16 RouteResultset (io.mycat.route.RouteResultset)14 AbstractPartitionAlgorithm (io.mycat.route.function.AbstractPartitionAlgorithm)14 HashMap (java.util.HashMap)14 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)12 SlotFunction (io.mycat.route.function.SlotFunction)12 ArrayList (java.util.ArrayList)12 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)10 MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)8 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)8 ConfigException (io.mycat.config.util.ConfigException)8 RouteResultsetNode (io.mycat.route.RouteResultsetNode)8 HashSet (java.util.HashSet)8 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)6 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)6 CacheService (io.mycat.cache.CacheService)6