Example usage for org.springframework.jdbc.core.namedparam NamedParameterJdbcOperations query

List of usage examples for org.springframework.jdbc.core.namedparam NamedParameterJdbcOperations query

Introduction

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

Prototype

<T> List<T> query(String sql, Map<String, ?> paramMap, RowMapper<T> rowMapper) throws DataAccessException;

Source Link

Document

Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, mapping each row to a Java object via a RowMapper.

Usage

From source file:com.opengamma.masterdb.portfolio.DbPortfolioMaster.java

/**
 * Gets a node by identifier.//  w  w  w  . j a  va 2 s.  c  o m
 * 
 * @param uniqueId the unique identifier, not null
 * @return the node, null if not found
 */
protected ManageablePortfolioNode getNodeById(final UniqueId uniqueId) {
    s_logger.debug("getNodeById {}", uniqueId);
    final DbMapSqlParameterSource args = new DbMapSqlParameterSource().addValue("node_id",
            extractRowId(uniqueId));
    final PortfolioDocumentExtractor extractor = new PortfolioDocumentExtractor(true, false);
    final NamedParameterJdbcOperations namedJdbc = getDbConnector().getJdbcTemplate();
    final String sql = getElSqlBundle().getSql("GetNodeById", args);
    final List<PortfolioDocument> docs = namedJdbc.query(sql, args, extractor);
    if (docs.isEmpty()) {
        throw new DataNotFoundException("Node not found: " + uniqueId);
    }
    return docs.get(0).getPortfolio().getRootNode(); // SQL loads desired node in place of the root node
}

From source file:com.opengamma.masterdb.portfolio.DbPortfolioMaster.java

/**
 * Gets a node by searching for the latest version of an object identifier.
 * //from   ww w.  j  a v a  2  s .  co  m
 * @param uniqueId the unique identifier, not null
 * @param versionAsOf the instant to fetch, not null
 * @param correctedTo the instant to fetch, not null
 * @return the node, null if not found
 */
protected ManageablePortfolioNode getNodeByInstants(final UniqueId uniqueId, final Instant versionAsOf,
        final Instant correctedTo) {
    s_logger.debug("getNodeByLatest {}", uniqueId);
    final Instant now = now();
    final DbMapSqlParameterSource args = new DbMapSqlParameterSource()
            .addValue("node_oid", extractOid(uniqueId))
            .addTimestamp("version_as_of_instant", Objects.firstNonNull(versionAsOf, now))
            .addTimestamp("corrected_to_instant", Objects.firstNonNull(correctedTo, now));
    final PortfolioDocumentExtractor extractor = new PortfolioDocumentExtractor(true, false);
    final NamedParameterJdbcOperations namedJdbc = getDbConnector().getJdbcTemplate();
    final String sql = getElSqlBundle().getSql("GetNodeByOidInstants", args);
    final List<PortfolioDocument> docs = namedJdbc.query(sql, args, extractor);
    if (docs.isEmpty()) {
        throw new DataNotFoundException("Node not found: " + uniqueId);
    }
    return docs.get(0).getPortfolio().getRootNode(); // SQL loads desired node in place of the root node
}

From source file:io.kahu.hawaii.util.pagination.PaginationHelper.java

public Page<E> fetchPage(final NamedParameterJdbcOperations jt, final String sqlCountRows,
        final String sqlFetchRows, final Map<String, Object> paramMap, final Pageable pageable,
        final RowMapper<E> rowMapper) {

    // first execute select count (needed for paging metadata)
    final int rowCount = jt.queryForObject(sqlCountRows, paramMap, Integer.class);

    if (maxRows != null && (rowCount > maxRows)) {
        // Threshold defined and total number of records is higher
        return new PageImpl<E>(new ArrayList<>(), new PageRequest(0, 1), rowCount);
    }/*from w w w  .j a  v  a 2 s.c  o  m*/
    // now fetch the actual objects (content)
    final List<E> pageItems = jt.query(sqlFetchRows, paramMap, (resultSet, rowNum) -> {
        return rowMapper.mapRow(resultSet, rowNum);
    });

    // return the page
    return new PageImpl<E>(pageItems, pageable, rowCount);
}

From source file:com.opengamma.masterdb.position.DbPositionMaster.java

