Search in sources :

Example 46 with SchemaConfig

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

the class DruidMysqlRouteStrategyTest method testAddLimitToSQL.

public void testAddLimitToSQL() throws Exception {
    final SchemaConfig schema = schemaMap.get("TESTDB");
    String sql = null;
    RouteResultset rrs = null;
    sql = "select * from orders";
    rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, 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(schema.getDefaultMaxLimit(), rrs.getLimitSize());
    // Assert.assertEquals("SELECT * FROM orders LIMIT 100", rrs.getNodes()[0].getStatement());
    MySqlStatementParser parser = new MySqlStatementParser("SELECT * FROM orders LIMIT 100");
    SQLStatement statement = parser.parseStatement();
    Assert.assertEquals(statement.toString(), rrs.getNodes()[0].getStatement());
    sql = "select * from goods";
    rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(schema.getDefaultMaxLimit(), rrs.getLimitSize());
    // Assert.assertEquals("select * from goods", rrs.getNodes()[0].getStatement());
    parser = new MySqlStatementParser("SELECT * FROM goods LIMIT 100");
    statement = parser.parseStatement();
    Assert.assertEquals(statement.toString(), rrs.getNodes()[0].getStatement());
    sql = "select * from goods limit 2 ,3";
    rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(1, rrs.getNodes().length);
    // Assert.assertEquals(-1, rrs.getLimitSize());
    Assert.assertEquals("select * from goods limit 2 ,3", rrs.getNodes()[0].getStatement());
    sql = "select * from notpartionTable limit 2 ,3";
    rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(3, rrs.getLimitSize());
    Assert.assertEquals("select * from notpartionTable limit 2 ,3", rrs.getNodes()[0].getStatement());
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 47 with SchemaConfig

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

the class DruidMysqlRouteStrategyTest method testERRouteMutiNode.

/**
 * testERRouteMutiNode
 *
 * @throws Exception
 */
@Test
public void testERRouteMutiNode() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    String sql = "select * from orders where customer_id in(1,2000001);";
    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"));
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) Test(org.junit.Test)

Example 48 with SchemaConfig

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

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(schema, 1, sql, 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(schema, 1, sql, 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 : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig)

Example 49 with SchemaConfig

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

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(schema, ServerParse.SELECT, sql, 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(schema, ServerParse.SELECT, sql, 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(schema, ServerParse.SELECT, sql, null, cachePool);
    } catch (Exception e) {
        Assert.assertEquals("Sharding column can't be updated: EMPLOYEE -> SHARDING_ID", e.getMessage());
    }
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) SQLNonTransientException(java.sql.SQLNonTransientException)

Example 50 with SchemaConfig

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

the class DruidMysqlRouteStrategyTest method testIgnoreSchema.

public void testIgnoreSchema() throws Exception {
    SchemaConfig schema = schemaMap.get("ignoreSchemaTest");
    String sql = "select * from offer where offer_id=1";
    RouteResultset rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals("cndb_dn", rrs.getNodes()[0].getName());
    Assert.assertEquals(sql, rrs.getNodes()[0].getStatement());
    sql = "select * from ignoreSchemaTest.offer1 where ignoreSchemaTest.offer1.offer_id=1";
    rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals("select * from offer1 where offer1.offer_id=1", rrs.getNodes()[0].getStatement());
    sql = "select * from ignoreSchemaTest2.offer where ignoreSchemaTest2.offer.offer_id=1";
    rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(sql, rrs.getNodes()[0].getStatement(), sql);
    sql = "select * from ignoreSchemaTest2.offer a,offer b  where ignoreSchemaTest2.offer.offer_id=1";
    rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals("select * from ignoreSchemaTest2.offer a,offer b  where ignoreSchemaTest2.offer.offer_id=1", rrs.getNodes()[0].getStatement());
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig)

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