Search in sources :

Example 81 with DalHints

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

the class BaseDalTableDaoShardByDbTest method testInsertMultipleAsListAsyncCallback.

@Test
public void testInsertMultipleAsListAsyncCallback() throws SQLException {
    DalHints hints;
    IntCallback callback;
    List<ClientTestModel> entities = new ArrayList<ClientTestModel>();
    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 {
        res = dao.insert(new DalHints(), entities);
        fail();
    } catch (Exception e) {
    }
    for (int i = 0; i < mod; i++) {
        int j = 1;
        // By shard
        hints = new DalHints().asyncExecution();
        res = dao.insert(hints.inShard(i), entities);
        assertNull(res);
        res = getIntArray(hints);
        assertResEquals(3, res);
        assertEquals(3 + j++ * 3, getCountByDb(dao, i));
        // By shardValue
        callback = new IntCallback();
        hints = new DalHints().callbackWith(callback);
        res = dao.insert(hints.setShardValue(i), entities);
        assertNull(res);
        res = callback.getIntArray();
        assertResEquals(3, res);
        assertEquals(3 + j++ * 3, getCountByDb(dao, i));
        // By shardColValue
        hints = new DalHints().asyncExecution();
        res = dao.insert(hints.setShardColValue("index", i), entities);
        assertNull(res);
        res = getIntArray(hints);
        assertResEquals(3, res);
        assertEquals(3 + j++ * 3, getCountByDb(dao, i));
        // By shardColValue
        callback = new IntCallback();
        hints = new DalHints().callbackWith(callback);
        res = dao.insert(hints.setShardColValue("tableIndex", i), entities);
        assertNull(res);
        res = callback.getIntArray();
        assertResEquals(3, res);
        assertEquals(3 + j++ * 3, getCountByDb(dao, i));
        // By fields same shard
        hints = new DalHints().asyncExecution();
        entities.get(0).setTableIndex(i);
        entities.get(1).setTableIndex(i);
        entities.get(2).setTableIndex(i);
        res = dao.insert(hints, entities);
        assertNull(res);
        res = getIntArray(hints);
        assertResEquals(3, res);
        assertEquals(3 + j++ * 3, getCountByDb(dao, i));
    }
    deleteAllShardsByDb(dao, mod);
    // By fields not same shard
    callback = new IntCallback();
    hints = new DalHints().callbackWith(callback);
    entities.get(0).setTableIndex(0);
    entities.get(1).setTableIndex(1);
    entities.get(2).setTableIndex(2);
    res = dao.insert(hints.continueOnError(), entities);
    assertNull(res);
    res = callback.getIntArray();
    assertResEquals(3, res);
    assertEquals(2, getCountByDb(dao, 0));
    assertEquals(1, getCountByDb(dao, 1));
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 82 with DalHints

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

the class BaseDalTableDaoShardByDbTest method testCombinedInsertAsyncCallback.

@Test
public void testCombinedInsertAsyncCallback() throws SQLException {
    if (!diff.supportInsertValues)
        return;
    DalHints hints;
    IntCallback callback;
    List<ClientTestModel> entities = new ArrayList<>();
    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);
    }
    KeyHolder holder = createKeyHolder();
    int res;
    try {
        res = dao.combinedInsert(new DalHints(), holder, entities);
        fail();
    } catch (Exception e) {
    }
    for (int i = 0; i < mod; i++) {
        int j = 1;
        // By shard
        hints = new DalHints().asyncExecution();
        holder = createKeyHolder();
        res = dao.combinedInsert(hints.inShard(i), holder, entities);
        assertEquals(0, res);
        res = getInt(hints);
        assertResEquals(3, res);
        assertEquals(3 + j++ * 3, getCountByDb(dao, i));
        assertKeyHolder(holder);
        // By shardValue
        callback = new IntCallback();
        hints = new DalHints().callbackWith(callback);
        holder = createKeyHolder();
        res = dao.combinedInsert(hints.setShardValue(i), holder, entities);
        assertEquals(0, res);
        res = callback.getInt();
        assertResEquals(3, res);
        assertEquals(3 + j++ * 3, getCountByDb(dao, i));
        assertKeyHolder(holder);
        // By shardColValue
        hints = new DalHints().asyncExecution();
        holder = createKeyHolder();
        res = dao.combinedInsert(hints.setShardColValue("index", i), holder, entities);
        assertEquals(0, res);
        res = getInt(hints);
        assertResEquals(3, res);
        assertEquals(3 + j++ * 3, getCountByDb(dao, i));
        assertKeyHolder(holder);
        // By shardColValue
        callback = new IntCallback();
        hints = new DalHints().callbackWith(callback);
        holder = createKeyHolder();
        res = dao.combinedInsert(hints.setShardColValue("tableIndex", i), holder, entities);
        assertEquals(0, res);
        res = callback.getInt();
        assertResEquals(3, res);
        assertEquals(3 + j++ * 3, getCountByDb(dao, i));
        assertKeyHolder(holder);
    }
