Search in sources :

Example 6 with SqlParameterValue

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

the class CallMetaDataContext method matchInParameterValuesWithCallParameters.

/**
	 * Match input parameter values with the parameters declared to be used in the call.
	 * @param inParameters the input values
	 * @return a Map containing the matched parameter names with the value taken from the input
	 */
public Map<String, ?> matchInParameterValuesWithCallParameters(Map<String, ?> inParameters) {
    if (!this.metaDataProvider.isProcedureColumnMetaDataUsed()) {
        return inParameters;
    }
    Map<String, String> callParameterNames = new HashMap<>(this.callParameters.size());
    for (SqlParameter parameter : this.callParameters) {
        if (parameter.isInputValueProvided()) {
            String parameterName = parameter.getName();
            String parameterNameToMatch = this.metaDataProvider.parameterNameToUse(parameterName);
            if (parameterNameToMatch != null) {
                callParameterNames.put(parameterNameToMatch.toLowerCase(), parameterName);
            }
        }
    }
    Map<String, Object> matchedParameters = new HashMap<>(inParameters.size());
    for (String parameterName : inParameters.keySet()) {
        String parameterNameToMatch = this.metaDataProvider.parameterNameToUse(parameterName);
        String callParameterName = callParameterNames.get(parameterNameToMatch.toLowerCase());
        if (callParameterName == null) {
            if (logger.isDebugEnabled()) {
                Object value = inParameters.get(parameterName);
                if (value instanceof SqlParameterValue) {
                    value = ((SqlParameterValue) value).getValue();
                }
                if (value != null) {
                    logger.debug("Unable to locate the corresponding IN or IN-OUT parameter for \"" + parameterName + "\" in the parameters used: " + callParameterNames.keySet());
                }
            }
        } else {
            matchedParameters.put(callParameterName, inParameters.get(parameterName));
        }
    }
    if (matchedParameters.size() < callParameterNames.size()) {
        for (String parameterName : callParameterNames.keySet()) {
            String parameterNameToMatch = this.metaDataProvider.parameterNameToUse(parameterName);
            String callParameterName = callParameterNames.get(parameterNameToMatch.toLowerCase());
            if (!matchedParameters.containsKey(callParameterName)) {
                logger.warn("Unable to locate the corresponding parameter value for '" + parameterName + "' within the parameter values provided: " + inParameters.keySet());
            }
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Matching " + inParameters.keySet() + " with " + callParameterNames.values());
        logger.debug("Found match for " + matchedParameters.keySet());
    }
    return matchedParameters;
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue)

Example 7 with SqlParameterValue

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

the class MapSqlParameterSource method addValues.

/**
	 * Add a Map of parameters to this parameter source.
	 * @param values a Map holding existing parameter values (can be {@code null})
	 * @return a reference to this parameter source,
	 * so it's possible to chain several calls together
	 */
public MapSqlParameterSource addValues(Map<String, ?> values) {
    if (values != null) {
        for (Map.Entry<String, ?> entry : values.entrySet()) {
            this.values.put(entry.getKey(), entry.getValue());
            if (entry.getValue() instanceof SqlParameterValue) {
                SqlParameterValue value = (SqlParameterValue) entry.getValue();
                registerSqlType(entry.getKey(), value.getSqlType());
            }
        }
    }
    return this;
}
Also used : SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 8 with SqlParameterValue

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

the class MapSqlParameterSourceTests method sqlParameterValueRegistersSqlType.

@Test
public void sqlParameterValueRegistersSqlType() throws Exception {
    MapSqlParameterSource msps = new MapSqlParameterSource("FOO", new SqlParameterValue(2, "Foo"));
    assertEquals("Correct SQL Type not registered", 2, msps.getSqlType("FOO"));
    MapSqlParameterSource msps2 = new MapSqlParameterSource();
    msps2.addValues(msps.getValues());
    assertEquals("Correct SQL Type not registered", 2, msps2.getSqlType("FOO"));
}
Also used : SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) Test(org.junit.Test)

Example 9 with SqlParameterValue

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

the class NamedParameterJdbcTemplateTests method testQueryWithResultSetExtractor.

@Test
public void testQueryWithResultSetExtractor() throws SQLException {
    given(resultSet.next()).willReturn(true);
    given(resultSet.getInt("id")).willReturn(1);
    given(resultSet.getString("forename")).willReturn("rod");
    params.put("id", new SqlParameterValue(Types.DECIMAL, 1));
    params.put("country", "UK");
    Customer cust = namedParameterTemplate.query(SELECT_NAMED_PARAMETERS, params, new ResultSetExtractor<Customer>() {

        @Override
        public Customer extractData(ResultSet rs) throws SQLException, DataAccessException {
            rs.next();
            Customer cust = new Customer();
            cust.setId(rs.getInt(COLUMN_NAMES[0]));
            cust.setForename(rs.getString(COLUMN_NAMES[1]));
            return cust;
        }
    });
    assertTrue("Customer id was assigned correctly", cust.getId() == 1);
    assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
    verify(connection).prepareStatement(SELECT_NAMED_PARAMETERS_PARSED);
    verify(preparedStatement).setObject(1, 1, Types.DECIMAL);
    verify(preparedStatement).setString(2, "UK");
    verify(preparedStatement).close();
    verify(connection).close();
}
Also used : SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) Customer(org.springframework.jdbc.Customer) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.Test)

Example 10 with SqlParameterValue

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

the class NamedParameterJdbcTemplateTests method testQueryWithRowCallbackHandler.

@Test
public void testQueryWithRowCallbackHandler() throws SQLException {
    given(resultSet.next()).willReturn(true, false);
    given(resultSet.getInt("id")).willReturn(1);
    given(resultSet.getString("forename")).willReturn("rod");
    params.put("id", new SqlParameterValue(Types.DECIMAL, 1));
    params.put("country", "UK");
    final List<Customer> customers = new LinkedList<>();
    namedParameterTemplate.query(SELECT_NAMED_PARAMETERS, params, new RowCallbackHandler() {

        @Override
        public void processRow(ResultSet rs) throws SQLException {
            Customer cust = new Customer();
            cust.setId(rs.getInt(COLUMN_NAMES[0]));
            cust.setForename(rs.getString(COLUMN_NAMES[1]));
            customers.add(cust);
        }
    });
    assertEquals(1, customers.size());
    assertTrue("Customer id was assigned correctly", customers.get(0).getId() == 1);
    assertTrue("Customer forename was assigned correctly", customers.get(0).getForename().equals("rod"));
    verify(connection).prepareStatement(SELECT_NAMED_PARAMETERS_PARSED);
    verify(preparedStatement).setObject(1, 1, Types.DECIMAL);
    verify(preparedStatement).setString(2, "UK");
    verify(preparedStatement).close();
    verify(connection).close();
}
Also used : SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) Customer(org.springframework.jdbc.Customer) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) RowCallbackHandler(org.springframework.jdbc.core.RowCallbackHandler) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

SqlParameterValue (org.springframework.jdbc.core.SqlParameterValue)11 Test (org.junit.Test)8 ResultSet (java.sql.ResultSet)5 SQLException (java.sql.SQLException)5 Customer (org.springframework.jdbc.Customer)4 LinkedHashMap (java.util.LinkedHashMap)2 SqlParameter (org.springframework.jdbc.core.SqlParameter)2 PreparedStatement (java.sql.PreparedStatement)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 DataAccessException (org.springframework.dao.DataAccessException)1 InvalidDataAccessApiUsageException (org.springframework.dao.InvalidDataAccessApiUsageException)1 RowCallbackHandler (org.springframework.jdbc.core.RowCallbackHandler)1 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)1