Search in sources :

Example 66 with SelectSqlBuilder

use of com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder in project dal by ctripcorp.

the class TestTableDao method queryAll.

/**
 * Get all records from table
 */
public List<TestTable> queryAll(DalHints hints) throws SQLException {
    hints = DalHints.createIfAbsent(hints);
    SelectSqlBuilder builder = new SelectSqlBuilder().selectAll().orderBy("ID", ASC);
    return client.query(builder, hints);
}
Also used : SelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder)

Example 67 with SelectSqlBuilder

use of com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder in project dal by ctripcorp.

the class BaseDalTableDaoShardByDbTableTest method testQueryListImplicitInAllDBAndAllTableShards.

@Test
public void testQueryListImplicitInAllDBAndAllTableShards() throws SQLException {
    // set implicitAllShards on
    DalPropertiesManager.getInstance().tearDown();
    DalPropertiesManager.getInstance().setDalPropertiesProvider(new LocalDalPropertiesProvider());
    DalPropertiesManager.getInstance().setup();
    SelectSqlBuilder ssb1 = new SelectSqlBuilder();
    ssb1.selectAll().from(TABLE_NAME).where(" id = 1 ");
    try {
        List<ClientTestModel> list = dao.query(ssb1, new DalHints());
        Assert.assertEquals(8, list.size());
    } catch (Exception e) {
        Assert.fail();
    }
    SelectSqlBuilder ssb2 = new SelectSqlBuilder();
    ssb2.selectAll().from(TABLE_NAME);
    try {
        List<ClientTestModel> list = dao.query(ssb2, new DalHints());
        Assert.assertEquals(20, list.size());
    } catch (Exception e) {
        Assert.fail();
    }
    SelectSqlBuilder ssb3 = new SelectSqlBuilder();
    ssb3.selectAll().from(TABLE_NAME).equal("dbIndex", 0, Types.INTEGER);
    try {
        List<ClientTestModel> list = dao.query(ssb3, new DalHints());
        Assert.assertEquals(10, list.size());
    } catch (Exception e) {
        Assert.fail();
    }
    SelectSqlBuilder ssb4 = new SelectSqlBuilder();
    ssb4.selectAll().from(TABLE_NAME).equal("tableIndex", 0, Types.INTEGER);
    try {
        List<ClientTestModel> list = dao.query(ssb4, new DalHints());
        Assert.assertEquals(2, list.size());
    } catch (Exception e) {
        Assert.fail();
    }
    // set implicitAllShards off
    DalPropertiesManager.getInstance().tearDown();
    DalPropertiesManager.getInstance().setDalPropertiesProvider(DalElementFactory.DEFAULT.getDalPropertiesProvider());
    DalPropertiesManager.getInstance().setup();
    try {
        List<ClientTestModel> list = dao.query(ssb1, new DalHints());
        Assert.fail();
    } catch (Exception e) {
        assertTrue(e.getMessage().contains("Can not locate table shard"));
    }
    SelectSqlBuilder ssb5 = new SelectSqlBuilder();
    ssb5.selectAll().from(TABLE_NAME).equal("dbIndex", 0, Types.INTEGER);
    try {
        List<ClientTestModel> list = dao.query(ssb5, new DalHints());
        Assert.fail();
    } catch (Exception e) {
        assertTrue(e.getMessage().contains("Can not locate table shard"));
    }
    SelectSqlBuilder ssb6 = new SelectSqlBuilder();
    ssb6.selectAll().from(TABLE_NAME).equal("tableIndex", 0, Types.INTEGER);
    try {
        List<ClientTestModel> list = dao.query(ssb6, new DalHints());
        Assert.fail();
    } catch (Exception e) {
        assertTrue(e.getMessage().contains("Can not locate shard"));
    }
}
Also used : LocalDalPropertiesProvider(com.ctrip.platform.dal.dao.configure.LocalDalPropertiesProvider) FreeSelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.FreeSelectSqlBuilder) SelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 68 with SelectSqlBuilder

use of com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder in project dal by ctripcorp.

the class BaseDalTableDaoShardByDbTableTest method testQueryListInAllDbShardsAndInTableShards.

@Test
public void testQueryListInAllDbShardsAndInTableShards() throws SQLException {
    SelectSqlBuilder ssb1 = new SelectSqlBuilder();
    ssb1.selectAll().from(TABLE_NAME).where(" id = 4 ");
    try {
        Set<String> set = new HashSet<>();
        set.add("0");
        set.add("1");
        set.add("2");
        List<ClientTestModel> list = dao.query(ssb1, new DalHints().inAllShards().inTableShards(set));
        Assert.assertEquals(0, list.size());
    } catch (Exception e) {
        Assert.fail();
    }
    SelectSqlBuilder ssb2 = new SelectSqlBuilder();
    ssb2.selectAll().from(TABLE_NAME).where(" id = 3 ");
    try {
        Set<String> set = new HashSet<>();
        set.add("0");
        set.add("1");
        set.add("2");
        List<ClientTestModel> list = dao.query(ssb2, new DalHints().inAllShards().inTableShards(set));
        Assert.assertEquals(2, list.size());
    } catch (Exception e) {
        Assert.fail();
    }
}
Also used : FreeSelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.FreeSelectSqlBuilder) SelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 69 with SelectSqlBuilder

