Search in sources :

Example 21 with SchemaConfig

use of com.alibaba.cobar.config.model.SchemaConfig in project cobar by alibaba.

the class ServerRouteTest method testTableMetaRead.

public void testTableMetaRead() throws Exception {
    final SchemaConfig schema = schemaMap.get("cndb");
    String sql = "desc offer";
    RouteResultset rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("offer_dn[0]", rrs.getNodes()[0].getName());
    Assert.assertEquals("desc offer", rrs.getNodes()[0].getStatement());
    sql = "desc cndb.offer";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("offer_dn[0]", rrs.getNodes()[0].getName());
    Assert.assertEquals("DESC offer", rrs.getNodes()[0].getStatement());
    sql = "SHOW FULL COLUMNS FROM  offer  IN db_name WHERE true";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    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 = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("offer_dn[0]", rrs.getNodes()[0].getName());
    Assert.assertEquals("SHOW FULL COLUMNS FROM offer WHERE TRUE", rrs.getNodes()[0].getStatement());
    sql = "SHOW INDEX  IN offer FROM  db_name";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("offer_dn[0]", rrs.getNodes()[0].getName());
    Assert.assertEquals("SHOW INDEX IN offer", rrs.getNodes()[0].getStatement());
    sql = "SHOW TABLES from db_name like 'solo'";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 4);
    NodeNameAsserter nameAsserter = new NodeNameAsserter("detail_dn[0]", "offer_dn[0]", "cndb_dn", "independent_dn[0]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    SimpleSQLAsserter sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SHOW TABLES LIKE 'solo'").addExpectSQL(1, "SHOW TABLES LIKE 'solo'").addExpectSQL(2, "SHOW TABLES LIKE 'solo'").addExpectSQL(3, "SHOW TABLES LIKE 'solo'");
    RouteNodeAsserter asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "SHOW TABLES in db_name ";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 4);
    nameAsserter = new NodeNameAsserter("detail_dn[0]", "offer_dn[0]", "cndb_dn", "independent_dn[0]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SHOW TABLES").addExpectSQL(1, "SHOW TABLES").addExpectSQL(2, "SHOW TABLES").addExpectSQL(3, "SHOW TABLES");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "SHOW TABLeS ";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 4);
    nameAsserter = new NodeNameAsserter("detail_dn[0]", "offer_dn[0]", "cndb_dn", "independent_dn[0]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SHOW TABLeS ").addExpectSQL(1, "SHOW TABLeS ").addExpectSQL(2, "SHOW TABLeS ").addExpectSQL(3, "SHOW TABLeS ");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
}
Also used : SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig)

Example 22 with SchemaConfig

use of com.alibaba.cobar.config.model.SchemaConfig in project cobar by alibaba.

the class ServerRouteTest method testDimension2Route.

