Search in sources :

Example 1 with DefaultResultCallback

use of com.ctrip.platform.dal.dao.helper.DefaultResultCallback in project dal by ctripcorp.

the class BaseDalTableDaoShardByDbTableTest method copy.

private DalHints copy(DalHints oldhints) {
    DalHints hints = oldhints.clone();
    if (hints.is(DalHintEnum.resultCallback)) {
        DefaultResultCallback callback = (DefaultResultCallback) hints.get(DalHintEnum.resultCallback);
        callback.reset();
    }
    return hints;
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DefaultResultCallback(com.ctrip.platform.dal.dao.helper.DefaultResultCallback)

Example 2 with DefaultResultCallback

use of com.ctrip.platform.dal.dao.helper.DefaultResultCallback 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());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DefaultResultCallback(com.ctrip.platform.dal.dao.helper.DefaultResultCallback) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) Test(org.junit.Test)

Example 3 with DefaultResultCallback

use of com.ctrip.platform.dal.dao.helper.DefaultResultCallback in project dal by ctripcorp.

the class BaseDalTabelDaoShardByTableTest method testBatchUpdateCallback.

@Test
public void testBatchUpdateCallback() throws SQLException {
    DefaultResultCallback callback = new DefaultResultCallback();
    DalHints hints = new DalHints().callbackWith(callback);
    List<ClientTestModel> entities = new ArrayList<>();
    for (int i = 0; i < 4; i++) {
        ClientTestModel model = new ClientTestModel();
        model.setId(i + 1);
        model.setAddress("CTRIP");
        entities.add(model);
    }
    int[] res;
    try {
        res = dao.batchUpdate(hints, entities);
        callback.waitForDone();
        assertTrue(!callback.isSuccess());
    } catch (Exception e) {
        fail();
    }
    // By fields same shard
    // holder = new KeyHolder();
    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");
    entities.get(3).setTableIndex(0);
    entities.get(3).setAddress("1234");
    dao.batchUpdate(new DalHints(), entities);
    List<ClientTestModel> result = dao.query("1=1", new StatementParameters(), new DalHints().inTableShard(0));
    for (ClientTestModel m : result) assertEquals("1234", m.getAddress());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DefaultResultCallback(com.ctrip.platform.dal.dao.helper.DefaultResultCallback) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 4 with DefaultResultCallback

use of com.ctrip.platform.dal.dao.helper.DefaultResultCallback in project dal by ctripcorp.

the class DalRequestExecutorTest method testExecuteCallback.

@Test
public void testExecuteCallback() {
    DalRequestExecutor test = new DalRequestExecutor();
    TestDalRequest request = new TestDalRequest(null, new Integer[] { 1 });
    DalHints hints = new DalHints();
    DefaultResultCallback callback = new DefaultResultCallback();
    try {
        Integer result = test.execute(hints.callbackWith(callback), request, true);
        assertNull(result);
        assertEquals(1, ((Integer) hints.getAsyncResult().get()).intValue());
        assertEquals(1, ((Integer) callback.getResult()).intValue());
    } catch (Exception e) {
        fail();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalRequestExecutor(com.ctrip.platform.dal.dao.task.DalRequestExecutor) DefaultResultCallback(com.ctrip.platform.dal.dao.helper.DefaultResultCallback) ExecutionException(java.util.concurrent.ExecutionException) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 5 with DefaultResultCallback

use of com.ctrip.platform.dal.dao.helper.DefaultResultCallback in project dal by ctripcorp.

the class BaseDalTabelDaoShardByTableTest method testQueryFirstWithWhereClauseFailedCallback.

@Test
public void testQueryFirstWithWhereClauseFailedCallback() throws SQLException {
    DalHints hints;
    DefaultResultCallback callback;
    String whereClause = "type=?";
    StatementParameters parameters = new StatementParameters();
    parameters.set(1, Types.SMALLINT, 10);
    try {
        callback = new DefaultResultCallback();
        hints = new DalHints().callbackWith(callback);
        Object model = dao.queryFirst(whereClause, parameters, hints.inTableShard(1));
        callback.waitForDone();
        assertNull(callback.getResult());
    } catch (Throwable e) {
        fail();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DefaultResultCallback(com.ctrip.platform.dal.dao.helper.DefaultResultCallback) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) Test(org.junit.Test)

Aggregations

DefaultResultCallback (com.ctrip.platform.dal.dao.helper.DefaultResultCallback)7 DalHints (com.ctrip.platform.dal.dao.DalHints)5 Test (org.junit.Test)4 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)3 SQLException (java.sql.SQLException)2 DalRequestExecutor (com.ctrip.platform.dal.dao.task.DalRequestExecutor)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1