use of com.ctrip.platform.dal.dao.DalTableDao in project dal by ctripcorp.
the class BatchUpdateTaskTestStub method testUpdatableWithIntVersionByDao.
@Test
public void testUpdatableWithIntVersionByDao() throws SQLException {
DalParser<UpdatableIntVersionModel> parser = new DalDefaultJpaParser<>(UpdatableIntVersionModel.class, getDbName());
DalTableDao<UpdatableIntVersionModel> dao = new DalTableDao<>(parser);
DalHints hints = new DalHints();
List<UpdatableIntVersionModel> pojos = dao.query("1=1", new StatementParameters(), new DalHints());
int i = 0;
for (UpdatableIntVersionModel model : pojos) {
model.setAddress("1122334455");
// Make the version incorrect
model.setTableIndex(-1);
}
int[] result = dao.batchUpdate(hints, pojos);
assertArrayEquals(new int[] { 0, 0, 0 }, result);
pojos = dao.query("1=1", new StatementParameters(), new DalHints());
for (UpdatableIntVersionModel model : pojos) //Still old value because version is incorrect
assertEquals("SH INFO", model.getAddress());
// Now the right case
Integer[] oldValue = new Integer[3];
pojos = dao.query("1=1", new StatementParameters(), new DalHints());
i = 0;
for (UpdatableIntVersionModel model : pojos) {
model.setAddress("1122334455");
oldValue[i++] = model.getTableIndex();
}
result = dao.batchUpdate(hints, pojos);
assertArrayEquals(new int[] { 1, 1, 1 }, result);
pojos = dao.query("1=1", new StatementParameters(), new DalHints());
i = 0;
for (UpdatableIntVersionModel model : pojos) {
assertEquals("1122334455", model.getAddress());
Assert.assertTrue(oldValue[i++] + 1 == model.getTableIndex());
}
}
use of com.ctrip.platform.dal.dao.DalTableDao in project dal by ctripcorp.
the class BatchUpdateTaskTestStub method testNotUpdatableVersion.
@Test
public void testNotUpdatableVersion() throws SQLException {
BatchUpdateTask<NonUpdatableVersionModel> test = new BatchUpdateTask<>();
DalParser<NonUpdatableVersionModel> parser = new DalDefaultJpaParser<>(NonUpdatableVersionModel.class, getDbName());
test.initialize(parser);
DalTableDao<NonUpdatableVersionModel> dao = new DalTableDao<>(parser);
DalHints hints = new DalHints();
List<NonUpdatableVersionModel> pojos = dao.query("1=1", new StatementParameters(), new DalHints());
for (NonUpdatableVersionModel model : pojos) {
model.setAddress("1122334455");
}
int[] result = execute(test, hints, pojos);
assertArrayEquals(new int[] { 1, 1, 1 }, result);
pojos = dao.query("1=1", new StatementParameters(), new DalHints());
for (NonUpdatableVersionModel model : pojos) assertEquals("1122334455", model.getAddress());
}
use of com.ctrip.platform.dal.dao.DalTableDao in project dal by ctripcorp.
the class BatchUpdateTaskTestStub method testDaoExecuteUpdatableEntity.
@Test
public void testDaoExecuteUpdatableEntity() throws SQLException {
DalTableDao<UpdatableClientTestModel> dao = new DalTableDao<>(UpdatableClientTestModel.class, getDbName());
DalHints hints = new DalHints();
try {
List<UpdatableClientTestModel> pojos = getAll(UpdatableClientTestModel.class);
for (UpdatableClientTestModel model : pojos) model.setAddress("1122334455");
int[] result = dao.batchUpdate(hints, pojos);
assertEquals(3, result.length);
assertArrayEquals(new int[] { 1, 1, 1 }, result);
assertEquals(3, getCount());
pojos = getAll(UpdatableClientTestModel.class);
for (UpdatableClientTestModel model : pojos) assertEquals("1122334455", model.getAddress());
} catch (SQLException e) {
e.printStackTrace();
fail();
}
}
use of com.ctrip.platform.dal.dao.DalTableDao in project dal by ctripcorp.
the class BatchUpdateTaskTestStub method testDaoExcludeColumns.
@Test
public void testDaoExcludeColumns() throws SQLException {
DalTableDao<UpdatableIntVersionModel> dao = new DalTableDao<>(UpdatableIntVersionModel.class, getDbName());
DalHints hints = new DalHints().exclude("dbIndex");
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());
}
}
use of com.ctrip.platform.dal.dao.DalTableDao in project dal by ctripcorp.
the class PartialQueryTableDaoUnitTest method testFindByPartial.
@Test
public void testFindByPartial() throws Exception {
DalTableDao<Person> client = new DalTableDao<>(new DalDefaultJpaParser<>(Person.class));
List<Person> pl;
pl = client.query("1=1", new StatementParameters(), new DalHints().partialQuery("Name", "CountryID").inShard(1).inTableShard(1));
assertPersonList(pl);
pl = client.queryFrom("1=1", new StatementParameters(), new DalHints().partialQuery("Name", "CountryID").inAllShards().inTableShard(1), 1, 10);
assertPersonList(pl);
Person sample = new Person();
sample.setCountryID(1);
pl = client.queryLike(sample, new DalHints().partialQuery("Name", "CountryID").inAllShards().inTableShard(1));
assertPersonList(pl);
pl = client.queryTop("1=1", new StatementParameters(), new DalHints().partialQuery("Name", "CountryID").inAllShards().inTableShard(1), 100);
assertPersonList(pl);
Person test = client.queryByPk(1, new DalHints().partialQuery("Name", "CountryID").inShard(1).inTableShard(1));
assertPerson(test);
test.setPeopleID(1);
test = client.queryByPk(test, new DalHints().partialQuery("Name", "CountryID").inShard(1).inTableShard(1));
assertPerson(test);
test = client.queryFirst("1=1", new StatementParameters(), new DalHints().partialQuery("Name", "CountryID").inShard(1).inTableShard(1));
assertPerson(test);
}
Aggregations