public void testDimension2Route() throws Exception {
    final SchemaConfig schema = schemaMap.get("cndb");
    String sql = "select * from product_visit where member_id='pavarotti17' and product_id=2345";
    RouteResultset rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals("offer_dn[9]", rrs.getNodes()[0].getName());
    Assert.assertEquals("select * from product_visit where member_id='pavarotti17' and product_id=2345", rrs.getNodes()[0].getStatement());
    sql = "select * from product_visit where member_id='pavarotti17' ";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 8);
    NodeNameAsserter nameAsserter = new NodeNameAsserter("offer_dn[25]", "offer_dn[17]", "offer_dn[9]", "offer_dn[1]", "offer_dn[29]", "offer_dn[21]", "offer_dn[5]", "offer_dn[13]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    SimpleSQLAsserter sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SELECT * FROM product_visit WHERE member_id = 'pavarotti17'").addExpectSQL(1, "SELECT * FROM product_visit WHERE member_id = 'pavarotti17'").addExpectSQL(2, "SELECT * FROM product_visit WHERE member_id = 'pavarotti17'").addExpectSQL(3, "SELECT * FROM product_visit WHERE member_id = 'pavarotti17'").addExpectSQL(4, "SELECT * FROM product_visit WHERE member_id = 'pavarotti17'").addExpectSQL(5, "SELECT * FROM product_visit WHERE member_id = 'pavarotti17'").addExpectSQL(6, "SELECT * FROM product_visit WHERE member_id = 'pavarotti17'").addExpectSQL(7, "SELECT * FROM product_visit WHERE member_id = 'pavarotti17'");
    RouteNodeAsserter asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select * from product_visit where member_id='abc' ";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 8);
    nameAsserter = new NodeNameAsserter("offer_dn[0]", "offer_dn[4]", "offer_dn[8]", "offer_dn[12]", "offer_dn[16]", "offer_dn[20]", "offer_dn[24]", "offer_dn[28]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SELECT * FROM product_visit WHERE member_id = 'abc'").addExpectSQL(1, "SELECT * FROM product_visit WHERE member_id = 'abc'").addExpectSQL(2, "SELECT * FROM product_visit WHERE member_id = 'abc'").addExpectSQL(3, "SELECT * FROM product_visit WHERE member_id = 'abc'").addExpectSQL(4, "SELECT * FROM product_visit WHERE member_id = 'abc'").addExpectSQL(5, "SELECT * FROM product_visit WHERE member_id = 'abc'").addExpectSQL(6, "SELECT * FROM product_visit WHERE member_id = 'abc'").addExpectSQL(7, "SELECT * FROM product_visit WHERE member_id = 'abc'");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "delete from product_visit where member_id='pavarotti17' or Member_id between 'abc' and 'abc'";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 16);
    nameAsserter = new NodeNameAsserter("offer_dn[0]", "offer_dn[4]", "offer_dn[8]", "offer_dn[12]", "offer_dn[16]", "offer_dn[20]", "offer_dn[24]", "offer_dn[28]", "offer_dn[1]", "offer_dn[5]", "offer_dn[9]", "offer_dn[13]", "offer_dn[17]", "offer_dn[21]", "offer_dn[25]", "offer_dn[29]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "DELETE FROM product_visit WHERE FALSE OR Member_id BETWEEN 'abc' AND 'abc'").addExpectSQL(1, "DELETE FROM product_visit WHERE FALSE OR Member_id BETWEEN 'abc' AND 'abc'").addExpectSQL(2, "DELETE FROM product_visit WHERE FALSE OR Member_id BETWEEN 'abc' AND 'abc'").addExpectSQL(3, "DELETE FROM product_visit WHERE FALSE OR Member_id BETWEEN 'abc' AND 'abc'").addExpectSQL(4, "DELETE FROM product_visit WHERE FALSE OR Member_id BETWEEN 'abc' AND 'abc'").addExpectSQL(5, "DELETE FROM product_visit WHERE FALSE OR Member_id BETWEEN 'abc' AND 'abc'").addExpectSQL(6, "DELETE FROM product_visit WHERE FALSE OR Member_id BETWEEN 'abc' AND 'abc'").addExpectSQL(7, "DELETE FROM product_visit WHERE FALSE OR Member_id BETWEEN 'abc' AND 'abc'").addExpectSQL(8, "DELETE FROM product_visit WHERE member_id = 'pavarotti17' OR FALSE").addExpectSQL(9, "DELETE FROM product_visit WHERE member_id = 'pavarotti17' OR FALSE").addExpectSQL(10, "DELETE FROM product_visit WHERE member_id = 'pavarotti17' OR FALSE").addExpectSQL(11, "DELETE FROM product_visit WHERE member_id = 'pavarotti17' OR FALSE").addExpectSQL(12, "DELETE FROM product_visit WHERE member_id = 'pavarotti17' OR FALSE").addExpectSQL(13, "DELETE FROM product_visit WHERE member_id = 'pavarotti17' OR FALSE").addExpectSQL(14, "DELETE FROM product_visit WHERE member_id = 'pavarotti17' OR FALSE").addExpectSQL(15, "DELETE FROM product_visit WHERE member_id = 'pavarotti17' OR FALSE");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select * from product_visit where  product_id=2345 ";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 4);
    nameAsserter = new NodeNameAsserter("offer_dn[8]", "offer_dn[9]", "offer_dn[10]", "offer_dn[11]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SELECT * FROM product_visit WHERE product_id = 2345").addExpectSQL(1, "SELECT * FROM product_visit WHERE product_id = 2345").addExpectSQL(2, "SELECT * FROM product_visit WHERE product_id = 2345").addExpectSQL(3, "SELECT * FROM product_visit WHERE product_id = 2345");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select * from product_visit where  product_id=1234 ";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 4);
    nameAsserter = new NodeNameAsserter("offer_dn[4]", "offer_dn[5]", "offer_dn[6]", "offer_dn[7]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SELECT * FROM product_visit WHERE product_id = 1234").addExpectSQL(1, "SELECT * FROM product_visit WHERE product_id = 1234").addExpectSQL(2, "SELECT * FROM product_visit WHERE product_id = 1234").addExpectSQL(3, "SELECT * FROM product_visit WHERE product_id = 1234");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select * from product_visit where  product_id=1234 or product_id=2345 ";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 8);
    nameAsserter = new NodeNameAsserter("offer_dn[4]", "offer_dn[5]", "offer_dn[6]", "offer_dn[7]", "offer_dn[8]", "offer_dn[9]", "offer_dn[10]", "offer_dn[11]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SELECT * FROM product_visit WHERE product_id = 1234 OR FALSE").addExpectSQL(1, "SELECT * FROM product_visit WHERE product_id = 1234 OR FALSE").addExpectSQL(2, "SELECT * FROM product_visit WHERE product_id = 1234 OR FALSE").addExpectSQL(3, "SELECT * FROM product_visit WHERE product_id = 1234 OR FALSE").addExpectSQL(4, "SELECT * FROM product_visit WHERE FALSE OR product_id = 2345").addExpectSQL(5, "SELECT * FROM product_visit WHERE FALSE OR product_id = 2345").addExpectSQL(6, "SELECT * FROM product_visit WHERE FALSE OR product_id = 2345").addExpectSQL(7, "SELECT * FROM product_visit WHERE FALSE OR product_id = 2345");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select * from product_visit where  product_id in (1234,2345) ";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 8);
    nameAsserter = new NodeNameAsserter("offer_dn[4]", "offer_dn[5]", "offer_dn[6]", "offer_dn[7]", "offer_dn[8]", "offer_dn[9]", "offer_dn[10]", "offer_dn[11]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SELECT * FROM product_visit WHERE product_id IN (1234)").addExpectSQL(1, "SELECT * FROM product_visit WHERE product_id IN (1234)").addExpectSQL(2, "SELECT * FROM product_visit WHERE product_id IN (1234)").addExpectSQL(3, "SELECT * FROM product_visit WHERE product_id IN (1234)").addExpectSQL(4, "SELECT * FROM product_visit WHERE product_id IN (2345)").addExpectSQL(5, "SELECT * FROM product_visit WHERE product_id IN (2345)").addExpectSQL(6, "SELECT * FROM product_visit WHERE product_id IN (2345)").addExpectSQL(7, "SELECT * FROM product_visit WHERE product_id IN (2345)");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
}
Also used : SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig)

