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.util.Assert; 024 025/** 026 * Object to represent a SQL parameter definition. 027 * 028 * <p>Parameters may be anonymous, in which case "name" is {@code null}. 029 * However, all parameters must define a SQL type according to {@link java.sql.Types}. 030 * 031 * @author Rod Johnson 032 * @author Thomas Risberg 033 * @author Juergen Hoeller 034 * @see java.sql.Types 035 */ 036public class SqlParameter { 037 038 // The name of the parameter, if any 039 private String name; 040 041 // SQL type constant from {@code java.sql.Types} 042 private final int sqlType; 043 044 // Used for types that are user-named like: STRUCT, DISTINCT, JAVA_OBJECT, named array types 045 private String typeName; 046 047 // The scale to apply in case of a NUMERIC or DECIMAL type, if any 048 private Integer scale; 049 050 051 /** 052 * Create a new anonymous SqlParameter, supplying the SQL type. 053 * @param sqlType SQL type of the parameter according to {@code java.sql.Types} 054 */ 055 public SqlParameter(int sqlType) { 056 this.sqlType = sqlType; 057 } 058 059 /** 060 * Create a new anonymous SqlParameter, supplying the SQL type. 061 * @param sqlType SQL type of the parameter according to {@code java.sql.Types} 062 * @param typeName the type name of the parameter (optional) 063 */ 064 public SqlParameter(int sqlType, String typeName) { 065 this.sqlType = sqlType; 066 this.typeName = typeName; 067 } 068 069 /** 070 * Create a new anonymous SqlParameter, supplying the SQL type. 071 * @param sqlType SQL type of the parameter according to {@code java.sql.Types} 072 * @param scale the number of digits after the decimal point 073 * (for DECIMAL and NUMERIC types) 074 */ 075 public SqlParameter(int sqlType, int scale) { 076 this.sqlType = sqlType; 077 this.scale = scale; 078 } 079 080 /** 081 * Create a new SqlParameter, supplying name and SQL type. 082 * @param name name of the parameter, as used in input and output maps 083 * @param sqlType SQL type of the parameter according to {@code java.sql.Types} 084 */ 085 public SqlParameter(String name, int sqlType) { 086 this.name = name; 087 this.sqlType = sqlType; 088 } 089 090 /** 091 * Create a new SqlParameter, supplying name and SQL type. 092 * @param name name of the parameter, as used in input and output maps 093 * @param sqlType SQL type of the parameter according to {@code java.sql.Types} 094 * @param typeName the type name of the parameter (optional) 095 */ 096 public SqlParameter(String name, int sqlType, String typeName) { 097 this.name = name; 098 this.sqlType = sqlType; 099 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, or {@code null} if anonymous. 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 processing of {@code 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; 182 if (types != null) { 183 result = new ArrayList<SqlParameter>(types.length); 184 for (int type : types) { 185 result.add(new SqlParameter(type)); 186 } 187 } 188 else { 189 result = new LinkedList<SqlParameter>(); 190 } 191 return result; 192 } 193 194}