Search in sources :

Example 1 with DalTableDao

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

the class PartialQueryTableDaoUnitTest method testFindBySelectedField.

@Test
public void testFindBySelectedField() throws Exception {
    DalTableDao<Person> client = new DalTableDao<>(new DalDefaultJpaParser<>(Person.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");
    builder.in("PeopleID", peopleIds, Types.INTEGER, false);
    builder.and();
    builder.in("CityID", cityIds, Types.INTEGER, false);
    try {
        List<Person> ret = client.query(builder, new DalHints().inAllShards().inTableShard(1));
        Assert.assertNull(ret.get(0).getCountryID());
        Assert.assertNull(ret.get(0).getPeopleID());
    } catch (DalException e) {
        Assert.fail();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalException(com.ctrip.platform.dal.exceptions.DalException) ArrayList(java.util.ArrayList) SelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Example 2 with DalTableDao

use of com.ctrip.platform.dal.dao.DalTableDao 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 : DalHints(com.ctrip.platform.dal.dao.DalHints) DalException(com.ctrip.platform.dal.exceptions.DalException) ArrayList(java.util.ArrayList) SelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Example 3 with DalTableDao

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

the class SingleUpdateTaskTestStub method testUpdatableWithVersionByDao.

@Test
public void testUpdatableWithVersionByDao() throws SQLException {
    DalParser<UpdatableVersionModel> parser = new DalDefaultJpaParser<>(UpdatableVersionModel.class, getDbName());
    DalTableDao<UpdatableVersionModel> dao = new DalTableDao<>(parser);
    DalHints hints = new DalHints();
    UpdatableVersionModel model = dao.query("1=1", new StatementParameters(), new DalHints()).get(0);
    model.setAddress("1122334455");
    model.getLastChanged().setTime(model.getLastChanged().getTime() + 100);
    int result = dao.update(hints, model);
    assertIntEquals(0, result);
    model = dao.queryByPk(model, new DalHints());
    assertEquals("SH INFO", model.getAddress());
    model = dao.query("1=1", new StatementParameters(), new DalHints()).get(0);
    model.setAddress("1122334455");
    result = dao.update(hints, model);
    assertIntEquals(1, result);
    model = dao.queryByPk(model, new DalHints());
    assertEquals("1122334455", model.getAddress());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) DalDefaultJpaParser(com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Example 4 with DalTableDao

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

the class SingleUpdateTaskTestStub method testIncludeColumnsByDao.

@Test
public void testIncludeColumnsByDao() throws SQLException {
    //Table Index and Address is not updatable
    DalParser<NonUpdatableModel> parser = new DalDefaultJpaParser<>(NonUpdatableModel.class, getDbName());
    DalHints hints = new DalHints();
    DalTableDao<NonUpdatableModel> dao = new DalTableDao<>(parser);
    NonUpdatableModel model = dao.query("1=1", new StatementParameters(), new DalHints()).get(0);
    String oldAddr = model.getAddress();
    Integer oldTableIndex = model.getTableIndex();
    Integer oldQuantity = model.getQuantity();
    model.setDbIndex(-100);
    model.setAddress("1122334455");
    model.setTableIndex(100);
    model.setQuantity(500);
    model.setType((short) 8);
    int result = dao.update(hints.include("dbIndex", "type"), model);
    assertIntEquals(1, result);
    model = dao.queryByPk(model, new DalHints());
    assertEquals(oldAddr, model.getAddress());
    assertEquals(oldTableIndex, model.getTableIndex());
    assertEquals(oldQuantity, model.getQuantity());
    assertEquals(-100, model.getDbIndex().intValue());
    assertEquals(8, model.getType().shortValue());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) DalDefaultJpaParser(com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Example 5 with DalTableDao

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

the class BatchUpdateTaskTestStub method testDaoIncludeExcludeColumns.

@Test
public void testDaoIncludeExcludeColumns() throws SQLException {
    DalTableDao<UpdatableIntVersionModel> dao = new DalTableDao<>(UpdatableIntVersionModel.class, getDbName());
    DalHints hints = new DalHints().exclude("dbIndex").include("quantity", "dbIndex", "address");
    List<UpdatableIntVersionModel> pojos = dao.query("1=1", new StatementParameters(), new DalHints());
    for (UpdatableIntVersionModel model : pojos) {
        model.setQuantity(500);
        model.setDbIndex(100);
        model.setAddress("1122334455");
    }
    int[] result = dao.batchUpdate(hints, pojos);
    assertArrayEquals(new int[] { 1, 1, 1 }, result);
    pojos = dao.query("1=1", new StatementParameters(), new DalHints());
    for (UpdatableIntVersionModel model : pojos) {
        assertEquals("1122334455", model.getAddress());
        assertEquals(500, model.getQuantity().intValue());
        assertEquals(0, model.getDbIndex().intValue());
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Aggregations

DalHints (com.ctrip.platform.dal.dao.DalHints)20 DalTableDao (com.ctrip.platform.dal.dao.DalTableDao)20 Test (org.junit.Test)20 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)16 DalDefaultJpaParser (com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser)13 BatchUpdateTask (com.ctrip.platform.dal.dao.task.BatchUpdateTask)8 SQLException (java.sql.SQLException)3 SelectSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder)2 DalException (com.ctrip.platform.dal.exceptions.DalException)2 Timestamp (java.sql.Timestamp)2 ArrayList (java.util.ArrayList)2