001/* 002 * Copyright 2002-2018 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; 018 019import org.springframework.lang.Nullable; 020 021/** 022 * Object to represent an SQL parameter value, including parameter meta-data 023 * such as the SQL type and the scale for numeric values. 024 * 025 * <p>Designed for use with {@link JdbcTemplate}'s operations that take an array of 026 * argument values: Each such argument value may be a {@code SqlParameterValue}, 027 * indicating the SQL type (and optionally the scale) instead of letting the 028 * template guess a default type. Note that this only applies to the operations with 029 * a 'plain' argument array, not to the overloaded variants with an explicit type array. 030 * 031 * @author Juergen Hoeller 032 * @since 2.0.5 033 * @see java.sql.Types 034 * @see JdbcTemplate#query(String, Object[], ResultSetExtractor) 035 * @see JdbcTemplate#query(String, Object[], RowCallbackHandler) 036 * @see JdbcTemplate#query(String, Object[], RowMapper) 037 * @see JdbcTemplate#update(String, Object[]) 038 */ 039public class SqlParameterValue extends SqlParameter { 040 041 @Nullable 042 private final Object value; 043 044 045 /** 046 * Create a new SqlParameterValue, supplying the SQL type. 047 * @param sqlType the SQL type of the parameter according to {@code java.sql.Types} 048 * @param value the value object 049 */ 050 public SqlParameterValue(int sqlType, @Nullable Object value) { 051 super(sqlType); 052 this.value = value; 053 } 054 055 /** 056 * Create a new SqlParameterValue, supplying the SQL type. 057 * @param sqlType the SQL type of the parameter according to {@code java.sql.Types} 058 * @param typeName the type name of the parameter (optional) 059 * @param value the value object 060 */ 061 public SqlParameterValue(int sqlType, @Nullable String typeName, @Nullable Object value) { 062 super(sqlType, typeName); 063 this.value = value; 064 } 065 066 /** 067 * Create a new SqlParameterValue, supplying the SQL type. 068 * @param sqlType the SQL type of the parameter according to {@code java.sql.Types} 069 * @param scale the number of digits after the decimal point 070 * (for DECIMAL and NUMERIC types) 071 * @param value the value object 072 */ 073 public SqlParameterValue(int sqlType, int scale, @Nullable Object value) { 074 super(sqlType, scale); 075 this.value = value; 076 } 077 078 /** 079 * Create a new SqlParameterValue based on the given SqlParameter declaration. 080 * @param declaredParam the declared SqlParameter to define a value for 081 * @param value the value object 082 */ 083 public SqlParameterValue(SqlParameter declaredParam, @Nullable Object value) { 084 super(declaredParam); 085 this.value = value; 086 } 087 088 089 /** 090 * Return the value object that this parameter value holds. 091 */ 092 @Nullable 093 public Object getValue() { 094 return this.value; 095 } 096 097}