Example 23 with SchemaConfig

use of com.alibaba.cobar.config.model.SchemaConfig in project cobar by alibaba.

the class ServerRouteTest method testRoute.

public void testRoute() throws Exception {
    String sql = "select * from offer.wp_image where member_id='pavarotti17' or member_id='1qq'";
    SchemaConfig schema = schemaMap.get("cndb");
    RouteResultset rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 2);
    NodeNameAsserter nameAsserter = new NodeNameAsserter("offer_dn[123]", "offer_dn[66]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    SimpleSQLAsserter sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SELECT * FROM wp_image WHERE member_id = 'pavarotti17' OR FALSE").addExpectSQL(1, "SELECT * FROM wp_image WHERE FALSE OR member_id = '1qq'");
    RouteNodeAsserter asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select * from independent where member='abc'";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    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 where member='abc'");
    }
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select * from independent A where cndb.a.member='abc'";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    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 AS A WHERE a.member = 'abc'");
    }
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select * from tb where member='abc'";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("cndb_dn", rrs.getNodes()[0].getName());
    Assert.assertEquals("select * from tb where member='abc'", rrs.getNodes()[0].getStatement());
    sql = "select * from offer.wp_image where member_id is null";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("offer_dn[48]", rrs.getNodes()[0].getName());
    Assert.assertEquals("SELECT * FROM wp_image WHERE member_id IS NULL", rrs.getNodes()[0].getStatement());
    sql = "select * from offer.wp_image where member_id between 'pavarotti17' and 'pavarotti17'";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("offer_dn[123]", rrs.getNodes()[0].getName());
    Assert.assertEquals("SELECT * FROM wp_image WHERE member_id BETWEEN 'pavarotti17' AND 'pavarotti17'", rrs.getNodes()[0].getStatement());
    sql = "select * from  offer A where a.member_id='abc' union select * from product_visit b where B.offer_id =123";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(128, rrs.getNodes().length);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    for (int i = 0; i < 128; i++) {
        Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[i].getReplicaIndex());
        Assert.assertEquals("offer_dn[" + i + "]", rrs.getNodes()[i].getName());
        Assert.assertEquals("select * from  offer A where a.member_id='abc' union select * from product_visit b where B.offer_id =123", rrs.getNodes()[i].getStatement());
    }
    sql = "update offer.offer a join offer_detail b set id=123 where a.offer_id=b.offer_id and a.offer_id=123 and group_id=234";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("offer_dn[44]", rrs.getNodes()[0].getName());
    Assert.assertEquals("UPDATE offer AS " + aliasConvert("a") + " INNER JOIN offer_detail AS " + aliasConvert("b") + " SET id = 123 WHERE a.offer_id = b.offer_id AND a.offer_id = 123 AND group_id = 234", rrs.getNodes()[0].getStatement());
    sql = "update    offer./*kjh*/offer a join offer_detail B set id:=123 where A.offer_id=b.offer_id and b.offer_id=123 and group_id=234";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("detail_dn[15]", rrs.getNodes()[0].getName());
    Assert.assertEquals("UPDATE offer AS " + aliasConvert("a") + " INNER JOIN offer_detail AS " + aliasConvert("b") + " SET id = 123 WHERE A.offer_id = b.offer_id AND b.offer_id = 123 AND group_id = 234", rrs.getNodes()[0].getStatement());
    sql = "select * from offer.wp_image where member_id in ('pavarotti17', 'qaa') or offer.wp_image.member_id='1qq'";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 3);
    nameAsserter = new NodeNameAsserter("offer_dn[123]", "offer_dn[10]", "offer_dn[66]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SELECT * FROM wp_image WHERE member_id IN ('pavarotti17') OR FALSE").addExpectSQL(1, "SELECT * FROM wp_image WHERE member_id IN ('qaa') OR FALSE").addExpectSQL(2, "SELECT * FROM wp_image WHERE FALSE OR wp_image.member_id = '1qq'");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select * from offer.wp_image,tb2 as t2 where member_id in ('pavarotti17', 'qaa') or offer.wp_image.member_id='1qq'";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(3, rrs.getNodes().length);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 3);
    nameAsserter = new NodeNameAsserter("offer_dn[123]", "offer_dn[10]", "offer_dn[66]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SELECT * FROM wp_image, tb2 AS " + aliasConvert("t2") + " WHERE member_id IN ('pavarotti17') OR FALSE").addExpectSQL(1, "SELECT * FROM wp_image, tb2 AS " + aliasConvert("t2") + " WHERE member_id IN ('qaa') OR FALSE").addExpectSQL(2, "SELECT * FROM wp_image, tb2 AS " + aliasConvert("t2") + " WHERE FALSE OR wp_image.member_id = '1qq'");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select * from offer.wp_image,tb2 as t2 where member_id in ('pavarotti17', 'sf', 's22f', 'sdddf', 'sd') ";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 4);
    nameAsserter = new NodeNameAsserter("offer_dn[123]", "offer_dn[126]", "offer_dn[74]", "offer_dn[26]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SELECT * FROM wp_image, tb2 AS " + aliasConvert("t2") + " WHERE member_id IN ('pavarotti17')").addExpectSQL(1, "SELECT * FROM wp_image, tb2 AS " + aliasConvert("t2") + " WHERE member_id IN ('sdddf')").addExpectSQL(2, "SELECT * FROM wp_image, tb2 AS " + aliasConvert("t2") + " WHERE member_id IN ('sf', 'sd')", "SELECT * FROM wp_image, tb2 AS " + aliasConvert("t2") + " WHERE member_id IN ('sd', 'sf')").addExpectSQL(3, "SELECT * FROM wp_image, tb2 AS " + aliasConvert("t2") + " WHERE member_id IN ('s22f')");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select * from tb2 as t2 ,offer.wp_image where member_id in ('pavarotti17', 'qaa') or offer.wp_image.member_id='1qq'";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 3);
    nameAsserter = new NodeNameAsserter("offer_dn[123]", "offer_dn[10]", "offer_dn[66]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SELECT * FROM tb2 AS " + aliasConvert("t2") + ", wp_image WHERE member_id IN ('pavarotti17') OR FALSE").addExpectSQL(1, "SELECT * FROM tb2 AS " + aliasConvert("t2") + ", wp_image WHERE member_id IN ('qaa') OR FALSE").addExpectSQL(2, "SELECT * FROM tb2 AS " + aliasConvert("t2") + ", wp_image WHERE FALSE OR wp_image.member_id = '1qq'");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select * from tb2 as t2 ,offer.wp_image where member_id in ('pavarotti17', 'qaa') or offer.wp_image.member_id='1qq' and t2.member_id='123'";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 3);
    nameAsserter = new NodeNameAsserter("offer_dn[123]", "offer_dn[10]", "offer_dn[66]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SELECT * FROM tb2 AS " + aliasConvert("t2") + ", wp_image WHERE member_id IN ('pavarotti17') OR FALSE AND t2.member_id = '123'").addExpectSQL(1, "SELECT * FROM tb2 AS " + aliasConvert("t2") + ", wp_image WHERE member_id IN ('qaa') OR FALSE AND t2.member_id = '123'").addExpectSQL(2, "SELECT * FROM tb2 AS " + aliasConvert("t2") + ", wp_image WHERE FALSE OR wp_image.member_id = '1qq' AND t2.member_id = '123'");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select * from wp_image wB inner join offer.offer o on wB.member_id=O.member_ID where wB.member_iD='pavarotti17' and o.id=3";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("offer_dn[123]", rrs.getNodes()[0].getName());
    Assert.assertEquals("SELECT * FROM wp_image AS " + aliasConvert("wB") + " INNER JOIN offer AS " + aliasConvert("o") + " ON wB.member_id = O.member_ID WHERE wB.member_iD = 'pavarotti17' AND o.id = 3", rrs.getNodes()[0].getStatement());
    sql = "select * from wp_image w inner join offer o on w.member_id=O.member_ID where w.member_iD in ('pavarotti17','13') and o.id=3";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("offer_dn[123]", "offer_dn[68]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SELECT * FROM wp_image AS " + aliasConvert("w") + " INNER JOIN offer AS " + aliasConvert("o") + " ON w.member_id = O.member_ID WHERE w.member_iD IN ('pavarotti17') AND o.id = 3").addExpectSQL(1, "SELECT * FROM wp_image AS " + aliasConvert("w") + " INNER JOIN offer AS " + aliasConvert("o") + " ON w.member_id = O.member_ID WHERE w.member_iD IN ('13') AND o.id = 3");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "insert into wp_image (member_id,gmt) values ('pavarotti17',now()),('123',now())";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("offer_dn[123]", "offer_dn[70]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "INSERT INTO wp_image (member_id, gmt) VALUES ('pavarotti17', NOW())").addExpectSQL(1, "INSERT INTO wp_image (member_id, gmt) VALUES ('123', NOW())");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
}
Also used : SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig)

