1 21 22 package org.apache.derby.impl.jdbc; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 26 import org.apache.derby.iapi.types.VariableSizeDataValue; 27 28 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 29 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 30 import org.apache.derby.iapi.sql.PreparedStatement; 31 import org.apache.derby.iapi.sql.execute.ExecPreparedStatement; 32 import org.apache.derby.iapi.sql.ResultSet; 33 import org.apache.derby.iapi.sql.Activation; 34 import org.apache.derby.iapi.sql.ParameterValueSet; 35 import org.apache.derby.iapi.sql.ResultDescription; 36 import org.apache.derby.iapi.types.DataTypeDescriptor; 37 import org.apache.derby.iapi.types.DataValueDescriptor; 38 import org.apache.derby.iapi.types.RawToBinaryFormatStream; 39 import org.apache.derby.iapi.types.ReaderToUTF8Stream; 40 41 import org.apache.derby.iapi.error.StandardException; 42 43 import org.apache.derby.iapi.services.io.LimitReader; 44 45 import org.apache.derby.iapi.reference.SQLState; 46 import org.apache.derby.iapi.reference.JDBC40Translation; 47 import org.apache.derby.iapi.reference.JDBC30Translation; 48 import org.apache.derby.iapi.reference.JDBC20Translation; 49 50 import java.util.Calendar ; 51 import java.util.Vector ; 52 53 58 import java.sql.ResultSetMetaData ; 59 import java.sql.SQLException ; 60 import java.sql.Date ; 61 import java.sql.Time ; 62 import java.sql.Timestamp ; 63 import java.sql.Clob ; 64 import java.sql.Blob ; 65 66 import java.io.InputStream ; 67 import java.io.DataInputStream ; 68 import java.io.IOException ; 69 import java.io.EOFException ; 70 import java.io.Reader ; 71 import java.sql.Types ; 72 73 import org.apache.derby.iapi.jdbc.BrokeredConnectionControl; 74 import org.apache.derby.iapi.jdbc.EngineParameterMetaData; 75 import org.apache.derby.iapi.jdbc.EnginePreparedStatement; 76 77 85 public abstract class EmbedPreparedStatement 86 extends EmbedStatement 87 implements EnginePreparedStatement 88 { 89 90 93 protected ResultSetMetaData rMetaData; 94 private String gcDuringGetMetaData; 99 100 protected PreparedStatement preparedStatement; 101 private Activation activation; 102 103 private BrokeredConnectionControl bcc=null; 104 105 109 public EmbedPreparedStatement (EmbedConnection conn, String sql, boolean forMetaData, 110 int resultSetType, int resultSetConcurrency, 111 int resultSetHoldability, 112 int autoGeneratedKeys, 113 int[] columnIndexes, 114 String [] columnNames) 115 throws SQLException { 116 117 super(conn, forMetaData, resultSetType, resultSetConcurrency, resultSetHoldability); 118 isPoolable = true; 120 121 if (sql == null) 123 throw newSQLException(SQLState.NULL_SQL_TEXT); 124 125 SQLText = sql; 127 128 try { 129 preparedStatement = lcc.prepareInternalStatement 130 (lcc.getDefaultSchema(), sql, resultSetConcurrency==JDBC20Translation.CONCUR_READ_ONLY, forMetaData); 131 132 addWarning(preparedStatement.getCompileTimeWarnings()); 133 134 activation = preparedStatement.getActivation(lcc, resultSetType == JDBC20Translation.TYPE_SCROLL_INSENSITIVE); 135 136 checkRequiresCallableStatement(activation); 137 138 if (autoGeneratedKeys == JDBC30Translation.RETURN_GENERATED_KEYS) 141 activation.setAutoGeneratedKeysResultsetInfo(columnIndexes, columnNames); 142 143 } catch (Throwable t) { 144 throw handleException(t); 145 } 146 } 147 148 153 protected void finalize() throws Throwable { 154 super.finalize(); 155 156 165 if (activation != null) 166 { 167 activation.markUnused(); 168 } 169 } 170 171 177 public final boolean execute(String sql) throws SQLException { 178 throw newSQLException(SQLState.NOT_FOR_PREPARED_STATEMENT, "execute(String)"); 179 } 180 public final boolean execute(String sql, int autoGenKeys) throws SQLException { 181 throw newSQLException(SQLState.NOT_FOR_PREPARED_STATEMENT, "execute(String, int)"); 182 } 183 public final boolean execute(String sql, int[] columnIndexes) throws SQLException { 184 throw newSQLException(SQLState.NOT_FOR_PREPARED_STATEMENT, "execute(String, int[])"); 185 } 186 public final boolean execute(String sql, String [] columnNames) throws SQLException { 187 throw newSQLException(SQLState.NOT_FOR_PREPARED_STATEMENT, "execute(String, String[])"); 188 } 189 public final java.sql.ResultSet executeQuery(String sql) throws SQLException { 190 throw newSQLException(SQLState.NOT_FOR_PREPARED_STATEMENT, "executeQuery(String)"); 191 } 192 public final int executeUpdate(String sql) throws SQLException { 193 throw newSQLException(SQLState.NOT_FOR_PREPARED_STATEMENT, "executeUpdate(String)"); 194 } 195 public final int executeUpdate(String sql, int autoGenKeys) throws SQLException { 196 throw newSQLException(SQLState.NOT_FOR_PREPARED_STATEMENT, "executeUpdate(String, int)"); 197 } 198 public final int executeUpdate(String sql, int[] columnIndexes) throws SQLException { 199 throw newSQLException(SQLState.NOT_FOR_PREPARED_STATEMENT, "executeUpdate(String, int[])"); 200 } 201 public final int executeUpdate(String sql, String [] columnNames) throws SQLException { 202 throw newSQLException(SQLState.NOT_FOR_PREPARED_STATEMENT, "executeUpdate(String, String[])"); 203 } 204 public final void addBatch(String sql) throws SQLException { 205 throw newSQLException(SQLState.NOT_FOR_PREPARED_STATEMENT, "addBatch(String)"); 206 } 207 208 209 229 void closeActions() throws SQLException { 230 231 if (bcc!=null) { 232 java.sql.PreparedStatement ps_app = 233 (java.sql.PreparedStatement )applicationStatement; 234 bcc.onStatementClose(ps_app); 235 } 236 preparedStatement = null; 238 239 try{ 240 setupContextStack(); 241 } catch (SQLException se) { 242 return; 245 } 246 try 247 { 248 activation.close(); 249 activation = null; 250 } catch (Throwable t) 251 { 252 throw handleException(t); 253 } finally { 254 restoreContextStack(); 255 } 256 } 257 258 265 266 273 public final java.sql.ResultSet executeQuery() throws SQLException { 274 try { 275 executeStatement(activation, true, false); 276 } catch(SQLException sqle) { 277 checkStatementValidity(sqle); 278 } 279 280 if (SanityManager.DEBUG) { 281 if (results == null) 282 SanityManager.THROWASSERT("no results returned on executeQuery()"); 283 } 284 285 return results; 286 } 287 288 297 public final int executeUpdate() throws SQLException { 298 try { 299 executeStatement(activation, false, true); 300 } catch(SQLException sqle) { 301 checkStatementValidity(sqle); 302 } 303 return updateCount; 304 } 305 306 315 public void setNull(int parameterIndex, int sqlType) throws SQLException { 316 317 checkForSupportedDataType(sqlType); 318 checkStatus(); 319 320 int jdbcTypeId = getParameterJDBCType(parameterIndex); 321 322 if (!DataTypeDescriptor.isJDBCTypeEquivalent(jdbcTypeId, sqlType)) { 323 324 throw dataTypeConversion(parameterIndex, Util.typeName(sqlType)); 325 } 326 327 try { 328 329 getParms().getParameterForSet(parameterIndex - 1).setToNull(); 330 } catch (StandardException t) { 331 throw EmbedResultSet.noStateChangeException(t); 332 } 333 334 } 335 336 346 public void setBoolean(int parameterIndex, boolean x) throws SQLException { 347 348 checkStatus(); 349 try { 350 351 getParms().getParameterForSet(parameterIndex - 1).setValue(x); 352 353 } catch (StandardException t) { 354 throw EmbedResultSet.noStateChangeException(t); 355 } 356 } 357 358 366 public void setByte(int parameterIndex, byte x) throws SQLException { 367 368 checkStatus(); 369 try { 370 371 getParms().getParameterForSet(parameterIndex - 1).setValue(x); 372 373 } catch (Throwable t) { 374 throw EmbedResultSet.noStateChangeException(t); 375 } 376 } 377 378 386 public void setShort(int parameterIndex, short x) throws SQLException { 387 388 checkStatus(); 389 try { 390 391 getParms().getParameterForSet(parameterIndex - 1).setValue(x); 392 393 } catch (Throwable t) { 394 throw EmbedResultSet.noStateChangeException(t); 395 } 396 } 397 398 406 public void setInt(int parameterIndex, int x) throws SQLException { 407 checkStatus(); 408 409 try { 410 411 getParms().getParameterForSet(parameterIndex - 1).setValue(x); 412 } catch (Throwable t) { 413 throw EmbedResultSet.noStateChangeException(t); 414 } 415 } 416 417 425 public void setLong(int parameterIndex, long x) throws SQLException { 426 checkStatus(); 427 try { 428 429 getParms().getParameterForSet(parameterIndex - 1).setValue(x); 430 431 } catch (Throwable t) { 432 throw EmbedResultSet.noStateChangeException(t); 433 } 434 435 } 436 437 445 public void setFloat(int parameterIndex, float x) throws SQLException { 446 checkStatus(); 447 try { 448 449 getParms().getParameterForSet(parameterIndex - 1).setValue(x); 450 451 } catch (Throwable t) { 452 throw EmbedResultSet.noStateChangeException(t); 453 } 454 455 } 456 457 465 public void setDouble(int parameterIndex, double x) throws SQLException { 466 checkStatus(); 467 468 try { 469 470 getParms().getParameterForSet(parameterIndex - 1).setValue(x); 471 472 } catch (Throwable t) { 473 throw EmbedResultSet.noStateChangeException(t); 474 } 475 476 } 477 478 488 public void setString(int parameterIndex, String x) throws SQLException { 489 checkStatus(); 490 try { 491 492 getParms().getParameterForSet(parameterIndex - 1).setValue(x); 493 494 } catch (Throwable t) { 495 throw EmbedResultSet.noStateChangeException(t); 496 } 497 } 498 499 509 public void setBytes(int parameterIndex, byte x[]) throws SQLException { 510 checkStatus(); 511 512 try { 513 514 getParms().getParameterForSet(parameterIndex - 1).setValue(x); 515 516 } catch (Throwable t) { 517 throw EmbedResultSet.noStateChangeException(t); 518 } 519 520 } 521 522 530 public void setDate(int parameterIndex, Date x) throws SQLException { 531 setDate( parameterIndex, x, (Calendar ) null); 532 } 533 534 542 public void setTime(int parameterIndex, Time x) throws SQLException { 543 setTime( parameterIndex, x, (Calendar ) null); 544 } 545 546 555 public void setTimestamp(int parameterIndex, Timestamp x) 556 throws SQLException { 557 setTimestamp( parameterIndex, x, (Calendar ) null); 558 } 559 560 569 public final void setAsciiStream(int parameterIndex, InputStream x, long length) 570 throws SQLException { 571 checkAsciiStreamConditions(parameterIndex); 572 java.io.Reader r = null; 573 574 if (x != null) 575 { 576 try { 579 r = new java.io.InputStreamReader (x, "ISO-8859-1"); 580 } catch (java.io.UnsupportedEncodingException uee) { 581 throw new SQLException (uee.getMessage()); 582 } 583 } 584 585 setCharacterStreamInternal(parameterIndex, r, false, length); 586 } 587 588 597 598 public final void setAsciiStream(int parameterIndex, InputStream x, int length) 599 throws SQLException { 600 setAsciiStream(parameterIndex,x,(long)length); 601 } 602 603 604 613 public void setUnicodeStream(int parameterIndex, InputStream x, int length) 614 throws SQLException 615 { 616 throw Util.notImplemented("setUnicodeStream"); 617 } 618 619 635 public final void setCharacterStream(int parameterIndex, 636 java.io.Reader reader, 637 long length) throws SQLException { 638 checkCharacterStreamConditions(parameterIndex); 639 setCharacterStreamInternal(parameterIndex, reader, false, length); 640 } 641 642 658 public final void setCharacterStream(int parameterIndex, 659 java.io.Reader reader, 660 int length) throws SQLException { 661 setCharacterStream(parameterIndex,reader,(long)length); 662 } 663 664 669 private final void checkCharacterStreamConditions(int parameterIndex) 670 throws SQLException { 671 checkStatus(); 672 int jdbcTypeId = getParameterJDBCType(parameterIndex); 673 if (!DataTypeDescriptor.isCharacterStreamAssignable(jdbcTypeId)) { 674 throw dataTypeConversion(parameterIndex, "java.io.Reader"); 675 } 676 } 677 678 683 private final void checkAsciiStreamConditions(int parameterIndex) 684 throws SQLException { 685 checkStatus(); 686 int jdbcTypeId = getParameterJDBCType(parameterIndex); 687 if (!DataTypeDescriptor.isAsciiStreamAssignable(jdbcTypeId)) { 688 throw dataTypeConversion(parameterIndex, 689 "java.io.InputStream(ASCII)"); 690 } 691 } 692 693 710 private void setCharacterStreamInternal(int parameterIndex, 711 Reader reader, 712 final boolean lengthLess, 713 long length) 714 throws SQLException 715 { 716 if (!lengthLess && length < 0) 718 throw newSQLException(SQLState.NEGATIVE_STREAM_LENGTH); 719 720 int jdbcTypeId = getParameterJDBCType(parameterIndex); 721 722 723 if (reader == null) { 724 setNull(parameterIndex, jdbcTypeId); 725 return; 726 } 727 728 734 if (!lengthLess && length > Integer.MAX_VALUE) 735 throw newSQLException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, 736 getParameterSQLType(parameterIndex)); 737 738 try { 739 ReaderToUTF8Stream utfIn; 740 ParameterValueSet pvs = getParms(); 741 DataTypeDescriptor dtd[] = preparedStatement 743 .getParameterTypes(); 744 int colWidth = dtd[parameterIndex - 1].getMaximumWidth(); 745 int usableLength = colWidth; 748 749 if (!lengthLess) { 750 int intLength = (int)length; 754 int truncationLength = 0; 755 756 usableLength = intLength; 757 758 if (jdbcTypeId == Types.CLOB) 770 { 771 772 if (intLength > colWidth) { 781 usableLength = colWidth; 782 truncationLength = intLength - usableLength; 783 } 784 } 785 utfIn = new ReaderToUTF8Stream(reader, usableLength, 787 truncationLength, getParameterSQLType(parameterIndex)); 788 } else { 789 utfIn = new ReaderToUTF8Stream(reader, colWidth, 792 getParameterSQLType(parameterIndex)); 793 } 794 795 pvs.getParameterForSet( 801 parameterIndex - 1).setValue(utfIn, usableLength); 802 803 } catch (StandardException t) { 804 throw EmbedResultSet.noStateChangeException(t); 805 } 806 } 807 808 823 public void setBinaryStream(int parameterIndex, InputStream x) 824 throws SQLException { 825 checkBinaryStreamConditions(parameterIndex); 826 setBinaryStreamInternal(parameterIndex, x, true, -1); 827 } 828 829 837 public final void setBinaryStream(int parameterIndex, InputStream x, long length) 838 throws SQLException { 839 checkBinaryStreamConditions(parameterIndex); 840 setBinaryStreamInternal(parameterIndex, x, false, length); 841 } 842 843 851 public final void setBinaryStream(int parameterIndex, InputStream x, int length) 852 throws SQLException { 853 setBinaryStream(parameterIndex,x,(long)length); 854 } 855 856 873 private void setBinaryStreamInternal(int parameterIndex, InputStream x, 874 final boolean lengthLess, long length) 875 throws SQLException 876 { 877 878 if ( !lengthLess && length < 0 ) 879 throw newSQLException(SQLState.NEGATIVE_STREAM_LENGTH); 880 881 int jdbcTypeId = getParameterJDBCType(parameterIndex); 882 if (x == null) { 883 setNull(parameterIndex, jdbcTypeId); 884 return; 885 } 886 887 if ( !lengthLess && length > Integer.MAX_VALUE ) { 894 throw newSQLException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, 895 getEmbedParameterSetMetaData().getParameterTypeName( 896 parameterIndex)); 897 } 898 899 try { 900 RawToBinaryFormatStream rawStream; 901 if (lengthLess) { 902 length = -1; 904 DataTypeDescriptor dtd[] = 905 preparedStatement.getParameterTypes(); 906 rawStream = new RawToBinaryFormatStream(x, 907 dtd[parameterIndex -1].getMaximumWidth(), 908 dtd[parameterIndex -1].getTypeName()); 909 } else { 910 rawStream = new RawToBinaryFormatStream(x, (int)length); 911 } 912 getParms().getParameterForSet(parameterIndex - 1).setValue( 913 rawStream, (int)length); 914 915 } catch (StandardException t) { 916 throw EmbedResultSet.noStateChangeException(t); 917 } 918 } 919 920 925 private final void checkBinaryStreamConditions(int parameterIndex) 926 throws SQLException { 927 checkStatus(); 928 int jdbcTypeId = getParameterJDBCType(parameterIndex); 929 if (!DataTypeDescriptor.isBinaryStreamAssignable(jdbcTypeId)) { 930 throw dataTypeConversion(parameterIndex, "java.io.InputStream"); 931 } 932 } 933 934 940 965 public void setNull(int paramIndex, 966 int sqlType, 967 String typeName) 968 throws SQLException { 969 setNull(paramIndex,sqlType); 970 } 971 972 979 public void addBatch() throws SQLException { 980 checkStatus(); 981 982 synchronized (getConnectionSynchronization()) { 987 if (batchStatements == null) 988 batchStatements = new Vector (); 989 990 batchStatements.addElement(getParms().getClone()); 996 clearParameters(); 997 } 998 } 999 1000 boolean executeBatchElement(Object batchElement) throws SQLException , StandardException { 1001 1002 ParameterValueSet temp = (ParameterValueSet) batchElement; 1003 1004 int numberOfParameters = temp.getParameterCount(); 1005 1006 for (int j=0; j<numberOfParameters; j++) { 1007 temp.getParameter(j).setInto(this, j + 1); 1008 } 1009 1010 return super.executeStatement(activation, false, true); 1011 } 1012 1013 1014 1015 1023 public void clearParameters() throws SQLException { 1024 checkStatus(); 1025 1026 ParameterValueSet pvs = getParms(); 1027 if (pvs != null) 1028 pvs.clearParameters(); 1029 } 1030 1031 1040 public java.sql.ResultSetMetaData getMetaData() throws SQLException 1041 { 1042 checkExecStatus(); 1043 synchronized (getConnectionSynchronization()) 1044 { 1045 ExecPreparedStatement execp = (ExecPreparedStatement)preparedStatement; 1047 1048 setupContextStack(); 1050 try { 1051 if (preparedStatement.isValid() == false) 1053 { 1054 preparedStatement.rePrepare(lcc); 1057 rMetaData = null; 1058 } 1059 if (gcDuringGetMetaData == null || gcDuringGetMetaData.equals(execp.getActivationClass().getName()) == false) 1064 { 1065 rMetaData = null; 1066 gcDuringGetMetaData = execp.getActivationClass().getName(); 1067 } 1068 if (rMetaData == null) 1069 { 1070 ResultDescription resd = preparedStatement.getResultDescription(); 1071 if (resd != null) 1072 { 1073 String statementType = resd.getStatementType(); 1080 if (statementType.equals("INSERT") || 1081 statementType.equals("UPDATE") || 1082 statementType.equals("DELETE")) 1083 rMetaData = null; 1084 else 1085 rMetaData = newEmbedResultSetMetaData(resd); 1086 } 1087 } 1088 } catch (Throwable t) { 1089 throw handleException(t); 1090 } finally { 1091 restoreContextStack(); 1092 } 1093 } 1094 return rMetaData; 1095 } 1096 1097 1100 1119 public final void setObject(int parameterIndex, Object x, int targetSqlType, int scale) 1120 throws SQLException { 1121 1122 if (x == null) { 1123 setNull(parameterIndex, targetSqlType); 1124 return; 1125 } 1126 1127 checkForSupportedDataType(targetSqlType); 1128 1129 int paramJDBCType = getParameterJDBCType(parameterIndex); 1130 1131 if (paramJDBCType != java.sql.Types.JAVA_OBJECT) { 1132 if (!DataTypeDescriptor.isJDBCTypeEquivalent(paramJDBCType, targetSqlType)) { 1133 throw dataTypeConversion(parameterIndex, Util.typeName(targetSqlType)); 1134 } 1135 } 1136 1137 setObject(parameterIndex, x); 1138 1139 1144 if ((paramJDBCType == Types.DECIMAL) || 1145 (paramJDBCType == Types.NUMERIC)) 1146 { 1147 setScale(parameterIndex, scale); 1148 } 1149 } 1150 1151 1155 public final void setObject(int parameterIndex, Object x, int targetSqlType) 1156 throws SQLException { 1157 setObject(parameterIndex, x, targetSqlType, 0); 1158 } 1159 1160 1177 public final void setObject(int parameterIndex, Object x) throws SQLException { 1178 checkStatus(); 1179 1180 1181 int colType = getParameterJDBCType(parameterIndex); 1182 1183 1188 if (x == null) { 1189 throw dataTypeConversion(parameterIndex, "null"); 1192 } 1193 1194 if (colType == org.apache.derby.iapi.reference.JDBC20Translation.SQL_TYPES_JAVA_OBJECT) { 1195 try { 1196 1197 getParms().setParameterAsObject(parameterIndex - 1, x); 1198 return; 1199 1200 } catch (Throwable t) { 1201 throw EmbedResultSet.noStateChangeException(t); 1202 } 1203 } 1204 1205 1206 1209 1212 if (x instanceof String ) { 1213 setString(parameterIndex, (String ) x); 1214 return; 1215 } 1216 1217 if (x instanceof Boolean ) { 1218 setBoolean(parameterIndex, ((Boolean ) x).booleanValue()); 1219 return; 1220 } 1221 if (x instanceof Byte ) { 1222 setByte(parameterIndex, ((Byte ) x).byteValue()); 1223 return; 1224 } 1225 if (x instanceof Short ) { 1226 setShort(parameterIndex, ((Short ) x).shortValue()); 1227 return; 1228 } 1229 if (x instanceof Integer ) { 1230 setInt(parameterIndex, ((Integer ) x).intValue()); 1231 return; 1232 } 1233 if (x instanceof Long ) { 1234 setLong(parameterIndex, ((Long ) x).longValue()); 1235 return; 1236 } 1237 1238 if (x instanceof Float ) { 1239 setFloat(parameterIndex, ((Float ) x).floatValue()); 1240 return; 1241 } 1242 if (x instanceof Double ) { 1243 setDouble(parameterIndex, ((Double ) x).doubleValue()); 1244 return; 1245 } 1246 1247 if (x instanceof byte[]) { 1248 setBytes(parameterIndex, (byte[]) x); 1249 return; 1250 } 1251 1252 if (x instanceof Date ) { 1253 setDate(parameterIndex, (Date ) x); 1254 return; 1255 } 1256 if (x instanceof Time ) { 1257 setTime(parameterIndex, (Time ) x); 1258 return; 1259 } 1260 if (x instanceof Timestamp ) { 1261 setTimestamp(parameterIndex, (Timestamp ) x); 1262 return; 1263 } 1264 1265 if (x instanceof Blob ) { 1266 setBlob(parameterIndex, (Blob ) x); 1267 return; 1268 } 1269 if (x instanceof Clob ) { 1270 setClob(parameterIndex, (Clob ) x); 1271 return; 1272 } 1273 1274 if (setObjectConvert(parameterIndex, x)) 1275 return; 1276 1277 1278 throw dataTypeConversion(parameterIndex, x.getClass().getName()); 1279 1280 } 1281 1282 1292 boolean setObjectConvert(int parameterIndex, Object x) throws SQLException 1293 { 1294 return false; 1295 } 1296 1297 1301 public final boolean execute() throws SQLException { 1302 boolean ret=false; 1303 try{ 1304 ret = executeStatement(activation, false, false); 1305 } catch(SQLException sqle) { 1306 checkStatementValidity(sqle); 1307 } 1308 return ret; 1309 } 1310 1318 public final void setDate(int parameterIndex, java.sql.Date x, Calendar cal) 1319 throws SQLException 1320 { 1321 checkStatus(); 1322 try { 1323 1324 getParms().getParameterForSet(parameterIndex - 1).setValue(x, cal); 1325 1326 } catch (Throwable t) { 1327 throw EmbedResultSet.noStateChangeException(t); 1328 } 1329 } 1330 1331 1339 public final void setTime(int parameterIndex, java.sql.Time x, Calendar cal) 1340 throws SQLException 1341 { 1342 checkStatus(); 1343 try { 1344 1345 getParms().getParameterForSet(parameterIndex - 1).setValue(x, cal); 1346 1347 } catch (Throwable t) { 1348 throw EmbedResultSet.noStateChangeException(t); 1349 } 1350 } 1351 1352 1361 public final void setTimestamp(int parameterIndex, java.sql.Timestamp x, Calendar cal) 1362 throws SQLException 1363 { 1364 checkStatus(); 1365 try { 1366 1367 getParms().getParameterForSet(parameterIndex - 1).setValue(x, cal); 1368 1369 } catch (StandardException t) { 1370 throw EmbedResultSet.noStateChangeException(t); 1371 } 1372 } 1373 1374 1375 1383 public void setBlob (int i, Blob x) 1384 throws SQLException 1385 { 1386 checkBlobConditions(i); 1387 if (x == null) 1388 setNull(i, Types.BLOB); 1389 else 1390 { 1391 long streamLength = x.length(); 1396 setBinaryStreamInternal(i, x.getBinaryStream(), false, 1397 streamLength); 1398 } 1399 } 1400 1401 1406 private final void checkClobConditions(int parameterIndex) 1407 throws SQLException { 1408 checkStatus(); 1409 if (getParameterJDBCType(parameterIndex) != Types.CLOB) { 1410 throw dataTypeConversion(parameterIndex, "java.sql.Clob"); 1411 } 1412 } 1413 1414 1422 public void setClob (int i, Clob x) 1423 throws SQLException 1424 { 1425 checkClobConditions(i); 1426 if (x == null) 1427 setNull(i, Types.CLOB); 1428 else 1429 { 1430 long streamLength = x.length(); 1442 1443 setCharacterStreamInternal(i, x.getCharacterStream(), 1444 false, streamLength); 1445 } 1446 1447 } 1448 1449 1456 public final ParameterValueSet getParms() { 1457 1458 return activation.getParameterValueSet(); 1459 } 1460 1461 1462 1468 protected final DataTypeDescriptor[] getTypes(int parameterIndex) 1469 throws SQLException { 1470 1471 DataTypeDescriptor[] types = preparedStatement.getParameterTypes(); 1472 1473 if (types == null) { 1474 throw newSQLException(SQLState.NO_INPUT_PARAMETERS); 1475 } 1476 1477 1478 if (parameterIndex < 1 || 1479 parameterIndex > types.length) { 1480 1481 1482 throw newSQLException(SQLState.LANG_INVALID_PARAM_POSITION, 1483 new Integer (parameterIndex), new Integer (types.length)); 1484 } 1485 return types; 1486 } 1487 1488 1494 protected int getParameterJDBCType(int parameterIndex) 1495 throws SQLException { 1496 1497 DataTypeDescriptor[] types = getTypes(parameterIndex); 1498 1499 int type = types[parameterIndex -1] == null ? 1500 Types.OTHER : 1501 types[parameterIndex - 1].getTypeId().getJDBCTypeId(); 1502 1503 if (SanityManager.DEBUG) { 1504 } 1509 1510 return type; 1511 } 1512 1513 1520 protected final String getParameterSQLType(int parameterIndex) 1521 throws SQLException { 1522 DataTypeDescriptor[] pTypes = getTypes(parameterIndex); 1523 return pTypes[parameterIndex-1].getTypeName(); 1524 } 1525 1526 1533 private void setScale(int parameterIndex, int scale) 1534 throws SQLException 1535 { 1536 checkStatus(); 1537 1538 if (scale < 0) 1539 throw newSQLException(SQLState.BAD_SCALE_VALUE, new Integer (scale)); 1540 1541 try { 1542 1543 ParameterValueSet pvs = getParms(); 1544 1545 1546 DataValueDescriptor value = pvs.getParameter(parameterIndex - 1); 1547 1548 1549 int origvaluelen = value.getLength(); 1550 ((VariableSizeDataValue) 1551 value).setWidth(VariableSizeDataValue.IGNORE_PRECISION, 1552 scale, 1553 false); 1554 1555 if (value.getLength() < origvaluelen) 1556 { 1557 activation.addWarning(StandardException.newWarning(SQLState.LANG_VALUE_TRUNCATED, value.getString())); 1558 } 1559 1560 } catch (StandardException t) { 1561 throw EmbedResultSet.noStateChangeException(t); 1562 } 1563 } 1564 1565 1566 1576 public EngineParameterMetaData getEmbedParameterSetMetaData() 1577 throws SQLException 1578 { 1579 checkExecStatus(); 1580 return new EmbedParameterSetMetaData( 1581 getParms(), preparedStatement.getParameterTypes()); 1582 1583 } 1584 1594 public final void setURL(int parameterIndex, java.net.URL x) 1595 throws SQLException 1596 { 1597 throw Util.notImplemented(); 1598 } 1599 1600 protected EmbedResultSetMetaData newEmbedResultSetMetaData(ResultDescription resultDesc) { 1605 1606 return factory.newEmbedResultSetMetaData(resultDesc.getColumnInfo()); 1607 } 1608 1609 public String toString() { 1610 1611 if (activation != null) 1612 return activation.getPreparedStatement().getObjectName(); 1613 return super.toString(); 1614 } 1615 1616 1619 public void transferParameters(EmbedPreparedStatement newStatement) throws SQLException { 1620 1621 try { 1622 newStatement.activation.setParameters(getParms(), preparedStatement.getParameterTypes()); 1623 } catch (StandardException se) { 1624 throw EmbedResultSet.noStateChangeException(se); 1625 } 1626 } 1627 1628 boolean executeStatement(Activation a, 1629 boolean executeQuery, boolean executeUpdate) 1630 throws SQLException { 1631 1632 checkExecStatus(); 1633 checkIfInMiddleOfBatch(); 1634 clearResultSets(); 1635 return super.executeStatement(a, executeQuery, executeUpdate); 1636 } 1637 1638 final SQLException dataTypeConversion(int column, String sourceType) 1639 throws SQLException { 1640 SQLException se = newSQLException(SQLState.LANG_DATA_TYPE_GET_MISMATCH, getEmbedParameterSetMetaData().getParameterTypeName(column), 1641 sourceType); 1642 return se; 1643 } 1644 1655 public void setBrokeredConnectionControl(BrokeredConnectionControl control) { 1656 bcc = control; 1657 } 1658 1659 1680 1681 private void checkStatementValidity(SQLException sqle) throws SQLException { 1682 1686 if(bcc != null && isClosed()) { 1687 bcc.onStatementErrorOccurred((java.sql.PreparedStatement ) 1690 applicationStatement,sqle); 1691 } 1692 throw sqle; 1693 } 1694 1695 1703 private void checkForSupportedDataType(int dataType) throws SQLException { 1704 1705 1716 switch (dataType) { 1717 case Types.ARRAY: 1718 case JDBC30Translation.DATALINK: 1719 case JDBC40Translation.NCHAR: 1720 case JDBC40Translation.NCLOB: 1721 case JDBC40Translation.NVARCHAR: 1722 case JDBC40Translation.LONGNVARCHAR: 1723 case Types.REF: 1724 case JDBC40Translation.ROWID: 1725 case JDBC40Translation.SQLXML: 1726 case Types.STRUCT: 1727 throw newSQLException(SQLState.DATA_TYPE_NOT_SUPPORTED, 1728 Util.typeName(dataType)); 1729 } 1730 } 1731 1732 1734 1750 public void setAsciiStream(int parameterIndex, InputStream x) 1751 throws SQLException { 1752 checkAsciiStreamConditions(parameterIndex); 1753 java.io.Reader asciiStream = null; 1754 1755 if (x != null) { 1756 try { 1759 asciiStream = new java.io.InputStreamReader (x, "ISO-8859-1"); 1760 } catch (java.io.UnsupportedEncodingException uee) { 1761 throw new SQLException (uee.getMessage()); 1762 } 1763 } 1764 1765 setCharacterStreamInternal(parameterIndex, asciiStream, true, -1); 1766 } 1767 1768 1789 public void setCharacterStream(int parameterIndex, Reader reader) 1790 throws SQLException { 1791 checkCharacterStreamConditions(parameterIndex); 1792 setCharacterStreamInternal(parameterIndex, reader, 1793 true, -1); 1794 } 1795 1796 1813 public void setClob(int parameterIndex, Reader reader) 1814 throws SQLException { 1815 checkClobConditions(parameterIndex); 1816 setCharacterStreamInternal(parameterIndex, reader, true, -1); 1817 } 1818 1819 1829 1830 1831 public void setClob(int parameterIndex, Reader reader, long length) 1832 throws SQLException { 1833 checkClobConditions(parameterIndex); 1834 setCharacterStreamInternal(parameterIndex, reader, false, length); 1835 } 1836 1837 1855 public void setBlob(int parameterIndex, InputStream inputStream) 1856 throws SQLException { 1857 checkBlobConditions(parameterIndex); 1858 setBinaryStreamInternal(parameterIndex, inputStream, true, -1); 1859 } 1860 1861 1874 1875 1876 public void setBlob(int parameterIndex, InputStream inputStream, long length) 1877 throws SQLException { 1878 checkBlobConditions(parameterIndex); 1879 setBinaryStreamInternal(parameterIndex, inputStream, false, length); 1880 } 1881 1882 1887 private final void checkBlobConditions(int parameterIndex) 1888 throws SQLException { 1889 checkStatus(); 1890 if (getParameterJDBCType(parameterIndex) != Types.BLOB) { 1891 throw dataTypeConversion(parameterIndex, "java.sql.Blob"); 1892 } 1893 } 1894} 1895 | Popular Tags |