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:org.terasoluna.gfw.tutorial.selenium.DBLogAssertOperations.java

private long getCountInLogContainsByRegexMessage(String xTrack, String loggerNamePattern,
        String messagePattern) {//from w  ww .jav a 2 s . c  o m

    StringBuilder sql = new StringBuilder();
    StringBuilder where = new StringBuilder();
    sql.append("SELECT COUNT(e.*) FROM logging_event e");
    where.append(" WHERE e.formatted_message REGEXP :message");
    if (StringUtils.hasText(xTrack)) {
        sql.append(" JOIN logging_event_property ep ON ep.event_id = e.event_id");
        where.append(" AND ep.mapped_key = 'X-Track' AND ep.mapped_value = :xTrack");
    }
    if (StringUtils.hasText(loggerNamePattern)) {
        where.append(" AND e.logger_name REGEXP :loggerName");
    }
    sql.append(where);

    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("xTrack", xTrack);
    params.addValue("loggerName", loggerNamePattern);
    params.addValue("message", messagePattern);

    return jdbcOperations.queryForObject(sql.toString(), params, Long.class);

}

From source file:org.terasoluna.gfw.tutorial.selenium.DBLogAssertOperations.java

/**
 * ??(??)??????/*from  ww  w  .j  a  v a2s .c o  m*/
 * <p>
 * X-Track,?????????????
 * </p>
 * @param xTrack ????????????null
 * @param loggerNamePattern ??????????null
 * @param messagePattern ?
 * @return??(??)??
 */
public List<String> getLogByRegexMessage(String xTrack, String loggerNamePattern, String messagePattern) {

    StringBuilder sql = new StringBuilder();
    StringBuilder where = new StringBuilder();
    sql.append("SELECT e.formatted_message FROM logging_event e");
    where.append(" WHERE e.formatted_message REGEXP :message");
    if (StringUtils.hasText(xTrack)) {
        sql.append(" JOIN logging_event_property ep ON ep.event_id = e.event_id");
        where.append(" AND ep.mapped_key = 'X-Track' AND ep.mapped_value = :xTrack");
    }
    if (StringUtils.hasText(loggerNamePattern)) {
        where.append(" AND e.logger_name REGEXP :loggerName");
    }
    StringBuilder orderBy = new StringBuilder();
    orderBy.append(" ORDER BY e.event_id ASC");
    sql.append(where).append(orderBy);

    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("xTrack", xTrack);
    params.addValue("loggerName", loggerNamePattern);
    params.addValue("message", messagePattern);

    return jdbcOperations.queryForList(sql.toString(), params, String.class);
}

From source file:com.ushahidi.swiftriver.core.api.dao.impl.JpaMediaDao.java

/**
 * Populate the droplet media table.//from w  w  w .  j a v  a2 s .co m
 * 
 * @param drops
 */
