use of com.ctrip.platform.dal.dao.KeyHolder 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());
}
use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class KeyHolderTest method testMergeSequential.
@Test
public void testMergeSequential() {
ExecutorService service = null;
service = new ThreadPoolExecutor(5, 50, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
KeyHolder test = new KeyHolder();
test.initialize(6);
test.requireMerge();
KeyHolder tmpHolder = new KeyHolder();
tmpHolder.addKey(buildKey(0));
tmpHolder.addKey(buildKey(1));
tmpHolder.addKey(buildKey(2));
test.addPatial(new Integer[] { 0, 1, 2 }, tmpHolder);
tmpHolder = new KeyHolder();
tmpHolder.addKey(buildKey(3));
tmpHolder.addKey(buildKey(4));
tmpHolder.addKey(buildKey(5));
test.addPatial(new Integer[] { 3, 4, 5 }, tmpHolder);
try {
int i = 0;
for (Number value : test.getIdList()) {
assertEquals(i++, value);
}
} catch (SQLException e) {
fail();
}
}
use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class KeyHolderTest method testMergeParallel.
@Test
public void testMergeParallel() {
ExecutorService service = null;
service = new ThreadPoolExecutor(5, 50, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
KeyHolder test = new KeyHolder();
test.initialize(30);
test.requireMerge();
List<Future<Boolean>> fList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
fList.add(service.submit(new KeyHolderTask(test, new Integer[] { i * 3, i * 3 + 1, i * 3 + 2 })));
}
for (Future<Boolean> f : fList) try {
f.get();
} catch (InterruptedException | ExecutionException e) {
fail();
}
assertTrue(test.isRequireMerge());
assertTrue(test.isMerged());
assertEquals(30, test.size());
try {
assertEquals(30, test.getIdList().size());
} catch (SQLException e1) {
fail();
}
try {
List<Number> ids = test.getIdList();
for (int i = 0; i < 30; i++) assertEquals(i, ids.get(i));
} catch (SQLException e) {
fail();
}
service.shutdown();
}
use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class CombinedInsertTaskTestStub method testExecuteWithIdInsertBackOldPojo.
@Test
public void testExecuteWithIdInsertBackOldPojo() throws SQLException {
CombinedInsertTask<ClientTestModel> test = new CombinedInsertTask<>();
test.initialize(new ClientTestDalParser(getDbName()));
DalHints hints = new DalHints().setIdentityBack();
if (enableKeyHolder)
hints.setKeyHolder(new KeyHolder());
try {
List<ClientTestModel> pojos = getAll();
for (ClientTestModel pojo : pojos) pojo.setId(null);
execute(test, hints, getAllMap(), pojos);
fail();
} catch (Throwable e) {
}
}
use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class DalDirectClientTestStub method updateTestInsertWithKeyHolder.
/**
* Test the update function with key-holder
*
* @throws SQLException
*/
@Test
public void updateTestInsertWithKeyHolder() throws SQLException {
if (!diff.supportGetGeneratedKeys)
return;
String insertSql = String.format("INSERT INTO %s VALUES(NULL, 10, 1, 'SH INFO', NULL)", TABLE_NAME);
StatementParameters parameters = new StatementParameters();
KeyHolder holder = new KeyHolder();
DalHints hints = new DalHints();
int count = client.update(insertSql, parameters, hints.setKeyHolder(holder));
Assert.assertEquals(1, count);
Assert.assertEquals(1, holder.size());
Assert.assertTrue(holder.getKeyList().get(0).containsKey("GENERATED_KEY"));
Assert.assertEquals(4, queryModelsByIds().size());
}
Aggregations