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();
}
}
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);
}
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;
}
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();
}
}
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();
}
}
Aggregations