Example usage for org.springframework.jdbc.core.namedparam SqlParameterSourceUtils getTypedValue

List of usage examples for org.springframework.jdbc.core.namedparam SqlParameterSourceUtils getTypedValue

Introduction

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

Prototype

@Nullable
public static Object getTypedValue(SqlParameterSource source, String parameterName) 

Source Link

Document

Create a wrapped value if parameter has type information, plain object if not.

Usage

From source file:paillard.florent.springframework.simplejdbcupdate.TableMetaDataContext.java

/**
 * Match the provided column names and values with the list of columns used.
 * /*from  w w  w. ja  v  a2s  .com*/
 * @param sqlParameterSource
 *            the parameter names and values
 * @param reconciledUpdatingColumns
 */
public List<Object> sortAndTypeInParameter(SqlParameterSource sqlParameterSource,
        List<String> reconciledUpdatingColumns) {
    List<Object> values = new ArrayList<Object>();
    // for parameter source lookups we need to provide caseinsensitive
    // lookup support since the
    // database metadata is not necessarily providing case sensitive column
    // names
    Map<?, ?> caseInsensitiveParameterNames = SqlParameterSourceUtils
            .extractCaseInsensitiveParameterNames(sqlParameterSource);
    for (String column : reconciledUpdatingColumns) {
        if (sqlParameterSource.hasValue(column)) {
            values.add(SqlParameterSourceUtils.getTypedValue(sqlParameterSource, column));
        } else {
            String lowerCaseName = column.toLowerCase();
            if (sqlParameterSource.hasValue(lowerCaseName)) {
                values.add(SqlParameterSourceUtils.getTypedValue(sqlParameterSource, lowerCaseName));
            } else {
                String propertyName = JdbcUtils.convertUnderscoreNameToPropertyName(column);
                if (sqlParameterSource.hasValue(propertyName)) {
                    values.add(SqlParameterSourceUtils.getTypedValue(sqlParameterSource, propertyName));
                } else {
                    if (caseInsensitiveParameterNames.containsKey(lowerCaseName)) {
                        values.add(SqlParameterSourceUtils.getTypedValue(sqlParameterSource,
                                (String) caseInsensitiveParameterNames.get(lowerCaseName)));
                    } else {
                        values.add(null);
                    }
                }
            }
        }
    }
    return values;
}

From source file:org.springframework.jdbc.core.metadata.CallMetaDataContext.java

/**
 * Match input parameter values with the parameters declared to be used in the call.
 * @param parameterSource the input values
 * @return a Map containing the matched parameter names with the value taken from the input
 *///from   ww w .  j av a 2 s  .c  om
public Map<String, Object> matchInParameterValuesWithCallParameters(SqlParameterSource parameterSource) {
    // For parameter source lookups we need to provide case-insensitive lookup support
    // since the database metadata is not necessarily providing case sensitive parameter names.
    Map<String, String> caseInsensitiveParameterNames = SqlParameterSourceUtils
            .extractCaseInsensitiveParameterNames(parameterSource);

    Map<String, String> callParameterNames = new HashMap<>(this.callParameters.size());
    Map<String, Object> matchedParameters = new HashMap<>(this.callParameters.size());
    for (SqlParameter parameter : this.callParameters) {
        if (parameter.isInputValueProvided()) {
            String parameterName = parameter.getName();
            String parameterNameToMatch = obtainMetaDataProvider().parameterNameToUse(parameterName);
            if (parameterNameToMatch != null) {
                callParameterNames.put(parameterNameToMatch.toLowerCase(), parameterName);
            }
            if (parameterName != null) {
                if (parameterSource.hasValue(parameterName)) {
                    matchedParameters.put(parameterName,
                            SqlParameterSourceUtils.getTypedValue(parameterSource, parameterName));
                } else {
                    String lowerCaseName = parameterName.toLowerCase();
                    if (parameterSource.hasValue(lowerCaseName)) {
                        matchedParameters.put(parameterName,
                                SqlParameterSourceUtils.getTypedValue(parameterSource, lowerCaseName));
                    } else {
                        String englishLowerCaseName = parameterName.toLowerCase(Locale.ENGLISH);
                        if (parameterSource.hasValue(englishLowerCaseName)) {
                            matchedParameters.put(parameterName, SqlParameterSourceUtils
                                    .getTypedValue(parameterSource, englishLowerCaseName));
                        } else {
                            String propertyName = JdbcUtils.convertUnderscoreNameToPropertyName(parameterName);
                            if (parameterSource.hasValue(propertyName)) {
                                matchedParameters.put(parameterName,
                                        SqlParameterSourceUtils.getTypedValue(parameterSource, propertyName));
                            } else {
                                if (caseInsensitiveParameterNames.containsKey(lowerCaseName)) {
                                    String sourceName = caseInsensitiveParameterNames.get(lowerCaseName);
                                    matchedParameters.put(parameterName,
                                            SqlParameterSourceUtils.getTypedValue(parameterSource, sourceName));
                                } else {
                                    logger.warn("Unable to locate the corresponding parameter value for '"
                                            + parameterName + "' within the parameter values provided: "
                                            + caseInsensitiveParameterNames.values());
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    if (logger.isDebugEnabled()) {
        logger.debug(
                "Matching " + caseInsensitiveParameterNames.values() + " with " + callParameterNames.values());
        logger.debug("Found match for " + matchedParameters.keySet());
    }
    return matchedParameters;
}

From source file:org.springframework.jdbc.core.metadata.TableMetaDataContext.java

/**
 * Match the provided column names and values with the list of columns used.
 * @param parameterSource the parameter names and values
 *//*from   w  ww  .java 2  s . co m*/
public List<Object> matchInParameterValuesWithInsertColumns(SqlParameterSource parameterSource) {
    List<Object> values = new ArrayList<>();
    // For parameter source lookups we need to provide case-insensitive lookup support since the
    // database metadata is not necessarily providing case-sensitive column names
    Map<String, String> caseInsensitiveParameterNames = SqlParameterSourceUtils
            .extractCaseInsensitiveParameterNames(parameterSource);
    for (String column : this.tableColumns) {
        if (parameterSource.hasValue(column)) {
            values.add(SqlParameterSourceUtils.getTypedValue(parameterSource, column));
        } else {
            String lowerCaseName = column.toLowerCase();
            if (parameterSource.hasValue(lowerCaseName)) {
                values.add(SqlParameterSourceUtils.getTypedValue(parameterSource, lowerCaseName));
            } else {
                String propertyName = JdbcUtils.convertUnderscoreNameToPropertyName(column);
                if (parameterSource.hasValue(propertyName)) {
                    values.add(SqlParameterSourceUtils.getTypedValue(parameterSource, propertyName));
                } else {
                    if (caseInsensitiveParameterNames.containsKey(lowerCaseName)) {
                        values.add(SqlParameterSourceUtils.getTypedValue(parameterSource,
                                caseInsensitiveParameterNames.get(lowerCaseName)));
                    } else {
                        values.add(null);
                    }
                }
            }
        }
    }
    return values;
}