Search in sources :

Example 21 with SqlParameter

use of org.springframework.jdbc.core.SqlParameter in project spring-framework by spring-projects.

the class SqlQueryTests method testNamedParameterInListQuery.

@Test
public void testNamedParameterInListQuery() throws SQLException {
    given(resultSet.next()).willReturn(true, true, false);
    given(resultSet.getInt("id")).willReturn(1, 2);
    given(resultSet.getString("forename")).willReturn("rod", "juergen");
    given(connection.prepareStatement(SELECT_ID_FORENAME_WHERE_ID_IN_LIST_1, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY)).willReturn(preparedStatement);
    class CustomerQuery extends MappingSqlQuery<Customer> {

        public CustomerQuery(DataSource ds) {
            super(ds, SELECT_ID_FORENAME_WHERE_ID_IN_LIST_2);
            setResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
            declareParameter(new SqlParameter("ids", Types.NUMERIC));
            compile();
        }

        @Override
        protected Customer mapRow(ResultSet rs, int rownum) throws SQLException {
            Customer cust = new Customer();
            cust.setId(rs.getInt(COLUMN_NAMES[0]));
            cust.setForename(rs.getString(COLUMN_NAMES[1]));
            return cust;
        }

        public List<Customer> findCustomers(List<Integer> ids) {
            Map<String, Object> params = new HashMap<>();
            params.put("ids", ids);
            return executeByNamedParam(params);
        }
    }
    CustomerQuery query = new CustomerQuery(dataSource);
    List<Integer> ids = new ArrayList<>();
    ids.add(1);
    ids.add(2);
    List<Customer> cust = query.findCustomers(ids);
    assertEquals("We got two customers back", cust.size(), 2);
    assertEquals("First customer id was assigned correctly", cust.get(0).getId(), 1);
    assertEquals("First customer forename was assigned correctly", cust.get(0).getForename(), "rod");
    assertEquals("Second customer id was assigned correctly", cust.get(1).getId(), 2);
    assertEquals("Second customer forename was assigned correctly", cust.get(1).getForename(), "juergen");
    verify(preparedStatement).setObject(1, 1, Types.NUMERIC);
    verify(preparedStatement).setObject(2, 2, Types.NUMERIC);
    verify(resultSet).close();
    verify(preparedStatement).close();
    verify(connection).close();
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) Customer(org.springframework.jdbc.Customer) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DataSource(javax.sql.DataSource) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 22 with SqlParameter

use of org.springframework.jdbc.core.SqlParameter in project spring-framework by spring-projects.

the class SqlQueryTests method testQueryWithoutEnoughParams.

@Test
public void testQueryWithoutEnoughParams() {
    MappingSqlQuery<Integer> query = new MappingSqlQuery<Integer>() {

        @Override
        protected Integer mapRow(ResultSet rs, int rownum) throws SQLException {
            return rs.getInt(1);
        }
    };
    query.setDataSource(dataSource);
    query.setSql(SELECT_ID_WHERE);
    query.declareParameter(new SqlParameter(COLUMN_NAMES[0], COLUMN_TYPES[0]));
    query.declareParameter(new SqlParameter(COLUMN_NAMES[1], COLUMN_TYPES[1]));
    query.compile();
    thrown.expect(InvalidDataAccessApiUsageException.class);
    query.execute();
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 23 with SqlParameter

use of org.springframework.jdbc.core.SqlParameter in project camel by apache.

the class BatchCallableStatementCreatorFactory method createParams.

private List<SqlParameter> createParams() {
    List<SqlParameter> params = new ArrayList<>();
    for (Object parameter : template.getParameterList()) {
        if (parameter instanceof InputParameter) {
            InputParameter inputParameter = (InputParameter) parameter;
            params.add(new SqlParameter(inputParameter.getName(), inputParameter.getSqlType()));
        } else {
            throw new UnsupportedOperationException("Only IN parameters supported by batch!");
        }
    }
    return params;
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) ArrayList(java.util.ArrayList) InputParameter(org.apache.camel.component.sql.stored.template.ast.InputParameter)

Example 24 with SqlParameter

use of org.springframework.jdbc.core.SqlParameter in project camel by apache.

the class BatchCallableStatementCreatorFactory method addParameter.

public void addParameter(CallableStatement callableStatement, Map batchRow) throws SQLException {
    int i = 1;
    for (SqlParameter parameter : getSqlParameterList()) {
        StatementCreatorUtils.setParameterValue(callableStatement, i, parameter, batchRow.get(parameter.getName()));
        i++;
    }
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter)

Example 25 with SqlParameter

use of org.springframework.jdbc.core.SqlParameter in project musiccabinet by hakko.

the class JdbcLastFmDao method setLastFmGroups.

@Override
public void setLastFmGroups(List<LastFmGroup> lastFmGroups) {
    jdbcTemplate.update("truncate music.lastfmgroup_import");
    String sql = "insert into music.lastfmgroup_import (group_name) values (?)";
    BatchSqlUpdate batchUpdate = new BatchSqlUpdate(jdbcTemplate.getDataSource(), sql);
    batchUpdate.setBatchSize(1000);
    batchUpdate.declareParameter(new SqlParameter("group_name", Types.VARCHAR));
    for (LastFmGroup group : lastFmGroups) {
        batchUpdate.update(new Object[] { group.getName() });
    }
    batchUpdate.flush();
    jdbcTemplate.execute("select music.update_lastfmgroup()");
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) LastFmGroup(com.github.hakko.musiccabinet.domain.model.library.LastFmGroup) BatchSqlUpdate(org.springframework.jdbc.object.BatchSqlUpdate)

Aggregations

SqlParameter (org.springframework.jdbc.core.SqlParameter)66 BatchSqlUpdate (org.springframework.jdbc.object.BatchSqlUpdate)23 Test (org.junit.Test)22 ResultSet (java.sql.ResultSet)15 DataSource (javax.sql.DataSource)15 HashMap (java.util.HashMap)12 Customer (org.springframework.jdbc.Customer)12 SqlOutParameter (org.springframework.jdbc.core.SqlOutParameter)6 LinkedHashMap (java.util.LinkedHashMap)5 ArrayList (java.util.ArrayList)4 InvalidDataAccessApiUsageException (org.springframework.dao.InvalidDataAccessApiUsageException)4 File (com.github.hakko.musiccabinet.domain.model.library.File)3 PreparedStatementCreatorFactory (org.springframework.jdbc.core.PreparedStatementCreatorFactory)3 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)3 Track (com.github.hakko.musiccabinet.domain.model.music.Track)2 PreparedStatement (java.sql.PreparedStatement)2 Map (java.util.Map)2 CallableStatementCreatorFactory (org.springframework.jdbc.core.CallableStatementCreatorFactory)2 SqlParameterValue (org.springframework.jdbc.core.SqlParameterValue)2 DriverManagerDataSource (org.springframework.jdbc.datasource.DriverManagerDataSource)2