Search in sources :

Example 11 with KeyHolder

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

the class CombinedInsertTaskTestStub method testExecute.

@Test
public void testExecute() {
    CombinedInsertTask<ClientTestModel> test = new CombinedInsertTask<>();
    test.initialize(new ClientTestDalParser(getDbName()));
    DalHints hints = new DalHints();
    if (enableKeyHolder)
        hints.setKeyHolder(new KeyHolder());
    try {
        execute(test, hints, getAllMap(), getAll());
        if (enableKeyHolder) {
            // You have to merge before get size
            assertEquals(3, hints.getKeyHolder().size());
        }
        assertEquals(3 + 3, getCount());
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) SQLException(java.sql.SQLException) CombinedInsertTask(com.ctrip.platform.dal.dao.task.CombinedInsertTask) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) Test(org.junit.Test)

Example 12 with KeyHolder

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

the class DalDirectClient method update.

@Override
public int update(String sql, StatementParameters parameters, final DalHints hints) throws SQLException {
    final KeyHolder generatedKeyHolder = hints.getKeyHolder();
    ConnectionAction<Integer> action = new ConnectionAction<Integer>() {

        @Override
        public Integer execute() throws Exception {
            conn = getConnection(hints, this);
            // For old generated free update, the parameters is nit compiled before invoke direct client
            parameters.compile();
            if (generatedKeyHolder == null)
                preparedStatement = createPreparedStatement(conn, sql, parameters, hints);
            else
                preparedStatement = createPreparedStatement(conn, sql, parameters, hints, generatedKeyHolder);
            DalWatcher.beginExecute();
            int rows = preparedStatement.executeUpdate();
            DalWatcher.endExectue();
            if (generatedKeyHolder == null)
                return rows;
            List<Map<String, Object>> generatedKeys = generatedKeyHolder.getKeyList();
            rs = preparedStatement.getGeneratedKeys();
            if (rs == null)
                return rows;
            DalRowMapperExtractor<Map<String, Object>> rse = new DalRowMapperExtractor<Map<String, Object>>(new DalColumnMapRowMapper());
            generatedKeys.addAll(rse.extract(rs));
            return rows;
        }
    };
    action.populate(generatedKeyHolder == null ? DalEventEnum.UPDATE_SIMPLE : DalEventEnum.UPDATE_KH, sql, parameters);
    return doInConnection(action, hints);
}
Also used : DalColumnMapRowMapper(com.ctrip.platform.dal.dao.helper.DalColumnMapRowMapper) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) DalRowMapperExtractor(com.ctrip.platform.dal.dao.helper.DalRowMapperExtractor)

Example 13 with KeyHolder

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

the class CombinedInsertTask method execute.

@Override
public Integer execute(DalHints hints, Map<Integer, Map<String, ?>> daoPojos, BulkTaskContext<T> taskContext) throws SQLException {
    StatementParameters parameters = new StatementParameters();
    StringBuilder values = new StringBuilder();
    Set<String> unqualifiedColumns = taskContext.getUnqualifiedColumns();
    List<String> finalInsertableColumns = buildValidColumnsForInsert(unqualifiedColumns);
    String insertColumns = combineColumns(finalInsertableColumns, COLUMN_SEPARATOR);
    int startIndex = 1;
    for (Integer index : daoPojos.keySet()) {
        Map<String, ?> pojo = daoPojos.get(index);
        removeUnqualifiedColumns(pojo, unqualifiedColumns);
        int paramCount = addParameters(startIndex, parameters, pojo, finalInsertableColumns);
        startIndex += paramCount;
        values.append(String.format("(%s),", combine("?", paramCount, ",")));
    }
    String sql = String.format(TMPL_SQL_MULTIPLE_INSERT, getTableName(hints), insertColumns, values.substring(0, values.length() - 2) + ")");
    KeyHolder keyHolder = hints.getKeyHolder();
    KeyHolder tmpHolder = keyHolder != null && keyHolder.isRequireMerge() ? new KeyHolder() : keyHolder;
    int count = client.update(sql, parameters, hints.setKeyHolder(tmpHolder));
    if (tmpHolder != null)
        keyHolder.addPatial(daoPojos.keySet().toArray(new Integer[daoPojos.size()]), tmpHolder);
    hints.setKeyHolder(keyHolder);
    return count;
}
Also used : StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder)

Example 14 with KeyHolder

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

the class KeyHolderTest method testGetIdList.

@Test
public void testGetIdList() {
    KeyHolder test = new KeyHolder();
    test.addKey(buildKey(1));
    try {
        assertEquals(1, test.getIdList().size());
        test.addKey(buildKey(10));
        assertEquals(2, test.getIdList().size());
        assertEquals(1, test.getIdList().get(0));
        assertEquals(10, test.getIdList().get(1));
    } catch (SQLException e) {
        fail();
    }
}
Also used : SQLException(java.sql.SQLException) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) Test(org.junit.Test)

Example 15 with KeyHolder

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

the class KeyHolderTest method testGetKeyInt.

@Test
public void testGetKeyInt() {
    KeyHolder test = new KeyHolder();
    test.addKey(buildKey(1));
    try {
        assertEquals(1, test.getKey(0).longValue());
        test.addKey(buildKey(10));
        assertEquals(10, test.getKey(1).longValue());
    } catch (SQLException e) {
        fail();
    }
}
Also used : SQLException(java.sql.SQLException) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) 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