use of com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder in project dal by ctripcorp.

the class BaseDalTableDaoShardByDbTableTest method testQueryListDbShardByAndTableShardBy.

@Test
public void testQueryListDbShardByAndTableShardBy() throws SQLException {
    int index = 1;
    List<String> list1 = new ArrayList<>();
    list1.add("1");
    list1.add("2");
    list1.add("4");
    list1.add("3");
    list1.add("6");
    list1.add("9");
    list1.add("8");
    list1.add("12");
    list1.add("16");
    list1.add("18");
    list1.add("24");
    list1.add("32");
    StatementParameters parameters1 = new StatementParameters();
    parameters1.setInParameter(index++, "quantity", Types.INTEGER, list1);
    SelectSqlBuilder ssb1 = new SelectSqlBuilder();
    ssb1.selectAll().from(TABLE_NAME).where(" quantity in (?) ").with(parameters1);
    // By DalTableDao
    try {
        List<ClientTestModel> result = dao.query(ssb1, new DalHints().shardBy("quantity").tableShardBy("quantity"));
        Assert.assertEquals(1, result.size());
        ClientTestModel model = result.get(0);
        Assert.assertEquals(2, model.getId().intValue());
        Assert.assertEquals(6, model.getQuantity().intValue());
        Assert.assertEquals(0, model.getDbIndex().intValue());
        Assert.assertEquals(2, model.getTableIndex().intValue());
    } catch (Exception e) {
        Assert.fail();
    }
}
Also used : FreeSelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.FreeSelectSqlBuilder) SelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 70 with SelectSqlBuilder

use of com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder in project dal by ctripcorp.

the class BaseDalTableDaoShardByDbTableTest method testQueryListInAllDbShardsAndTableShardBy.

@Test
public void testQueryListInAllDbShardsAndTableShardBy() throws SQLException {
    int index = 1;
    List<String> list1 = new ArrayList<>();
    list1.add("0");
    StatementParameters parameters1 = new StatementParameters();
    parameters1.setInParameter(index, "tableIndex", Types.INTEGER, list1);
    SelectSqlBuilder ssb1 = new SelectSqlBuilder();
    ssb1.selectAll().from(TABLE_NAME).where(" tableIndex in (?) ").with(parameters1);
    try {
        List<ClientTestModel> result = dao.query(ssb1, new DalHints().inAllShards().tableShardBy("tableIndex"));
        Assert.assertEquals(2, result.size());
    } catch (Exception e) {
        Assert.fail();
    }
    List<String> list2 = new ArrayList<>();
    list2.add("0");
    list2.add("1");
    StatementParameters parameters2 = new StatementParameters();
    parameters2.setInParameter(index, "tableIndex", Types.INTEGER, list2);
    SelectSqlBuilder ssb2 = new SelectSqlBuilder();
    ssb2.selectAll().from(TABLE_NAME).where(" tableIndex in (?) ").with(parameters2);
    try {
        List<ClientTestModel> result = dao.query(ssb2, new DalHints().inAllShards().tableShardBy("tableIndex"));
        Assert.assertEquals(6, result.size());
    } catch (Exception e) {
        Assert.fail();
    }
}
Also used : FreeSelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.FreeSelectSqlBuilder) SelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder) SQLException(java.sql.SQLException) Test(org.junit.Test)

Aggregations

SelectSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder)93 Test (org.junit.Test)71 ArrayList (java.util.ArrayList)35 FreeSelectSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.FreeSelectSqlBuilder)28 DalHints (com.ctrip.platform.dal.dao.DalHints)15 SQLException (java.sql.SQLException)15 ClientTestModel (com.ctrip.platform.dal.dao.unitbase.ClientTestModel)5 HashSet (java.util.HashSet)5 DalTableDao (com.ctrip.platform.dal.dao.DalTableDao)4 DalException (com.ctrip.platform.dal.exceptions.DalException)4 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)3 ClientTestModel (test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel)3 LocalDalPropertiesProvider (com.ctrip.platform.dal.dao.configure.LocalDalPropertiesProvider)1 GenTaskByFreeSql (com.ctrip.platform.dal.daogen.entity.GenTaskByFreeSql)1 GenTaskBySqlBuilder (com.ctrip.platform.dal.daogen.entity.GenTaskBySqlBuilder)1