Search in sources :

Example 46 with KeyHolder

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

the class DaoOfLoginUser method insertUser.

public int insertUser(LoginUser user) throws SQLException {
    if (null == user)
        return 0;
    KeyHolder keyHolder = new KeyHolder();
    DalHints hints = DalHints.createIfAbsent(null);
    client.insert(hints, keyHolder, user);
    return keyHolder.getKey().intValue();
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder)

Example 47 with KeyHolder

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

the class BaseDalTabelDaoShardByTableTest method testCrossShardInsert.

@Test
public void testCrossShardInsert() {
    if (!diff.supportInsertValues)
        return;
    try {
        deleteAllShards();
        ClientTestModel p = new ClientTestModel();
        ClientTestModel[] pList = new ClientTestModel[6];
        p = new ClientTestModel();
        p.setId(1);
        p.setAddress("aaa");
        p.setTableIndex(0);
        pList[0] = p;
        p = new ClientTestModel();
        p.setId(2);
        p.setAddress("aaa");
        p.setTableIndex(1);
        pList[1] = p;
        p = new ClientTestModel();
        p.setId(3);
        p.setAddress("aaa");
        p.setTableIndex(2);
        pList[2] = p;
        p = new ClientTestModel();
        p.setId(4);
        p.setAddress("aaa");
        p.setTableIndex(3);
        pList[3] = p;
        p = new ClientTestModel();
        p.setId(5);
        p.setAddress("aaa");
        p.setTableIndex(4);
        pList[4] = p;
        p = new ClientTestModel();
        p.setId(5);
        p.setAddress("aaa");
        p.setTableIndex(5);
        pList[5] = p;
        // new KeyHolder();
        KeyHolder keyHolder = null;
        dao.combinedInsert(new DalHints(), keyHolder, Arrays.asList(pList));
        assertEquals(2, getCount(0));
        assertEquals(2, getCount(1));
        assertEquals(1, getCount(2));
        assertEquals(1, getCount(3));
    } catch (Exception e) {
        fail();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 48 with KeyHolder

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

the class BaseDalTabelDaoShardByTableTest method testCombinedInsertAsyncCallbackWithPkInsertBack.

@Test
public void testCombinedInsertAsyncCallbackWithPkInsertBack() throws SQLException {
    if (!INSERT_PK_BACK_ALLOWED)
        return;
    DalTableDao<ClientTestModel> dao = new DalTableDao<ClientTestModel>(ClientTestModel.class, databaseName, TABLE_NAME);
    ClientTestModel[] entities = new ClientTestModel[3];
    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" + i);
        entities[i] = model;
    }
    int res;
    for (int i = 0; i < mod; i++) {
        int j = 1;
        KeyHolder holder = new KeyHolder();
        // By tabelShard
        // holder = new KeyHolder();
        DalHints hints = asyncHints();
        res = dao.combinedInsert(hints.inTableShard(i).setIdentityBack(), holder, Arrays.asList(entities));
        res = assertInt(res, hints);
        for (ClientTestModel model : entities) {
            assertEquals(dao.queryByPk(model, new DalHints().inTableShard(i)).getAddress(), model.getAddress());
        }
        // By tableShardValue
        holder = new KeyHolder();
        hints = intHints();
        res = dao.combinedInsert(hints.setTableShardValue(i).setIdentityBack(), holder, Arrays.asList(entities));
        res = assertInt(res, hints);
        for (ClientTestModel model : entities) {
            assertEquals(dao.queryByPk(model, new DalHints().inTableShard(i)).getAddress(), model.getAddress());
        }
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Example 49 with KeyHolder

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

the class BaseDalTabelDaoShardByTableTest method testInsertSingle.

/**
 * Test Insert multiple entities one by one
 * @throws SQLException
 */
@Test
public void testInsertSingle() throws SQLException {
    ClientTestModel model = new ClientTestModel();
    model.setQuantity(10 + 1 % 3);
    model.setType(((Number) (1 % 3)).shortValue());
    model.setAddress("CTRIP");
    int res;
    try {
        res = dao.insert(new DalHints(), model);
        fail();
    } catch (Throwable e) {
    }
    for (int i = 0; i < mod; i++) {
        int j = 1;
        // By tabelShard
        res = dao.insert(new DalHints().inTableShard(i), model);
        assertEquals((i + 1) + j++ * 1, getCount(i));
        // By tableShardValue
        res = dao.insert(new DalHints().setTableShardValue(i), model);
        assertEquals((i + 1) + j++ * 1, getCount(i));
        // By shardColValue
        res = dao.insert(new DalHints().setShardColValue("index", i), model);
        assertEquals((i + 1) + j++ * 1, getCount(i));
        // By shardColValue
        res = dao.insert(new DalHints().setShardColValue("tableIndex", i), model);
        assertEquals((i + 1) + j++ * 1, getCount(i));
        // By fields
        model.setTableIndex(i);
        res = dao.insert(new DalHints(), model);
        assertEquals((i + 1) + j++ * 1, getCount(i));
        if (!INSERT_PK_BACK_ALLOWED)
            continue;
        // Test insert with keyholder or keyholder is null
        KeyHolder holder = new KeyHolder();
        res = dao.insert(new DalHints().inTableShard(i).setIdentityBack(), holder, model);
        assertEquals((i + 1) + j++ * 1, getCount(i));
        assertNotNull(holder.getKey());
        assertNotNull(model.getId());
        assertEquals(holder.getKey().intValue(), model.getId().intValue());
        // Test insert without keyholder or keyholder is null
        holder = null;
        res = dao.insert(new DalHints().inTableShard(i).setIdentityBack(), holder, model);
        assertEquals((i + 1) + j++ * 1, getCount(i));
        assertNotNull(model.getId());
        // Test insert without keyholder
        res = dao.insert(new DalHints().inTableShard(i).setIdentityBack(), model);
        assertEquals((i + 1) + j++ * 1, getCount(i));
        assertNotNull(model.getId());
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) Test(org.junit.Test)

Example 50 with KeyHolder

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

the class BaseDalTabelDaoShardByTableTest method testCombinedInsert.

/**
 * Test Insert multiple entities with one SQL Statement
 * @throws SQLException
 */
@Test
public void testCombinedInsert() throws SQLException {
    if (!diff.supportInsertValues)
        return;
    ClientTestModel[] entities = new ClientTestModel[3];
    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[i] = model;
    }
    KeyHolder holder = new KeyHolder();
    int res;
    try {
        res = dao.combinedInsert(new DalHints(), holder, Arrays.asList(entities));
        fail();
    } catch (Exception e) {
    }
    for (int i = 0; i < mod; i++) {
        int j = 1;
        holder = null;
        // By tabelShard
        // holder = new KeyHolder();
        res = dao.combinedInsert(new DalHints().inTableShard(i), holder, Arrays.asList(entities));
        assertEquals((i + 1) + j++ * 3, getCount(i));
        // By tableShardValue
        // holder = new KeyHolder();
        res = dao.combinedInsert(new DalHints().setTableShardValue(i), holder, Arrays.asList(entities));
        assertEquals((i + 1) + j++ * 3, getCount(i));
        // By shardColValue
        // holder = new KeyHolder();
        res = dao.combinedInsert(new DalHints().setShardColValue("index", i), holder, Arrays.asList(entities));
        assertEquals((i + 1) + j++ * 3, getCount(i));
        // By shardColValue
        // holder = new KeyHolder();
        res = dao.combinedInsert(new DalHints().setShardColValue("tableIndex", i), holder, Arrays.asList(entities));
        assertEquals((i + 1) + j++ * 3, getCount(i));
    }
// For combined insert, the shard id must be defined or change bd deduced.
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) SQLException(java.sql.SQLException) Test(org.junit.Test)

Aggregations

KeyHolder (com.ctrip.platform.dal.dao.KeyHolder)60 Test (org.junit.Test)46 DalHints (com.ctrip.platform.dal.dao.DalHints)45 SQLException (java.sql.SQLException)32 ArrayList (java.util.ArrayList)20 DalTableDao (com.ctrip.platform.dal.dao.DalTableDao)7 Timestamp (java.sql.Timestamp)5 Map (java.util.Map)5 CombinedInsertTask (com.ctrip.platform.dal.dao.task.CombinedInsertTask)4 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)2 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