Search in sources :

Example 56 with DalHints

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

the class DalQueryDaoTest method testQueryTopAllShardsWithClassAsync.

@Test
public void testQueryTopAllShardsWithClassAsync() {
    try {
        DalHints hints = new DalHints();
        List<Integer> result = dao.queryTop(sqlListQuantity, parameters(), hints.inAllShards().asyncExecution(), Integer.class, 4);
        assertNull(result);
        Future<List<Integer>> fr = (Future<List<Integer>>) hints.getAsyncResult();
        result = fr.get();
        assertEquals(4, result.size());
        Integer t = result.get(0);
        assertEquals(new Integer(10), t);
    } catch (Exception e) {
        fail();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DalHints(com.ctrip.platform.dal.dao.DalHints) Future(java.util.concurrent.Future) ArrayList(java.util.ArrayList) List(java.util.List) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 57 with DalHints

use of com.ctrip.platform.dal.dao.DalHints 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();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) After(org.junit.After)

Example 58 with DalHints

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

the class BaseDalTableDaoShardByDbTableTest method testCrossShardDelete.

public void testCrossShardDelete(DalHints oldhints) {
    try {
        reset();
        for (int i = 0; i < mod; i++) {
            for (int j = 0; j < tableMod; j++) {
                Assert.assertEquals(j + 1, getCount(i, j));
            }
        }
        ClientTestModel[] pList = new ClientTestModel[mod * (1 + tableMod) * tableMod / 2];
        int x = 0;
        for (int i = 0; i < mod; i++) {
            for (int j = 0; j < tableMod; j++) {
                for (int k = 0; k < j + 1; k++) {
                    ClientTestModel p = new ClientTestModel();
                    p = new ClientTestModel();
                    p.setId(1 + k);
                    p.setAddress("aaa");
                    p.setDbIndex(i);
                    p.setTableIndex(j);
                    pList[x++] = p;
                }
            }
        }
        DalHints hints = copy(oldhints);
        int[] res = dao.batchDelete(hints, Arrays.asList(pList));
        assertIntArray(res, hints);
        for (int i = 0; i < mod; i++) {
            for (int j = 0; j < tableMod; j++) {
                Assert.assertEquals(0, getCount(i, j));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) SQLException(java.sql.SQLException)

Example 59 with DalHints

use of com.ctrip.platform.dal.dao.DalHints 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 60 with DalHints

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

the class BaseDalTabelDaoShardByTableTest method testInsertMultipleAsListAsyncCallback.

@Test
public void testInsertMultipleAsListAsyncCallback() throws SQLException {
    List<ClientTestModel> entities = new ArrayList<ClientTestModel>();
    DalHints hints;
    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);
    }
    int[] res;
    try {
        hints = new DalHints().asyncExecution();
        res = dao.insert(hints, entities);
        res = assertIntArray(res, hints);
        fail();
    } catch (Exception e) {
    }
    for (int i = 0; i < mod; i++) {
        int j = 1;
        // By tabelShard
        hints = asyncHints();
        res = dao.insert(hints.inTableShard(i), entities);
        res = assertIntArray(res, hints);
        assertEquals((i + 1) + j++ * 3, getCount(i));
        // By tableShardValue
        hints = intHints();
        res = dao.insert(hints.setTableShardValue(i), entities);
        res = assertIntArray(res, hints);
        assertEquals((i + 1) + j++ * 3, getCount(i));
        // By shardColValue
        hints = asyncHints();
        res = dao.insert(hints.setShardColValue("index", i), entities);
        res = assertIntArray(res, hints);
        assertEquals((i + 1) + j++ * 3, getCount(i));
        // By shardColValue
        hints = intHints();
        res = dao.insert(hints.setShardColValue("tableIndex", i), entities);
        res = assertIntArray(res, hints);
        assertEquals((i + 1) + j++ * 3, getCount(i));
        // By fields same shard
        hints = asyncHints();
        entities.get(0).setTableIndex(i);
        entities.get(1).setTableIndex(i);
        entities.get(2).setTableIndex(i);
        res = dao.insert(hints, entities);
        res = assertIntArray(res, hints);
        assertEquals((i + 1) + j++ * 3, getCount(i));
    }
    deleteAllShards();
    // By fields not same shard
    entities.get(0).setTableIndex(0);
    entities.get(1).setTableIndex(1);
    entities.get(2).setTableIndex(2);
    hints = intHints();
    res = dao.insert(hints.continueOnError(), entities);
    res = assertIntArray(res, hints);
    assertEquals(1, getCount(0));
    assertEquals(1, getCount(1));
    assertEquals(1, getCount(2));
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) Test(org.junit.Test)

Aggregations

DalHints (com.ctrip.platform.dal.dao.DalHints)772 Test (org.junit.Test)532 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)317 SQLException (java.sql.SQLException)298 ArrayList (java.util.ArrayList)132 ClientTestModel (test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel)61 List (java.util.List)48 KeyHolder (com.ctrip.platform.dal.dao.KeyHolder)45 FreeSelectSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.FreeSelectSqlBuilder)39 Future (java.util.concurrent.Future)32 DalTableDao (com.ctrip.platform.dal.dao.DalTableDao)29 SelectionContext (com.ctrip.platform.dal.dao.configure.SelectionContext)27 DalDefaultJpaParser (com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser)27 DataBase (com.ctrip.platform.dal.dao.configure.DataBase)26 BeforeClass (org.junit.BeforeClass)26 AfterClass (org.junit.AfterClass)25 HashMap (java.util.HashMap)23 FreeUpdateSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.FreeUpdateSqlBuilder)20 DalClient (com.ctrip.platform.dal.dao.DalClient)18 BatchUpdateTask (com.ctrip.platform.dal.dao.task.BatchUpdateTask)18