Search in sources :

Example 41 with KeyHolder

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

the class DalTableDaoTestStub method testCombinedInsertWithKeyInsertBack.

/**
 * Test Insert multiple entities with one SQL Statement
 * @throws SQLException
 */
@Test
public void testCombinedInsertWithKeyInsertBack() throws SQLException {
    if (!diff.supportInsertValues || !diff.supportGetGeneratedKeys)
        return;
    DalTableDao<ClientTestModelJpa> dao = new DalTableDao(new DalDefaultJpaParser<>(ClientTestModelJpa.class, dbName));
    List<ClientTestModelJpa> entities = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        ClientTestModelJpa model = new ClientTestModelJpa();
        model.setQuantity(10 + 1 % 3);
        model.setType(((Number) (1 % 3)).shortValue());
        model.setAddress("CTRIP");
        entities.add(model);
    }
    KeyHolder holder = diff.supportGetGeneratedKeys ? new KeyHolder() : null;
    DalHints hints = new DalHints();
    int res = dao.combinedInsert(hints.setIdentityBack(), holder, entities);
    assertEquals(3, res, 4 + 3);
    Assert.assertEquals(3, holder.size());
    Assert.assertTrue(holder.getKeyList().get(0).containsKey("GENERATED_KEY"));
    int i = 0;
    for (ClientTestModelJpa pojo : entities) Assert.assertEquals(holder.getKey(i++).intValue(), pojo.getId().intValue());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ArrayList(java.util.ArrayList) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Example 42 with KeyHolder

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

the class KeyHolderTest method testMergeSequential.

@Test
public void testMergeSequential() {
    ExecutorService service = null;
    service = new ThreadPoolExecutor(5, 50, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
    KeyHolder test = new KeyHolder();
    test.initialize(6);
    test.requireMerge();
    KeyHolder tmpHolder = new KeyHolder();
    tmpHolder.addKey(buildKey(0));
    tmpHolder.addKey(buildKey(1));
    tmpHolder.addKey(buildKey(2));
    test.addPatial(new Integer[] { 0, 1, 2 }, tmpHolder);
    tmpHolder = new KeyHolder();
    tmpHolder.addKey(buildKey(3));
    tmpHolder.addKey(buildKey(4));
    tmpHolder.addKey(buildKey(5));
    test.addPatial(new Integer[] { 3, 4, 5 }, tmpHolder);
    try {
        int i = 0;
        for (Number value : test.getIdList()) {
            assertEquals(i++, value);
        }
    } catch (SQLException e) {
        fail();
    }
}
Also used : SQLException(java.sql.SQLException) ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Test(org.junit.Test)

Example 43 with KeyHolder

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

the class KeyHolderTest method testMergeParallel.

@Test
public void testMergeParallel() {
    ExecutorService service = null;
    service = new ThreadPoolExecutor(5, 50, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
    KeyHolder test = new KeyHolder();
    test.initialize(30);
    test.requireMerge();
    List<Future<Boolean>> fList = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        fList.add(service.submit(new KeyHolderTask(test, new Integer[] { i * 3, i * 3 + 1, i * 3 + 2 })));
    }
    for (Future<Boolean> f : fList) try {
        f.get();
    } catch (InterruptedException | ExecutionException e) {
        fail();
    }
    assertTrue(test.isRequireMerge());
    assertTrue(test.isMerged());
    assertEquals(30, test.size());
    try {
        assertEquals(30, test.getIdList().size());
    } catch (SQLException e1) {
        fail();
    }
    try {
        List<Number> ids = test.getIdList();
        for (int i = 0; i < 30; i++) assertEquals(i, ids.get(i));
    } catch (SQLException e) {
        fail();
    }
    service.shutdown();
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Test(org.junit.Test)

Example 44 with KeyHolder

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

the class CombinedInsertTaskTestStub method testExecuteWithIdInsertBackOldPojo.

@Test
public void testExecuteWithIdInsertBackOldPojo() throws SQLException {
    CombinedInsertTask<ClientTestModel> test = new CombinedInsertTask<>();
    test.initialize(new ClientTestDalParser(getDbName()));
    DalHints hints = new DalHints().setIdentityBack();
    if (enableKeyHolder)
        hints.setKeyHolder(new KeyHolder());
    try {
        List<ClientTestModel> pojos = getAll();
        for (ClientTestModel pojo : pojos) pojo.setId(null);
        execute(test, hints, getAllMap(), pojos);
        fail();
    } catch (Throwable e) {
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) CombinedInsertTask(com.ctrip.platform.dal.dao.task.CombinedInsertTask) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) Test(org.junit.Test)

Example 45 with KeyHolder

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

the class DalDirectClientTestStub method updateTestInsertWithKeyHolder.

/**
 * Test the update function with key-holder
 *
 * @throws SQLException
 */
@Test
public void updateTestInsertWithKeyHolder() throws SQLException {
    if (!diff.supportGetGeneratedKeys)
        return;
    String insertSql = String.format("INSERT INTO %s VALUES(NULL, 10, 1, 'SH INFO', NULL)", TABLE_NAME);
    StatementParameters parameters = new StatementParameters();
    KeyHolder holder = new KeyHolder();
    DalHints hints = new DalHints();
    int count = client.update(insertSql, parameters, hints.setKeyHolder(holder));
    Assert.assertEquals(1, count);
    Assert.assertEquals(1, holder.size());
    Assert.assertTrue(holder.getKeyList().get(0).containsKey("GENERATED_KEY"));
    Assert.assertEquals(4, queryModelsByIds().size());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) 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