use of com.ctrip.platform.dal.dao.StatementParameters in project dal by ctripcorp.
the class DalTableDaoShardByDbTableSqlSvrTest method tearDown.
@After
public void tearDown() throws SQLException {
String sql = "DELETE FROM " + TABLE_NAME;
StatementParameters parameters = new StatementParameters();
DalHints hints = new DalHints();
sql = "DELETE FROM " + TABLE_NAME;
parameters = new StatementParameters();
hints = new DalHints();
try {
for (int j = 0; j < mod; j++) {
for (int i = 0; i < tableMod; i++) {
clientSqlSvr.update(sql + "_" + i, parameters, hints.inShard(j));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
use of com.ctrip.platform.dal.dao.StatementParameters in project dal by ctripcorp.
the class BaseDalTableDaoShardByDbTableTest method testQueryTopWithWhereClause.
/**
* Test Query the top rows with where clause
* @throws SQLException
*/
private void testQueryTopWithWhereClause(int shardId, DalHints oldhints) throws SQLException {
List<ClientTestModel> models = null;
DalHints hints;
for (int i = 0; i < tableMod; i++) {
String whereClause = "type=?";
StatementParameters parameters = new StatementParameters();
parameters.set(1, Types.SMALLINT, 1);
// By tabelShard
hints = copy(oldhints);
models = dao.queryTop(whereClause, parameters, hints.inTableShard(i), i + 1);
models = assertModels(models, hints);
assertQueryX(shardId, models, i);
// By tableShardValue
hints = copy(oldhints);
models = dao.queryTop(whereClause, parameters, hints.setTableShardValue(i), i + 1);
models = assertModels(models, hints);
assertQueryX(shardId, models, i);
// By shardColValue
hints = copy(oldhints);
models = dao.queryTop(whereClause, parameters, hints.setShardColValue("table", i), i + 1);
models = assertModels(models, hints);
assertQueryX(shardId, models, i);
// By shardColValue
hints = copy(oldhints);
models = dao.queryTop(whereClause, parameters, hints.setShardColValue("tableIndex", i), i + 1);
models = assertModels(models, hints);
assertQueryX(shardId, models, i);
whereClause += " and tableIndex=? and dbIndex=?";
// By parameters
parameters = new StatementParameters();
parameters.set(1, "type", Types.SMALLINT, 1);
parameters.set(2, "tableIndex", Types.SMALLINT, i);
parameters.set(3, "dbIndex", Types.SMALLINT, shardId);
hints = copy(oldhints);
models = dao.queryTop(whereClause, parameters, hints, i + 1);
models = assertModels(models, hints);
assertQueryX(shardId, models, i);
}
}
use of com.ctrip.platform.dal.dao.StatementParameters in project dal by ctripcorp.
the class BaseDalTabelDaoShardByTableTest method testQueryTopWithWhereClauseFailedCallback.
@Test
public void testQueryTopWithWhereClauseFailedCallback() throws SQLException {
String whereClause = "type=?";
StatementParameters parameters = new StatementParameters();
parameters.set(1, Types.SMALLINT, 10);
List<ClientTestModel> models;
DefaultResultCallback callback = new DefaultResultCallback();
DalHints hints = new DalHints().callbackWith(callback);
models = dao.queryTop(whereClause, parameters, hints, 2);
try {
callback.waitForDone();
} catch (InterruptedException e) {
e.printStackTrace();
}
assertTrue(!callback.isSuccess());
assertNull(callback.getResult());
assertNotNull(callback.getError());
models = dao.queryTop(whereClause, parameters, new DalHints().inTableShard(1), 2);
assertTrue(null != models);
assertEquals(0, models.size());
}
use of com.ctrip.platform.dal.dao.StatementParameters in project dal by ctripcorp.
the class BaseDalTabelDaoShardByTableTest method testDeleteMultiple.
/**
* Test delete multiple entities
* @throws SQLException
*/
@Test
public void testDeleteMultiple() throws SQLException {
List<ClientTestModel> entities = new ArrayList<>();
for (int i = 0; i < 3; i++) {
ClientTestModel model = new ClientTestModel();
model.setId(i + 1);
model.setQuantity(10 + 1 % 3);
model.setType(((Number) (1 % 3)).shortValue());
model.setAddress("CTRIP");
entities.add(model);
}
int[] res;
// By tabelShard
assertEquals(1, getCount(0));
res = dao.delete(new DalHints().inTableShard(0), entities);
assertEquals(0, getCount(0));
// By tableShardValue
assertEquals(2, getCount(1));
res = dao.delete(new DalHints().setTableShardValue(1), entities);
assertEquals(0, getCount(1));
// By shardColValue
assertEquals(3, getCount(2));
res = dao.delete(new DalHints().setShardColValue("index", 2), entities);
assertEquals(0, getCount(2));
// By shardColValue
assertEquals(4, getCount(3));
res = dao.delete(new DalHints().setShardColValue("tableIndex", 3), entities);
assertEquals(1, getCount(3));
// By fields same shard
// holder = new KeyHolder();
entities.get(0).setTableIndex(0);
entities.get(1).setTableIndex(1);
entities.get(2).setTableIndex(2);
dao.insert(new DalHints(), entities);
assertEquals(1, getCount(0));
assertEquals(1, getCount(1));
assertEquals(1, getCount(2));
entities.set(0, dao.queryFirst("1=1", new StatementParameters(), new DalHints().inTableShard(0)));
entities.set(1, dao.queryFirst("1=1", new StatementParameters(), new DalHints().inTableShard(1)));
entities.set(2, dao.queryFirst("1=1", new StatementParameters(), new DalHints().inTableShard(2)));
res = dao.delete(new DalHints(), entities);
assertEquals(0, getCount(0));
assertEquals(0, getCount(1));
assertEquals(0, getCount(2));
}
use of com.ctrip.platform.dal.dao.StatementParameters in project dal by ctripcorp.
the class BaseDalTabelDaoShardByTableTest method testUpdatePlain.
/**
* Test plain update with SQL
* @throws SQLException
*/
@Test
public void testUpdatePlain() throws SQLException {
String sql = "UPDATE " + TABLE_NAME + " SET address = 'CTRIP' WHERE id = 1";
StatementParameters parameters = new StatementParameters();
DalHints hints = new DalHints();
int res;
try {
res = dao.update(sql, parameters, hints);
fail();
} catch (Exception e) {
}
// By tabelShard
UpdateSqlBuilder usb = new UpdateSqlBuilder(TABLE_NAME, dao.getDatabaseCategory());
usb.update("address", "CTRIP", Types.VARCHAR);
usb.equal("id", "1", Types.INTEGER);
res = dao.update(usb, new DalHints().inTableShard(0));
assertEquals("CTRIP", dao.queryByPk(1, new DalHints().inTableShard(0)).getAddress());
// By tableShardValue
assertEquals(2, getCount(1));
res = dao.update(usb, new DalHints().setTableShardValue(1));
assertEquals("CTRIP", dao.queryByPk(1, new DalHints().setTableShardValue(1)).getAddress());
// By shardColValue
assertEquals(3, getCount(2));
res = dao.update(usb, new DalHints().setShardColValue("index", 2));
assertEquals("CTRIP", dao.queryByPk(1, new DalHints().setShardColValue("index", 2)).getAddress());
// By shardColValue
assertEquals(4, getCount(3));
res = dao.update(usb, new DalHints().setShardColValue("tableIndex", 3));
assertEquals("CTRIP", dao.queryByPk(1, new DalHints().setShardColValue("tableIndex", 3)).getAddress());
}
Aggregations