Example 24 with SchemaConfig

use of com.alibaba.cobar.config.model.SchemaConfig in project cobar by alibaba.

the class ServerRouteTest method testRouteInsertLong.

public void testRouteInsertLong() throws Exception {
    StringBuilder sb = new StringBuilder("insert into offer_detail (offer_id, gmt) values ");
    for (int i = 0; i < 1024; ++i) {
        if (i > 0)
            sb.append(", ");
        sb.append("(" + i + ", now())");
    }
    SchemaConfig schema = schemaMap.get("cndb");
    RouteResultset rrs = ServerRouter.route(schema, sb.toString(), null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 128);
    IndexedNodeNameAsserter nameAsserter = new IndexedNodeNameAsserter("detail_dn", 0, 128);
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    RouteNodeAsserter asserter = new RouteNodeAsserter(nameAsserter, new ParseredSQLAsserter() {

        @Override
        protected void assertAST(SQLStatement stmt, int nodeIndex) {
            DMLInsertStatement insert = (DMLInsertStatement) stmt;
            List<RowExpression> rows = insert.getRowList();
            Assert.assertNotNull(rows);
            Assert.assertEquals(8, rows.size());
            List<Integer> vals = new ArrayList<Integer>(8);
            for (RowExpression row : rows) {
                int val = ((Number) row.getRowExprList().get(0).evaluation(null)).intValue();
                vals.add(val);
            }
            Assert.assertEquals(8, vals.size());
            for (int i = 8 * nodeIndex; i < 8 * nodeIndex + 8; ++i) {
                Assert.assertTrue(vals.contains(i));
            }
        }
    });
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
}
Also used : SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) SQLStatement(com.alibaba.cobar.parser.ast.stmt.SQLStatement) DMLInsertStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLInsertStatement) ArrayList(java.util.ArrayList) List(java.util.List)

