Search in sources :

Example 36 with KeyHolder

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

the class BaseDalTableDaoShardByDbTableTest method testInsertMultipleAsListWithKeyHolder.

/**
	 * Test Insert multiple entities with key-holder
	 * @throws SQLException
	 */
private void testInsertMultipleAsListWithKeyHolder(int shardId, DalHints oldhints) throws SQLException {
    DalHints hints;
    List<ClientTestModel> entities = createListNoId(3);
    KeyHolder holder = new KeyHolder();
    int[] res;
    try {
        hints = copy(oldhints);
        res = dao.insert(hints, holder, entities);
        assertIntArray(res, hints);
        Assert.fail();
    } catch (Exception e) {
    }
    for (int i = 0; i < tableMod; i++) {
        int j = 1;
        // By tabelShard
        holder = createKeyHolder();
        hints = copy(oldhints);
        res = dao.insert(hints.inTableShard(i), holder, entities);
        res = assertIntArray(res, hints);
        assertResEquals(3, res);
        Assert.assertEquals((i + 1) + j++ * 3, getCount(shardId, i));
        assertKeyHolder(holder);
        // By tableShardValue
        holder = createKeyHolder();
        hints = copy(oldhints);
        res = dao.insert(hints.setTableShardValue(i), holder, entities);
        res = assertIntArray(res, hints);
        assertResEquals(3, res);
        Assert.assertEquals((i + 1) + j++ * 3, getCount(shardId, i));
        assertKeyHolder(holder);
        // By shardColValue
        holder = createKeyHolder();
        hints = copy(oldhints);
        res = dao.insert(hints.setShardColValue("table", i), holder, entities);
        res = assertIntArray(res, hints);
        assertResEquals(3, res);
        Assert.assertEquals((i + 1) + j++ * 3, getCount(shardId, i));
        assertKeyHolder(holder);
        // By shardColValue
        holder = createKeyHolder();
        hints = copy(oldhints);
        res = dao.insert(hints.setShardColValue("tableIndex", i), holder, entities);
        res = assertIntArray(res, hints);
        assertResEquals(3, res);
        Assert.assertEquals((i + 1) + j++ * 3, getCount(shardId, i));
        assertKeyHolder(holder);
        // By fields same shard
        holder = createKeyHolder();
        entities.get(0).setTableIndex(i);
        entities.get(1).setTableIndex(i);
        entities.get(2).setTableIndex(i);
        hints = copy(oldhints);
        res = dao.insert(hints, holder, entities);
        res = assertIntArray(res, hints);
        assertResEquals(3, res);
        Assert.assertEquals((i + 1) + j++ * 3, getCount(shardId, i));
        assertKeyHolder(holder);
    }
    deleteAllShards(shardId);
    // By fields not same shard
    holder = createKeyHolder();
    entities.get(0).setTableIndex(0);
    entities.get(1).setTableIndex(1);
    entities.get(2).setTableIndex(2);
    hints = copy(oldhints);
    res = dao.insert(hints, holder, entities);
    res = assertIntArray(res, hints);
    Assert.assertEquals(1, getCount(shardId, 0));
    Assert.assertEquals(1, getCount(shardId, 1));
    Assert.assertEquals(1, getCount(shardId, 2));
    assertResEquals(3, res);
    assertKeyHolder(holder);
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) SQLException(java.sql.SQLException)

Example 37 with KeyHolder

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

the class BaseDalTableDaoShardByDbTableTest method testCombinedInsertFail.

private void testCombinedInsertFail(DalHints hints) throws SQLException {
    reset();
    List<ClientTestModel> entities = create(3);
    KeyHolder holder = createKeyHolder();
    int res;
    try {
        hints = copy(hints);
        res = dao.combinedInsert(hints, holder, entities);
        res = assertInt(res, hints);
        Assert.fail();
    } catch (Exception e) {
    }
}
Also used : KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) SQLException(java.sql.SQLException)

Example 38 with KeyHolder

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

the class BaseDalTableDaoShardByDbTableTest method testInsertMultipleAsListWithKeyHolderFail.

