Search in sources :

Example 16 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

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(false);
    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 = "company";
    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(schema, sqlType, sql, 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 = "COMPANY";
    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(schema, sqlType, sql, 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 = "COMPANY";
    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(schema, sqlType, sql, 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 = "COMPANY";
    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(schema, sqlType, sql, null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) ArrayList(java.util.ArrayList) TableConfig(com.actiontech.dble.config.model.TableConfig) CacheService(com.actiontech.dble.cache.CacheService) Test(org.junit.Test)

Example 17 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

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(schema, ServerParse.DESCRIBE, sql, 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(schema, ServerParse.DESCRIBE, sql, 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(schema, ServerParse.DESCRIBE, sql, 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(schema, ServerParse.SHOW, sql, 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(schema, ServerParse.SHOW, sql, 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(schema, ServerParse.SHOW, sql, 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(schema, ServerParse.SHOW, sql, 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 : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) Test(org.junit.Test)

Example 18 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

the class DQLRouteTest method test.

@Test
public void test() throws Exception {
    String stmt = "select * from `offer` where id = 100";
    SchemaConfig schema = schemaMap.get("mysqldb");
    RouteResultset rrs = new RouteResultset(stmt, 7);
    SQLStatementParser parser = null;
    parser = new MySqlStatementParser(stmt);
    SQLStatement statement;
    ServerSchemaStatVisitor visitor = null;
    try {
        statement = parser.parseStatement();
        visitor = new ServerSchemaStatVisitor();
    } catch (Exception t) {
        throw new SQLSyntaxErrorException(t);
    }
    ctx = new DruidShardingParseInfo();
    List<RouteCalculateUnit> taskList = visitorParse(rrs, statement, visitor);
    Assert.assertEquals(true, !taskList.get(0).getTablesAndConditions().isEmpty());
}
Also used : DruidShardingParseInfo(com.actiontech.dble.route.parser.druid.DruidShardingParseInfo) RouteCalculateUnit(com.actiontech.dble.route.parser.druid.RouteCalculateUnit) ServerSchemaStatVisitor(com.actiontech.dble.route.parser.druid.ServerSchemaStatVisitor) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) Test(org.junit.Test)

Example 19 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

the class DruidMysqlRouteStrategyTest method testSelectNoTable.

/**
 * select 1
 * select 1 union all select 2
 *
 * @throws Exception
 */
public void testSelectNoTable() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    String sql = "select 1";
    RouteResultset rrs = null;
    rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
    sql = "select 1 union select 2";
    rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig)

Example 20 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

the class DruidMysqlRouteStrategyTest method testOr.

/**
 * testOr
 *
 * @throws Exception
 */
@Test
public void testOr() throws Exception {
    // 0-200M=0
    // 200M1-400M=1
    // 400M1-600M=2
    // 600M1-800M=3
    // 800M1-1000M=4
    SchemaConfig schema = schemaMap.get("TESTDB");
    String sql = "select * from customer where sharding_id=10000 or 1=1;";
    RouteResultset rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
    Assert.assertTrue(rrs.getNodes().length == 2);
    Assert.assertTrue(rrs.getNodes()[0].getName().equals("dn1"));
    Assert.assertTrue(rrs.getNodes()[1].getName().equals("dn2"));
    sql = "select * from customer where sharding_id = 10000 or sharding_id = 10010";
    rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
    Assert.assertTrue(rrs.getNodes()[0].getName().equals("dn1"));
    Assert.assertTrue(rrs.getNodes()[1].getName().equals("dn2"));
    sql = "select * from customer where sharding_id = 10000 or user_id = 'wangwu'";
    rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
    Assert.assertTrue(rrs.getNodes()[0].getName().equals("dn1"));
    Assert.assertTrue(rrs.getNodes()[1].getName().equals("dn2"));
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) Test(org.junit.Test)

Aggregations

SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)70 TableConfig (com.actiontech.dble.config.model.TableConfig)16 SQLNonTransientException (java.sql.SQLNonTransientException)16 Test (org.junit.Test)15 RouteResultset (com.actiontech.dble.route.RouteResultset)6 PhysicalDBNode (com.actiontech.dble.backend.datasource.PhysicalDBNode)5 ServerConfig (com.actiontech.dble.config.ServerConfig)5 UserConfig (com.actiontech.dble.config.model.UserConfig)5 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)5 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)5 CacheService (com.actiontech.dble.cache.CacheService)4 EOFPacket (com.actiontech.dble.net.mysql.EOFPacket)4 FieldPacket (com.actiontech.dble.net.mysql.FieldPacket)4 RowDataPacket (com.actiontech.dble.net.mysql.RowDataPacket)4 StringPtr (com.actiontech.dble.plan.common.ptr.StringPtr)4 ByteBuffer (java.nio.ByteBuffer)4 ArrayList (java.util.ArrayList)4 PhysicalDBPool (com.actiontech.dble.backend.datasource.PhysicalDBPool)3 ERTable (com.actiontech.dble.config.model.ERTable)3 FirewallConfig (com.actiontech.dble.config.model.FirewallConfig)3