private void insertDropletMedia(List<Drop> drops) {

    // List of drop IDs in the drops list
    List<Long> dropIds = new ArrayList<Long>();
    // List of media in a drop
    Map<Long, Set<Long>> dropletMediaMap = new HashMap<Long, Set<Long>>();
    // List of drops and the media that is the drop image
    final List<long[]> dropImages = new ArrayList<long[]>();
    for (Drop drop : drops) {

        if (drop.getMedia() == null)
            continue;

        dropIds.add(drop.getId());

        for (Media media : drop.getMedia()) {
            Set<Long> m = null;
            if (dropletMediaMap.containsKey(drop.getId())) {
                m = dropletMediaMap.get(drop.getId());
            } else {
                m = new HashSet<Long>();
                dropletMediaMap.put(drop.getId(), m);
            }

            // Is this the drop image?
            if (drop.getImage() != null && media.getUrl().equals(drop.getImage().getUrl())) {
                long[] dropImage = { drop.getId(), media.getId() };
                dropImages.add(dropImage);
            }

            m.add(media.getId());
        }
    }

    // Find droplet media that already exist in the db
    String sql = "SELECT droplet_id, media_id FROM droplets_media WHERE droplet_id in (:ids)";

    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("ids", dropIds);

    List<Map<String, Object>> results = this.namedJdbcTemplate.queryForList(sql, params);

    // Remove already existing droplet_media from our Set
    for (Map<String, Object> result : results) {
        long dropletId = ((Number) result.get("droplet_id")).longValue();
        long mediaId = ((Number) result.get("media_id")).longValue();

        Set<Long> mediaSet = dropletMediaMap.get(dropletId);
        if (mediaSet != null) {
            mediaSet.remove(mediaId);
        }
    }

    // Insert the remaining items in the set into the db
    sql = "INSERT INTO droplets_media (droplet_id, media_id) VALUES (?,?)";

    final List<long[]> dropletMediaList = new ArrayList<long[]>();
    for (Long dropletId : dropletMediaMap.keySet()) {
        for (Long mediaId : dropletMediaMap.get(dropletId)) {
            long[] dropletMedia = { dropletId, mediaId };
            dropletMediaList.add(dropletMedia);
        }
    }
    jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            long[] dropletMedia = dropletMediaList.get(i);
            ps.setLong(1, dropletMedia[0]);
            ps.setLong(2, dropletMedia[1]);
        }

        public int getBatchSize() {
            return dropletMediaList.size();
        }
    });

    if (dropImages.size() > 0) {
        sql = "UPDATE droplets SET droplet_image = ? WHERE id = ?";
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                long[] update = dropImages.get(i);
                ps.setLong(1, update[1]);
                ps.setLong(2, update[0]);
            }

            public int getBatchSize() {
                return dropImages.size();
            }
        });
    }
}

From source file:org.terasoluna.gfw.tutorial.selenium.DBLogAssertOperations.java

protected void assertNotContainsLevels(String xTrack, String... levels) {

    StringBuilder sql = new StringBuilder();
    StringBuilder where = new StringBuilder();
    sql.append("SELECT COUNT(e.*) FROM logging_event e");
    where.append(" WHERE e.level_string IN (");
    for (int i = 0; i < levels.length; i++) {
        if (0 < i) {
            where.append(",");
        }// w  w  w. j  a va  2 s.com
        where.append(":level").append(i);
    }
    where.append(")");
    if (StringUtils.hasText(xTrack)) {
        sql.append(" JOIN logging_event_property ep ON ep.event_id = e.event_id");
        where.append(" AND ep.mapped_key = 'X-Track' AND ep.mapped_value = :xTrack");
    }
    sql.append(where);

    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("xTrack", xTrack);
    for (int i = 0; i < levels.length; i++) {
        params.addValue("level" + i, levels[i]);
    }
    Long count = jdbcOperations.queryForObject(sql.toString(), params, Long.class);
    assertThat(count, is(0L));
}

From source file:org.terasoluna.gfw.tutorial.selenium.DBLogAssertOperations.java

/**
 * ??(??)???????????/*from w  ww  . j a  va 2s  .c  om*/
 * <p>
 * X-Track,?????????????
 * </p>
 * @param xTrack ????????????null
 * @param loggerNamePattern ??????????null
 * @param messagePattern ?
 * @param exceptionMessagePattern ?()
 */
