View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.jdbc.core;
18  
19  import java.util.LinkedList;
20  import java.util.List;
21  
22  import org.springframework.util.Assert;
23  
24  /**
25   * Object to represent a SQL parameter definition.
26   *
27   * <p>Parameters may be anonymous, in which case "name" is {@code null}.
28   * However, all parameters must define a SQL type according to {@link java.sql.Types}.
29   *
30   * @author Rod Johnson
31   * @author Thomas Risberg
32   * @author Juergen Hoeller
33   * @see java.sql.Types
34   */
35  public class SqlParameter {
36  
37  	/** The name of the parameter, if any */
38  	private String name;
39  
40  	/** SQL type constant from {@code java.sql.Types} */
41  	private final int sqlType;
42  
43  	/** Used for types that are user-named like: STRUCT, DISTINCT, JAVA_OBJECT, named array types */
44  	private String typeName;
45  
46  
47  	/** The scale to apply in case of a NUMERIC or DECIMAL type, if any */
48  	private Integer scale;
49  
50  
51  	/**
52  	 * Create a new anonymous SqlParameter, supplying the SQL type.
53  	 * @param sqlType SQL type of the parameter according to {@code java.sql.Types}
54  	 */
55  	public SqlParameter(int sqlType) {
56  		this.sqlType = sqlType;
57  	}
58  
59  	/**
60  	 * Create a new anonymous SqlParameter, supplying the SQL type.
61  	 * @param sqlType SQL type of the parameter according to {@code java.sql.Types}
62  	 * @param typeName the type name of the parameter (optional)
63  	 */
64  	public SqlParameter(int sqlType, String typeName) {
65  		this.sqlType = sqlType;
66  		this.typeName = typeName;
67  	}
68  
69  	/**
70  	 * Create a new anonymous SqlParameter, supplying the SQL type.
71  	 * @param sqlType SQL type of the parameter according to {@code java.sql.Types}
72  	 * @param scale the number of digits after the decimal point
73  	 * (for DECIMAL and NUMERIC types)
74  	 */
75  	public SqlParameter(int sqlType, int scale) {
76  		this.sqlType = sqlType;
77  		this.scale = scale;
78  	}
79  
80  	/**
81  	 * Create a new SqlParameter, supplying name and SQL type.
82  	 * @param name name of the parameter, as used in input and output maps
83  	 * @param sqlType SQL type of the parameter according to {@code java.sql.Types}
84  	 */
85  	public SqlParameter(String name, int sqlType) {
86  		this.name = name;
87  		this.sqlType = sqlType;
88  	}
89  
90  	/**
91  	 * Create a new SqlParameter, supplying name and SQL type.
92  	 * @param name name of the parameter, as used in input and output maps
93  	 * @param sqlType SQL type of the parameter according to {@code java.sql.Types}
94  	 * @param typeName the type name of the parameter (optional)
95  	 */
96  	public SqlParameter(String name, int sqlType, String typeName) {
97  		this.name = name;
98  		this.sqlType = sqlType;
99  		this.typeName = typeName;
100 	}
101 
102 	/**
103 	 * Create a new SqlParameter, supplying name and SQL type.
104 	 * @param name name of the parameter, as used in input and output maps
105 	 * @param sqlType SQL type of the parameter according to {@code java.sql.Types}
106 	 * @param scale the number of digits after the decimal point
107 	 * (for DECIMAL and NUMERIC types)
108 	 */
109 	public SqlParameter(String name, int sqlType, int scale) {
110 		this.name = name;
111 		this.sqlType = sqlType;
112 		this.scale = scale;
113 	}
114 
115 	/**
116 	 * Copy constructor.
117 	 * @param otherParam the SqlParameter object to copy from
118 	 */
119 	public SqlParameter(SqlParameter otherParam) {
120 		Assert.notNull(otherParam, "SqlParameter object must not be null");
121 		this.name = otherParam.name;
122 		this.sqlType = otherParam.sqlType;
123 		this.typeName = otherParam.typeName;
124 		this.scale = otherParam.scale;
125 	}
126 
127 
128 	/**
129 	 * Return the name of the parameter.
130 	 */
131 	public String getName() {
132 		return this.name;
133 	}
134 
135 	/**
136 	 * Return the SQL type of the parameter.
137 	 */
138 	public int getSqlType() {
139 		return this.sqlType;
140 	}
141 
142 	/**
143 	 * Return the type name of the parameter, if any.
144 	 */
145 	public String getTypeName() {
146 		return this.typeName;
147 	}
148 
149 	/**
150 	 * Return the scale of the parameter, if any.
151 	 */
152 	public Integer getScale() {
153 		return this.scale;
154 	}
155 
156 
157 	/**
158 	 * Return whether this parameter holds input values that should be set
159 	 * before execution even if they are {@code null}.
160 	 * <p>This implementation always returns {@code true}.
161 	 */
162 	public boolean isInputValueProvided() {
163 		return true;
164 	}
165 
166 	/**
167 	 * Return whether this parameter is an implicit return parameter used during the
168 	 * results preocessing of the CallableStatement.getMoreResults/getUpdateCount.
169 	 * <p>This implementation always returns {@code false}.
170 	 */
171 	public boolean isResultsParameter() {
172 		return false;
173 	}
174 
175 
176 	/**
177 	 * Convert a list of JDBC types, as defined in {@code java.sql.Types},
178 	 * to a List of SqlParameter objects as used in this package.
179 	 */
180 	public static List<SqlParameter> sqlTypesToAnonymousParameterList(int... types) {
181 		List<SqlParameter> result = new LinkedList<SqlParameter>();
182 		if (types != null) {
183 			for (int type : types) {
184 				result.add(new SqlParameter(type));
185 			}
186 		}
187 		return result;
188 	}
189 
190 }