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, int sqlType) 

Source Link

Document

Add a parameter to this parameter source.

Usage

From source file:io.lavagna.common.QueryType.java

private static SqlParameterSource extractParameters(Method m, Object[] args) {

    Class<?>[] parameterTypes = m.getParameterTypes();
    Annotation[][] parameterAnnotations = m.getParameterAnnotations();
    if (parameterAnnotations == null || parameterAnnotations.length == 0) {
        return new EmptySqlParameterSource();
    }//w  w w.j  a va 2 s . c o  m

    MapSqlParameterSource ps = new MapSqlParameterSource();
    for (int i = 0; i < args.length; i++) {
        String name = parameterName(parameterAnnotations[i]);
        if (name != null) {
            ps.addValue(name, args[i], StatementCreatorUtils.javaTypeToSqlParameterType(parameterTypes[i]));
        }
    }

    return ps;
}

From source file:alfio.datamapper.QueryType.java

private static SqlParameterSource extractParameters(Method m, Object[] args) {

    Annotation[][] parameterAnnotations = m.getParameterAnnotations();
    if (parameterAnnotations == null || parameterAnnotations.length == 0) {
        return new EmptySqlParameterSource();
    }/*from w w w  .  j a v  a 2  s.  co  m*/

    MapSqlParameterSource ps = new MapSqlParameterSource();
    Class<?>[] parameterTypes = m.getParameterTypes();
    for (int i = 0; i < args.length; i++) {
        String name = parameterName(parameterAnnotations[i]);
        if (name != null) {
            if (args[i] != null && ZonedDateTime.class.isAssignableFrom(parameterTypes[i])) {
                ZonedDateTime dateTime = ZonedDateTime.class.cast(args[i]);
                final ZonedDateTime utc = dateTime.withZoneSameInstant(ZoneId.of("UTC"));
                Calendar c = Calendar.getInstance();
                c.setTimeZone(TimeZone.getTimeZone("UTC"));
                c.setTimeInMillis(utc.toInstant().toEpochMilli());
                ps.addValue(name, c, Types.TIMESTAMP);
            } else {
                ps.addValue(name, args[i], StatementCreatorUtils.javaTypeToSqlParameterType(parameterTypes[i]));
            }
        }
    }

    return ps;
}

From source file:org.smigo.species.JdbcSpeciesDao.java

@Override
public int addSpecies(int userId) {
    MapSqlParameterSource s = new MapSqlParameterSource();
    s.addValue("creator", userId, Types.INTEGER);
    return insertSpecies.executeAndReturnKey(s).intValue();
}

From source file:com.github.ferstl.spring.jdbc.oracle.OracleNamedParameterJdbcTemplateTest.java

@Test
public void setWithType() throws SQLException {
    MapSqlParameterSource source = new MapSqlParameterSource(new HashMap<String, Object>(2));
    source.addValue("ten", 10, Types.NUMERIC);
    source.addValue("twenty", null, Types.VARCHAR);
    String sql = "SELECT 1 FROM dual WHERE 1 = :ten or 20 = :twenty";
    PreparedStatementCreator preparedStatementCreator = this.namedJdbcTemplate.getPreparedStatementCreator(sql,
            source);/*w ww  .java2  s  . c  o  m*/

    Connection connection = mock(Connection.class);
    PreparedStatement preparedStatement = mock(PreparedStatement.class);
    OraclePreparedStatement oracleStatement = mock(OraclePreparedStatement.class);

    when(connection.prepareStatement(sql)).thenReturn(preparedStatement);
    when(preparedStatement.unwrap(OraclePreparedStatement.class)).thenReturn(oracleStatement);

    preparedStatementCreator.createPreparedStatement(connection);

    verify(oracleStatement).setObjectAtName("ten", 10, Types.NUMERIC);
    verify(oracleStatement).setNullAtName("twenty", Types.VARCHAR);
}

From source file:org.owasp.proxy.http.dao.JdbcMessageDAO.java

private void addTimestamp(MapSqlParameterSource params, String name, long time) {
    params.addValue(name, time == 0 ? null : new Timestamp(time), Types.TIMESTAMP);
}

From source file:org.owasp.proxy.http.dao.JdbcMessageDAO.java

public boolean deleteConversation(int id) throws DataAccessException {
    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue(ID, id, Types.INTEGER);
    return getNamedParameterJdbcTemplate().update(DELETE_CONVERSATION, params) > 0;
}

From source file:org.owasp.proxy.http.dao.JdbcMessageDAO.java

public int saveMessageContent(byte[] messageContent) throws DataAccessException {
    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue(CONTENT, messageContent, Types.LONGVARBINARY);
    params.addValue(SIZE, messageContent.length, Types.INTEGER);
    KeyHolder key = new GeneratedKeyHolder();
    getNamedParameterJdbcTemplate().update(INSERT_CONTENT, params, key);
    return key.getKey().intValue();
}

From source file:org.owasp.proxy.http.dao.JdbcMessageDAO.java

public void saveResponseHeader(MutableResponseHeader responseHeader, int contentId) throws DataAccessException {
    saveMessageHeader(responseHeader, contentId);
    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue(ID, responseHeader.getId(), Types.INTEGER);
    addTimestamp(params, RESPONSE_HEADER_TIME, responseHeader.getHeaderTime());
    addTimestamp(params, RESPONSE_CONTENT_TIME, responseHeader.getContentTime());
    getNamedParameterJdbcTemplate().update(INSERT_RESPONSE, params);
}

From source file:org.owasp.proxy.http.dao.JdbcMessageDAO.java

private void saveMessageHeader(MutableMessageHeader header, int contentId) {
    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue(HEADER, header.getHeader(), Types.LONGVARBINARY);
    params.addValue(CONTENTID, contentId != -1 ? contentId : null, Types.INTEGER);
    KeyHolder key = new GeneratedKeyHolder();
    getNamedParameterJdbcTemplate().update(INSERT_HEADER, params, key);
    header.setId(key.getKey().intValue());
}

From source file:org.owasp.proxy.http.dao.JdbcMessageDAO.java

public int saveConversation(int requestId, int responseId) throws DataAccessException {
    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue(REQUESTID, requestId, Types.INTEGER);
    params.addValue(RESPONSEID, responseId, Types.INTEGER);

    KeyHolder key = new GeneratedKeyHolder();
    getNamedParameterJdbcTemplate().update(INSERT_CONVERSATION, params, key);
    return key.getKey().intValue();
}