use of com.ctrip.platform.dal.dao.StatementParameters in project dal by ctripcorp.
the class BaseDalTableDaoShardByDbTest method testBatchUpdateMultiple.
@Test
public void testBatchUpdateMultiple() throws SQLException {
DalHints hints = new DalHints();
List<ClientTestModel> entities = new ArrayList<>();
for (int i = 0; i < 3; i++) {
ClientTestModel model = new ClientTestModel();
model.setId(i + 1);
model.setAddress("CTRIP");
entities.add(model);
}
int[] ress;
try {
ress = dao.batchUpdate(hints, entities);
fail();
} catch (Exception e) {
}
// By fields same shard
// holder = createKeyHolder();
entities.get(0).setTableIndex(0);
entities.get(0).setAddress("1234");
entities.get(1).setTableIndex(0);
entities.get(1).setAddress("1234");
entities.get(2).setTableIndex(0);
entities.get(2).setAddress("1234");
ress = dao.batchUpdate(new DalHints(), entities);
assertResEquals(3, ress);
List<ClientTestModel> result = dao.query("1=1", new StatementParameters(), new DalHints().inShard(0));
for (ClientTestModel m : result) assertEquals("1234", m.getAddress());
}
use of com.ctrip.platform.dal.dao.StatementParameters in project dal by ctripcorp.
the class BaseDalTableDaoShardByDbTest method testUpdateMultipleAsyncCallback.
@Test
public void testUpdateMultipleAsyncCallback() throws SQLException {
DalHints hints;
IntCallback callback;
hints = new DalHints();
List<ClientTestModel> entities = new ArrayList<>();
for (int i = 0; i < 3; i++) {
ClientTestModel model = new ClientTestModel();
model.setId(i + 1);
model.setAddress("CTRIP");
entities.add(model);
}
int[] ress;
try {
ress = dao.update(hints, entities);
fail();
} catch (Exception e) {
}
int res;
// By shard
hints = new DalHints().asyncExecution();
entities.get(0).setAddress("test1");
res = dao.update(hints.inShard(0), entities.get(0));
assertEquals(0, res);
res = getInt(hints);
assertResEquals(1, res);
assertEquals("test1", dao.queryByPk(1, new DalHints().inShard(0)).getAddress());
// By shardValue
callback = new IntCallback();
hints = new DalHints().callbackWith(callback);
entities.get(1).setQuantity(-11);
res = dao.update(hints.setShardValue(1), entities.get(1));
assertEquals(0, res);
res = callback.getInt();
assertResEquals(1, res);
assertEquals(-11, dao.queryByPk(2, new DalHints().inShard(1)).getQuantity().intValue());
// By shardColValue
hints = new DalHints().asyncExecution();
entities.get(2).setType((short) 3);
res = dao.update(hints.setShardColValue("index", 2), entities.get(2));
assertEquals(0, res);
res = getInt(hints);
assertResEquals(1, res);
assertEquals((short) 3, dao.queryByPk(3, new DalHints().inShard(0)).getType().shortValue());
// By shardColValue
callback = new IntCallback();
hints = new DalHints().callbackWith(callback);
entities.get(0).setAddress("testa");
res = dao.update(hints.setShardColValue("tableIndex", 3), entities.get(0));
assertEquals(0, res);
res = callback.getInt();
assertResEquals(1, res);
assertEquals("testa", dao.queryByPk(1, new DalHints().inShard(1)).getAddress());
// By fields same shard
// holder = createKeyHolder();
hints = new DalHints().asyncExecution();
entities.get(0).setTableIndex(0);
entities.get(0).setAddress("1234");
entities.get(1).setTableIndex(0);
entities.get(1).setAddress("1234");
entities.get(2).setTableIndex(0);
entities.get(2).setAddress("1234");
ress = dao.update(hints, entities);
assertNull(ress);
res = getInt(hints);
assertResEquals(3, res);
List<ClientTestModel> result = dao.query("1=1", new StatementParameters(), new DalHints().inShard(0));
for (ClientTestModel m : result) assertEquals("1234", m.getAddress());
}
use of com.ctrip.platform.dal.dao.StatementParameters in project dal by ctripcorp.
the class BaseDalTableDaoShardByDbTest method testDeleteWithWhereClauseShardsAsync.
@Test
public void testDeleteWithWhereClauseShardsAsync() throws SQLException {
String whereClause = "type=?";
StatementParameters parameters = new StatementParameters();
parameters.set(1, Types.SMALLINT, 1);
DalHints hints = new DalHints().asyncExecution();
int res;
// By shards
Set<String> shards = new HashSet<>();
shards.add("0");
shards.add("1");
assertEquals(3, getCountByDb(dao, 0));
assertEquals(3, getCountByDb(dao, 1));
res = dao.delete(whereClause, parameters, hints.inShards(shards));
assertEquals(0, res);
res = getInt(hints);
assertResEquals(6, res);
assertEquals(0, dao.query(whereClause, parameters, new DalHints().inShards(shards)).size());
}
use of com.ctrip.platform.dal.dao.StatementParameters in project dal by ctripcorp.
the class BaseDalTableDaoShardByDbTableTest method deleteAllShards.
public void deleteAllShards(int shardId) throws SQLException {
for (int i = 0; i < tableMod; i++) {
int j = 1;
dao.delete("1=1", new StatementParameters(), new DalHints().inShard(shardId).inTableShard(i));
}
}
use of com.ctrip.platform.dal.dao.StatementParameters in project dal by ctripcorp.
the class BaseDalTableDaoShardByDbTest method testUpdatePlainAllShardsAsync.
@Test
public void testUpdatePlainAllShardsAsync() throws SQLException {
String sql = "UPDATE " + TABLE_NAME + " SET address = 'CTRIP' WHERE id = 1";
StatementParameters parameters = new StatementParameters();
DalHints hints = new DalHints().asyncExecution();
int res;
// By allShards
sql = "UPDATE " + TABLE_NAME + " SET address = 'CTRIP' WHERE id = 1";
res = dao.update(sql, parameters, hints.inAllShards());
assertEquals(0, res);
res = getInt(hints);
assertResEquals(2, res);
assertEquals("CTRIP", dao.queryByPk(1, new DalHints().inShard(0)).getAddress());
assertEquals("CTRIP", dao.queryByPk(1, new DalHints().inShard(1)).getAddress());
}
Aggregations