1 21 22 package org.apache.derby.impl.sql; 23 24 import org.apache.derby.iapi.services.loader.ClassInspector; 25 26 import org.apache.derby.iapi.sql.ParameterValueSet; 27 28 import org.apache.derby.iapi.types.DataValueDescriptor; 29 import org.apache.derby.iapi.types.BooleanDataValue; 30 import org.apache.derby.iapi.types.BitDataValue; 31 import org.apache.derby.iapi.types.DateTimeDataValue; 32 import org.apache.derby.iapi.types.NumberDataValue; 33 import org.apache.derby.iapi.types.StringDataValue; 34 import org.apache.derby.iapi.types.UserDataValue; 35 import org.apache.derby.iapi.types.TypeId; 36 import org.apache.derby.iapi.types.DataTypeDescriptor; 37 import org.apache.derby.iapi.types.*; 38 39 import org.apache.derby.iapi.reference.SQLState; 40 41 import org.apache.derby.iapi.reference.JDBC30Translation; 42 43 import org.apache.derby.iapi.error.StandardException; 44 45 import org.apache.derby.iapi.services.sanity.SanityManager; 46 47 import org.apache.derby.iapi.types.*; 48 import org.apache.derby.iapi.types.*; 49 50 import java.sql.Types ; 51 52 import java.lang.reflect.Array ; 53 54 59 final class GenericParameter 60 { 61 62 private static int DECIMAL_PARAMETER_DEFAULT_PRECISION = 31; 66 private static int DECIMAL_PARAMETER_DEFAULT_SCALE = 15; 67 68 69 72 private final GenericParameterValueSet pvs; 73 74 77 private DataValueDescriptor value; 78 79 82 int jdbcTypeId; 83 84 87 String declaredClassName; 88 89 92 short parameterMode; 93 94 97 boolean isSet; 98 99 102 private final boolean isReturnOutputParameter; 103 104 107 int registerOutType = Types.NULL; 108 111 int registerOutScale = -1; 112 113 117 118 int registerOutPrecision = -1; 119 120 126 GenericParameter 127 ( 128 GenericParameterValueSet pvs, 129 boolean isReturnOutputParameter 130 ) 131 { 132 this.pvs = pvs; 133 parameterMode = (this.isReturnOutputParameter = isReturnOutputParameter) 134 ? (short) JDBC30Translation.PARAMETER_MODE_OUT : (short) JDBC30Translation.PARAMETER_MODE_IN; 135 } 136 137 146 public GenericParameter getClone(GenericParameterValueSet pvs) 147 { 148 GenericParameter gpClone = new GenericParameter(pvs, isReturnOutputParameter); 149 gpClone.initialize(this.getValue().getClone(), jdbcTypeId, declaredClassName); 150 gpClone.isSet = true; 151 152 return gpClone; 153 } 154 155 159 void initialize(DataValueDescriptor value, int jdbcTypeId, String className) 160 { 161 this.value = value; 162 this.jdbcTypeId = jdbcTypeId; 163 this.declaredClassName = className; 164 } 165 166 167 171 void clear() 172 { 173 isSet = false; 174 } 175 176 177 183 DataValueDescriptor getValue() 184 { 185 return value; 186 } 187 188 189 195 203 void setOutParameter(int sqlType, int scale) 204 throws StandardException 205 { 206 if (registerOutType == sqlType) { 208 if (scale == registerOutScale) 209 return; 210 } 211 212 switch (parameterMode) { 213 case JDBC30Translation.PARAMETER_MODE_IN: 214 case JDBC30Translation.PARAMETER_MODE_UNKNOWN: 215 default: 216 throw StandardException.newException(SQLState.LANG_NOT_OUT_PARAM, getJDBCParameterNumberStr()); 217 218 case JDBC30Translation.PARAMETER_MODE_IN_OUT: 219 case JDBC30Translation.PARAMETER_MODE_OUT: 220 if (!DataTypeDescriptor.isJDBCTypeEquivalent(jdbcTypeId, sqlType)) 222 throw throwInvalidOutParamMap(sqlType); 223 break; 224 225 } 226 227 registerOutType = sqlType; 228 229 } 230 231 private StandardException throwInvalidOutParamMap(int sqlType) { 232 233 236 237 String jdbcTypesName = org.apache.derby.impl.jdbc.Util.typeName(sqlType); 238 239 TypeId typeId = TypeId.getBuiltInTypeId(jdbcTypeId); 240 String thisTypeName = typeId == null ? declaredClassName : typeId.getSQLTypeName(); 241 242 StandardException e = StandardException.newException(SQLState.LANG_INVALID_OUT_PARAM_MAP, 243 getJDBCParameterNumberStr(), 244 jdbcTypesName, thisTypeName); 245 246 return e; 247 } 248 249 250 251 263 void validate() throws StandardException 264 { 265 switch (parameterMode) { 266 case JDBC30Translation.PARAMETER_MODE_UNKNOWN: 267 break; 268 case JDBC30Translation.PARAMETER_MODE_IN: 269 break; 270 case JDBC30Translation.PARAMETER_MODE_IN_OUT: 271 case JDBC30Translation.PARAMETER_MODE_OUT: 272 if (registerOutType == Types.NULL) { 273 throw StandardException.newException(SQLState.NEED_TO_REGISTER_PARAM, 274 getJDBCParameterNumberStr(), 275 org.apache.derby.catalog.types.RoutineAliasInfo.parameterMode(parameterMode)); 276 } 277 break; 278 } 279 } 280 281 286 int getScale() 287 { 288 return (registerOutScale == -1 ? 0 : registerOutScale); 290 } 291 292 293 int getPrecision() 294 { 295 return registerOutPrecision; 296 297 } 298 299 305 308 String getJDBCParameterNumberStr() 309 { 310 return Integer.toString(pvs.getParameterNumber(this)); 311 } 312 313 public String toString() 314 { 315 326 if (value == null) 327 { 328 return "null"; 329 } 330 else 331 { 332 try 333 { 334 return value.getTraceString(); 335 } 336 catch (StandardException se) 337 { 338 return "unexpected exception from getTraceString() - " + se; 339 } 340 } 341 } 342 } 343 | Popular Tags |