Search in sources :

Example 1 with DalRowMapperExtractor

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;
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ClientTestModel(test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) DalRowMapperExtractor(com.ctrip.platform.dal.dao.helper.DalRowMapperExtractor)

Example 2 with DalRowMapperExtractor

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);
}
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 3 with DalRowMapperExtractor

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;
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ClientTestModel(test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) DalRowMapperExtractor(com.ctrip.platform.dal.dao.helper.DalRowMapperExtractor)

Example 4 with DalRowMapperExtractor

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());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalCustomRowMapper(com.ctrip.platform.dal.dao.helper.DalCustomRowMapper) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) Map(java.util.Map) DalRowMapperExtractor(com.ctrip.platform.dal.dao.helper.DalRowMapperExtractor) Test(org.junit.Test)

Example 5 with DalRowMapperExtractor

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());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalCustomRowMapper(com.ctrip.platform.dal.dao.helper.DalCustomRowMapper) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) Map(java.util.Map) DalRowMapperExtractor(com.ctrip.platform.dal.dao.helper.DalRowMapperExtractor) Test(org.junit.Test)

Aggregations

DalRowMapperExtractor (com.ctrip.platform.dal.dao.helper.DalRowMapperExtractor)5 DalHints (com.ctrip.platform.dal.dao.DalHints)4 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 DalCustomRowMapper (com.ctrip.platform.dal.dao.helper.DalCustomRowMapper)2 SQLException (java.sql.SQLException)2 Test (org.junit.Test)2 ClientTestModel (test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel)2 KeyHolder (com.ctrip.platform.dal.dao.KeyHolder)1 DalColumnMapRowMapper (com.ctrip.platform.dal.dao.helper.DalColumnMapRowMapper)1 LinkedHashMap (java.util.LinkedHashMap)1