1 21 22 package org.apache.derby.impl.sql; 23 24 import org.apache.derby.iapi.services.loader.ClassFactory; 25 import org.apache.derby.iapi.services.loader.ClassInspector; 26 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 27 28 import org.apache.derby.iapi.sql.ParameterValueSet; 29 30 import org.apache.derby.iapi.types.DataTypeDescriptor; 31 import org.apache.derby.iapi.types.DataValueFactory; 32 import org.apache.derby.iapi.types.DataValueDescriptor; 33 import org.apache.derby.iapi.types.UserDataValue; 34 35 import org.apache.derby.iapi.reference.SQLState; 36 37 import org.apache.derby.iapi.error.StandardException; 38 39 import org.apache.derby.iapi.services.sanity.SanityManager; 40 41 import java.io.InputStream ; 42 import java.sql.Date ; 43 import java.sql.Time ; 44 import java.sql.Timestamp ; 45 import java.sql.Types ; 46 import org.apache.derby.iapi.reference.JDBC30Translation; 47 48 55 56 final class GenericParameterValueSet implements ParameterValueSet 57 { 58 private final GenericParameter[] parms; 60 final ClassInspector ci; 61 private final boolean hasReturnOutputParam; 62 63 64 72 GenericParameterValueSet(ClassInspector ci, int numParms, boolean hasReturnOutputParam) 73 { 74 this.ci = ci; 75 this.hasReturnOutputParam = hasReturnOutputParam; 76 parms = new GenericParameter[numParms]; 77 for (int i = 0; i < numParms; i++) 78 { 79 83 parms[i] = new GenericParameter(this, (hasReturnOutputParam && i == 0)); 84 } 85 } 86 87 90 private GenericParameterValueSet(int numParms, GenericParameterValueSet pvs) 91 { 92 this.hasReturnOutputParam = pvs.hasReturnOutputParam; 93 this.ci = pvs.ci; 94 parms = new GenericParameter[numParms]; 95 for (int i = 0; i < numParms; i++) 96 { 97 parms[i] = pvs.getGenericParameter(i).getClone(this); 98 } 99 } 100 101 104 105 109 public void initialize(DataTypeDescriptor[] types) 110 { 111 for (int i = 0; i < parms.length; i++) 112 { 113 DataTypeDescriptor dtd = types[i]; 114 115 parms[i].initialize(dtd.getNull(), 116 dtd.getJDBCTypeId(), dtd.getTypeId().getCorrespondingJavaTypeName()); 117 } 118 } 119 120 public void setParameterMode(int position, int mode) { 121 parms[position].parameterMode = (short) mode; 122 } 123 124 127 public void clearParameters() 128 { 129 for (int i = 0; i < parms.length; i++) 130 { 131 parms[i].clear(); 132 } 133 } 134 135 140 public int getParameterCount() 141 { 142 return parms.length; 143 } 144 145 151 public DataValueDescriptor getParameter( int position ) throws StandardException 152 { 153 try { 154 return parms[position].getValue(); 155 } catch (ArrayIndexOutOfBoundsException e) { 156 checkPosition(position); 157 return null; 158 } 159 } 160 161 162 163 public DataValueDescriptor getParameterForSet(int position) throws StandardException { 164 165 try { 166 167 GenericParameter gp = parms[position]; 168 if (gp.parameterMode == JDBC30Translation.PARAMETER_MODE_OUT) 169 throw StandardException.newException(SQLState.LANG_RETURN_OUTPUT_PARAM_CANNOT_BE_SET); 170 171 gp.isSet = true; 172 173 return gp.getValue(); 174 } catch (ArrayIndexOutOfBoundsException e) { 175 checkPosition(position); 176 return null; 177 } 178 179 } 180 181 public DataValueDescriptor getParameterForGet(int position) throws StandardException { 182 183 try { 184 185 GenericParameter gp = parms[position]; 186 187 switch (gp.parameterMode) { 188 case JDBC30Translation.PARAMETER_MODE_IN: 189 case JDBC30Translation.PARAMETER_MODE_UNKNOWN: 190 throw StandardException.newException(SQLState.LANG_NOT_OUTPUT_PARAMETER, Integer.toString(position + 1)); 191 } 192 193 return gp.getValue(); 194 } catch (ArrayIndexOutOfBoundsException e) { 195 checkPosition(position); 196 return null; 197 } 198 } 199 200 public void setParameterAsObject(int position, Object value) throws StandardException { 201 202 UserDataValue dvd = (UserDataValue) getParameterForSet(position); 203 204 GenericParameter gp = parms[position]; 205 if (value != null) { 206 207 { 208 209 boolean throwError; 210 ClassNotFoundException t = null; 211 try { 212 throwError = !ci.instanceOf(gp.declaredClassName, value); 213 } catch (ClassNotFoundException cnfe) { 214 t = cnfe; 215 throwError = true; 216 } 217 218 if (throwError) { 219 throw StandardException.newException(SQLState.LANG_DATA_TYPE_SET_MISMATCH, t, 220 ClassInspector.readableClassName(value.getClass()), gp.declaredClassName); 221 } 222 } 223 224 } 225 226 dvd.setValue(value); 227 } 228 229 232 public boolean allAreSet() 233 { 234 for (int i = 0; i < parms.length; i++) 235 { 236 GenericParameter gp = parms[i]; 237 if (!gp.isSet) 238 { 239 switch (gp.parameterMode) { 240 case JDBC30Translation.PARAMETER_MODE_OUT: 241 break; 242 case JDBC30Translation.PARAMETER_MODE_IN_OUT: 243 case JDBC30Translation.PARAMETER_MODE_UNKNOWN: 244 case JDBC30Translation.PARAMETER_MODE_IN: 245 return false; 246 } 247 } 248 } 249 250 return true; 251 } 252 253 256 public void transferDataValues(ParameterValueSet pvstarget) throws StandardException 257 { 258 int firstParam = pvstarget.hasReturnOutputParameter() ? 1 : 0; 260 for (int i = firstParam; i < parms.length;i++) 261 { 262 263 GenericParameter oldp = parms[i]; 264 265 if (oldp.registerOutType != Types.NULL) { 266 267 pvstarget.registerOutParameter(i, oldp.registerOutType, oldp.registerOutScale); 268 269 } 270 271 if (oldp.isSet) 272 { 273 pvstarget.getParameterForSet(i).setValue(oldp.getValue()); 274 } 275 } 276 } 277 278 GenericParameter getGenericParameter(int position) 279 { 280 return(parms[position]); 281 } 282 283 284 public String toString() 285 { 286 289 StringBuffer strbuf = new StringBuffer (); 290 291 for (int ctr = 0; ctr < parms.length; ctr++) 292 { 293 strbuf.append("begin parameter #" + (ctr + 1) + ": "); 294 strbuf.append(parms[ctr].toString()); 295 strbuf.append(" :end parameter "); 296 } 297 298 return strbuf.toString(); 299 } 300 301 310 private void checkPosition(int position) throws StandardException 311 { 312 if (position < 0 || position >= parms.length) 313 { 314 315 if (parms.length == 0) 316 throw StandardException.newException(SQLState.NO_INPUT_PARAMETERS); 317 318 throw StandardException.newException(SQLState.LANG_INVALID_PARAM_POSITION, 319 String.valueOf(position+1), 320 String.valueOf(parms.length)); 321 } 322 } 323 324 325 public ParameterValueSet getClone() 326 { 327 return(new GenericParameterValueSet(parms.length, this)); 328 } 329 330 336 346 public void registerOutParameter(int parameterIndex, int sqlType, int scale) 347 throws StandardException 348 { 349 checkPosition(parameterIndex); 350 parms[parameterIndex].setOutParameter(sqlType, scale); 351 } 352 353 365 public void validate() throws StandardException 366 { 367 for (int i = 0; i < parms.length; i++) 368 { 369 parms[i].validate(); 370 } 371 } 372 373 374 380 public int getParameterNumber(GenericParameter theParam) 381 { 382 for (int i = 0; i < parms.length; i++) 383 { 384 if (parms[i] == theParam) 385 { 386 return i+1; 387 } 388 } 389 return 0; 390 } 391 392 400 public boolean checkNoDeclaredOutputParameters() { 401 402 boolean hasDeclaredOutputParameter = false; 403 for (int i=0; i<parms.length; i++) { 404 405 GenericParameter gp = parms[i]; 406 407 switch (gp.parameterMode) { 408 case JDBC30Translation.PARAMETER_MODE_IN: 409 break; 410 case JDBC30Translation.PARAMETER_MODE_IN_OUT: 411 case JDBC30Translation.PARAMETER_MODE_OUT: 412 hasDeclaredOutputParameter = true; 413 break; 414 case JDBC30Translation.PARAMETER_MODE_UNKNOWN: 415 gp.parameterMode = JDBC30Translation.PARAMETER_MODE_IN; 416 break; 417 } 418 } 419 return hasDeclaredOutputParameter; 420 } 421 422 427 public short getParameterMode(int parameterIndex) 428 { 429 short mode = parms[parameterIndex - 1].parameterMode; 430 return mode; 433 } 434 435 443 public boolean hasReturnOutputParameter() 444 { 445 return hasReturnOutputParam; 446 } 447 448 454 public DataValueDescriptor getReturnValueForSet() throws StandardException 455 { 456 checkPosition(0); 457 458 if (SanityManager.DEBUG) 459 { 460 if (!hasReturnOutputParam) 461 SanityManager.THROWASSERT("getReturnValueForSet called on non-return parameter"); 462 } 463 464 return parms[0].getValue(); 465 } 466 467 474 public int getScale(int parameterIndex) 475 { 476 return parms[parameterIndex-1].getScale(); 477 } 478 479 486 public int getPrecision(int parameterIndex) 487 { 488 return parms[parameterIndex-1].getPrecision(); 489 } 490 491 } 492 | Popular Tags |