use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class BaseDalTabelDaoShardByTableTest method testInsertMultipleAsListWithKeyHolderAsyncCallback.
@Test
public void testInsertMultipleAsListWithKeyHolderAsyncCallback() throws SQLException {
DalHints hints;
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 {
hints = asyncHints();
res = dao.insert(hints, holder, entities);
res = assertIntArray(res, hints);
fail();
} catch (Exception e) {
}
for (int i = 0; i < mod; i++) {
int j = 1;
holder = null;
// By tabelShard
// holder = new KeyHolder();
hints = asyncHints();
res = dao.insert(hints.inTableShard(i), holder, entities);
res = assertIntArray(res, hints);
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();
hints = intHints();
res = dao.insert(hints.setTableShardValue(i), holder, entities);
res = assertIntArray(res, hints);
assertEquals((i + 1) + j++ * 3, getCount(i));
// By shardColValue
// holder = new KeyHolder();
hints = asyncHints();
res = dao.insert(hints.setShardColValue("index", i), holder, entities);
res = assertIntArray(res, hints);
assertEquals((i + 1) + j++ * 3, getCount(i));
// By shardColValue
// holder = new KeyHolder();
hints = intHints();
res = dao.insert(hints.setShardColValue("tableIndex", i), holder, entities);
res = assertIntArray(res, hints);
assertEquals((i + 1) + j++ * 3, getCount(i));
// By fields same shard
// holder = new KeyHolder();
hints = asyncHints();
entities.get(0).setTableIndex(i);
entities.get(1).setTableIndex(i);
entities.get(2).setTableIndex(i);
res = dao.insert(hints, holder, entities);
res = assertIntArray(res, hints);
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);
hints = intHints();
res = dao.insert(hints, null, entities);
res = assertIntArray(res, hints);
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"));
}
use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class DalConcurrentMysqlTest method setUpBeforeClass.
@BeforeClass
public static void setUpBeforeClass() throws Exception {
DalHints hints = new DalHints();
String[] sqls = new String[] { DROP_TABLE_SQL, CREATE_TABLE_SQL };
client.batchUpdate(sqls, hints);
List<ClientTestModel> models = new ArrayList<>();
for (int i = 0; i < INSERT_COUNT; i++) {
ClientTestModel model = new ClientTestModel();
model.setAddress("SH-" + i);
model.setLastChanged(new Timestamp(System.currentTimeMillis()));
model.setQuantity(i % 10);
model.setType((short) (i % 3));
models.add(model);
}
KeyHolder holder = new KeyHolder();
int[] counts = dao.insert(hints, holder, models);
System.out.println("The insert ClientTestModel count: " + counts);
generateIds = holder.getIdList();
System.out.println("Ids: " + generateIds.size());
Assert.assertEquals(INSERT_COUNT, generateIds.size());
}
use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class DalConcurrentSqlServerTestStub method setUpBeforeClass.
/**
* Create the test tables
* @throws Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
DalHints hints = new DalHints();
StatementParameters parameters = new StatementParameters();
String[] sqls = new String[] { DROP_TABLE_SQL, CREATE_TABLE_SQL };
for (int i = 0; i < sqls.length; i++) {
client.update(sqls[i], parameters, hints);
}
List<ClientTestModel> models = new ArrayList<>();
for (int i = 0; i < INSERT_COUNT; i++) {
ClientTestModel model = new ClientTestModel();
model.setAddress("SH-" + i);
model.setLastChanged(new Timestamp(System.currentTimeMillis()));
model.setQuantity(i % 10);
model.setType((short) (i % 3));
models.add(model);
}
KeyHolder holder = new KeyHolder();
int[] counts = dao.insert(hints, holder, models);
System.out.println("The insert ClientTestModel count: " + counts);
generateIds = holder.getIdList();
System.out.println("Ids: " + generateIds.size());
Assert.assertEquals(INSERT_COUNT, generateIds.size());
}
use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class DalTableDaoTestStub method testCombinedInsert.
/**
* Test Insert multiple entities with one SQL Statement
* @throws SQLException
*/
@Test
public void testCombinedInsert() throws SQLException {
if (!diff.supportInsertValues)
return;
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 = diff.supportGetGeneratedKeys ? new KeyHolder() : null;
DalHints hints = new DalHints();
int res = dao.combinedInsert(hints, holder, entities);
assertEquals(3, res, 4 + 3);
if (diff.supportGetGeneratedKeys) {
Assert.assertEquals(3, holder.size());
Assert.assertTrue(holder.getKeyList().get(0).containsKey("GENERATED_KEY"));
}
}
use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class BaseDalTabelDaoShardByTableTest method testCrossShardInsertCallback.
@Test
public void testCrossShardInsertCallback() {
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 = intHints();
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();
}
}
Aggregations