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();
}
}
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());
}
}
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());
}
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());
}
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());
}
}
Aggregations