001/* 002 * Copyright 2002-2020 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.jdbc.core.namedparam; 018 019import java.util.HashMap; 020import java.util.Map; 021 022import org.springframework.jdbc.core.SqlParameterValue; 023 024/** 025 * Class that provides helper methods for the use of {@link SqlParameterSource}, 026 * in particular with {@link NamedParameterJdbcTemplate}. 027 * 028 * @author Thomas Risberg 029 * @author Juergen Hoeller 030 * @since 2.5 031 */ 032public class SqlParameterSourceUtils { 033 034 /** 035 * Create an array of {@link MapSqlParameterSource} objects populated with data from 036 * the values passed in. This will define what is included in a batch operation. 037 * @param valueMaps array of {@link Map} instances containing the values to be used 038 * @return an array of {@link SqlParameterSource} 039 * @see MapSqlParameterSource 040 * @see NamedParameterJdbcTemplate#batchUpdate(String, Map[]) 041 */ 042 public static SqlParameterSource[] createBatch(Map<String, ?>[] valueMaps) { 043 MapSqlParameterSource[] batch = new MapSqlParameterSource[valueMaps.length]; 044 for (int i = 0; i < valueMaps.length; i++) { 045 batch[i] = new MapSqlParameterSource(valueMaps[i]); 046 } 047 return batch; 048 } 049 050 /** 051 * Create an array of {@link BeanPropertySqlParameterSource} objects populated with data 052 * from the values passed in. This will define what is included in a batch operation. 053 * @param beans object array of beans containing the values to be used 054 * @return an array of {@link SqlParameterSource} 055 * @see BeanPropertySqlParameterSource 056 * @see NamedParameterJdbcTemplate#batchUpdate(String, SqlParameterSource[]) 057 */ 058 public static SqlParameterSource[] createBatch(Object[] beans) { 059 BeanPropertySqlParameterSource[] batch = new BeanPropertySqlParameterSource[beans.length]; 060 for (int i = 0; i < beans.length; i++) { 061 batch[i] = new BeanPropertySqlParameterSource(beans[i]); 062 } 063 return batch; 064 } 065 066 /** 067 * Create a wrapped value if parameter has type information, plain object if not. 068 * @param source the source of parameter values and type information 069 * @param parameterName the name of the parameter 070 * @return the value object 071 * @see SqlParameterValue 072 */ 073 public static Object getTypedValue(SqlParameterSource source, String parameterName) { 074 int sqlType = source.getSqlType(parameterName); 075 if (sqlType != SqlParameterSource.TYPE_UNKNOWN) { 076 return new SqlParameterValue(sqlType, source.getTypeName(parameterName), source.getValue(parameterName)); 077 } 078 else { 079 return source.getValue(parameterName); 080 } 081 } 082 083 /** 084 * Create a Map of case insensitive parameter names together with the original name. 085 * @param parameterSource the source of parameter names 086 * @return the Map that can be used for case insensitive matching of parameter names 087 */ 088 public static Map<String, String> extractCaseInsensitiveParameterNames(SqlParameterSource parameterSource) { 089 Map<String, String> caseInsensitiveParameterNames = new HashMap<String, String>(); 090 if (parameterSource instanceof BeanPropertySqlParameterSource) { 091 String[] propertyNames = ((BeanPropertySqlParameterSource) parameterSource).getReadablePropertyNames(); 092 for (String name : propertyNames) { 093 caseInsensitiveParameterNames.put(name.toLowerCase(), name); 094 } 095 } 096 else if (parameterSource instanceof MapSqlParameterSource) { 097 for (String name : ((MapSqlParameterSource) parameterSource).getValues().keySet()) { 098 caseInsensitiveParameterNames.put(name.toLowerCase(), name); 099 } 100 } 101 return caseInsensitiveParameterNames; 102 } 103 104}