/**
 * Gets a trade by identifier./* w  w w. ja va 2s .  co  m*/
 * 
 * @param uniqueId the unique identifier, not null
 * @return the trade, null if not found
 */
protected ManageableTrade getTradeById(final UniqueId uniqueId) {
    s_logger.debug("getTradeById {}", uniqueId);
    final DbMapSqlParameterSource args = new DbMapSqlParameterSource().addValue("trade_id",
            extractRowId(uniqueId));
    final PositionDocumentExtractor extractor = new PositionDocumentExtractor();
    final NamedParameterJdbcOperations namedJdbc = getDbConnector().getJdbcTemplate();
    final String sql = getElSqlBundle().getSql("GetTradeById", args);
    final List<PositionDocument> docs = namedJdbc.query(sql, args, extractor);
    if (docs.isEmpty()) {
        throw new DataNotFoundException("Trade not found: " + uniqueId);
    }
    return docs.get(0).getPosition().getTrades().get(0); // SQL loads desired trade as only trade
}

From source file:com.opengamma.masterdb.position.DbPositionMaster.java

/**
 * Gets a trade by searching for the latest version of an object identifier.
 * /* ww  w  .j a  v  a 2 s.co  m*/
 * @param uniqueId the unique identifier, not null
 * @param versionAsOf the instant to fetch, not null
 * @param correctedTo the instant to fetch, not null
 * @return the trade, null if not found
 */
protected ManageableTrade getTradeByInstants(final UniqueId uniqueId, final Instant versionAsOf,
        final Instant correctedTo) {
    s_logger.debug("getTradeByLatest {}", uniqueId);
    final Instant now = now();
    final DbMapSqlParameterSource args = new DbMapSqlParameterSource()
            .addValue("trade_oid", extractOid(uniqueId))
            .addTimestamp("version_as_of_instant", Objects.firstNonNull(versionAsOf, now))
            .addTimestamp("corrected_to_instant", Objects.firstNonNull(correctedTo, now));
    final PositionDocumentExtractor extractor = new PositionDocumentExtractor();
    final NamedParameterJdbcOperations namedJdbc = getDbConnector().getJdbcTemplate();
    final String sql = getElSqlBundle().getSql("GetTradeByOidInstants", args);
    final List<PositionDocument> docs = namedJdbc.query(sql, args, extractor);
    if (docs.isEmpty()) {
        throw new DataNotFoundException("Trade not found: " + uniqueId);
    }
    return docs.get(0).getPosition().getTrades().get(0); // SQL loads desired trade as only trade
}

From source file:org.geoserver.jdbcconfig.internal.DbMappings.java

private Map<Integer, Map<String, PropertyType>> loadPropertyTypes(NamedParameterJdbcOperations template) {
    final String query = "select oid, target_property, type_id, name, collection, text from property_type";
    RowMapper<PropertyType> rowMapper = new RowMapper<PropertyType>() {
        @Override/*  w ww.  j  a va 2 s.  com*/
        public PropertyType mapRow(ResultSet rs, int rowNum) throws SQLException {
            Integer oid = rs.getInt(1);
            Integer targetPropertyOid = (Integer) rs.getObject(2);
            Integer objectTypeOid = rs.getInt(3);
            String propertyName = rs.getString(4);
            Boolean collectionProperty = rs.getBoolean(5);
            Boolean textProperty = rs.getBoolean(6);

            PropertyType pt = new PropertyType(oid, targetPropertyOid, objectTypeOid, propertyName,
                    collectionProperty, textProperty);

            return pt;
        }
    };

    final List<PropertyType> propertyTypes;
    {
        final Map<String, ?> params = Collections.emptyMap();
        propertyTypes = template.query(query, params, rowMapper);
    }

    Map<Integer, Map<String, PropertyType>> perTypeProps = Maps.newHashMap();
    for (PropertyType pt : propertyTypes) {
        Integer objectType = pt.getObjectTypeOid();
        Map<String, PropertyType> typeProperties = perTypeProps.get(objectType);
        if (typeProperties == null) {
            typeProperties = Maps.newHashMap();
            perTypeProps.put(objectType, typeProperties);
        }
        typeProperties.put(pt.getPropertyName(), pt);
    }
    return perTypeProps;
}