Example usage for org.springframework.jdbc.core.namedparam MapSqlParameterSource addValue

List of usage examples for org.springframework.jdbc.core.namedparam MapSqlParameterSource addValue

Introduction

In this page you can find the example usage for org.springframework.jdbc.core.namedparam MapSqlParameterSource addValue.

Prototype

public MapSqlParameterSource addValue(String paramName, @Nullable Object value) 

Source Link

Document

Add a parameter to this parameter source.

Usage

From source file:ru.org.linux.user.UserTagDao.java

/**
 *      ./* w  w w .j av  a2 s . co  m*/
 *
 * @param oldTagId   ? 
 * @param newTagId    
 */
public void replaceTag(int oldTagId, int newTagId) {
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("new_tag_id", newTagId);
    parameters.addValue("old_tag_id", oldTagId);
    jdbcTemplate.update(
            "UPDATE user_tags SET tag_id=:new_tag_id WHERE tag_id=:old_tag_id "
                    + "AND user_id NOT IN (SELECT user_id FROM user_tags WHERE tag_id=:new_tag_id)",
            parameters);
}

From source file:ru.org.linux.user.UserTagDao.java

/**
 *    ?.//from   w  ww . j  a  v  a2  s .  co m
 *
 * @param userId       ?
 * @param tagId        
 * @param isFavorite    (true)   (false)
 */
public void deleteTag(int userId, int tagId, boolean isFavorite) {
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("user_id", userId);
    parameters.addValue("tag_id", tagId);
    parameters.addValue("is_favorite", isFavorite);

    jdbcTemplate.update(
            "DELETE FROM user_tags WHERE user_id=:user_id and tag_id=:tag_id and is_favorite=:is_favorite",
            parameters);
}

From source file:ru.org.linux.user.UserTagDao.java

/**
 *  ?? ID ,     ? ?  ./*from   w  w w. jav  a 2s .c  o m*/
 *
 * @param userId   ?,    ? 
 * @param tags   ??  
 * @return ?? ID 
 */
public List<Integer> getUserIdListByTags(int userId, List<String> tags) {
    if (tags.isEmpty()) {
        return ImmutableList.of();
    }
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("values", tags);
    parameters.addValue("user_id", userId);

    return jdbcTemplate.queryForList(
            "select distinct user_id from user_tags where tag_id in (select id from tags_values where value in ( :values )) "
                    + "AND is_favorite = true AND user_id <> :user_id",
            parameters, Integer.class);
}

From source file:com.devicehive.dao.rdbms.DeviceDaoRdbmsImpl.java

/**
 * Method change statuses for devices with guids that consists in the list
 *
 * @param status new status//from   ww  w  .  ja  v  a 2s.  c  om
 * @param guids  list of guids
 */
public void changeStatusForDevices(String status, List<String> guids) {
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("status", status);
    parameters.addValue("guids", guids);
    jdbcTemplate.update(UPDATE_DEVICES_STATUSES, parameters);
}

From source file:ru.org.linux.user.UserTagDao.java

/**
 *    ./*from w  w w.  j av a2 s  . com*/
 *
 * @param userId       ?
 * @param tagId        
 * @param isFavorite    (true)   (false)
 */
public void addTag(int userId, int tagId, boolean isFavorite) {
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("user_id", userId);
    parameters.addValue("tag_id", tagId);
    parameters.addValue("is_favorite", isFavorite);

    try {
        jdbcTemplate.update(
                "INSERT INTO user_tags (user_id, tag_id, is_favorite) VALUES(:user_id, :tag_id, :is_favorite)",
                parameters);
    } catch (DuplicateKeyException ex) {
        logger.debug("Tag already added to favs", ex);
    }
}

From source file:com.joliciel.jochre.boundaries.BoundaryDaoJdbc.java

@Override
public void deleteSplit(Split split) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("split_id", split.getId());
    String sql = null;/*from   w  w  w.  jav a 2s  . c o  m*/

    sql = "delete from ocr_split where split_id = :split_id";

    LOG.debug(sql);
    logParameters(paramSource);
    jt.update(sql, paramSource);
}

From source file:com.devicehive.dao.rdbms.DeviceDaoRdbmsImpl.java

/**
 * Method return a Map where KEY is a device guid from guids list and
 * VALUE is OfflineTimeout from deviceClass for device with current guid.
 *
 * @param guids list of guids//from w w w . ja  va  2  s.co  m
 */
