use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class MySqlHelperTest method replaceTest.
@Test
public void replaceTest() throws SQLException {
int count = 3;
List<Person> persons = new ArrayList<>();
for (int i = 1; i <= count; i++) {
Person pojo1 = new Person();
pojo1.setID(i);
pojo1.setName("forest" + i);
pojo1.setBirth(new Timestamp(System.currentTimeMillis()));
persons.add(pojo1);
}
client.delete(hints, persons);
KeyHolder holder = new KeyHolder();
helper.replace(holder, hints, persons);
List<Map<String, Object>> generateKeys = holder.getKeyList();
assertTrue(generateKeys.size() == 3);
assertTrue(generateKeys.get(0).containsKey("GENERATED_KEY"));
persons.get(1).setName("jack1");
helper.replace(holder, new DalHints(), persons);
Person rep = client.queryByPk(2, hints);
assertTrue(rep.getName().equals("jack1"));
client.delete(hints, persons);
}
use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class MySqlHelperTest method multipleInsert.
@Test
public void multipleInsert() throws SQLException {
int count = 3;
List<Person> persons = new ArrayList<>();
for (int i = 1; i <= count; i++) {
Person pojo1 = new Person();
pojo1.setID(i);
pojo1.setName("forest" + i);
pojo1.setBirth(new Timestamp(System.currentTimeMillis()));
persons.add(pojo1);
}
KeyHolder holder = new KeyHolder();
client.combinedInsert(hints, holder, persons);
List<Map<String, Object>> generateKeys = holder.getKeyList();
assertTrue(generateKeys.size() == 3);
assertTrue(generateKeys.get(0).containsKey("GENERATED_KEY"));
for (Map<String, Object> genk : generateKeys) {
Number id = (Number) genk.get("GENERATED_KEY");
Person person = new Person();
person.setID(id.intValue());
client.delete(hints, person);
}
}
use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class BaseDalTableDaoShardByDbTableTest method testCrossShardCombinedInsert.
private void testCrossShardCombinedInsert(DalHints oldhints) throws SQLException {
try {
deleteAllShardsByDbTable(dao, mod, tableMod);
ClientTestModel[] pList = new ClientTestModel[mod * (1 + tableMod) * tableMod / 2];
int x = 0;
for (int i = 0; i < mod; i++) {
for (int j = 0; j < tableMod; j++) {
for (int k = 0; k < j + 1; k++) {
ClientTestModel p = new ClientTestModel();
p = new ClientTestModel();
p.setId(1 + k);
p.setAddress("aaa");
p.setDbIndex(i);
p.setTableIndex(j);
pList[x++] = p;
}
}
}
KeyHolder keyHolder = createKeyHolder();
DalHints hints = copy(oldhints);
// hints.set(DalHintEnum.sequentialExecution);
int res = dao.combinedInsert(hints, keyHolder, Arrays.asList(pList));
assertInt(res, hints);
assertKeyHolderCrossShard(keyHolder);
for (int i = 0; i < mod; i++) {
for (int j = 0; j < tableMod; j++) {
Assert.assertEquals(j + 1, getCount(i, j));
}
}
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
}
use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class BaseDalTabelDaoShardByTableTest method testCrossShardInsertAsync.
@Test
public void testCrossShardInsertAsync() {
if (!diff.supportInsertValues)
return;
try {
deleteAllShards();
ClientTestModel p = new ClientTestModel();
ClientTestModel[] pList = new ClientTestModel[6];
p = new ClientTestModel();
p.setId(1);
p.setAddress("aaa");
p.setTableIndex(0);
pList[0] = p;
p = new ClientTestModel();
p.setId(2);
p.setAddress("aaa");
p.setTableIndex(1);
pList[1] = p;
p = new ClientTestModel();
p.setId(3);
p.setAddress("aaa");
p.setTableIndex(2);
pList[2] = p;
p = new ClientTestModel();
p.setId(4);
p.setAddress("aaa");
p.setTableIndex(3);
pList[3] = p;
p = new ClientTestModel();
p.setId(5);
p.setAddress("aaa");
p.setTableIndex(4);
pList[4] = p;
p = new ClientTestModel();
p.setId(5);
p.setAddress("aaa");
p.setTableIndex(5);
pList[5] = p;
//new KeyHolder();
KeyHolder keyHolder = null;
DalHints hints = asyncHints();
int res = dao.combinedInsert(hints, keyHolder, Arrays.asList(pList));
res = assertInt(res, hints);
assertEquals(2, getCount(0));
assertEquals(2, getCount(1));
assertEquals(1, getCount(2));
assertEquals(1, getCount(3));
} catch (Exception e) {
fail();
}
}
use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class BaseDalTabelDaoShardByTableTest method testInsertMultipleAsListWithKeyHolder.
/**
* Test Insert multiple entities with key-holder
* @throws SQLException
*/
@Test
public void testInsertMultipleAsListWithKeyHolder() throws SQLException {
List<ClientTestModel> entities = new ArrayList<ClientTestModel>();
for (int i = 0; i < 3; i++) {
ClientTestModel model = new ClientTestModel();
model.setQuantity(10 + 1 % 3);
model.setType(((Number) (1 % 3)).shortValue());
model.setAddress("CTRIP");
entities.add(model);
}
KeyHolder holder = new KeyHolder();
int[] res;
try {
res = dao.insert(new DalHints(), holder, entities);
fail();
} catch (Exception e) {
}
for (int i = 0; i < mod; i++) {
int j = 1;
holder = null;
// By tabelShard
// holder = new KeyHolder();
res = dao.insert(new DalHints().inTableShard(i), holder, entities);
assertEquals((i + 1) + j++ * 3, getCount(i));
// assertEquals(3, res);
// assertEquals(3, holder.getKeyList().size());
// assertTrue(holder.getKey(0).longValue() > 0);
// assertTrue(holder.getKeyList().get(0).containsKey("GENERATED_KEYS"));
// By tableShardValue
// holder = new KeyHolder();
res = dao.insert(new DalHints().setTableShardValue(i), holder, entities);
assertEquals((i + 1) + j++ * 3, getCount(i));
// By shardColValue
// holder = new KeyHolder();
res = dao.insert(new DalHints().setShardColValue("index", i), holder, entities);
assertEquals((i + 1) + j++ * 3, getCount(i));
// By shardColValue
// holder = new KeyHolder();
res = dao.insert(new DalHints().setShardColValue("tableIndex", i), holder, entities);
assertEquals((i + 1) + j++ * 3, getCount(i));
// By fields same shard
// holder = new KeyHolder();
entities.get(0).setTableIndex(i);
entities.get(1).setTableIndex(i);
entities.get(2).setTableIndex(i);
res = dao.insert(new DalHints(), holder, entities);
assertEquals((i + 1) + j++ * 3, getCount(i));
}
deleteAllShards();
// By fields not same shard
holder = new KeyHolder();
entities.get(0).setTableIndex(0);
entities.get(1).setTableIndex(1);
entities.get(2).setTableIndex(2);
res = dao.insert(new DalHints(), null, entities);
assertEquals(1, getCount(0));
assertEquals(1, getCount(1));
assertEquals(1, getCount(2));
// assertEquals(3, res);
// assertEquals(3, holder.getKeyList().size());
// assertTrue(holder.getKey(0).longValue() > 0);
// assertTrue(holder.getKeyList().get(0).containsKey("GENERATED_KEYS"));
}
Aggregations