Search in sources :

Example 76 with SelectSqlBuilder

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

the class BaseDalTabelDaoShardByTableTest method testQueryObjectBySimpleTypeInTableShards.

@Test
public void testQueryObjectBySimpleTypeInTableShards() throws SQLException {
    Set<String> tableShards = new HashSet<>();
    tableShards.add("0");
    tableShards.add("1");
    SelectSqlBuilder ssb1 = new SelectSqlBuilder();
    ssb1.select("id").from(TABLE_NAME).where(" id = 1 ").requireSingle();
    try {
        Integer id1 = dao.queryObject(ssb1, new DalHints().inTableShards(tableShards), Integer.class);
    } catch (Exception e) {
        assertEquals("It is expected to return only 1 result. But the actually count is more than 1", e.getMessage());
    }
    SelectSqlBuilder ssb2 = new SelectSqlBuilder();
    ssb2.select("id").from(TABLE_NAME).where(" id = 1 ").requireFirst();
    try {
        Integer id2 = dao.queryObject(ssb2, new DalHints().inTableShards(tableShards), Integer.class);
        assertTrue(id2 != null);
    } 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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 77 with SelectSqlBuilder

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

the class BaseDalTabelDaoShardByTableTest method testCountTableShardBy.

@Test
public void testCountTableShardBy() throws SQLException {
    int index = 1;
    List<Integer> list = new ArrayList<>();
    list.add(0);
    list.add(1);
    StatementParameters parameters = new StatementParameters();
    parameters.setInParameter(index, "tableIndex", Types.BIGINT, list);
    SelectSqlBuilder ssb1 = new SelectSqlBuilder();
    ssb1.selectAll().from(TABLE_NAME).where(" tableIndex in (?) ").with(parameters).selectCount();
    Number count1 = dao.count(ssb1, new DalHints().tableShardBy("tableIndex"));
    assertEquals(3L, count1);
}
Also used : ArrayList(java.util.ArrayList) FreeSelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.FreeSelectSqlBuilder) SelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder) Test(org.junit.Test)

Example 78 with SelectSqlBuilder

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

the class PartialQueryTableDaoUnitTest method testDetectFieldNotExist.

@Test
public void testDetectFieldNotExist() throws Exception {
    DalTableDao<PersonWithoutName> client = new DalTableDao<>(new DalDefaultJpaParser<>(PersonWithoutName.class));
    List<Integer> peopleIds = new ArrayList<>();
    peopleIds.add(1);
    peopleIds.add(2);
    peopleIds.add(3);
    List<Integer> cityIds = new ArrayList<>();
    cityIds.add(1);
    cityIds.add(2);
    cityIds.add(3);
    SelectSqlBuilder builder = new SelectSqlBuilder();
    builder.select("DataChange_LastTime", "CityID", "Name", "ProvinceID", "PeopleID", "CountryID");
    builder.in("PeopleID", peopleIds, Types.INTEGER, false);
    builder.and();
    builder.in("CityID", cityIds, Types.INTEGER, false);
    try {
        client.query(builder, new DalHints().inAllShards().inTableShard(1));
        Assert.fail();
    } catch (DalException e) {
        e.printStackTrace();
        assertEquals(ErrorCode.FieldNotExists.getCode(), e.getErrorCode());
    }
}
Also used : DalException(com.ctrip.platform.dal.exceptions.DalException) ArrayList(java.util.ArrayList) SelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder)

Example 79 with SelectSqlBuilder

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

the class BaseDalTabelDaoShardByTableTest method testQueryObjectInAllTableShards.

@Test
public void testQueryObjectInAllTableShards() throws SQLException {
    // requireSingle result in exception
    SelectSqlBuilder ssb1 = new SelectSqlBuilder();
    ssb1.selectAll().from(TABLE_NAME).where(" id = 1 ").requireSingle();
    try {
        ClientTestModel model = dao.queryObject(ssb1, new DalHints().inAllTableShards());
        Assert.fail();
    } catch (Exception e) {
        assertEquals("It is expected to return only 1 result. But the actually count is more than 1", e.getMessage());
    }
    // requireFirst returns first result,which is not recommended when used in inAllTableShards
    SelectSqlBuilder ssb2 = new SelectSqlBuilder();
    ssb2.selectAll().from(TABLE_NAME).where(" id = 1 ").requireFirst();
    try {
        ClientTestModel model = dao.queryObject(ssb2, new DalHints().inAllTableShards());
        assertTrue(model != null);
    } 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 80 with SelectSqlBuilder

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

the class BaseDalTabelDaoShardByTableTest method testQueryObjectTableShardBy.

@Test
public void testQueryObjectTableShardBy() throws SQLException {
    int index = 1;
    List<String> list = new ArrayList<>();
    list.add("0");
    list.add("1");
    StatementParameters parameters = new StatementParameters();
    parameters.setInParameter(index, "tableIndex", Types.INTEGER, list);
    SelectSqlBuilder ssb1 = new SelectSqlBuilder();
    ssb1.selectAll().from(TABLE_NAME).where(" tableIndex in (?) ").with(parameters).requireSingle();
    try {
        ClientTestModel model1 = dao.queryObject(ssb1, new DalHints().tableShardBy("tableIndex"));
    } catch (Exception e) {
        assertEquals("It is expected to return only 1 result. But the actually count is more than 1", e.getMessage());
    }
    SelectSqlBuilder ssb2 = new SelectSqlBuilder();
    ssb2.selectAll().from(TABLE_NAME).where(" tableIndex in (?) ").with(parameters).requireFirst();
    try {
        ClientTestModel model2 = dao.queryObject(ssb2, new DalHints().tableShardBy("tableIndex"));
        assertTrue(model2 != null);
    } catch (Exception e) {
        Assert.fail();
    }
}
Also used : ArrayList(java.util.ArrayList) 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