public Map<String, Integer> getOfflineTimeForDevices(List<String> guids) {
    final Map<String, Integer> deviceInfo = new HashMap<>();
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("guids", guids);
    List<Map<String, Object>> results = jdbcTemplate.queryForList(GET_DEVICES_GUIDS_AND_OFFLINE_TIMEOUT,
            parameters);
    results.stream()
            .forEach(map -> deviceInfo.put((String) map.get("guid"), (Integer) map.get("offline_timeout")));
    return deviceInfo;
}

From source file:org.inbio.modeling.core.dao.impl.ConfigDAOImpl.java

@Override
public String findParameter(String parameterName) {
    String sqlStatement = null;//from   w  ww  . j a  v  a2s .c om
    String parameterValue = null;
    MapSqlParameterSource args = null;

    sqlStatement = "SELECT value FROM " + this.table + " " + " WHERE name = :parameter_name";

    args = new MapSqlParameterSource();
    args.addValue("parameter_name", parameterName);

    parameterValue = getSimpleJdbcTemplate().queryForObject(sqlStatement, String.class, args);

    return parameterValue;
}

From source file:org.sakuli.services.receiver.database.dao.impl.DaoTestCaseImpl.java

@Override
public void saveTestCaseResult(final TestCase testCase) {
    logger.info("Save results for test case \"" + testCase.getId() + "\"");

    //create a map for the sql parameters
    MapSqlParameterSource tcParameters = new MapSqlParameterSource();
    tcParameters.addValue("sahi_suites_id", testSuite.getDbPrimaryKey());
    tcParameters.addValue("caseID", testCase.getId());
    tcParameters.addValue("result", testCase.getState().getErrorCode());
    tcParameters.addValue("result_desc", testCase.getState());
    tcParameters.addValue("name", testCase.getName());
    tcParameters.addValue("guid", testSuite.getGuid());
    tcParameters.addValue("start", testCase.getStartDateAsUnixTimestamp());
    tcParameters.addValue("stop", testCase.getStopDateAsUnixTimestamp());
    tcParameters.addValue("warning", testCase.getWarningTime());
    tcParameters.addValue("critical", testCase.getCriticalTime());
    tcParameters.addValue("browser", testSuite.getBrowserInfo());
    tcParameters.addValue("lastpage", testCase.getLastURL());

    //try to save the screenshot
    try {/* ww w . ja  va 2  s. c  om*/
        if (testCase.getScreenShotPath() != null) {
            final InputStream blobIs = Files.newInputStream(testCase.getScreenShotPath());
            final int length = (int) testCase.getScreenShotPath().toFile().length();
            tcParameters.addValue("screenshot", new SqlLobValue(blobIs, length, lobHandler), Types.BLOB);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    tcParameters.addValue("duration", testCase.getDuration());
    tcParameters.addValue("msg", testCase.getExceptionMessages());

    //generate the sql-statement
    SimpleJdbcInsert insertTCResults = new SimpleJdbcInsert(getDataSource()).withTableName("sahi_cases")
            .usingGeneratedKeyColumns("id");

    logger.info(
            "write the following values to 'sahi_cases': " + tcParameters.getValues() + " => now execute ....");

    int dbPrimaryKey = insertTCResults.executeAndReturnKey(tcParameters).intValue();

    logger.info("test case '" + testCase.getId() + "' has been written to 'sahi_cases' with  primaryKey="
            + dbPrimaryKey);
    testCase.setDbPrimaryKey(dbPrimaryKey);
}

From source file:ru.org.linux.user.UserTagDao.java

/**
 *  ?? ?  ? ?./*from www  .  j  av  a2  s . c o  m*/
 *
 * @param userId       ?
 * @param isFavorite    (true)   (false)
 * @return ??  ?
 */
public ImmutableList<String> getTags(int userId, boolean isFavorite) {
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("user_id", userId);
    parameters.addValue("is_favorite", isFavorite);

    final ImmutableList.Builder<String> tags = ImmutableList.builder();

    jdbcTemplate.query("SELECT tags_values.value FROM user_tags, tags_values WHERE "
            + "user_tags.user_id=:user_id AND tags_values.id=user_tags.tag_id AND user_tags.is_favorite=:is_favorite "
            + "ORDER BY value", parameters, new RowCallbackHandler() {
                @Override
                public void processRow(ResultSet rs) throws SQLException {
                    tags.add(rs.getString("value"));
                }
            });

    return tags.build();
}