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