Example 25 with SchemaConfig

use of com.alibaba.cobar.config.model.SchemaConfig in project cobar by alibaba.

the class ServerRouteTest method testBackquotedColumn.

public void testBackquotedColumn() throws Exception {
    final SchemaConfig schema = schemaMap.get("cndb");
    String sql = "select * from wp_image where `seLect`='pavarotti17' ";
    RouteResultset rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("offer_dn[123]", rrs.getNodes()[0].getName());
    Assert.assertEquals("select * from wp_image where `seLect`='pavarotti17' ", rrs.getNodes()[0].getStatement());
}
Also used : SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig)

Aggregations

SchemaConfig (com.alibaba.cobar.config.model.SchemaConfig)27 CobarConfig (com.alibaba.cobar.CobarConfig)6 MySQLDataNode (com.alibaba.cobar.mysql.MySQLDataNode)5 MySQLDataSource (com.alibaba.cobar.mysql.MySQLDataSource)4 RowDataPacket (com.alibaba.cobar.net.mysql.RowDataPacket)4 CobarCluster (com.alibaba.cobar.CobarCluster)3 EOFPacket (com.alibaba.cobar.net.mysql.EOFPacket)3 FieldPacket (com.alibaba.cobar.net.mysql.FieldPacket)3 ByteBuffer (java.nio.ByteBuffer)3 DataSourceConfig (com.alibaba.cobar.config.model.DataSourceConfig)2 QuarantineConfig (com.alibaba.cobar.config.model.QuarantineConfig)2 UserConfig (com.alibaba.cobar.config.model.UserConfig)2 ConfigException (com.alibaba.cobar.config.util.ConfigException)2 SQLNonTransientException (java.sql.SQLNonTransientException)2 ArrayList (java.util.ArrayList)2 CobarNode (com.alibaba.cobar.CobarNode)1 ConfigInitializer (com.alibaba.cobar.ConfigInitializer)1 TableConfig (com.alibaba.cobar.config.model.TableConfig)1 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)1 SQLStatement (com.alibaba.cobar.parser.ast.stmt.SQLStatement)1