1 21 22 package org.apache.derby.impl.jdbc; 23 24 import org.apache.derby.iapi.sql.ParameterValueSet; 25 import org.apache.derby.iapi.sql.Activation; 26 import org.apache.derby.iapi.error.StandardException; 27 import org.apache.derby.iapi.types.DataValueDescriptor; 28 29 import org.apache.derby.iapi.services.sanity.SanityManager; 30 31 import org.apache.derby.iapi.reference.JDBC30Translation; 32 import org.apache.derby.iapi.reference.SQLState; 33 34 import java.net.URL ; 35 import java.sql.Blob ; 36 import java.sql.CallableStatement ; 37 import java.sql.Clob ; 38 import java.sql.SQLException ; 39 import java.sql.Date ; 40 import java.sql.Time ; 41 import java.sql.Timestamp ; 42 import java.util.Calendar ; 43 44 49 public abstract class EmbedCallableStatement extends EmbedPreparedStatement 50 implements CallableStatement 51 { 52 56 private boolean hasReturnOutputParameter; 57 58 protected boolean wasNull; 59 60 63 public EmbedCallableStatement (EmbedConnection conn, String sql, 64 int resultSetType, 65 int resultSetConcurrency, 66 int resultSetHoldability) 67 throws SQLException 68 { 69 super(conn, sql, false, 70 resultSetType, 71 resultSetConcurrency, 72 resultSetHoldability, 73 JDBC30Translation.NO_GENERATED_KEYS, 74 null, 75 null); 76 77 ParameterValueSet pvs = getParms(); 79 80 hasReturnOutputParameter = pvs.hasReturnOutputParameter(); 82 } 83 84 protected void checkRequiresCallableStatement(Activation activation) { 85 } 86 87 protected final boolean executeStatement(Activation a, 88 boolean executeQuery, boolean executeUpdate) 89 throws SQLException 90 { 91 checkExecStatus(); 94 synchronized (getConnectionSynchronization()) 95 { 96 wasNull = false; 97 ParameterValueSet pvs = getParms(); 98 try 99 { 100 pvs.validate(); 101 102 } catch (StandardException e) 103 { 104 throw EmbedResultSet.noStateChangeException(e); 105 } 106 107 110 boolean execResult = super.executeStatement(a, executeQuery, 111 (executeUpdate && (! hasReturnOutputParameter))); 112 113 118 if (hasReturnOutputParameter) 119 { 120 if (SanityManager.DEBUG) 121 { 122 SanityManager.ASSERT(results!=null, "null results even though we are supposed to have a return parameter"); 123 } 124 boolean gotRow = results.next(); 125 if (SanityManager.DEBUG) 126 { 127 SanityManager.ASSERT(gotRow, "the return resultSet didn't have any rows"); 128 } 129 130 try 131 { 132 DataValueDescriptor returnValue = pvs.getReturnValueForSet(); 133 returnValue.setValueFromResultSet(results, 1, true); 134 } catch (StandardException e) 135 { 136 throw EmbedResultSet.noStateChangeException(e); 137 } 138 finally { 139 results = null; 140 } 141 142 execResult = false; 146 } 147 return execResult; 148 } 149 } 150 151 155 156 160 public final void registerOutParameter(int parameterIndex, int sqlType) 161 throws SQLException 162 { 163 checkStatus(); 164 165 try { 166 getParms().registerOutParameter(parameterIndex-1, sqlType, -1); 167 } catch (StandardException e) 168 { 169 throw EmbedResultSet.noStateChangeException(e); 170 } 171 } 172 173 177 public final void registerOutParameter(int parameterIndex, int sqlType, int scale) 178 throws SQLException 179 { 180 checkStatus(); 181 182 if (scale < 0) 183 throw newSQLException(SQLState.BAD_SCALE_VALUE, new Integer (scale)); 184 try { 185 getParms().registerOutParameter(parameterIndex-1, sqlType, scale); 186 } catch (StandardException e) 187 { 188 throw EmbedResultSet.noStateChangeException(e); 189 } 190 191 } 192 193 194 201 public void registerOutParameter(int parameterIndex, int sqlType, 202 String typeName) 203 throws SQLException 204 { 205 throw Util.notImplemented("registerOutParameter"); 206 } 207 208 209 210 214 public boolean wasNull() throws SQLException 215 { 216 checkStatus(); 217 return wasNull; 218 } 219 220 224 public String getString(int parameterIndex) throws SQLException 225 { 226 checkStatus(); 227 try { 228 String v = getParms().getParameterForGet(parameterIndex-1).getString(); 229 wasNull = (v == null); 230 return v; 231 232 } catch (StandardException e) 233 { 234 throw EmbedResultSet.noStateChangeException(e); 235 } 236 } 237 238 242 public boolean getBoolean(int parameterIndex) throws SQLException 243 { 244 checkStatus(); 245 try { 246 DataValueDescriptor param = getParms().getParameterForGet(parameterIndex-1); 247 boolean v = param.getBoolean(); 248 wasNull = (!v) && param.isNull(); 249 return v; 250 } catch (StandardException e) 251 { 252 throw EmbedResultSet.noStateChangeException(e); 253 } 254 255 } 256 257 261 public byte getByte(int parameterIndex) throws SQLException 262 { 263 checkStatus(); 264 try { 265 DataValueDescriptor param = getParms().getParameterForGet(parameterIndex-1); 266 byte b = param.getByte(); 267 wasNull = (b == 0) && param.isNull(); 268 return b; 269 } catch (StandardException e) 270 { 271 throw EmbedResultSet.noStateChangeException(e); 272 } 273 274 } 275 276 280 public short getShort(int parameterIndex) throws SQLException 281 { 282 checkStatus(); 283 try { 284 DataValueDescriptor param = getParms().getParameterForGet(parameterIndex-1); 285 short s = param.getShort(); 286 wasNull = (s == 0) && param.isNull(); 287 return s; 288 } catch (StandardException e) 289 { 290 throw EmbedResultSet.noStateChangeException(e); 291 } 292 293 } 294 295 299 public int getInt(int parameterIndex) throws SQLException 300 { 301 checkStatus(); 302 303 try { 304 DataValueDescriptor param = getParms().getParameterForGet(parameterIndex-1); 305 int v = param.getInt(); 306 wasNull = (v == 0) && param.isNull(); 307 return v; 308 309 } catch (StandardException e) 310 { 311 throw EmbedResultSet.noStateChangeException(e); 312 } 313 } 314 315 319 public long getLong(int parameterIndex) throws SQLException 320 { 321 checkStatus(); 322 try { 323 DataValueDescriptor param = getParms().getParameterForGet(parameterIndex-1); 324 long v = param.getLong(); 325 wasNull = (v == 0L) && param.isNull(); 326 return v; 327 } catch (StandardException e) 328 { 329 throw EmbedResultSet.noStateChangeException(e); 330 } 331 332 } 333 334 338 public float getFloat(int parameterIndex) throws SQLException 339 { 340 checkStatus(); 341 try { 342 DataValueDescriptor param = getParms().getParameterForGet(parameterIndex-1); 343 float v = param.getFloat(); 344 wasNull = (v == 0.0) && param.isNull(); 345 return v; 346 } catch (StandardException e) 347 { 348 throw EmbedResultSet.noStateChangeException(e); 349 } 350 } 351 352 356 public double getDouble(int parameterIndex) throws SQLException 357 { 358 checkStatus(); 359 try { 360 DataValueDescriptor param = getParms().getParameterForGet(parameterIndex-1); 361 double v = param.getDouble(); 362 wasNull = (v == 0.0) && param.isNull(); 363 return v; 364 } catch (StandardException e) 365 { 366 throw EmbedResultSet.noStateChangeException(e); 367 } 368 369 } 370 371 375 public byte[] getBytes(int parameterIndex) throws SQLException 376 { 377 checkStatus(); 378 try { 379 byte[] v = getParms().getParameterForGet(parameterIndex-1).getBytes(); 380 wasNull = (v == null); 381 return v; 382 } catch (StandardException e) 383 { 384 throw EmbedResultSet.noStateChangeException(e); 385 } 386 387 } 388 389 393 public Date getDate(int parameterIndex) throws SQLException 394 { 395 checkStatus(); 396 try { 397 Date v = getParms().getParameterForGet(parameterIndex-1).getDate(getCal()); 398 wasNull = (v == null); 399 return v; 400 } catch (StandardException e) 401 { 402 throw EmbedResultSet.noStateChangeException(e); 403 } 404 405 } 406 407 411 public Time getTime(int parameterIndex) throws SQLException 412 { 413 checkStatus(); 414 try { 415 Time v = getParms().getParameterForGet(parameterIndex-1).getTime(getCal()); 416 wasNull = (v == null); 417 return v; 418 } catch (StandardException e) 419 { 420 throw EmbedResultSet.noStateChangeException(e); 421 } 422 423 } 424 425 429 public Timestamp getTimestamp(int parameterIndex) 430 throws SQLException 431 { 432 checkStatus(); 433 try { 434 Timestamp v = getParms().getParameterForGet(parameterIndex-1).getTimestamp(getCal()); 435 wasNull = (v == null); 436 return v; 437 } catch (StandardException e) 438 { 439 throw EmbedResultSet.noStateChangeException(e); 440 } 441 } 442 450 public java.sql.Date getDate(int parameterIndex, Calendar cal) 451 throws SQLException 452 { 453 return getDate(parameterIndex); 454 } 455 456 464 public java.sql.Time getTime(int parameterIndex, Calendar cal) 465 throws SQLException 466 { 467 return getTime(parameterIndex); 468 } 469 470 479 public java.sql.Timestamp getTimestamp(int parameterIndex, Calendar cal) 480 throws SQLException 481 { 482 return getTimestamp(parameterIndex); 483 } 484 485 489 public final Object getObject(int parameterIndex) throws SQLException 490 { 491 checkStatus(); 492 try { 493 Object v = getParms().getParameterForGet(parameterIndex-1).getObject(); 494 wasNull = (v == null); 495 return v; 496 497 } catch (StandardException e) 498 { 499 throw EmbedResultSet.noStateChangeException(e); 500 } 501 } 502 512 public URL getURL(int parameterIndex) 513 throws SQLException 514 { 515 throw Util.notImplemented(); 516 } 517 518 528 public void setURL(String parameterName, URL val) 529 throws SQLException 530 { 531 throw Util.notImplemented(); 532 } 533 534 543 public URL getURL(String parameterName) 544 throws SQLException 545 { 546 throw Util.notImplemented(); 547 } 548 549 558 public Blob getBlob (int i) throws SQLException { 559 throw Util.notImplemented(); 560 } 561 562 571 public Clob getClob (int i) throws SQLException { 572 throw Util.notImplemented(); 573 } 574 public void addBatch() throws SQLException { 575 576 checkStatus(); 577 ParameterValueSet pvs = getParms(); 578 579 int numberOfParameters = pvs.getParameterCount(); 580 581 for (int j=1; j<=numberOfParameters; j++) { 582 583 switch (pvs.getParameterMode(j)) { 584 case JDBC30Translation.PARAMETER_MODE_IN: 585 case JDBC30Translation.PARAMETER_MODE_UNKNOWN: 586 break; 587 case JDBC30Translation.PARAMETER_MODE_OUT: 588 case JDBC30Translation.PARAMETER_MODE_IN_OUT: 589 throw newSQLException(SQLState.OUTPUT_PARAMS_NOT_ALLOWED); 590 } 591 } 592 593 super.addBatch(); 594 } 595 } 596 597 | Popular Tags |