private void testInsertMultipleAsListWithKeyHolderFail(DalHints hints) throws SQLException {
    reset();
    List<ClientTestModel> entities = createListNoId(3);
    KeyHolder holder = createKeyHolder();
    int[] res;
    try {
        hints = copy(hints);
        res = dao.insert(hints, holder, entities);
        res = assertIntArray(res, hints);
        Assert.fail();
    } catch (Exception e) {
    }
}
Also used : KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) SQLException(java.sql.SQLException)

Example 39 with KeyHolder

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

the class BaseDalTableDaoShardByDbTableTest method testInsertMultipleAsListWithKeyHolderByFields.

private void testInsertMultipleAsListWithKeyHolderByFields(DalHints hints) throws SQLException {
    reset();
    List<ClientTestModel> entities = createListNoId(3);
    int[] res;
    KeyHolder holder = createKeyHolder();
    deleteAllShardsByDbTable(dao, mod, tableMod);
    // By fields not same shard
    holder = createKeyHolder();
    entities.get(0).setTableIndex(0);
    entities.get(0).setDbIndex(0);
    entities.get(1).setTableIndex(1);
    entities.get(1).setDbIndex(1);
    entities.get(2).setTableIndex(2);
    entities.get(2).setDbIndex(2);
    hints = copy(hints);
    res = dao.insert(hints, holder, entities);
    res = assertIntArray(res, hints);
    assertResEquals(3, res);
    Assert.assertEquals(1, getCount(0, 0));
    Assert.assertEquals(1, getCount(1, 1));
    Assert.assertEquals(1, getCount(0, 2));
    assertKeyHolder(holder);
}
Also used : KeyHolder(com.ctrip.platform.dal.dao.KeyHolder)

Example 40 with KeyHolder

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

the class CombinedInsertTaskTestStub method testExecuteWithId.

@Test
public void testExecuteWithId() {
    CombinedInsertTask<ClientTestModel> test = new CombinedInsertTask<>();
    test.initialize(new ClientTestDalParser(getDbName()));
    DalHints hints = new DalHints().enableIdentityInsert();
    if (enableKeyHolder)
        hints.setKeyHolder(new KeyHolder());
    try {
        if (this instanceof CombinedInsertTaskSqlSvrTest)
            SqlServerTestInitializer.turnOnIdentityInsert();
        List<ClientTestModel> pojos = getAll();
        int i = 111;
        for (ClientTestModel pojo : pojos) {
            pojo.setId(new Integer(i++));
        }
        execute(test, hints, null, pojos);
        assertEquals(6, getCount());
        pojos = getAll();
        Set<Integer> ids = new HashSet<>();
        for (ClientTestModel pojo : pojos) {
            ids.add(pojo.getId());
        }
        assertTrue(ids.contains(111));
        assertTrue(ids.contains(112));
        assertTrue(ids.contains(113));
        if (enableKeyHolder) {
            // You have to merge before get size
            assertEquals(3, hints.getKeyHolder().size());
        }
        assertEquals(3 + 3, getCount());
        if (this instanceof CombinedInsertTaskSqlSvrTest)
            SqlServerTestInitializer.turnOffIdentityInsert();
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) CombinedInsertTask(com.ctrip.platform.dal.dao.task.CombinedInsertTask) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) SQLException(java.sql.SQLException) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

KeyHolder (com.ctrip.platform.dal.dao.KeyHolder)42 Test (org.junit.Test)32 SQLException (java.sql.SQLException)28 DalHints (com.ctrip.platform.dal.dao.DalHints)27 ArrayList (java.util.ArrayList)13 Timestamp (java.sql.Timestamp)5 Map (java.util.Map)5 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)3 CombinedInsertTask (com.ctrip.platform.dal.dao.task.CombinedInsertTask)3 ExecutorService (java.util.concurrent.ExecutorService)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 BeforeClass (org.junit.BeforeClass)2 ClientTestModel (test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel)2 DalColumnMapRowMapper (com.ctrip.platform.dal.dao.helper.DalColumnMapRowMapper)1 DalDefaultJpaParser (com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser)1 DalRowMapperExtractor (com.ctrip.platform.dal.dao.helper.DalRowMapperExtractor)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Future (java.util.concurrent.Future)1