001: /*
002: * Copyright 2002-2007 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: * http://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:
017: package org.springframework.jdbc.core;
018:
019: import java.util.LinkedList;
020: import java.util.List;
021:
022: import org.springframework.util.Assert;
023:
024: /**
025: * Object to represent a SQL parameter definition.
026: *
027: * <p>Parameters may be anonymous, in which case "name" is <code>null</code>.
028: * However, all parameters must define a SQL type according to {@link java.sql.Types}.
029: *
030: * @author Rod Johnson
031: * @author Thomas Risberg
032: * @author Juergen Hoeller
033: * @see java.sql.Types
034: */
035: public class SqlParameter {
036:
037: /** The name of the parameter, if any */
038: private String name;
039:
040: /** SQL type constant from <code>java.sql.Types</code> */
041: private final int sqlType;
042:
043: /** Used for types that are user-named like: STRUCT, DISTINCT, JAVA_OBJECT, named array types */
044: private String typeName;
045:
046: /** The scale to apply in case of a NUMERIC or DECIMAL type, if any */
047: private Integer scale;
048:
049: /**
050: * Create a new anonymous SqlParameter, supplying the SQL type.
051: * @param sqlType SQL type of the parameter according to <code>java.sql.Types</code>
052: */
053: public SqlParameter(int sqlType) {
054: this .sqlType = sqlType;
055: }
056:
057: /**
058: * Create a new anonymous SqlParameter, supplying the SQL type.
059: * @param sqlType SQL type of the parameter according to <code>java.sql.Types</code>
060: * @param typeName the type name of the parameter (optional)
061: */
062: public SqlParameter(int sqlType, String typeName) {
063: this .sqlType = sqlType;
064: this .typeName = typeName;
065: }
066:
067: /**
068: * Create a new anonymous SqlParameter, supplying the SQL type.
069: * @param sqlType SQL type of the parameter according to <code>java.sql.Types</code>
070: * @param scale the number of digits after the decimal point
071: * (for DECIMAL and NUMERIC types)
072: */
073: public SqlParameter(int sqlType, int scale) {
074: this .sqlType = sqlType;
075: this .scale = new Integer(scale);
076: }
077:
078: /**
079: * Create a new SqlParameter, supplying name and SQL type.
080: * @param name name of the parameter, as used in input and output maps
081: * @param sqlType SQL type of the parameter according to <code>java.sql.Types</code>
082: */
083: public SqlParameter(String name, int sqlType) {
084: this .name = name;
085: this .sqlType = sqlType;
086: }
087:
088: /**
089: * Create a new SqlParameter, supplying name and SQL type.
090: * @param name name of the parameter, as used in input and output maps
091: * @param sqlType SQL type of the parameter according to <code>java.sql.Types</code>
092: * @param typeName the type name of the parameter (optional)
093: */
094: public SqlParameter(String name, int sqlType, String typeName) {
095: this .name = name;
096: this .sqlType = sqlType;
097: this .typeName = typeName;
098: }
099:
100: /**
101: * Create a new SqlParameter, supplying name and SQL type.
102: * @param name name of the parameter, as used in input and output maps
103: * @param sqlType SQL type of the parameter according to <code>java.sql.Types</code>
104: * @param scale the number of digits after the decimal point
105: * (for DECIMAL and NUMERIC types)
106: */
107: public SqlParameter(String name, int sqlType, int scale) {
108: this .name = name;
109: this .sqlType = sqlType;
110: this .scale = new Integer(scale);
111: }
112:
113: /**
114: * Copy constructor.
115: * @param otherParam the SqlParameter object to copy from
116: */
117: public SqlParameter(SqlParameter otherParam) {
118: Assert.notNull(otherParam,
119: "SqlParameter object must not be null");
120: this .name = otherParam.name;
121: this .sqlType = otherParam.sqlType;
122: this .typeName = otherParam.typeName;
123: this .scale = otherParam.scale;
124: }
125:
126: /**
127: * Return the name of the parameter.
128: */
129: public String getName() {
130: return this .name;
131: }
132:
133: /**
134: * Return the SQL type of the parameter.
135: */
136: public int getSqlType() {
137: return this .sqlType;
138: }
139:
140: /**
141: * Return the type name of the parameter, if any.
142: */
143: public String getTypeName() {
144: return this .typeName;
145: }
146:
147: /**
148: * Return the scale of the parameter, if any.
149: */
150: public Integer getScale() {
151: return this .scale;
152: }
153:
154: /**
155: * Return whether this parameter holds input values that should be set
156: * before execution even if they are <code>null</code>.
157: * <p>This implementation always returns <code>true</code>.
158: */
159: public boolean isInputValueProvided() {
160: return true;
161: }
162:
163: /**
164: * Return whether this parameter is an implicit return parameter used during the
165: * results preocessing of the CallableStatement.getMoreResults/getUpdateCount.
166: * <p>This implementation always returns <code>false</code>.
167: */
168: public boolean isResultsParameter() {
169: return false;
170: }
171:
172: /**
173: * Convert a list of JDBC types, as defined in <code>java.sql.Types</code>,
174: * to a List of SqlParameter objects as used in this package.
175: */
176: public static List sqlTypesToAnonymousParameterList(int[] types) {
177: List result = new LinkedList();
178: if (types != null) {
179: for (int i = 0; i < types.length; i++) {
180: result.add(new SqlParameter(types[i]));
181: }
182: }
183: return result;
184: }
185:
186: }
|