public void assertContainsByRegexExceptionMessage(String xTrack, String loggerNamePattern,
        String messagePattern, String exceptionMessagePattern) {

    StringBuilder sql = new StringBuilder();
    StringBuilder where = new StringBuilder();
    sql.append("SELECT COUNT(e.*) FROM logging_event e");
    where.append(" WHERE e.formatted_message REGEXP :message");

    sql.append(" JOIN logging_event_exception ee ON ee.event_id = e.event_id");
    where.append(" AND ee.I = '0' AND ee.TRACE_LINE REGEXP :exceptionMessage");

    if (StringUtils.hasText(xTrack)) {
        sql.append(" JOIN logging_event_property ep ON ep.event_id = e.event_id");
        where.append(" AND ep.mapped_key = 'X-Track' AND ep.mapped_value = :xTrack");
    }
    if (StringUtils.hasText(loggerNamePattern)) {
        where.append(" AND e.logger_name REGEXP :loggerName");
    }
    sql.append(where);

    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("xTrack", xTrack);
    params.addValue("loggerName", loggerNamePattern);
    params.addValue("message", messagePattern);
    params.addValue("exceptionMessage", exceptionMessagePattern);
    Long count = jdbcOperations.queryForObject(sql.toString(), params, Long.class);
    assertThat(count, is(1L));
}

From source file:com.joliciel.jochre.doc.DocumentDaoJdbc.java

@Override
public void deleteJochrePage(JochrePage page) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("page_id", page.getId());
    String sql = null;//w w w . j  a v a  2  s  .c o  m

    sql = "delete from ocr_page" + " WHERE page_id = :page_id";

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

From source file:com.joliciel.jochre.doc.DocumentDaoJdbc.java

@Override
public List<JochrePage> findJochrePages(JochreDocument jochreDocument) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_PAGE + " FROM ocr_page WHERE page_doc_id=:page_doc_id"
            + " ORDER BY page_index";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("page_doc_id", jochreDocument.getId());

    LOG.info(sql);/*from w  w  w . jav a  2  s .  c  om*/
    logParameters(paramSource);
    @SuppressWarnings("unchecked")
    List<JochrePage> jochrePages = jt.query(sql, paramSource,
            new JochrePageMapper(this.getDocumentServiceInternal()));

    return jochrePages;
}

From source file:com.joliciel.jochre.doc.DocumentDaoJdbc.java

@Override
public List<Author> findAuthors(JochreDocument jochreDocument) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_AUTHOR + " FROM ocr_author"
            + " INNER JOIN ocr_doc_author_map ON docauthor_author_id = author_id"
            + " WHERE docauthor_doc_id=:docauthor_doc_id" + " ORDER BY author_last_name, author_first_name";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("docauthor_doc_id", jochreDocument.getId());

    LOG.info(sql);/* w w w. j  a  v  a2  s . c  o  m*/
    logParameters(paramSource);
    @SuppressWarnings("unchecked")
    List<Author> authors = jt.query(sql, paramSource, new AuthorMapper(this.getDocumentServiceInternal()));

    return authors;
}

From source file:com.joliciel.jochre.doc.DocumentDaoJdbc.java

@Override
public JochrePage loadJochrePage(int jochrePageId) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_PAGE + " FROM ocr_page WHERE page_id=:page_id";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("page_id", jochrePageId);

    LOG.info(sql);//  w  ww .  j  a  v a  2 s  . co  m
    logParameters(paramSource);
    JochrePage jochrePage = null;
    try {
        jochrePage = (JochrePage) jt.queryForObject(sql, paramSource,
                new JochrePageMapper(this.getDocumentServiceInternal()));
    } catch (EmptyResultDataAccessException ex) {
        ex.hashCode();
    }
    return jochrePage;
}

From source file:com.joliciel.jochre.doc.DocumentDaoJdbc.java

@Override
public JochreDocument loadJochreDocument(int jochreDocumentId) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_DOCUMENT + " FROM ocr_document WHERE doc_id=:doc_id";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("doc_id", jochreDocumentId);

    LOG.info(sql);//  w  ww  . j  a v a 2s  .c om
    logParameters(paramSource);
    JochreDocument jochreDocument = null;
    try {
        jochreDocument = (JochreDocument) jt.queryForObject(sql, paramSource,
                new JochreDocumentMapper(this.getDocumentServiceInternal()));
    } catch (EmptyResultDataAccessException ex) {
        ex.hashCode();
    }
    return jochreDocument;
}