use of com.ctrip.platform.dal.dao.DalTableDao in project dal by ctripcorp.
the class BatchUpdateTaskTestStub method testUpdatableWithIntVersion.
@Test
public void testUpdatableWithIntVersion() throws SQLException {
BatchUpdateTask<UpdatableIntVersionModel> test = new BatchUpdateTask<>();
DalParser<UpdatableIntVersionModel> parser = new DalDefaultJpaParser<>(UpdatableIntVersionModel.class, getDbName());
test.initialize(parser);
DalTableDao<UpdatableIntVersionModel> dao = new DalTableDao<>(parser);
DalHints hints = new DalHints();
Integer[] oldValue = new Integer[3];
List<UpdatableIntVersionModel> pojos = dao.query("1=1", new StatementParameters(), new DalHints());
int i = 0;
for (UpdatableIntVersionModel model : pojos) {
model.setAddress("1122334455");
oldValue[i++] = model.getTableIndex();
}
int[] result = execute(test, 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 testVersionNotSet.
@Test
public void testVersionNotSet() throws SQLException {
BatchUpdateTask<UpdatableVersionModel> test = new BatchUpdateTask<>();
DalParser<UpdatableVersionModel> parser = new DalDefaultJpaParser<>(UpdatableVersionModel.class, getDbName());
test.initialize(parser);
DalTableDao<UpdatableVersionModel> dao = new DalTableDao<UpdatableVersionModel>(parser);
DalHints hints = new DalHints();
List<UpdatableVersionModel> pojos = dao.query("1=1", new StatementParameters(), new DalHints());
for (UpdatableVersionModel model : pojos) {
model.setAddress("1122334455");
model.setLastChanged(null);
}
try {
execute(test, hints, pojos);
fail();
} catch (SQLException e) {
assertEquals(ErrorCode.ValidateVersion.getMessage(), e.getMessage());
}
}
use of com.ctrip.platform.dal.dao.DalTableDao in project dal by ctripcorp.
the class BatchUpdateTaskTestStub method testDaoUpdateIfChangedField.
@Test
public void testDaoUpdateIfChangedField() throws SQLException {
DalTableDao<UpdatableClientTestModel> dao = new DalTableDao<>(UpdatableClientTestModel.class, getDbName());
DalHints hints = new DalHints();
try {
List<UpdatableClientTestModel> pojos = getAll(UpdatableClientTestModel.class);
int i = 0;
for (UpdatableClientTestModel model : pojos) {
model.setType((Short) null);
if (i % 2 == 0)
model.setQuantity(-100);
if (i % 2 == 1)
model.setDbIndex(-200);
if (i % 2 == 1)
model.setTableIndex(-300);
i++;
}
int[] result = dao.batchUpdate(hints, pojos);
assertArrayEquals(new int[] { 1, 1, 1 }, result);
i = 0;
pojos = getAll(UpdatableClientTestModel.class);
for (UpdatableClientTestModel model : pojos) {
assertNull(model.getType());
assertNotNull(model.getLastChanged());
if (i % 2 == 0)
assertEquals(model.getQuantity().intValue(), -100);
else
assertEquals(model.getQuantity().intValue(), 10 + i);
if (i % 2 == 1)
assertEquals(model.getDbIndex().intValue(), -200);
else
assertEquals(model.getDbIndex().intValue(), 0);
if (i % 2 == 1)
assertEquals(model.getTableIndex().intValue(), -300);
else
assertEquals(model.getTableIndex().intValue(), i);
i++;
}
} catch (SQLException e) {
e.printStackTrace();
fail();
}
}
use of com.ctrip.platform.dal.dao.DalTableDao in project dal by ctripcorp.
the class BatchUpdateTaskTestStub method testIncludeExcludeColumns.
@Test
public void testIncludeExcludeColumns() throws SQLException {
BatchUpdateTask<UpdatableIntVersionModel> test = new BatchUpdateTask<>();
DalParser<UpdatableIntVersionModel> parser = new DalDefaultJpaParser<>(UpdatableIntVersionModel.class, getDbName());
test.initialize(parser);
DalTableDao<UpdatableIntVersionModel> dao = new DalTableDao<>(parser);
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 = execute(test, 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 DalTableDaoTestStub method testCombinedInsertWithKeyInsertBack.
/**
* Test Insert multiple entities with one SQL Statement
* @throws SQLException
*/
@Test
public void testCombinedInsertWithKeyInsertBack() throws SQLException {
if (!diff.supportInsertValues || !diff.supportGetGeneratedKeys)
return;
DalTableDao<ClientTestModelJpa> dao = new DalTableDao(new DalDefaultJpaParser<>(ClientTestModelJpa.class, dbName));
List<ClientTestModelJpa> entities = new ArrayList<>();
for (int i = 0; i < 3; i++) {
ClientTestModelJpa model = new ClientTestModelJpa();
model.setQuantity(10 + 1 % 3);
model.setType(((Number) (1 % 3)).shortValue());
model.setAddress("CTRIP");
entities.add(model);
}
KeyHolder holder = diff.supportGetGeneratedKeys ? new KeyHolder() : null;
DalHints hints = new DalHints();
int res = dao.combinedInsert(hints.setIdentityBack(), holder, entities);
assertEquals(3, res, 4 + 3);
Assert.assertEquals(3, holder.size());
Assert.assertTrue(holder.getKeyList().get(0).containsKey("GENERATED_KEY"));
int i = 0;
for (ClientTestModelJpa pojo : entities) Assert.assertEquals(holder.getKey(i++).intValue(), pojo.getId().intValue());
}
Aggregations