// For combined insert, the shard id must be defined or change bd deduced.
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ArrayList(java.util.ArrayList) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 83 with DalHints

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

the class BaseDalTableDaoShardByDbTableTest method testQueryByPk.

private void testQueryByPk(int shardId, DalHints oldHints) throws SQLException {
    ClientTestModel model = null;
    DalHints hints;
    for (int i = 0; i < tableMod; i++) {
        int id = 1;
        // By tabelShard
        hints = copy(oldHints);
        if (i % 2 == 0)
            model = dao.queryByPk(1, hints.inTableShard(String.valueOf(i)));
        else
            model = dao.queryByPk(1, hints.inTableShard(i));
        model = assertModel(model, hints);
        assertQueryByPk(shardId, model, i, id);
        // By tableShardValue
        hints = copy(oldHints);
        if (i % 2 == 0)
            model = dao.queryByPk(1, hints.setTableShardValue(String.valueOf(i)));
        else
            model = dao.queryByPk(1, hints.setTableShardValue(i));
        model = assertModel(model, hints);
        assertQueryByPk(shardId, model, i, id);
        // By shardColValue
        hints = copy(oldHints);
        if (i % 2 == 0)
            model = dao.queryByPk(1, hints.setShardColValue("table", String.valueOf(i)));
        else
            model = dao.queryByPk(1, hints.setShardColValue("table", i));
        model = assertModel(model, hints);
        assertQueryByPk(shardId, model, i, id);
        // By shardColValue
        hints = copy(oldHints);
        if (i % 2 == 0)
            model = dao.queryByPk(1, hints.setShardColValue("tableIndex", String.valueOf(i)));
        else
            model = dao.queryByPk(1, hints.setShardColValue("tableIndex", i));
        model = assertModel(model, hints);
        assertQueryByPk(shardId, model, i, id);
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints)

Example 84 with DalHints

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

the class BaseDalTableDaoShardByDbTableTest method testDeleteWithWhereClause.

@Test
public void testDeleteWithWhereClause() throws SQLException {
    testDeleteWithWhereClause(new DalHints());
    testDeleteWithWhereClause(asyncHints());
    testDeleteWithWhereClause(intHints());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) Test(org.junit.Test)

Example 85 with DalHints

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

the class BaseDalTableDaoShardByDbTableTest method testCrossShardBatchInsert.

public void testCrossShardBatchInsert(DalHints oldhints) {
    try {
        deleteAllShardsByDbTable(dao, mod, tableMod);
        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.batchInsert(hints, Arrays.asList(pList));
        assertIntArray(res, hints);
        for (int i = 0; i < mod; i++) {
            for (int j = 0; j < tableMod; j++) {
                Assert.assertEquals(j + 1, getCount(i, j));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) SQLException(java.sql.SQLException)

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