use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class DaoOfLoginUser method insertUser.
public int insertUser(LoginUser user) throws SQLException {
if (null == user)
return 0;
KeyHolder keyHolder = new KeyHolder();
DalHints hints = DalHints.createIfAbsent(null);
client.insert(hints, keyHolder, user);
return keyHolder.getKey().intValue();
}
use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class BaseDalTabelDaoShardByTableTest method testCrossShardInsert.
@Test
public void testCrossShardInsert() {
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;
dao.combinedInsert(new DalHints(), keyHolder, Arrays.asList(pList));
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 testCombinedInsertAsyncCallbackWithPkInsertBack.
@Test
public void testCombinedInsertAsyncCallbackWithPkInsertBack() throws SQLException {
if (!INSERT_PK_BACK_ALLOWED)
return;
DalTableDao<ClientTestModel> dao = new DalTableDao<ClientTestModel>(ClientTestModel.class, databaseName, TABLE_NAME);
ClientTestModel[] entities = new ClientTestModel[3];
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" + i);
entities[i] = model;
}
int res;
for (int i = 0; i < mod; i++) {
int j = 1;
KeyHolder holder = new KeyHolder();
// By tabelShard
// holder = new KeyHolder();
DalHints hints = asyncHints();
res = dao.combinedInsert(hints.inTableShard(i).setIdentityBack(), holder, Arrays.asList(entities));
res = assertInt(res, hints);
for (ClientTestModel model : entities) {
assertEquals(dao.queryByPk(model, new DalHints().inTableShard(i)).getAddress(), model.getAddress());
}
// By tableShardValue
holder = new KeyHolder();
hints = intHints();
res = dao.combinedInsert(hints.setTableShardValue(i).setIdentityBack(), holder, Arrays.asList(entities));
res = assertInt(res, hints);
for (ClientTestModel model : entities) {
assertEquals(dao.queryByPk(model, new DalHints().inTableShard(i)).getAddress(), model.getAddress());
}
}
}
use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class BaseDalTabelDaoShardByTableTest method testInsertSingle.
/**
* Test Insert multiple entities one by one
* @throws SQLException
*/
@Test
public void testInsertSingle() throws SQLException {
ClientTestModel model = new ClientTestModel();
model.setQuantity(10 + 1 % 3);
model.setType(((Number) (1 % 3)).shortValue());
model.setAddress("CTRIP");
int res;
try {
res = dao.insert(new DalHints(), model);
fail();
} catch (Throwable e) {
}
for (int i = 0; i < mod; i++) {
int j = 1;
// By tabelShard
res = dao.insert(new DalHints().inTableShard(i), model);
assertEquals((i + 1) + j++ * 1, getCount(i));
// By tableShardValue
res = dao.insert(new DalHints().setTableShardValue(i), model);
assertEquals((i + 1) + j++ * 1, getCount(i));
// By shardColValue
res = dao.insert(new DalHints().setShardColValue("index", i), model);
assertEquals((i + 1) + j++ * 1, getCount(i));
// By shardColValue
res = dao.insert(new DalHints().setShardColValue("tableIndex", i), model);
assertEquals((i + 1) + j++ * 1, getCount(i));
// By fields
model.setTableIndex(i);
res = dao.insert(new DalHints(), model);
assertEquals((i + 1) + j++ * 1, getCount(i));
if (!INSERT_PK_BACK_ALLOWED)
continue;
// Test insert with keyholder or keyholder is null
KeyHolder holder = new KeyHolder();
res = dao.insert(new DalHints().inTableShard(i).setIdentityBack(), holder, model);
assertEquals((i + 1) + j++ * 1, getCount(i));
assertNotNull(holder.getKey());
assertNotNull(model.getId());
assertEquals(holder.getKey().intValue(), model.getId().intValue());
// Test insert without keyholder or keyholder is null
holder = null;
res = dao.insert(new DalHints().inTableShard(i).setIdentityBack(), holder, model);
assertEquals((i + 1) + j++ * 1, getCount(i));
assertNotNull(model.getId());
// Test insert without keyholder
res = dao.insert(new DalHints().inTableShard(i).setIdentityBack(), model);
assertEquals((i + 1) + j++ * 1, getCount(i));
assertNotNull(model.getId());
}
}
use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class BaseDalTabelDaoShardByTableTest method testCombinedInsert.
/**
* Test Insert multiple entities with one SQL Statement
* @throws SQLException
*/
@Test
public void testCombinedInsert() throws SQLException {
if (!diff.supportInsertValues)
return;
ClientTestModel[] entities = new ClientTestModel[3];
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[i] = model;
}
KeyHolder holder = new KeyHolder();
int res;
try {
res = dao.combinedInsert(new DalHints(), holder, Arrays.asList(entities));
fail();
} catch (Exception e) {
}
for (int i = 0; i < mod; i++) {
int j = 1;
holder = null;
// By tabelShard
// holder = new KeyHolder();
res = dao.combinedInsert(new DalHints().inTableShard(i), holder, Arrays.asList(entities));
assertEquals((i + 1) + j++ * 3, getCount(i));
// By tableShardValue
// holder = new KeyHolder();
res = dao.combinedInsert(new DalHints().setTableShardValue(i), holder, Arrays.asList(entities));
assertEquals((i + 1) + j++ * 3, getCount(i));
// By shardColValue
// holder = new KeyHolder();
res = dao.combinedInsert(new DalHints().setShardColValue("index", i), holder, Arrays.asList(entities));
assertEquals((i + 1) + j++ * 3, getCount(i));
// By shardColValue
// holder = new KeyHolder();
res = dao.combinedInsert(new DalHints().setShardColValue("tableIndex", i), holder, Arrays.asList(entities));
assertEquals((i + 1) + j++ * 3, getCount(i));
}
// For combined insert, the shard id must be defined or change bd deduced.
}
Aggregations