use of com.ctrip.platform.dal.dao.helper.DalRowMapperExtractor in project dal by ctripcorp.
the class DalDirectClientSqlServerTest method queryModelsByIds.
/**
* Get the models all in dal_client_test by specified IDs
*
* @param ids
* @return The list of ClientTestModel
*/
private List<ClientTestModel> queryModelsByIds(int... ids) {
List<ClientTestModel> models = new ArrayList<ClientTestModel>();
String querySql = "";
if (null != ids && ids.length > 0) {
Integer[] idds = new Integer[ids.length];
for (int i = 0; i < idds.length; i++) {
idds[i] = ids[i];
}
querySql = "SELECT * FROM %s WHERE id in(%s)";
String inClause = StringUtils.join(idds, ",");
querySql = String.format(querySql, TABLE_NAME, inClause);
} else {
querySql = "SELECT * FROM " + TABLE_NAME;
}
StatementParameters parameters = new StatementParameters();
DalHints hints = new DalHints();
try {
models = client.query(querySql, parameters, hints, new DalRowMapperExtractor<ClientTestModel>(mapper));
} catch (SQLException e) {
e.printStackTrace();
}
return models;
}
use of com.ctrip.platform.dal.dao.helper.DalRowMapperExtractor 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.helper.DalRowMapperExtractor in project dal by ctripcorp.
the class DalDirectClientTestStub method queryModelsByIds.
/**
* Get the models all in dal_client_test by specified IDs
*
* @param ids
* @return The list of ClientTestModel
*/
private List<ClientTestModel> queryModelsByIds(int... ids) {
List<ClientTestModel> models = new ArrayList<ClientTestModel>();
String querySql = "";
if (null != ids && ids.length > 0) {
Integer[] idds = new Integer[ids.length];
for (int i = 0; i < idds.length; i++) {
idds[i] = ids[i];
}
querySql = "SELECT * FROM %s WHERE id in(%s)";
String inClause = StringUtils.join(idds, ",");
querySql = String.format(querySql, TABLE_NAME, inClause);
} else {
querySql = "SELECT * FROM " + TABLE_NAME;
}
StatementParameters parameters = new StatementParameters();
DalHints hints = new DalHints();
try {
models = client.query(querySql, parameters, hints, new DalRowMapperExtractor<ClientTestModel>(mapper));
} catch (SQLException e) {
e.printStackTrace();
}
return models;
}
use of com.ctrip.platform.dal.dao.helper.DalRowMapperExtractor in project dal by ctripcorp.
the class DalCustomRowMapperTest method testArray.
@Test
public void testArray() throws SQLException {
DalCustomRowMapper mapper = new DalCustomRowMapper("id", "quantity", "type");
String sql = "select id, quantity, type from " + database.getTableName();
DalRowMapperExtractor<Map<String, Object>> rse = new DalRowMapperExtractor<Map<String, Object>>(mapper);
List<Map<String, Object>> rest = database.getClient().query(sql, new StatementParameters(), new DalHints(), rse);
Assert.assertEquals(3, rest.size());
Assert.assertEquals("1", rest.get(0).get("id").toString());
}
use of com.ctrip.platform.dal.dao.helper.DalRowMapperExtractor in project dal by ctripcorp.
the class DalCustomRowMapperTest method testList.
@Test
public void testList() throws SQLException {
List<String> columns = new ArrayList<String>();
columns.add("id");
DalCustomRowMapper mapper = new DalCustomRowMapper(columns);
String sql = "select id from " + database.getTableName();
DalRowMapperExtractor<Map<String, Object>> rse = new DalRowMapperExtractor<Map<String, Object>>(mapper);
List<Map<String, Object>> rest = database.getClient().query(sql, new StatementParameters(), new DalHints(), rse);
Assert.assertEquals(3, rest.size());
Assert.assertEquals("1", rest.get(0).get("id").toString());
}
Aggregations