KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > am > PreparedStatement


1 /*
2
3    Derby - Class org.apache.derby.client.am.PreparedStatement
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20 */

21
22 package org.apache.derby.client.am;
23
24 import org.apache.derby.shared.common.reference.JDBC40Translation;
25 import org.apache.derby.shared.common.reference.SQLState;
26
27 import java.io.InputStream JavaDoc;
28 import java.io.Reader JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import org.apache.derby.client.ClientPooledConnection;
32 import org.apache.derby.jdbc.ClientDriver;
33
34 public class PreparedStatement extends Statement
35         implements java.sql.PreparedStatement JavaDoc,
36         PreparedStatementCallbackInterface {
37     //---------------------navigational cheat-links-------------------------------
38
// Cheat-links are for convenience only, and are not part of the conceptual model.
39
// Warning:
40
// Cheat-links should only be defined for invariant state data.
41
// That is, the state data is set by the constructor and never changes.
42

43     // Alias for downcast (MaterialPreparedStatementProxy) super.materialStatement.
44
public MaterialPreparedStatement materialPreparedStatement_ = null;
45
46     //-----------------------------state------------------------------------------
47

48     public String JavaDoc sql_;
49
50     // This variable is only used by Batch.
51
// True if a call sql statement has an OUT or INOUT parameter registered.
52
public boolean outputRegistered_ = false;
53
54     // Parameter inputs are cached as objects so they may be sent on execute()
55
public Object JavaDoc[] parameters_;
56
57     boolean[] parameterSet_;
58     boolean[] parameterRegistered_;
59     
60     void setInput(int parameterIndex, Object JavaDoc input) {
61         parameters_[parameterIndex - 1] = input;
62         parameterSet_[parameterIndex - 1] = true;
63     }
64
65     public ColumnMetaData parameterMetaData_; // type information for input sqlda
66

67     private ArrayList JavaDoc parameterTypeList;
68
69
70     // The problem with storing the scrollable ResultSet associated with cursorName in scrollableRS_ is
71
// that when the PreparedStatement is re-executed, it has a new ResultSet, however, we always do
72
// the reposition on the ResultSet that was stored in scrollableRS_, and we never update scrollableRS_
73
// when PreparedStatement is re-execute. So the new ResultSet that needs to be repositioned never
74
// gets repositioned.
75
// So instead of caching the scrollableRS_, we will cache the cursorName. And re-retrieve the scrollable
76
// result set from the map using this cursorName every time the PreparedStatement excutes.
77
String JavaDoc positionedUpdateCursorName_ = null;
78     
79     // the ClientPooledConnection object used to notify of the events that occur
80
// on this prepared statement object
81
protected final ClientPooledConnection pooledConnection_;
82
83
84     private void initPreparedStatement() {
85         materialPreparedStatement_ = null;
86         sql_ = null;
87         outputRegistered_ = false;
88         parameters_ = null;
89         parameterSet_ = null;
90         parameterRegistered_ = null;
91         parameterMetaData_ = null;
92         parameterTypeList = null;
93         isAutoCommittableStatement_ = true;
94         isPreparedStatement_ = true;
95     }
96
97     protected void initResetPreparedStatement() {
98         outputRegistered_ = false;
99         isPreparedStatement_ = true;
100
101         if (parameterMetaData_ != null) {
102             resetParameters();
103         }
104     }
105
106     public void reset(boolean fullReset) throws SqlException {
107         if (fullReset) {
108             connection_.resetPrepareStatement(this);
109         } else {
110             super.initResetPreparedStatement();
111             initResetPreparedStatement();
112         }
113     }
114
115     private void resetParameters() {
116         for (int i = 0; i < parameterMetaData_.columns_; i++) {
117             parameters_[i] = null;
118             parameterSet_[i] = false;
119             parameterRegistered_[i] = false;
120         }
121     }
122
123     /**
124      *
125      * The PreparedStatement constructor used for JDBC 2 positioned update
126      * statements. Called by material statement constructors.
127      * It has the ClientPooledConnection as one of its parameters
128      * this is used to raise the Statement Events when the prepared
129      * statement is closed
130      *
131      * @param agent The instance of NetAgent associated with this
132      * CallableStatement object.
133      * @param connection The connection object associated with this
134      * PreparedStatement Object.
135      * @param sql A String object that is the SQL statement to be sent
136      * to the database.
137      * @param section Section
138      * @param cpc The ClientPooledConnection wraps the underlying physical
139      * connection associated with this prepared statement.
140      * It is used to pass the Statement closed and the Statement
141      * error occurred events that occur back to the
142      * ClientPooledConnection.
143      * @throws SqlException
144      *
145      */

146
147     public PreparedStatement(Agent agent,
148                              Connection connection,
149                              String JavaDoc sql,
150                              Section section,ClientPooledConnection cpc)
151                              throws SqlException {
152         super(agent, connection);
153         // PreparedStatement is poolable by default
154
isPoolable = true;
155         initPreparedStatement(sql, section);
156         pooledConnection_ = cpc;
157     }
158     
159     public void resetPreparedStatement(Agent agent,
160                                        Connection connection,
161                                        String JavaDoc sql,
162                                        Section section) throws SqlException {
163         super.resetStatement(agent, connection);
164         initPreparedStatement();
165         initPreparedStatement(sql, section);
166     }
167
168     private void initPreparedStatement(String JavaDoc sql, Section section) throws SqlException {
169         sql_ = sql;
170         isPreparedStatement_ = true;
171
172         parseSqlAndSetSqlModes(sql_);
173         section_ = section;
174     }
175
176     /**
177      * The PreparedStatementConstructor used for jdbc 2 prepared statements
178      * with scroll attributes. Called by material statement constructors.
179      * It has the ClientPooledConnection as one of its parameters
180      * this is used to raise the Statement Events when the prepared
181      * statement is closed
182      *
183      * @param agent The instance of NetAgent associated with this
184      * CallableStatement object.
185      * @param connection The connection object associated with this
186      * PreparedStatement Object.
187      * @param sql A String object that is the SQL statement
188      * to be sent to the database.
189      * @param type One of the ResultSet type constants.
190      * @param concurrency One of the ResultSet concurrency constants.
191      * @param holdability One of the ResultSet holdability constants.
192      * @param autoGeneratedKeys a flag indicating whether auto-generated
193      * keys should be returned.
194      * @param columnNames an array of column names indicating the columns that
195      * should be returned from the inserted row or rows.
196      * @param cpc The ClientPooledConnection wraps the underlying physical
197      * connection associated with this prepared statement
198      * it is used to pass the Statement closed and the Statement
199      * error occurred events that occur back to the
200      * ClientPooledConnection.
201      * @throws SqlException
202      */

203     public PreparedStatement(Agent agent,
204                              Connection connection,
205                              String JavaDoc sql,
206                              int type, int concurrency, int holdability,
207                              int autoGeneratedKeys, String JavaDoc[] columnNames,
208                              ClientPooledConnection cpc)
209                              throws SqlException {
210         super(agent, connection, type, concurrency, holdability,
211               autoGeneratedKeys, columnNames);
212         // PreparedStatement is poolable by default
213
isPoolable = true;
214         initPreparedStatement(sql);
215         pooledConnection_ = cpc;
216     }
217
218
219     public void resetPreparedStatement(Agent agent,
220                                        Connection connection,
221                                        String JavaDoc sql,
222                                        int type, int concurrency, int holdability, int autoGeneratedKeys, String JavaDoc[] columnNames) throws SqlException {
223         super.resetStatement(agent, connection, type, concurrency, holdability, autoGeneratedKeys, columnNames);
224         initPreparedStatement();
225         initPreparedStatement(sql);
226     }
227
228     private void initPreparedStatement(String JavaDoc sql) throws SqlException {
229         sql_ = super.escape(sql);
230         parseSqlAndSetSqlModes(sql_);
231         isPreparedStatement_ = true;
232
233         // Check for positioned update statement and assign a section from the
234
// same package as the corresponding query section.
235
// Scan the sql for an "update...where current of <cursor-name>".
236
String JavaDoc cursorName = null;
237         if (sqlUpdateMode_ == isDeleteSql__ || sqlUpdateMode_ == isUpdateSql__) {
238             String JavaDoc[] sqlAndCursorName = extractCursorNameFromWhereCurrentOf(sql_);
239             if (sqlAndCursorName != null) {
240                 cursorName = sqlAndCursorName[0];
241                 sql_ = sqlAndCursorName[1];
242             }
243         }
244         if (cursorName != null) {
245             positionedUpdateCursorName_ = cursorName;
246             // Get a new section from the same package as the query section
247
section_ = agent_.sectionManager_.getPositionedUpdateSection(cursorName, false); // false means get a regular section
248

249             if (section_ == null) {
250                 throw new SqlException(agent_.logWriter_,
251                     new ClientMessageId(SQLState.CURSOR_INVALID_CURSOR_NAME), cursorName);
252             }
253
254             //scrollableRS_ = agent_.sectionManager_.getPositionedUpdateResultSet (cursorName);
255

256             // if client's cursor name is set, and the cursor name in the positioned update
257
// string is the same as the client's cursor name, replace client's cursor name
258
// with the server's cursor name.
259
// if the cursor name supplied in the sql string is different from the cursorName
260
// set by setCursorName(), then server will return "cursor name not defined" error,
261
// and no subsititution is made here.
262
if (section_.getClientCursorName() != null && // cursor name is user defined
263
cursorName.compareTo(section_.getClientCursorName()) == 0)
264             // client's cursor name is substituted with section's server cursor name
265
{
266                 sql_ = substituteClientCursorNameWithServerCursorName(sql_, section_);
267             }
268         } else {
269             // We don't need to analyze the sql text to determine if it is a query or not.
270
// This is up to the server to decide, we just pass thru the sql on flowPrepare().
271
section_ = agent_.sectionManager_.getDynamicSection(resultSetHoldability_);
272         }
273     }
274
275     public void resetPreparedStatement(Agent agent,
276                                        Connection connection,
277                                        String JavaDoc sql,
278                                        Section section,
279                                        ColumnMetaData parameterMetaData,
280                                        ColumnMetaData resultSetMetaData) throws SqlException {
281         resetPreparedStatement(agent, connection, sql, section);
282         initPreparedStatement(parameterMetaData, resultSetMetaData);
283     }
284
285     private void initPreparedStatement(ColumnMetaData parameterMetaData,
286                                        ColumnMetaData resultSetMetaData) throws SqlException {
287         isPreparedStatement_ = true;
288         parameterMetaData_ = parameterMetaData;
289         resultSetMetaData_ = resultSetMetaData;
290         if (parameterMetaData_ != null) {
291             parameters_ = new Object JavaDoc[parameterMetaData_.columns_];
292             //parameterSetOrRegistered_ = new boolean[parameterMetaData_.columns_];
293
parameterSet_ = new boolean[parameterMetaData_.columns_];
294             parameterRegistered_ = new boolean[parameterMetaData_.columns_];
295         }
296     }
297
298     // called immediately after the constructor by Connection prepare*() methods
299
void prepare() throws SqlException {
300         try {
301             // flow prepare, no static initialization is needed
302
// already checked if columnNames is not null and server supports select from insert
303
// in prepareStatementX()
304
if (sqlUpdateMode_ == isInsertSql__ && generatedKeysColumnNames_ != null) {
305                 flowPrepareForSelectFromInsert();
306             } else {
307                 flowPrepareDescribeInputOutput();
308             }
309         } catch (SqlException e) {
310             this.markClosed();
311             throw e;
312         }
313     }
314
315
316     //------------------- Prohibited overrides from Statement --------------------
317

318     public boolean execute(String JavaDoc sql) throws SQLException JavaDoc {
319         if (agent_.loggingEnabled()) {
320             agent_.logWriter_.traceEntry(this, "execute", sql);
321         }
322         throw new SqlException(agent_.logWriter_,
323             new ClientMessageId(SQLState.NOT_FOR_PREPARED_STATEMENT),
324             "execute(String)").getSQLException();
325     }
326
327     public java.sql.ResultSet JavaDoc executeQuery(String JavaDoc sql) throws SQLException JavaDoc {
328         if (agent_.loggingEnabled()) {
329             agent_.logWriter_.traceEntry(this, "executeQuery", sql);
330         }
331         throw new SqlException(agent_.logWriter_,
332             new ClientMessageId(SQLState.NOT_FOR_PREPARED_STATEMENT),
333             "executeQuery(String)").getSQLException();
334     }
335
336     public int executeUpdate(String JavaDoc sql) throws SQLException JavaDoc {
337         if (agent_.loggingEnabled()) {
338             agent_.logWriter_.traceEntry(this, "executeUpdate", sql);
339         }
340         throw new SqlException(agent_.logWriter_,
341             new ClientMessageId(SQLState.NOT_FOR_PREPARED_STATEMENT),
342             "executeUpdate(String)").getSQLException();
343     }
344     // ---------------------------jdbc 1------------------------------------------
345

346     public java.sql.ResultSet JavaDoc executeQuery() throws SQLException JavaDoc {
347         try
348         {
349             synchronized (connection_) {
350                 if (agent_.loggingEnabled()) {
351                     agent_.logWriter_.traceEntry(this, "executeQuery");
352                 }
353                 ResultSet resultSet = executeQueryX();
354                 if (agent_.loggingEnabled()) {
355                     agent_.logWriter_.traceExit(this, "executeQuery", resultSet);
356                 }
357                 return resultSet;
358             }
359         }
360         catch ( SqlException se ) {
361             checkStatementValidity(se);
362             throw se.getSQLException();
363         }
364     }
365
366     // also called by some DBMD methods
367
ResultSet executeQueryX() throws SqlException {
368         flowExecute(executeQueryMethod__);
369         return resultSet_;
370     }
371
372
373     public int executeUpdate() throws SQLException JavaDoc {
374         try
375         {
376             synchronized (connection_) {
377                 if (agent_.loggingEnabled()) {
378                     agent_.logWriter_.traceEntry(this, "executeUpdate");
379                 }
380                 int updateValue = executeUpdateX();
381                 if (agent_.loggingEnabled()) {
382                     agent_.logWriter_.traceExit(this, "executeUpdate", updateValue);
383                 }
384                 return updateValue;
385             }
386         }
387         catch ( SqlException se ) {
388             checkStatementValidity(se);
389             throw se.getSQLException();
390         }
391     }
392
393     private int executeUpdateX() throws SqlException {
394         flowExecute(executeUpdateMethod__);
395         return updateCount_;
396     }
397
398     public void setNull(int parameterIndex, int jdbcType) throws SQLException JavaDoc {
399         try
400         {
401             synchronized (connection_) {
402                 if (agent_.loggingEnabled()) {
403                     agent_.logWriter_.traceEntry(this, "setNull", parameterIndex, jdbcType);
404                 }
405                 setNullX(parameterIndex, jdbcType);
406             }
407         }
408         catch ( SqlException se )
409         {
410             throw se.getSQLException();
411         }
412     }
413
414     // also used by DBMD methods
415
void setNullX(int parameterIndex, int jdbcType) throws SqlException {
416         checkForSupportedDataType(jdbcType);
417         super.checkForClosedStatement(); // investigate what can be pushed up to setNull
418
parameterIndex = checkSetterPreconditions(parameterIndex);
419         parameterMetaData_.clientParamtertype_[parameterIndex - 1] = jdbcType;
420
421         if (!parameterMetaData_.nullable_[parameterIndex - 1]) {
422             throw new SqlException(agent_.logWriter_,
423                 new ClientMessageId(SQLState.LANG_NULL_INTO_NON_NULL),
424                 new Integer JavaDoc(parameterIndex));
425         }
426         setInput(parameterIndex, null);
427     }
428
429     public void setNull(int parameterIndex, int jdbcType, String JavaDoc typeName) throws SQLException JavaDoc {
430         try
431         {
432             synchronized (connection_) {
433                 if (agent_.loggingEnabled()) {
434                     agent_.logWriter_.traceEntry(this, "setNull", parameterIndex, jdbcType, typeName);
435                 }
436                 super.checkForClosedStatement();
437                 setNull(parameterIndex, jdbcType);
438             }
439         }
440         catch ( SqlException se )
441         {
442             throw se.getSQLException();
443         }
444     }
445
446     public void setBoolean(int parameterIndex, boolean x) throws SQLException JavaDoc {
447         try
448         {
449             synchronized (connection_) {
450                 if (agent_.loggingEnabled()) {
451                     agent_.logWriter_.traceEntry(this, "setBoolean", parameterIndex, x);
452                 }
453                 parameterIndex = checkSetterPreconditions(parameterIndex);
454                 parameterMetaData_.clientParamtertype_[parameterIndex - 1] = java.sql.Types.BIT;
455                 setInput(parameterIndex, new Short JavaDoc((short) (x ? 1 : 0)));
456             }
457         }
458         catch ( SqlException se )
459         {
460             throw se.getSQLException();
461         }
462     }
463
464     public void setByte(int parameterIndex, byte x) throws SQLException JavaDoc {
465         try
466         {
467             synchronized (connection_) {
468                 if (agent_.loggingEnabled()) {
469                     agent_.logWriter_.traceEntry(this, "setByte", parameterIndex, x);
470                 }
471                 parameterIndex = checkSetterPreconditions(parameterIndex);
472                 parameterMetaData_.clientParamtertype_[parameterIndex - 1] = java.sql.Types.TINYINT;
473                 setInput(parameterIndex, new Short JavaDoc(x));
474             }
475         }
476         catch ( SqlException se )
477         {
478             throw se.getSQLException();
479         }
480     }
481
482     public void setShort(int parameterIndex, short x) throws SQLException JavaDoc {
483         try
484         {
485             synchronized (connection_) {
486                 if (agent_.loggingEnabled()) {
487                     agent_.logWriter_.traceEntry(this, "setShort", parameterIndex, x);
488                 }
489                 setShortX(parameterIndex, x);
490             }
491         }
492         catch ( SqlException se )
493         {
494             throw se.getSQLException();
495         }
496     }
497
498     // also used by DBMD methods
499
void setShortX(int parameterIndex, short x) throws SqlException {
500         parameterIndex = checkSetterPreconditions(parameterIndex);
501         parameterMetaData_.clientParamtertype_[parameterIndex - 1] = java.sql.Types.SMALLINT;
502         setInput(parameterIndex, new Short JavaDoc(x));
503
504     }
505
506     public void setInt(int parameterIndex, int x) throws SQLException JavaDoc {
507         try
508         {
509             synchronized (connection_) {
510                 if (agent_.loggingEnabled()) {
511                     agent_.logWriter_.traceEntry(this, "setInt", parameterIndex, x);
512                 }
513                 setIntX(parameterIndex, x);
514             }
515         }
516         catch ( SqlException se )
517         {
518             throw se.getSQLException();
519         }
520     }
521
522     // also used by DBMD methods
523
void setIntX(int parameterIndex, int x) throws SqlException {
524         parameterIndex = checkSetterPreconditions(parameterIndex);
525         parameterMetaData_.clientParamtertype_[parameterIndex - 1] = java.sql.Types.INTEGER;
526         setInput(parameterIndex, new Integer JavaDoc(x));
527     }
528
529
530     public void setLong(int parameterIndex, long x) throws SQLException JavaDoc {
531         try
532         {
533             synchronized (connection_) {
534                 if (agent_.loggingEnabled()) {
535                     agent_.logWriter_.traceEntry(this, "setLong", parameterIndex, x);
536                 }
537                 parameterIndex = checkSetterPreconditions(parameterIndex);
538                 parameterMetaData_.clientParamtertype_[parameterIndex - 1] = java.sql.Types.BIGINT;
539                 setInput(parameterIndex, new Long JavaDoc(x));
540             }
541         }
542         catch ( SqlException se )
543         {
544             throw se.getSQLException();
545         }
546     }
547
548     public void setFloat(int parameterIndex, float x) throws SQLException JavaDoc {
549         try
550         {
551             synchronized (connection_) {
552                 if (agent_.loggingEnabled()) {
553                     agent_.logWriter_.traceEntry(this, "setFloat", parameterIndex, x);
554                 }
555                 parameterIndex = checkSetterPreconditions(parameterIndex);
556                 parameterMetaData_.clientParamtertype_[parameterIndex - 1] = java.sql.Types.REAL;
557                 setInput(parameterIndex, new Float JavaDoc(x));
558             }
559         }
560         catch ( SqlException se )
561         {
562             throw se.getSQLException();
563         }
564     }
565
566     public void setDouble(int parameterIndex, double x) throws SQLException JavaDoc {
567         try
568         {
569             synchronized (connection_) {
570                 if (agent_.loggingEnabled()) {
571                     agent_.logWriter_.traceEntry(this, "setDouble", parameterIndex, x);
572                 }
573                 parameterIndex = checkSetterPreconditions(parameterIndex);
574                 parameterMetaData_.clientParamtertype_[parameterIndex - 1] = java.sql.Types.DOUBLE;
575                 setInput(parameterIndex, new Double JavaDoc(x));
576             }
577         }
578         catch ( SqlException se )
579         {
580             throw se.getSQLException();
581         }
582     }
583
584     public void setBigDecimal(int parameterIndex, java.math.BigDecimal JavaDoc x) throws SQLException JavaDoc {
585         try
586         {
587             synchronized (connection_) {
588                 if (agent_.loggingEnabled()) {
589                     agent_.logWriter_.traceEntry(this, "setBigDecimal", parameterIndex, x);
590                 }
591                 parameterIndex = checkSetterPreconditions(parameterIndex);
592                 parameterMetaData_.clientParamtertype_[parameterIndex - 1] = java.sql.Types.DECIMAL;
593                 if (x == null) {
594                     setNull(parameterIndex, java.sql.Types.DECIMAL);
595                     return;
596                 }
597                 int registerOutScale = 0;
598                 setInput(parameterIndex, x);
599             }
600         }
601         catch ( SqlException se )
602         {
603             throw se.getSQLException();
604         }
605     }
606
607     public void setDate(int parameterIndex, java.sql.Date JavaDoc x) throws SQLException JavaDoc {
608         try
609         {
610             synchronized (connection_) {
611                 if (agent_.loggingEnabled()) {
612                     agent_.logWriter_.traceEntry(this, "setDate", parameterIndex, x);
613                 }
614                 checkForClosedStatement();
615                 parameterIndex = checkSetterPreconditions(parameterIndex);
616                 parameterMetaData_.clientParamtertype_[parameterIndex - 1] = java.sql.Types.DATE;
617                 if (x == null) {
618                     setNull(parameterIndex, java.sql.Types.DATE);
619                     return;
620                 }
621                 setInput(parameterIndex, x);
622             }
623         }
624         catch ( SqlException se )
625         {
626             throw se.getSQLException();
627         }
628     }
629
630     public void setDate(int parameterIndex,
631                         java.sql.Date JavaDoc x,
632                         java.util.Calendar JavaDoc calendar) throws SQLException JavaDoc {
633         try
634         {
635             synchronized (connection_) {
636                 if (agent_.loggingEnabled()) {
637                     agent_.logWriter_.traceEntry(this, "setDate", parameterIndex, x, calendar);
638                 }
639                 checkForClosedStatement();
640                 if (calendar == null) {
641                     throw new SqlException(agent_.logWriter_,
642                         new ClientMessageId(SQLState.INVALID_API_PARAMETER),
643                         "null", "calendar", "setDate");
644                 }
645                 java.util.Calendar JavaDoc targetCalendar = java.util.Calendar.getInstance(calendar.getTimeZone());
646                 targetCalendar.clear();
647                 targetCalendar.setTime(x);
648                 java.util.Calendar JavaDoc defaultCalendar = java.util.Calendar.getInstance();
649                 defaultCalendar.clear();
650                 defaultCalendar.setTime(x);
651                 long timeZoneOffset =
652                         targetCalendar.get(java.util.Calendar.ZONE_OFFSET) - defaultCalendar.get(java.util.Calendar.ZONE_OFFSET) +
653                         targetCalendar.get(java.util.Calendar.DST_OFFSET) - defaultCalendar.get(java.util.Calendar.DST_OFFSET);
654                 java.sql.Date JavaDoc adjustedDate = ((timeZoneOffset == 0) || (x == null)) ? x : new java.sql.Date JavaDoc(x.getTime() + timeZoneOffset);
655                 setDate(parameterIndex, adjustedDate);
656             }
657         }
658         catch ( SqlException se )
659         {
660             throw se.getSQLException();
661         }
662     }
663
664     public void setTime(int parameterIndex, java.sql.Time JavaDoc x) throws SQLException JavaDoc {
665         try
666         {
667             synchronized (connection_) {
668                 if (agent_.loggingEnabled()) {
669                     agent_.logWriter_.traceEntry(this, "setTime", parameterIndex, x);
670                 }
671                 parameterIndex = checkSetterPreconditions(parameterIndex);
672                 parameterMetaData_.clientParamtertype_[parameterIndex - 1] = java.sql.Types.TIME;
673                 if (x == null) {
674                     setNull(parameterIndex, java.sql.Types.TIME);
675                     return;
676                 }
677                 setInput(parameterIndex, x);
678
679             }
680         }
681         catch ( SqlException se )
682         {
683             throw se.getSQLException();
684         }
685     }
686
687     public void setTime(int parameterIndex,
688                         java.sql.Time JavaDoc x,
689                         java.util.Calendar JavaDoc calendar) throws SQLException JavaDoc {
690         try
691         {
692             synchronized (connection_) {
693                 if (agent_.loggingEnabled()) {
694                     agent_.logWriter_.traceEntry(this, "setTime", parameterIndex, x, calendar);
695                 }
696                 checkForClosedStatement();
697                 if (calendar == null) {
698                     throw new SqlException(agent_.logWriter_,
699                         new ClientMessageId(SQLState.INVALID_API_PARAMETER),
700                         "null", "calendar", "setTime()");
701                 }
702                 java.util.Calendar JavaDoc targetCalendar = java.util.Calendar.getInstance(calendar.getTimeZone());
703                 targetCalendar.clear();
704                 targetCalendar.setTime(x);
705                 java.util.Calendar JavaDoc defaultCalendar = java.util.Calendar.getInstance();
706                 defaultCalendar.clear();
707                 defaultCalendar.setTime(x);
708                 long timeZoneOffset =
709                         targetCalendar.get(java.util.Calendar.ZONE_OFFSET) - defaultCalendar.get(java.util.Calendar.ZONE_OFFSET) +
710                         targetCalendar.get(java.util.Calendar.DST_OFFSET) - defaultCalendar.get(java.util.Calendar.DST_OFFSET);
711                 java.sql.Time JavaDoc adjustedTime = ((timeZoneOffset == 0) || (x == null)) ? x : new java.sql.Time JavaDoc(x.getTime() + timeZoneOffset);
712                 setTime(parameterIndex, adjustedTime);
713             }
714         }
715         catch ( SqlException se )
716         {
717             throw se.getSQLException();
718         }
719     }
720
721     public void setTimestamp(int parameterIndex, java.sql.Timestamp JavaDoc x) throws SQLException JavaDoc {
722         try
723         {
724             synchronized (connection_) {
725                 if (agent_.loggingEnabled()) {
726                     agent_.logWriter_.traceEntry(this, "setTimestamp", parameterIndex, x);
727                 }
728                 parameterIndex = checkSetterPreconditions(parameterIndex);
729                 parameterMetaData_.clientParamtertype_[parameterIndex - 1] = java.sql.Types.TIMESTAMP;
730
731                 if (x == null) {
732                     setNull(parameterIndex, java.sql.Types.TIMESTAMP);
733                     return;
734                 }
735                 setInput(parameterIndex, x);
736                 // once the nanosecond field of timestamp is trim to microsecond for DERBY, should we throw a warning
737
//if (getParameterType (parameterIndex) == java.sql.Types.TIMESTAMP && x.getNanos() % 1000 != 0)
738
// accumulateWarning (new SqlWarning (agent_.logWriter_, "DERBY timestamp can only store up to microsecond, conversion from nanosecond to microsecond causes rounding."));
739
}
740         }
741         catch ( SqlException se )
742         {
743             throw se.getSQLException();
744         }
745     }
746
747     public void setTimestamp(int parameterIndex,
748                              java.sql.Timestamp JavaDoc x,
749                              java.util.Calendar JavaDoc calendar) throws SQLException JavaDoc {
750         try
751         {
752             synchronized (connection_) {
753                 if (agent_.loggingEnabled()) {
754                     agent_.logWriter_.traceEntry(this, "setTimestamp", parameterIndex, x, calendar);
755                 }
756                 checkForClosedStatement();
757                 if (calendar == null) {
758                     throw new SqlException(agent_.logWriter_,
759                         new ClientMessageId(SQLState.INVALID_API_PARAMETER),
760                         "null", "calendar", "setTimestamp()");
761                 }
762                 java.util.Calendar JavaDoc targetCalendar = java.util.Calendar.getInstance(calendar.getTimeZone());
763                 targetCalendar.clear();
764                 targetCalendar.setTime(x);
765                 java.util.Calendar JavaDoc defaultCalendar = java.util.Calendar.getInstance();
766                 defaultCalendar.clear();
767                 defaultCalendar.setTime(x);
768                 long timeZoneOffset =
769                         targetCalendar.get(java.util.Calendar.ZONE_OFFSET) - defaultCalendar.get(java.util.Calendar.ZONE_OFFSET) +
770                         targetCalendar.get(java.util.Calendar.DST_OFFSET) - defaultCalendar.get(java.util.Calendar.DST_OFFSET);
771                 java.sql.Timestamp JavaDoc adjustedTimestamp = ((timeZoneOffset == 0) || (x == null)) ? x : new java.sql.Timestamp JavaDoc(x.getTime() + timeZoneOffset);
772                 if (x != null) {
773                     adjustedTimestamp.setNanos(x.getNanos());
774                 }
775                 setTimestamp(parameterIndex, adjustedTimestamp);
776             }
777         }
778         catch ( SqlException se )
779         {
780             throw se.getSQLException();
781         }
782     }
783
784     public void setString(int parameterIndex, String JavaDoc x) throws SQLException JavaDoc {
785         try
786         {
787             synchronized (connection_) {
788                 if (agent_.loggingEnabled()) {
789                     agent_.logWriter_.traceEntry(this, "setString", parameterIndex, x);
790                 }
791                 setStringX(parameterIndex, x);
792             }
793         }
794         catch ( SqlException se )
795         {
796             throw se.getSQLException();
797         }
798     }
799
800     // also used by DBMD methods
801
void setStringX(int parameterIndex, String JavaDoc x) throws SqlException {
802         parameterIndex = checkSetterPreconditions(parameterIndex);
803         parameterMetaData_.clientParamtertype_[parameterIndex - 1] = java.sql.Types.LONGVARCHAR;
804         if (x == null) {
805             setNullX(parameterIndex, java.sql.Types.LONGVARCHAR);
806             return;
807         }
808         setInput(parameterIndex, x);
809     }
810
811     public void setBytes(int parameterIndex, byte[] x) throws SQLException JavaDoc {
812         try
813         {
814             synchronized (connection_) {
815                 if (agent_.loggingEnabled()) {
816                     agent_.logWriter_.traceEntry(this, "setBytes", parameterIndex, x);
817                 }
818                 setBytesX(parameterIndex, x);
819             }
820         }
821         catch ( SqlException se )
822         {
823             throw se.getSQLException();
824         }
825     }
826
827     // also used by BLOB
828
public void setBytesX(int parameterIndex, byte[] x) throws SqlException {
829         parameterIndex = checkSetterPreconditions(parameterIndex);
830         parameterMetaData_.clientParamtertype_[parameterIndex - 1] = java.sql.Types.LONGVARBINARY;
831         if (x == null) {
832             setNullX(parameterIndex, java.sql.Types.LONGVARBINARY);
833             return;
834         }
835         setInput(parameterIndex, x);
836
837     }
838
839     /**
840      * sets the parameter to the Binary Stream object
841      *
842      * @param parameterIndex the first parameter is 1, the second is 2, ...
843      * @param x the java input stream which contains the binary parameter value
844      * @param length the number of bytes in the stream
845      * @exception SQLException thrown on failure.
846      */

847
848     public void setBinaryStream(int parameterIndex,
849                                 java.io.InputStream JavaDoc x,
850                                 long length) throws SQLException JavaDoc {
851         try
852         {
853             synchronized (connection_) {
854                 if (agent_.loggingEnabled()) {
855                     agent_.logWriter_.traceEntry(this, "setBinaryStream", parameterIndex, "<input stream>", new Long JavaDoc(length));
856                 }
857                  if(length > Integer.MAX_VALUE) {
858                     throw new SqlException(agent_.logWriter_,
859                         new ClientMessageId(SQLState.CLIENT_LENGTH_OUTSIDE_RANGE_FOR_DATATYPE),
860                         new Long JavaDoc(length), new Integer JavaDoc(Integer.MAX_VALUE)).getSQLException();
861                 }
862                 setBinaryStreamX(parameterIndex, x, (int)length);
863             }
864         }
865         catch ( SqlException se )
866         {
867             throw se.getSQLException();
868         }
869     }
870
871     /**
872      * sets the parameter to the Binary Stream object
873      *
874      * @param parameterIndex the first parameter is 1, the second is 2, ...
875      * @param x the java input stream which contains the binary parameter value
876      * @param length the number of bytes in the stream
877      * @exception SQLException thrown on failure.
878      */

879
880     public void setBinaryStream(int parameterIndex,
881                                 java.io.InputStream JavaDoc x,
882                                 int length) throws SQLException JavaDoc {
883         setBinaryStream(parameterIndex,x,(long)length);
884     }
885
886     protected void setBinaryStreamX(int parameterIndex,
887                                  java.io.InputStream JavaDoc x,
888                                  int length) throws SqlException {
889         parameterIndex = checkSetterPreconditions(parameterIndex);
890         parameterMetaData_.clientParamtertype_[parameterIndex - 1] = java.sql.Types.BLOB;
891         if (x == null) {
892             setNullX(parameterIndex, java.sql.Types.BLOB);
893             return;
894         }
895         Blob blob;
896         if (length == -1) {
897             // Create a blob of unknown length. This might cause an
898
// OutOfMemoryError due to the temporary implementation in Blob.
899
// The whole stream will be materialzied. See comments in Blob.
900
blob = new Blob(agent_, x);
901         } else {
902             blob = new Blob(agent_, x, length);
903         }
904         setInput(parameterIndex, blob);
905     }
906
907     /**
908      * We do this inefficiently and read it all in here. The target type
909      * is assumed to be a String.
910      *
911      * @param parameterIndex the first parameter is 1, the second is 2, ...
912      * @param x the java input stream which contains the ASCII parameter value
913      * @param length the number of bytes in the stream
914      * @exception SQLException thrown on failure.
915      */

916
917     public void setAsciiStream(int parameterIndex,
918                                java.io.InputStream JavaDoc x,
919                                long length) throws SQLException JavaDoc {
920         try
921         {
922             synchronized (connection_) {
923                 if (agent_.loggingEnabled()) {
924                     agent_.logWriter_.traceEntry(this, "setAsciiStream", parameterIndex, "<input stream>", new Long JavaDoc(length));
925                 }
926                 parameterIndex = checkSetterPreconditions(parameterIndex);
927                 parameterMetaData_.clientParamtertype_[parameterIndex - 1] = java.sql.Types.CLOB;
928                 if (x == null) {
929                     setNull(parameterIndex, java.sql.Types.CLOB);
930                     return;
931                 }
932                 if(length > Integer.MAX_VALUE) {
933                     throw new SqlException(agent_.logWriter_,
934                         new ClientMessageId(SQLState.CLIENT_LENGTH_OUTSIDE_RANGE_FOR_DATATYPE),
935                         new Long JavaDoc(length), new Integer JavaDoc(Integer.MAX_VALUE)).getSQLException();
936                 }
937                 setInput(parameterIndex, new Clob(agent_, x, "US-ASCII", (int)length));
938             }
939         }
940         catch ( SqlException se )
941         {
942             throw se.getSQLException();
943         }
944     }
945
946     /**
947      * We do this inefficiently and read it all in here. The target type
948      * is assumed to be a String.
949      *
950      * @param parameterIndex the first parameter is 1, the second is 2, ...
951      * @param x the java input stream which contains the ASCII parameter value
952      * @param length the number of bytes in the stream
953      * @exception SQLException thrown on failure.
954      */

955     public void setAsciiStream(int parameterIndex,
956                                java.io.InputStream JavaDoc x,
957                                int length) throws SQLException JavaDoc {
958         setAsciiStream(parameterIndex,x,(long)length);
959     }
960
961     /**
962      * Sets the specified parameter to the given input stream. Deprecated
963      * in JDBC 3.0 and this method will always just throw a feature not
964      * implemented exception.
965      *
966      * @param parameterIndex the first parameter is 1, the second is 2, ...
967      * @param x the java input stream which contains the UNICODE parameter
968      * value
969      * @param length the number of bytes in the stream
970      * @exception SQLException throws feature not implemented.
971      */

972     public void setUnicodeStream(int parameterIndex,
973                                  java.io.InputStream JavaDoc x,
974                                  int length) throws SQLException JavaDoc {
975         if (agent_.loggingEnabled()) {
976             agent_.logWriter_.traceDeprecatedEntry(this, "setUnicodeStream",
977                                                    parameterIndex,
978                                                    "<input stream>", length);
979         }
980
981         throw SQLExceptionFactory.notImplemented ("setUnicodeStream");
982     }
983
984     /**
985      * Sets the designated parameter to the given <code>Reader</code> object.
986      * When a very large UNICODE value is input to a LONGVARCHAR parameter, it
987      * may be more practical to send it via a <code>java.io.Reader</code>
988      * object. The data will be read from the stream as needed until
989      * end-of-file is reached. The JDBC driver will do any necessary conversion
990      * from UNICODE to the database char format.
991      *
992      * @param parameterIndex the first parameter is 1, the second is 2, ...
993      * @param x the <code>java.io.Reader</code> object that contains the
994      * Unicode data
995      * @throws SQLException if a database access error occurs or this method is
996      * called on a closed <code>PreparedStatement</code>
997      */

998     public void setCharacterStream(int parameterIndex, Reader JavaDoc x)
999             throws SQLException JavaDoc {
1000        synchronized (connection_) {
1001            if (agent_.loggingEnabled()) {
1002                agent_.logWriter_.traceEntry(this, "setCharacterStream",
1003                        parameterIndex, x);
1004            }
1005            try {
1006                parameterIndex = checkSetterPreconditions(parameterIndex);
1007                parameterMetaData_.clientParamtertype_[parameterIndex -1] =
1008                    java.sql.Types.CLOB;
1009                if (x == null) {
1010                    setNull(parameterIndex, java.sql.Types.CLOB);
1011                    return;
1012                }
1013                setInput(parameterIndex, new Clob(agent_, x));
1014            } catch (SqlException se) {
1015                throw se.getSQLException();
1016            }
1017        }
1018    }
1019
1020     /**
1021     * Sets the designated parameter to the given Reader, which will have
1022     * the specified number of bytes.
1023     *
1024     * @param parameterIndex the index of the parameter to which this set
1025     * method is applied
1026     * @param x the java Reader which contains the UNICODE value
1027     * @param length the number of bytes in the stream
1028     * @exception SQLException thrown on failure.
1029     *
1030     */

1031
1032    public void setCharacterStream(int parameterIndex,
1033                                   java.io.Reader JavaDoc x,
1034                                   long length) throws SQLException JavaDoc {
1035        try
1036        {
1037            synchronized (connection_) {
1038                if (agent_.loggingEnabled()) {
1039                    agent_.logWriter_.traceEntry(this, "setCharacterStream", parameterIndex, x, new Long JavaDoc(length));
1040                }
1041                parameterIndex = checkSetterPreconditions(parameterIndex);
1042                parameterMetaData_.clientParamtertype_[parameterIndex - 1] = java.sql.Types.CLOB;
1043                if (x == null) {
1044                    setNull(parameterIndex, java.sql.Types.CLOB);
1045                    return;
1046                }
1047                if(length > Integer.MAX_VALUE) {
1048                    throw new SqlException(agent_.logWriter_,
1049                        new ClientMessageId(SQLState.CLIENT_LENGTH_OUTSIDE_RANGE_FOR_DATATYPE),
1050                        new Long JavaDoc(length), new Integer JavaDoc(Integer.MAX_VALUE)).getSQLException();
1051                }
1052                setInput(parameterIndex, new Clob(agent_, x, (int)length));
1053            }
1054        }
1055        catch ( SqlException se )
1056        {
1057            throw se.getSQLException();
1058        }
1059    }
1060
1061     /**
1062     * Sets the designated parameter to the given Reader, which will have
1063     * the specified number of bytes.
1064     *
1065     * @param parameterIndex the index of the parameter to which this
1066     * set method is applied
1067     * @param x the java Reader which contains the UNICODE value
1068     * @param length the number of bytes in the stream
1069     * @exception SQLException thrown on failure.
1070     *
1071     */

1072
1073    public void setCharacterStream(int parameterIndex,
1074                                   java.io.Reader JavaDoc x,
1075                                   int length) throws SQLException JavaDoc {
1076        setCharacterStream(parameterIndex,x,(long)length);
1077    }
1078
1079    public void setBlob(int parameterIndex, java.sql.Blob JavaDoc x) throws SQLException JavaDoc {
1080        try
1081        {
1082            synchronized (connection_) {
1083                if (agent_.loggingEnabled()) {
1084                    agent_.logWriter_.traceEntry(this, "setBlob", parameterIndex, x);
1085                }
1086                setBlobX(parameterIndex, x);
1087            }
1088        }
1089        catch ( SqlException se )
1090        {
1091            throw se.getSQLException();
1092        }
1093    }
1094
1095    // also used by Blob
1096
public void setBlobX(int parameterIndex, java.sql.Blob JavaDoc x) throws SqlException {
1097        parameterIndex = checkSetterPreconditions(parameterIndex);
1098        parameterMetaData_.clientParamtertype_[parameterIndex - 1] = java.sql.Types.BLOB;
1099        if (x == null) {
1100            setNullX(parameterIndex, java.sql.Types.BLOB);
1101            return;
1102        }
1103        setInput(parameterIndex, x);
1104    }
1105
1106    public void setClob(int parameterIndex, java.sql.Clob JavaDoc x) throws SQLException JavaDoc {
1107        try
1108        {
1109            synchronized (connection_) {
1110                if (agent_.loggingEnabled()) {
1111                    agent_.logWriter_.traceEntry(this, "setClob", parameterIndex, x);
1112                }
1113                setClobX(parameterIndex, x);
1114            }
1115        }
1116        catch ( SqlException se )
1117        {
1118            throw se.getSQLException();
1119        }
1120    }
1121
1122    // also used by Clob
1123
void setClobX(int parameterIndex, java.sql.Clob JavaDoc x) throws SqlException {
1124        parameterIndex = checkSetterPreconditions(parameterIndex);
1125        parameterMetaData_.clientParamtertype_[parameterIndex - 1] = java.sql.Types.CLOB;
1126        if (x == null) {
1127            this.setNullX(parameterIndex, Types.CLOB);
1128            return;
1129        }
1130        setInput(parameterIndex, x);
1131    }
1132
1133
1134    public void setArray(int parameterIndex, java.sql.Array JavaDoc x) throws SQLException JavaDoc {
1135        try
1136        {
1137            synchronized (connection_) {
1138                if (agent_.loggingEnabled()) {
1139                    agent_.logWriter_.traceEntry(this, "setArray", parameterIndex, x);
1140                }
1141                parameterIndex = checkSetterPreconditions(parameterIndex);
1142                throw new SqlException(agent_.logWriter_,
1143                    new ClientMessageId(SQLState.JDBC_METHOD_NOT_IMPLEMENTED));
1144            }
1145        }
1146        catch ( SqlException se )
1147        {
1148            throw se.getSQLException();
1149        }
1150    }
1151
1152    public void setRef(int parameterIndex, java.sql.Ref JavaDoc x) throws SQLException JavaDoc {
1153        try
1154        {
1155            synchronized (connection_) {
1156                if (agent_.loggingEnabled()) {
1157                    agent_.logWriter_.traceEntry(this, "setRef", parameterIndex, x);
1158                }
1159                parameterIndex = checkSetterPreconditions(parameterIndex);
1160                throw new SqlException(agent_.logWriter_,
1161                    new ClientMessageId(SQLState.JDBC_METHOD_NOT_IMPLEMENTED));
1162            }
1163        }
1164        catch ( SqlException se )
1165        {
1166            throw se.getSQLException();
1167        }
1168    }
1169
1170    // The Java compiler uses static binding, so we must use instanceof
1171
// rather than to rely on separate setObject() methods for
1172
// each of the Java Object instance types recognized below.
1173
public void setObject(int parameterIndex, Object JavaDoc x) throws SQLException JavaDoc {
1174        try
1175        {
1176            synchronized (connection_) {
1177                if (agent_.loggingEnabled()) {
1178                    agent_.logWriter_.traceEntry(this, "setObject", parameterIndex, x);
1179                }
1180                super.checkForClosedStatement();
1181                if (x instanceof String JavaDoc) {
1182                    setString(parameterIndex, (String JavaDoc) x);
1183                } else if (x instanceof Integer JavaDoc) {
1184                    setInt(parameterIndex, ((Integer JavaDoc) x).intValue());
1185                } else if (x instanceof Double JavaDoc) {
1186                    setDouble(parameterIndex, ((Double JavaDoc) x).doubleValue());
1187                } else if (x instanceof Float JavaDoc) {
1188                    setFloat(parameterIndex, ((Float JavaDoc) x).floatValue());
1189                } else if (x instanceof Boolean JavaDoc) {
1190                    setBoolean(parameterIndex, ((Boolean JavaDoc) x).booleanValue());
1191                } else if (x instanceof Long JavaDoc) {
1192                    setLong(parameterIndex, ((Long JavaDoc) x).longValue());
1193                } else if (x instanceof byte[]) {
1194                    setBytes(parameterIndex, (byte[]) x);
1195                } else if (x instanceof java.math.BigDecimal JavaDoc) {
1196                    setBigDecimal(parameterIndex, (java.math.BigDecimal JavaDoc) x);
1197                } else if (x instanceof java.sql.Date JavaDoc) {
1198                    setDate(parameterIndex, (java.sql.Date JavaDoc) x);
1199                } else if (x instanceof java.sql.Time JavaDoc) {
1200                    setTime(parameterIndex, (java.sql.Time JavaDoc) x);
1201                } else if (x instanceof java.sql.Timestamp JavaDoc) {
1202                    setTimestamp(parameterIndex, (java.sql.Timestamp JavaDoc) x);
1203                } else if (x instanceof java.sql.Blob JavaDoc) {
1204                    setBlob(parameterIndex, (java.sql.Blob JavaDoc) x);
1205                } else if (x instanceof java.sql.Clob JavaDoc) {
1206                    setClob(parameterIndex, (java.sql.Clob JavaDoc) x);
1207                } else if (x instanceof java.sql.Array JavaDoc) {
1208                    setArray(parameterIndex, (java.sql.Array JavaDoc) x);
1209                } else if (x instanceof java.sql.Ref JavaDoc) {
1210                    setRef(parameterIndex, (java.sql.Ref JavaDoc) x);
1211                } else if (x instanceof Short JavaDoc) {
1212                    setShort(parameterIndex, ((Short JavaDoc) x).shortValue());
1213                } else if (x instanceof Byte JavaDoc) {
1214                    setByte(parameterIndex, ((Byte JavaDoc) x).byteValue());
1215                } else {
1216                    checkSetterPreconditions(parameterIndex);
1217                    throw new SqlException(agent_.logWriter_,
1218                        new ClientMessageId(SQLState.UNSUPPORTED_TYPE));
1219                }
1220            }
1221        }
1222        catch ( SqlException se )
1223        {
1224            throw se.getSQLException();
1225        }
1226    }
1227
1228    public void setObject(int parameterIndex, Object JavaDoc x, int targetJdbcType) throws SQLException JavaDoc {
1229        try
1230        {
1231            synchronized (connection_) {
1232                if (agent_.loggingEnabled()) {
1233                    agent_.logWriter_.traceEntry(this, "setObject", parameterIndex, x, targetJdbcType);
1234                }
1235                setObjectX(parameterIndex, x, targetJdbcType, 0);
1236            }
1237        }
1238        catch ( SqlException se )
1239        {
1240            throw se.getSQLException();
1241        }
1242    }
1243
1244    public void setObject(int parameterIndex,
1245                          Object JavaDoc x,
1246                          int targetJdbcType,
1247                          int scale) throws SQLException JavaDoc {
1248        try
1249        {
1250            synchronized (connection_) {
1251                if (agent_.loggingEnabled()) {
1252                    agent_.logWriter_.traceEntry(this, "setObject", parameterIndex, x, targetJdbcType, scale);
1253                }
1254                setObjectX(parameterIndex, x, targetJdbcType, scale);
1255            }
1256        }
1257        catch ( SqlException se )
1258        {
1259            throw se.getSQLException();
1260        }
1261    }
1262
1263    private void setObjectX(int parameterIndex,
1264                            Object JavaDoc x,
1265                            int targetJdbcType,
1266                            int scale) throws SqlException {
1267        parameterIndex = checkSetterPreconditions(parameterIndex);
1268        checkForValidScale(scale);
1269
1270        if (x == null) {
1271            setNullX(parameterIndex, targetJdbcType);
1272            return;
1273        }
1274
1275        checkForSupportedDataType(targetJdbcType);
1276
1277        // JDBC Spec specifies that conversion should occur on the client if
1278
// the targetJdbcType is specified.
1279

1280        int inputParameterType = CrossConverters.getInputJdbcType(targetJdbcType);
1281        parameterMetaData_.clientParamtertype_[parameterIndex - 1] = inputParameterType;
1282        x = agent_.crossConverters_.setObject(inputParameterType, x);
1283
1284        // Set to round down on setScale like embedded does in SQLDecimal
1285
try {
1286            if (targetJdbcType == java.sql.Types.DECIMAL || targetJdbcType == java.sql.Types.NUMERIC) {
1287                x = ((java.math.BigDecimal JavaDoc) x).setScale(scale, java.math.BigDecimal.ROUND_DOWN);
1288            }
1289        } catch (ArithmeticException JavaDoc ae) {
1290            // Any problems with scale should have already been caught by
1291
// checkForvalidScale
1292
throw new SqlException(agent_.logWriter_,
1293                new ClientMessageId(SQLState.JAVA_EXCEPTION),
1294                new Object JavaDoc[] {ae.getClass().getName(), ae.getMessage()}, ae);
1295        }
1296        try {
1297            setObject(parameterIndex, x);
1298        } catch ( SQLException JavaDoc se ) {
1299            throw new SqlException(se);
1300        }
1301    }
1302
1303    // Since parameters are cached as objects in parameters_[],
1304
// java null may be used to represent SQL null.
1305
public void clearParameters() throws SQLException JavaDoc {
1306        try
1307        {
1308            synchronized (connection_) {
1309                if (agent_.loggingEnabled()) {
1310                    agent_.logWriter_.traceEntry(this, "clearParameters");
1311                }
1312                checkForClosedStatement();
1313                if (parameterMetaData_ != null) {
1314                    for (int i = 0; i < parameters_.length; i++) {
1315                        parameters_[i] = null;
1316                    }
1317
1318                    for (int i = 0; i < parameterSet_.length; i++) {
1319                        parameterSet_[i] = false;
1320                    }
1321                }
1322            }
1323        }
1324        catch ( SqlException se )
1325        {
1326            throw se.getSQLException();
1327        }
1328    }
1329
1330    public boolean execute() throws SQLException JavaDoc {
1331        try
1332        {
1333            synchronized (connection_) {
1334                if (agent_.loggingEnabled()) {
1335                    agent_.logWriter_.traceEntry(this, "execute");
1336                }
1337                boolean b = executeX();
1338                if (agent_.loggingEnabled()) {
1339                    agent_.logWriter_.traceExit(this, "execute", b);
1340                }
1341                return b;
1342            }
1343        }
1344        catch ( SqlException se ) {
1345            checkStatementValidity(se);
1346            throw se.getSQLException();
1347        }
1348    }
1349
1350    // also used by SQLCA
1351
boolean executeX() throws SqlException {
1352        flowExecute(executeMethod__);
1353
1354        return resultSet_ != null;
1355    }
1356
1357    //--------------------------JDBC 2.0-----------------------------
1358

1359    public void addBatch() throws SQLException JavaDoc {
1360        try
1361        {
1362            synchronized (connection_) {
1363                if (agent_.loggingEnabled()) {
1364                    agent_.logWriter_.traceEntry(this, "addBatch");
1365                }
1366                checkForClosedStatement();
1367                checkThatAllParametersAreSet();
1368                
1369                if (parameterTypeList == null) {
1370                    parameterTypeList = new ArrayList JavaDoc();
1371                }
1372
1373                // ASSERT: since OUT/INOUT parameters are not allowed, there should
1374
// be no problem in sharing the JDBC Wrapper object instances
1375
// since they will not be modified by the driver.
1376

1377                // batch up the parameter values -- deep copy req'd
1378

1379                if (parameterMetaData_ != null) {
1380                    Object JavaDoc[] inputsClone = new Object JavaDoc[parameters_.length];
1381                    System.arraycopy(parameters_, 0, inputsClone, 0, parameters_.length);
1382
1383                    batch_.add(inputsClone);
1384                    
1385                    // Get a copy of the parameter type data and save it in a list
1386
// which will be used later on at the time of batch execution.
1387
parameterTypeList.add(parameterMetaData_.clientParamtertype_.clone());
1388                } else {
1389                    batch_.add(null);
1390                    parameterTypeList.add(null);
1391                }
1392            }
1393        }
1394        catch ( SqlException se )
1395        {
1396            throw se.getSQLException();
1397        }
1398    }
1399
1400    // Batch requires that input types are exact, we perform no input cross conversion for Batch.
1401
// If so, this is an external semantic, and should go into the release notes
1402
public int[] executeBatch() throws SQLException JavaDoc, BatchUpdateException {
1403        try
1404        {
1405            synchronized (connection_) {
1406                if (agent_.loggingEnabled()) {
1407                    agent_.logWriter_.traceEntry(this, "executeBatch");
1408                }
1409                int[] updateCounts = null;
1410                updateCounts = executeBatchX(false);
1411
1412                if (agent_.loggingEnabled()) {
1413                    agent_.logWriter_.traceExit(this, "executeBatch", updateCounts);
1414                }
1415                return updateCounts;
1416            }
1417        }
1418        catch ( SqlException se )
1419        {
1420            throw se.getSQLException();
1421        }
1422    }
1423
1424    public java.sql.ResultSetMetaData JavaDoc getMetaData() throws SQLException JavaDoc {
1425        try
1426        {
1427            synchronized (connection_) {
1428                if (agent_.loggingEnabled()) {
1429                    agent_.logWriter_.traceEntry(this, "getMetaData");
1430                }
1431                ColumnMetaData resultSetMetaData = getMetaDataX();
1432                if (agent_.loggingEnabled()) {
1433                    agent_.logWriter_.traceExit(this, "getMetaData", resultSetMetaData);
1434                }
1435                return resultSetMetaData;
1436            }
1437        }
1438        catch ( SqlException se )
1439        {
1440            throw se.getSQLException();
1441        }
1442    }
1443
1444    private ColumnMetaData getMetaDataX() throws SqlException {
1445        super.checkForClosedStatement();
1446        return resultSetMetaData_;
1447    }
1448
1449    //------------------------- JDBC 3.0 -----------------------------------
1450

1451    public boolean execute(String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc {
1452        if (agent_.loggingEnabled()) {
1453            agent_.logWriter_.traceEntry(this, "execute", sql, autoGeneratedKeys);
1454        }
1455        throw new SqlException(agent_.logWriter_,
1456            new ClientMessageId(SQLState.NOT_FOR_PREPARED_STATEMENT),
1457            "execute(String, int)").getSQLException();
1458    }
1459
1460    public boolean execute(String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException JavaDoc {
1461        if (agent_.loggingEnabled()) {
1462            agent_.logWriter_.traceEntry(this, "execute", sql, columnNames);
1463        }
1464        throw new SqlException(agent_.logWriter_,
1465            new ClientMessageId(SQLState.NOT_FOR_PREPARED_STATEMENT),
1466            "execute(String, String[])").getSQLException();
1467    }
1468
1469    public boolean execute(String JavaDoc sql, int[] columnIndexes) throws SQLException JavaDoc {
1470        if (agent_.loggingEnabled()) {
1471            agent_.logWriter_.traceEntry(this, "execute", sql, columnIndexes);
1472        }
1473        throw new SqlException(agent_.logWriter_,
1474            new ClientMessageId(SQLState.NOT_FOR_PREPARED_STATEMENT),
1475            "execute(String, int[])").getSQLException();
1476    }
1477
1478    public int executeUpdate(String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc {
1479        if (agent_.loggingEnabled()) {
1480            agent_.logWriter_.traceEntry(this, "executeUpdate", autoGeneratedKeys);
1481        }
1482        throw new SqlException(agent_.logWriter_,
1483            new ClientMessageId(SQLState.NOT_FOR_PREPARED_STATEMENT),
1484            "executeUpdate(String, int)").getSQLException();
1485    }
1486
1487    public int executeUpdate(String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException JavaDoc {
1488        if (agent_.loggingEnabled()) {
1489            agent_.logWriter_.traceEntry(this, "executeUpdate", columnNames);
1490        }
1491        throw new SqlException(agent_.logWriter_,
1492            new ClientMessageId(SQLState.NOT_FOR_PREPARED_STATEMENT),
1493            "executeUpdate(String, String[])").getSQLException();
1494    }
1495
1496    public int executeUpdate(String JavaDoc sql, int[] columnIndexes) throws SQLException JavaDoc {
1497        if (agent_.loggingEnabled()) {
1498            agent_.logWriter_.traceEntry(this, "executeUpdate", columnIndexes);
1499        }
1500        throw new SqlException(agent_.logWriter_,
1501            new ClientMessageId(SQLState.NOT_FOR_PREPARED_STATEMENT),
1502            "execute(String, int[])").getSQLException();
1503    }
1504
1505    public void setURL(int parameterIndex, java.net.URL JavaDoc x) throws SQLException JavaDoc {
1506        if (agent_.loggingEnabled()) {
1507            agent_.logWriter_.traceEntry(this, "setURL", parameterIndex, x);
1508        }
1509        jdbc3FeatureNotSupported(false);
1510    }
1511
1512    public java.sql.ParameterMetaData JavaDoc getParameterMetaData() throws SQLException JavaDoc {
1513        try
1514        {
1515            synchronized (connection_) {
1516                if (agent_.loggingEnabled()) {
1517                    agent_.logWriter_.traceEntry(this, "getParameterMetaData");
1518                }
1519                Object JavaDoc parameterMetaData = getParameterMetaDataX();
1520                if (agent_.loggingEnabled()) {
1521                    agent_.logWriter_.traceExit(this, "getParameterMetaData", parameterMetaData);
1522                }
1523                return (java.sql.ParameterMetaData JavaDoc) parameterMetaData;
1524            }
1525        }
1526        catch ( SqlException se )
1527        {
1528            throw se.getSQLException();
1529        }
1530    }
1531
1532    private ParameterMetaData getParameterMetaDataX() throws SqlException {
1533        super.checkForClosedStatement();
1534        ParameterMetaData pm = ClientDriver.getFactory().newParameterMetaData
1535                (parameterMetaData_ != null
1536                ? parameterMetaData_
1537                : ClientDriver.getFactory().newColumnMetaData(agent_.logWriter_, 0));
1538        if (escapedProcedureCallWithResult_) {
1539            pm.escapedProcedureCallWithResult_ = true;
1540        }
1541        return pm;
1542    }
1543
1544    // ------------------------ box car and callback methods --------------------------------
1545

1546    public void writeExecute(Section section,
1547                             ColumnMetaData parameterMetaData,
1548                             Object JavaDoc[] inputs,
1549                             int numInputColumns,
1550                             boolean outputExpected,
1551                             // This is a hint to the material layer that more write commands will follow.
1552
// It is ignored by the driver in all cases except when blob data is written,
1553
// in which case this boolean is used to optimize the implementation.
1554
// Otherwise we wouldn't be able to chain after blob data is sent.
1555
// Current servers have a restriction that blobs can only be chained with blobs
1556
boolean chainedWritesFollowingSetLob) throws SqlException {
1557        materialPreparedStatement_.writeExecute_(section,
1558                parameterMetaData,
1559                inputs,
1560                numInputColumns,
1561                outputExpected,
1562                chainedWritesFollowingSetLob);
1563    }
1564
1565
1566    public void readExecute() throws SqlException {
1567        materialPreparedStatement_.readExecute_();
1568    }
1569
1570    public void writeOpenQuery(Section section,
1571                               int fetchSize,
1572                               int resultSetType,
1573                               int numInputColumns,
1574                               ColumnMetaData parameterMetaData,
1575                               Object JavaDoc[] inputs) throws SqlException {
1576        materialPreparedStatement_.writeOpenQuery_(section,
1577                fetchSize,
1578                resultSetType,
1579                numInputColumns,
1580                parameterMetaData,
1581                inputs);
1582    }
1583
1584    public void writeDescribeInput(Section section) throws SqlException {
1585        materialPreparedStatement_.writeDescribeInput_(section);
1586    }
1587
1588    public void readDescribeInput() throws SqlException {
1589        materialPreparedStatement_.readDescribeInput_();
1590    }
1591
1592    public void completeDescribeInput(ColumnMetaData parameterMetaData, Sqlca sqlca) {
1593        int sqlcode = super.completeSqlca(sqlca);
1594        if (sqlcode < 0) {
1595            return;
1596        }
1597
1598
1599        parameterMetaData_ = parameterMetaData;
1600
1601        // The following code handles the case when
1602
// sqlxParmmode is not supported, in which case server will return 0 (unknown), and
1603
// this could clobber our guessed value for sqlxParmmode. This is a problem.
1604
// We can solve this problem for Non-CALL statements, since the parmmode is always IN (1).
1605
// But what about CALL statements. If CALLs are describable, then we have no
1606
// problem, we assume server won't return unknown.
1607
// If CALLs are not describable then nothing gets clobbered because we won't
1608
// parse out extended describe, so again no problem.
1609
if (sqlMode_ != isCall__ && parameterMetaData_ != null) {
1610            for (int i = 0; i < parameterMetaData_.columns_; i++) {
1611                parameterMetaData_.sqlxParmmode_[i] = 1; // 1 means IN parameter
1612
}
1613        }
1614
1615        if (agent_.loggingEnabled()) {
1616            agent_.logWriter_.traceParameterMetaData(this, parameterMetaData_);
1617        }
1618    }
1619
1620    public void writeDescribeOutput(Section section) throws SqlException {
1621        materialPreparedStatement_.writeDescribeOutput_(section);
1622    }
1623
1624    public void readDescribeOutput() throws SqlException {
1625        materialPreparedStatement_.readDescribeOutput_();
1626    }
1627
1628    public void completeDescribeOutput(ColumnMetaData resultSetMetaData, Sqlca sqlca) {
1629        int sqlcode = super.completeSqlca(sqlca);
1630        if (sqlcode < 0) {
1631            return;
1632        }
1633        resultSetMetaData_ = resultSetMetaData;
1634        if (agent_.loggingEnabled()) {
1635            agent_.logWriter_.traceResultSetMetaData(this, resultSetMetaData);
1636        }
1637    }
1638
1639    void writePrepareDescribeInputOutput() throws SqlException {
1640        // Notice that sql_ is passed in since in general ad hoc sql must be passed in for unprepared statements
1641
writePrepareDescribeOutput(sql_, section_);
1642        writeDescribeInput(section_);
1643    }
1644
1645    void readPrepareDescribeInputOutput() throws SqlException {
1646        readPrepareDescribeOutput();
1647        readDescribeInput();
1648        completePrepareDescribe();
1649    }
1650
1651    void writePrepareDescribeInput() throws SqlException {
1652        // performance will be better if we flow prepare with output enable vs. prepare then describe input for callable
1653
// Notice that sql_ is passed in since in general ad hoc sql must be passed in for unprepared statements
1654
writePrepare(sql_, section_);
1655        writeDescribeInput(section_);
1656    }
1657
1658    void readPrepareDescribeInput() throws SqlException {
1659        readPrepare();
1660        readDescribeInput();
1661        completePrepareDescribe();
1662    }
1663
1664    void completePrepareDescribe() {
1665        if (parameterMetaData_ == null) {
1666            return;
1667        }
1668        parameters_ = expandObjectArray(parameters_, parameterMetaData_.columns_);
1669        parameterSet_ = expandBooleanArray(parameterSet_, parameterMetaData_.columns_);
1670        parameterRegistered_ = expandBooleanArray(parameterRegistered_, parameterMetaData_.columns_);
1671    }
1672
1673    private Object JavaDoc[] expandObjectArray(Object JavaDoc[] array, int newLength) {
1674        if (array == null) {
1675            Object JavaDoc[] newArray = new Object JavaDoc[newLength];
1676            return newArray;
1677        }
1678        if (array.length < newLength) {
1679            Object JavaDoc[] newArray = new Object JavaDoc[newLength];
1680            System.arraycopy(array, 0, newArray, 0, array.length);
1681            return newArray;
1682        }
1683        return array;
1684    }
1685
1686    private boolean[] expandBooleanArray(boolean[] array, int newLength) {
1687        if (array == null) {
1688            boolean[] newArray = new boolean[newLength];
1689            return newArray;
1690        }
1691        if (array.length < newLength) {
1692            boolean[] newArray = new boolean[newLength];
1693            System.arraycopy(array, 0, newArray, 0, array.length);
1694            return newArray;
1695        }
1696        return array;
1697    }
1698
1699    void flowPrepareForSelectFromInsert() throws SqlException {
1700        agent_.beginWriteChain(this);
1701        writePrepareDescribeInputOutput(constructSelectFromInsertSQL(sql_), section_);
1702        agent_.flow(this);
1703        readPrepareDescribeInputOutput();
1704        agent_.endReadChain();
1705    }
1706
1707    void writePrepareDescribeInputOutput(String JavaDoc sql,
1708                                         Section section) throws SqlException {
1709        // Notice that sql_ is passed in since in general ad hoc sql must be passed in for unprepared statements
1710
writePrepareDescribeOutput(sql, section);
1711        writeDescribeInput(section);
1712    }
1713
1714    void flowPrepareDescribeInputOutput() throws SqlException {
1715        agent_.beginWriteChain(this);
1716        if (sqlMode_ == isCall__) {
1717            writePrepareDescribeInput();
1718            agent_.flow(this);
1719            readPrepareDescribeInput();
1720            agent_.endReadChain();
1721        } else {
1722            writePrepareDescribeInputOutput();
1723            agent_.flow(this);
1724            readPrepareDescribeInputOutput();
1725            agent_.endReadChain();
1726        }
1727    }
1728
1729    void flowExecute(int executeType) throws SqlException {
1730        super.checkForClosedStatement();
1731        super.clearWarningsX();
1732        super.checkForAppropriateSqlMode(executeType, sqlMode_);
1733        checkThatAllParametersAreSet();
1734
1735        if (sqlMode_ == isUpdate__) {
1736            updateCount_ = 0;
1737        } else {
1738            updateCount_ = -1;
1739        }
1740
1741        // DERBY-1036: Moved check till execute time to comply with embedded
1742
// behavior. Since we check here and not in setCursorName, several
1743
// statements can have the same cursor name as long as their result
1744
// sets are not simultaneously open.
1745

1746        if (sqlMode_ == isQuery__) {
1747            checkForDuplicateCursorName();
1748        }
1749
1750            agent_.beginWriteChain(this);
1751
1752            boolean piggybackedAutocommit = super.writeCloseResultSets(true); // true means permit auto-commits
1753

1754            int numInputColumns;
1755            boolean outputExpected;
1756            try
1757            {
1758                numInputColumns = (parameterMetaData_ != null) ? parameterMetaData_.getColumnCount() : 0;
1759                outputExpected = (resultSetMetaData_ != null && resultSetMetaData_.getColumnCount() > 0);
1760            }
1761            catch ( SQLException JavaDoc se )
1762            {
1763                // Generate a SqlException for this, we don't want to throw
1764
// SQLException in this internal method
1765
throw new SqlException(se);
1766            }
1767            boolean chainAutoCommit = false;
1768            boolean commitSubstituted = false;
1769            boolean repositionedCursor = false;
1770            boolean timeoutSent = false;
1771            ResultSet scrollableRS = null;
1772
1773            if (doWriteTimeout) {
1774                timeoutArrayList.set(0, TIMEOUT_STATEMENT + timeout_);
1775                writeSetSpecialRegister(timeoutArrayList);
1776                doWriteTimeout = false;
1777                timeoutSent = true;
1778            }
1779            switch (sqlMode_) {
1780            case isUpdate__:
1781                if (positionedUpdateCursorName_ != null) {
1782                    scrollableRS = agent_.sectionManager_.getPositionedUpdateResultSet(positionedUpdateCursorName_);
1783                }
1784                if (scrollableRS != null && !scrollableRS.isRowsetCursor_) {
1785                    repositionedCursor =
1786                            scrollableRS.repositionScrollableResultSetBeforeJDBC1PositionedUpdateDelete();
1787                    if (!repositionedCursor) {
1788                        scrollableRS = null;
1789                    }
1790                }
1791
1792                chainAutoCommit = connection_.willAutoCommitGenerateFlow() && isAutoCommittableStatement_;
1793
1794                if (sqlUpdateMode_ == isInsertSql__ && generatedKeysColumnNames_ != null) {
1795                    writeOpenQuery(section_,
1796                            fetchSize_,
1797                            resultSetType_,
1798                            numInputColumns,
1799                            parameterMetaData_,
1800                            parameters_);
1801                } else {
1802                    boolean chainOpenQueryForAutoGeneratedKeys = (sqlUpdateMode_ == isInsertSql__ && autoGeneratedKeys_ == RETURN_GENERATED_KEYS);
1803                    writeExecute(section_,
1804                            parameterMetaData_,
1805                            parameters_,
1806                            numInputColumns,
1807                            outputExpected,
1808                            (chainAutoCommit || chainOpenQueryForAutoGeneratedKeys)// chain flag
1809
); // chain flag
1810

1811                    if (chainOpenQueryForAutoGeneratedKeys) {
1812                        prepareAutoGeneratedKeysStatement();
1813                        writeOpenQuery(preparedStatementForAutoGeneratedKeys_.section_,
1814                                preparedStatementForAutoGeneratedKeys_.fetchSize_,
1815                                preparedStatementForAutoGeneratedKeys_.resultSetType_);
1816                    }
1817                }
1818
1819                if (chainAutoCommit) {
1820                    // we have encountered an error in writing the execute, so do not
1821
// flow an autocommit
1822
if (agent_.accumulatedReadExceptions_ != null) {
1823                        // currently, the only write exception we encounter is for
1824
// data truncation: SQLSTATE 01004, so we don't bother checking for this
1825
connection_.writeCommitSubstitute_();
1826                        commitSubstituted = true;
1827                    } else {
1828                        // there is no write error, so flow the commit
1829
connection_.writeCommit();
1830                    }
1831                }
1832                break;
1833
1834            case isQuery__:
1835                writeOpenQuery(section_,
1836                        fetchSize_,
1837                        resultSetType_,
1838                        numInputColumns,
1839                        parameterMetaData_,
1840                        parameters_);
1841                break;
1842
1843            case isCall__:
1844                writeExecuteCall(outputRegistered_, // if no out/inout parameter, outputExpected = false
1845
null,
1846                        section_,
1847                        fetchSize_,
1848                        false, // do not suppress ResultSets for regular CALLs
1849
resultSetType_,
1850                        parameterMetaData_,
1851                        parameters_); // cross conversion
1852
break;
1853            }
1854
1855            agent_.flow(this);
1856
1857            super.readCloseResultSets(true); // true means permit auto-commits
1858

1859            // turn inUnitOfWork_ flag back on and add statement
1860
// back on commitListeners_ list if they were off
1861
// by an autocommit chained to a close cursor.
1862
if (piggybackedAutocommit) {
1863                connection_.completeTransactionStart();
1864            }
1865
1866            super.markResultSetsClosed(true); // true means remove from list of commit and rollback listeners
1867

1868            if (timeoutSent) {
1869                readSetSpecialRegister(); // Read response to the EXCSQLSET
1870
}
1871
1872            switch (sqlMode_) {
1873            case isUpdate__:
1874                // do not need to reposition for a rowset cursor
1875
if (scrollableRS != null && !scrollableRS.isRowsetCursor_) {
1876                    scrollableRS.readPositioningFetch_();
1877                }
1878
1879                if (sqlUpdateMode_ == isInsertSql__ && generatedKeysColumnNames_ != null) {
1880                    readOpenQuery();
1881                    if (resultSet_ != null) {
1882                        generatedKeysResultSet_ = resultSet_;
1883                        resultSet_ = null;
1884                        updateCount_ = 1;
1885                    }
1886                } else {
1887                    readExecute();
1888
1889                    if (sqlUpdateMode_ == isInsertSql__ && autoGeneratedKeys_ == RETURN_GENERATED_KEYS) {
1890                        readPrepareAutoGeneratedKeysStatement();
1891                        preparedStatementForAutoGeneratedKeys_.readOpenQuery();
1892                        generatedKeysResultSet_ = preparedStatementForAutoGeneratedKeys_.resultSet_;
1893                        preparedStatementForAutoGeneratedKeys_.resultSet_ = null;
1894                    }
1895                }
1896
1897                if (chainAutoCommit) {
1898                    if (commitSubstituted) {
1899                        connection_.readCommitSubstitute_();
1900                    } else {
1901                        connection_.readCommit();
1902                    }
1903                }
1904                break;
1905
1906            case isQuery__:
1907                try {
1908                    readOpenQuery();
1909                } catch (DisconnectException dise) {
1910                    throw dise;
1911                } catch (SqlException e) {
1912                    throw e;
1913                }
1914                // resultSet_ is null if open query failed.
1915
// check for null resultSet_ before using it.
1916
if (resultSet_ != null) {
1917                    resultSet_.parseScrollableRowset();
1918                    //if (resultSet_.scrollable_) resultSet_.getRowCount();
1919

1920                    // DERBY-1183: If we set it up earlier, the entry in
1921
// clientCursorNameCache_ gets wiped out by the closing of
1922
// result sets happening during readCloseResultSets above
1923
// because ResultSet#markClosed calls
1924
// Statement#removeClientCursorNameFromCache.
1925
setupCursorNameCacheAndMappings();
1926                }
1927                break;
1928
1929            case isCall__:
1930                readExecuteCall();
1931                break;
1932
1933            }
1934
1935
1936            try {
1937                agent_.endReadChain();
1938            } catch (SqlException e) {
1939                throw e;
1940
1941            }
1942
1943            if (sqlMode_ == isCall__) {
1944                parseStorProcReturnedScrollableRowset();
1945                checkForStoredProcResultSetCount(executeType);
1946                // When there are no result sets back, we will commit immediately when autocommit is true.
1947
// make sure a commit is not performed when making the call to the sqlca message procedure
1948
if (connection_.autoCommit_ && resultSet_ == null && resultSetList_ == null && isAutoCommittableStatement_) {
1949                    connection_.flowAutoCommit();
1950                }
1951            }
1952
1953            // The JDBC spec says that executeUpdate() should return 0
1954
// when no row count is returned.
1955
if (executeType == executeUpdateMethod__ && updateCount_ < 0) {
1956                updateCount_ = 0;
1957            }
1958
1959            // Throw an exception if holdability returned by the server is different from requested.
1960
if (resultSet_ != null && resultSet_.resultSetHoldability_ != resultSetHoldability_ && sqlMode_ != isCall__) {
1961                throw new SqlException(agent_.logWriter_,
1962                    new ClientMessageId(SQLState.UNABLE_TO_OPEN_RESULTSET_WITH_REQUESTED_HOLDABILTY),
1963                        new Integer JavaDoc(resultSetHoldability_));
1964            }
1965    }
1966
1967    public int[] executeBatchX(boolean supportsQueryBatchRequest)
1968        throws SqlException, SQLException JavaDoc, BatchUpdateException {
1969        synchronized (connection_) {
1970            checkForClosedStatement(); // Per jdbc spec (see Statement.close() javadoc)
1971
clearWarningsX(); // Per jdbc spec 0.7, also see getWarnings() javadoc
1972
return executeBatchRequestX(supportsQueryBatchRequest);
1973        }
1974    }
1975
1976
1977    private int[] executeBatchRequestX(boolean supportsQueryBatchRequest)
1978            throws SqlException, BatchUpdateException {
1979        SqlException chainBreaker = null;
1980        int batchSize = batch_.size();
1981        int[] updateCounts = new int[batchSize];
1982        int numInputColumns;
1983        try {
1984            numInputColumns = parameterMetaData_ == null ? 0 : parameterMetaData_.getColumnCount();
1985        } catch ( SQLException JavaDoc se ) {
1986            throw new SqlException(se);
1987        }
1988        Object JavaDoc[] savedInputs = null; // used to save/restore existing parameters
1989
boolean timeoutSent = false;
1990
1991        if (batchSize == 0) {
1992            return updateCounts;
1993        }
1994        // The network client has a hard limit of 65,534 commands in a single
1995
// DRDA request. This is because DRDA uses a 2-byte correlation ID,
1996
// and the values 0 and 0xffff are reserved as special values. So
1997
// that imposes an upper limit on the batch size we can support:
1998
if (batchSize > 65534)
1999            throw new BatchUpdateException(agent_.logWriter_,
2000                new ClientMessageId(SQLState.TOO_MANY_COMMANDS_FOR_BATCH),
2001                new Integer JavaDoc(65534), updateCounts);
2002
2003        // Initialize all the updateCounts to indicate failure
2004
// This is done to account for "chain-breaking" errors where we cannot
2005
// read any more replies
2006
for (int i = 0; i < batchSize; i++) {
2007            updateCounts[i] = -3;
2008        }
2009
2010        if (!supportsQueryBatchRequest && sqlMode_ == isQuery__) {
2011            throw new BatchUpdateException(agent_.logWriter_,
2012                new ClientMessageId(SQLState.CANNOT_BATCH_QUERIES), updateCounts);
2013        }
2014        if (supportsQueryBatchRequest && sqlMode_ != isQuery__) {
2015            throw new BatchUpdateException(agent_.logWriter_,
2016                new ClientMessageId(SQLState.QUERY_BATCH_ON_NON_QUERY_STATEMENT),
2017                updateCounts);
2018        }
2019
2020        resultSetList_ = null;
2021
2022
2023        if (sqlMode_ == isQuery__) {
2024            indexOfCurrentResultSet_ = -1; //reset ResultSetList
2025
resultSetList_ = new ResultSet[batchSize];
2026        }
2027
2028
2029        //save the current input set so it can be restored
2030
savedInputs = parameters_;
2031
2032        agent_.beginBatchedWriteChain(this);
2033        boolean chainAutoCommit = connection_.willAutoCommitGenerateFlow() && isAutoCommittableStatement_;
2034
2035        if (doWriteTimeout) {
2036            timeoutArrayList.set(0, TIMEOUT_STATEMENT + timeout_);
2037            writeSetSpecialRegister(timeoutArrayList);
2038            doWriteTimeout = false;
2039            timeoutSent = true;
2040        }
2041
2042        for (int i = 0; i < batchSize; i++) {
2043            parameterMetaData_.clientParamtertype_ = (int[]) parameterTypeList.get(i);
2044            parameters_ = (Object JavaDoc[]) batch_.get(i);
2045
2046            if (sqlMode_ != isCall__) {
2047                boolean outputExpected;
2048                try {
2049                    outputExpected = (resultSetMetaData_ != null && resultSetMetaData_.getColumnCount() > 0);
2050                } catch ( SQLException JavaDoc se ) {
2051                    throw new SqlException(se);
2052                }
2053
2054                writeExecute(section_,
2055                        parameterMetaData_,
2056                        parameters_,
2057                        numInputColumns,
2058                        outputExpected,
2059                        chainAutoCommit || (i != batchSize - 1)); // more statements to chain
2060
} else if (outputRegistered_) // make sure no output parameters are registered
2061
{
2062                throw new BatchUpdateException(agent_.logWriter_,
2063                    new ClientMessageId(SQLState.OUTPUT_PARAMS_NOT_ALLOWED),
2064                    updateCounts);
2065            } else {
2066                writeExecuteCall(false, // no output expected for batched CALLs
2067
null, // no procedure name supplied for prepared CALLs
2068
section_,
2069                        fetchSize_,
2070                        true, // suppress ResultSets for batch
2071
resultSetType_,
2072                        parameterMetaData_,
2073                        parameters_);
2074            }
2075        }
2076
2077        boolean commitSubstituted = false;
2078        if (chainAutoCommit) {
2079            // we have encountered an error in writing the execute, so do not
2080
// flow an autocommit
2081
if (agent_.accumulatedReadExceptions_ != null) {
2082                // currently, the only write exception we encounter is for
2083
// data truncation: SQLSTATE 01004, so we don't bother checking for this
2084
connection_.writeCommitSubstitute_();
2085                commitSubstituted = true;
2086            } else {
2087                // there is no write error, so flow the commit
2088
connection_.writeCommit();
2089            }
2090        }
2091
2092        agent_.flowBatch(this, batchSize);
2093
2094        if (timeoutSent) {
2095            readSetSpecialRegister(); // Read response to the EXCSQLSET
2096
}
2097
2098        try {
2099            for (int i = 0; i < batchSize; i++) {
2100                agent_.setBatchedExceptionLabelIndex(i);
2101                parameters_ = (Object JavaDoc[]) batch_.get(i);
2102                if (sqlMode_ != isCall__) {
2103                    readExecute();
2104                } else {
2105                    readExecuteCall();
2106                }
2107                updateCounts[i] = updateCount_;
2108
2109            }
2110
2111            agent_.disableBatchedExceptionTracking(); // to prvent the following readCommit() from getting a batch label
2112
if (chainAutoCommit) {
2113                if (!commitSubstituted) {
2114                    connection_.readCommit();
2115                } else {
2116                    connection_.readCommitSubstitute_();
2117                }
2118            }
2119        }
2120
2121                // for chain-breaking exception only, all read() methods do their own accumulation
2122
// this catches the entire accumulated chain, we need to be careful not to
2123
// reaccumulate it on the agent since the batch labels will be overwritten if
2124
// batch exception tracking is enabled.
2125
catch (SqlException e) { // for chain-breaking exception only
2126
chainBreaker = e;
2127            chainBreaker.setNextException(new SqlException(agent_.logWriter_,
2128                new ClientMessageId(SQLState.BATCH_CHAIN_BREAKING_EXCEPTION)));
2129        }
2130        // We need to clear the batch before any exception is thrown from agent_.endBatchedReadChain().
2131
batch_.clear();
2132        parameterTypeList = null;
2133
2134        // restore the saved input set, setting it to "current"
2135
parameters_ = savedInputs;
2136
2137        agent_.endBatchedReadChain(updateCounts, chainBreaker);
2138
2139        return updateCounts;
2140
2141    }
2142
2143
2144    //------------------material layer event callbacks follow-----------------------
2145

2146    boolean listenToUnitOfWork_ = false;
2147
2148    public void listenToUnitOfWork() {
2149        if (!listenToUnitOfWork_) {
2150            listenToUnitOfWork_ = true;
2151            connection_.CommitAndRollbackListeners_.put(this,null);
2152        }
2153    }
2154
2155    public void completeLocalCommit(java.util.Iterator JavaDoc listenerIterator) {
2156        if (section_ != null) {
2157            openOnServer_ = false;
2158        }
2159        listenerIterator.remove();
2160        listenToUnitOfWork_ = false;
2161    }
2162
2163    public void completeLocalRollback(java.util.Iterator JavaDoc listenerIterator) {
2164        if (section_ != null) {
2165            openOnServer_ = false;
2166        }
2167        listenerIterator.remove();
2168        listenToUnitOfWork_ = false;
2169    }
2170
2171    //----------------------------internal use only helper methods----------------
2172

2173    /**
2174     * Returns the name of the java.sql interface implemented by this class.
2175     * @return name of java.sql interface
2176     */

2177    protected String JavaDoc getJdbcStatementInterfaceName() {
2178        return "java.sql.PreparedStatement";
2179    }
2180
2181    private int checkSetterPreconditions(int parameterIndex) throws SqlException {
2182        super.checkForClosedStatement();
2183        parameterIndex = checkForEscapedCallWithResult(parameterIndex);
2184        checkForValidParameterIndex(parameterIndex);
2185        return parameterIndex;
2186    }
2187
2188    void checkForValidParameterIndex(int parameterIndex) throws SqlException {
2189        if (parameterMetaData_ == null || parameterIndex < 1 || parameterIndex > parameterMetaData_.columns_) {
2190            throw new SqlException(agent_.logWriter_,
2191                new ClientMessageId(SQLState.LANG_INVALID_PARAM_POSITION),
2192                new Integer JavaDoc(parameterIndex),
2193                new Integer JavaDoc(parameterMetaData_.columns_));
2194        }
2195    }
2196
2197    private void checkThatAllParametersAreSet() throws SqlException {
2198        if (parameterMetaData_ != null) {
2199            for (int i = 0; i < parameterMetaData_.columns_; i++) {
2200                if (!parameterSet_[i] && !parameterRegistered_[i]) {
2201                    throw new SqlException(agent_.logWriter_,
2202                        new ClientMessageId(SQLState.LANG_MISSING_PARMS));
2203                }
2204            }
2205        }
2206    }
2207
2208
2209    private int checkForEscapedCallWithResult(int parameterIndex) throws SqlException {
2210        if (escapedProcedureCallWithResult_) {
2211            if (parameterIndex == 1) {
2212                throw new SqlException(agent_.logWriter_,
2213                    new ClientMessageId(SQLState.LANG_RETURN_OUTPUT_PARAM_CANNOT_BE_SET));
2214            } else {
2215                parameterIndex--;
2216            }
2217        }
2218        return parameterIndex;
2219    }
2220
2221    void checkForValidScale(int scale) throws SqlException {
2222        if (scale < 0 || scale > 31) {
2223            throw new SqlException(agent_.logWriter_,
2224                new ClientMessageId(SQLState.BAD_SCALE_VALUE),
2225                new Integer JavaDoc(scale));
2226        }
2227    }
2228
2229    /**
2230     * Checks whether a data type is supported for
2231     * <code>setObject(int, Object, int)</code> and
2232     * <code>setObject(int, Object, int, int)</code>.
2233     *
2234     * @param dataType the data type to check
2235     * @exception SqlException if the type is not supported
2236     */

2237    private void checkForSupportedDataType(int dataType) throws SqlException {
2238
2239        // JDBC 4.0 javadoc for setObject() says:
2240
//
2241
// Throws: (...) SQLFeatureNotSupportedException - if
2242
// targetSqlType is a ARRAY, BLOB, CLOB, DATALINK,
2243
// JAVA_OBJECT, NCHAR, NCLOB, NVARCHAR, LONGNVARCHAR, REF,
2244
// ROWID, SQLXML or STRUCT data type and the JDBC driver does
2245
// not support this data type
2246
//
2247
// Of these types, we only support BLOB, CLOB and
2248
// (sort of) JAVA_OBJECT.
2249

2250        switch (dataType) {
2251        case java.sql.Types.ARRAY:
2252        case java.sql.Types.DATALINK:
2253        case JDBC40Translation.NCHAR:
2254        case JDBC40Translation.NCLOB:
2255        case JDBC40Translation.NVARCHAR:
2256        case JDBC40Translation.LONGNVARCHAR:
2257        case java.sql.Types.REF:
2258        case JDBC40Translation.ROWID:
2259        case JDBC40Translation.SQLXML:
2260        case java.sql.Types.STRUCT:
2261            throw new SqlException
2262                (agent_.logWriter_,
2263                 new ClientMessageId(SQLState.DATA_TYPE_NOT_SUPPORTED),
2264                 Types.getTypeString(dataType));
2265        }
2266    }
2267
2268    void checkScaleForINOUTDecimal(int parameterIndex, int registerOutScale) throws SqlException {
2269        java.math.BigDecimal JavaDoc decimalInput = (java.math.BigDecimal JavaDoc) parameters_[parameterIndex - 1];
2270        if (decimalInput == null) {
2271            return;
2272        }
2273        // if the register out scale is greater than input scale, input scale is stored in sqlScale_
2274
if (registerOutScale > parameterMetaData_.sqlScale_[parameterIndex - 1]) {
2275            int inputLength = decimalInput.toString().length();
2276            int scaleDifference = registerOutScale - decimalInput.scale();
2277            if (decimalInput.signum() == -1) {
2278                inputLength--;
2279            }
2280            // if the new Decimal (with bigger scale) cannot fit into the DA
2281
if ((32 - scaleDifference) < inputLength) {
2282                // TODO - FINISH THIS
2283
throw new SqlException(agent_.logWriter_,
2284                    new ClientMessageId(SQLState.REGOUTPARAM_SCALE_DOESNT_MATCH_SETTER));
2285            }
2286            // if the new Decimal (with bigger scale) can fit
2287
else {
2288                parameters_[parameterIndex - 1] = decimalInput.setScale(registerOutScale);
2289                parameterMetaData_.sqlScale_[parameterIndex - 1] = registerOutScale;
2290            }
2291        }
2292        // if the register out sacle is smaller than input scale
2293
else if (registerOutScale < parameterMetaData_.sqlScale_[parameterIndex - 1]) {
2294            // remove 0's at the end of input
2295
try {
2296                // if the new Decimal (with smaller scale) can fit
2297
parameters_[parameterIndex - 1] = decimalInput.setScale(registerOutScale);
2298                parameterMetaData_.sqlScale_[parameterIndex - 1] = registerOutScale;
2299            } catch (ArithmeticException JavaDoc e) {
2300                // if the new Decimal (with smaller scale) cannot fit into the DA
2301
throw new SqlException(agent_.logWriter_,
2302                    new ClientMessageId(SQLState.REGOUTPARAM_SCALE_DOESNT_MATCH_SETTER));
2303            }
2304        }
2305    }
2306
2307    /* (non-Javadoc)
2308     * @see org.apache.derby.client.am.Statement#markClosed(boolean)
2309     */

2310    protected void markClosed(boolean removeListener){
2311        if(pooledConnection_ != null)
2312            pooledConnection_.onStatementClose(this);
2313        super.markClosed(removeListener);
2314        
2315        if (parameterMetaData_ != null) {
2316            parameterMetaData_.markClosed();
2317            parameterMetaData_ = null;
2318        }
2319        sql_ = null;
2320
2321        // Apparently, the JVM is not smart enough to traverse parameters_[] and null
2322
// out its members when the entire array is set to null (parameters_=null;).
2323
if (parameters_ != null) {
2324            for (int i = 0; i < parameters_.length; i++) {
2325                parameters_[i] = null;
2326            }
2327        }
2328        parameters_ = null;
2329
2330        if(removeListener)
2331            connection_.CommitAndRollbackListeners_.remove(this);
2332    }
2333    
2334    //jdbc 4.0 methods
2335

2336    /**
2337     * Sets the designated parameter to the given input stream.
2338     * When a very large ASCII value is input to a <code>LONGVARCHAR</code>
2339     * parameter, it may be more practical to send it via a
2340     * <code>java.io.InputStream</code>. Data will be read from the stream as
2341     * needed until end-of-file is reached. The JDBC driver will do any
2342     * necessary conversion from ASCII to the database char format.
2343     *
2344     * @param parameterIndex the first parameter is 1, the second is 2, ...
2345     * @param x the Java input stream that contains the ASCII parameter value
2346     * @throws SQLException if a database access error occurs or this method is
2347     * called on a closed <code>PreparedStatement</code>
2348     */

2349    public void setAsciiStream(int parameterIndex, InputStream JavaDoc x)
2350            throws SQLException JavaDoc {
2351        synchronized (connection_) {
2352            if (agent_.loggingEnabled()) {
2353                agent_.logWriter_.traceEntry(this, "setAsciiStream",
2354                        parameterIndex, x);
2355            }
2356            try {
2357                parameterIndex = checkSetterPreconditions(parameterIndex);
2358                parameterMetaData_.clientParamtertype_[parameterIndex - 1] = java.sql.Types.CLOB;
2359                if (x == null) {
2360                    setNull(parameterIndex, java.sql.Types.CLOB);
2361                    return;
2362                }
2363                setInput(parameterIndex, new Clob(agent_, x, "US-ASCII"));
2364            } catch (SqlException se) {
2365                throw se.getSQLException();
2366            }
2367        }
2368    }
2369
2370    /**
2371     * Sets the designated parameter to the given input stream.
2372     * When a very large binary value is input to a <code>LONGVARBINARY</code>
2373     * parameter, it may be more practical to send it via a
2374     * <code>java.io.InputStream</code> object. The data will be read from the
2375     * stream as needed until end-of-file is reached.
2376     *
2377     * @param parameterIndex the first parameter is 1, the second is 2, ...
2378     * @param x the java input stream which contains the binary parameter value
2379     * @throws SQLException if a database access error occurs or this method is
2380     * called on a closed <code>PreparedStatement</code>
2381     */

2382    public void setBinaryStream(int parameterIndex, InputStream JavaDoc x)
2383            throws SQLException JavaDoc {
2384        synchronized (connection_) {
2385            if (agent_.loggingEnabled()) {
2386                agent_.logWriter_.traceEntry(this, "setBinaryStream",
2387                        parameterIndex, x);
2388            }
2389            try {
2390                setBinaryStreamX(parameterIndex, x, -1);
2391            } catch (SqlException se) {
2392                throw se.getSQLException();
2393            }
2394        }
2395    }
2396
2397    /**
2398     * Sets the designated parameter to a <code>Reader</code> object.
2399     *
2400     * @param parameterIndex index of the first parameter is 1, the second is
2401     * 2, ...
2402     * @param reader an object that contains the data to set the parameter
2403     * value to.
2404     * @throws SQLException if parameterIndex does not correspond to a
2405     * parameter marker in the SQL statement; if a database access error
2406     * occurs; this method is called on a closed PreparedStatementor if
2407     * parameterIndex does not correspond to a parameter marker in the SQL
2408     * statement
2409     */

2410    public void setClob(int parameterIndex, Reader JavaDoc reader)
2411            throws SQLException JavaDoc {
2412        synchronized (connection_) {
2413            if (agent_.loggingEnabled()) {
2414                agent_.logWriter_.traceEntry(this, "setClob",
2415                        parameterIndex, reader);
2416            }
2417            try {
2418                checkForClosedStatement();
2419            } catch (SqlException se) {
2420                throw se.getSQLException();
2421            }
2422            setInput(parameterIndex, new Clob(agent_, reader));
2423        }
2424    }
2425
2426   /**
2427     * Sets the designated parameter to a Reader object.
2428     *
2429     * @param parameterIndex index of the first parameter is 1, the second is 2, ...
2430     * @param reader An object that contains the data to set the parameter value to.
2431     * @param length the number of characters in the parameter data.
2432     * @throws SQLException if parameterIndex does not correspond to a parameter
2433     * marker in the SQL statement, or if the length specified is less than zero.
2434     *
2435     */

2436    
2437    public void setClob(int parameterIndex, Reader JavaDoc reader, long length)
2438    throws SQLException JavaDoc{
2439        synchronized (connection_) {
2440            if (agent_.loggingEnabled()) {
2441                agent_.logWriter_.traceEntry(this, "setClob",
2442                        parameterIndex, reader, new Long JavaDoc(length));
2443            }
2444            try {
2445                checkForClosedStatement();
2446            } catch (SqlException se) {
2447                throw se.getSQLException();
2448            }
2449            if(length > Integer.MAX_VALUE)
2450                throw new SqlException(agent_.logWriter_,
2451                    new ClientMessageId(SQLState.BLOB_TOO_LARGE_FOR_CLIENT),
2452                    new Long JavaDoc(length), new Integer JavaDoc(Integer.MAX_VALUE)).getSQLException();
2453            else
2454                setInput(parameterIndex, new Clob(agent_, reader, (int)length));
2455        }
2456    }
2457
2458    /**
2459     * Sets the designated parameter to a <code>InputStream</code> object.
2460     * This method differs from the <code>setBinaryStream(int, InputStream)
2461     * </code> method because it informs the driver that the parameter value
2462     * should be sent to the server as a <code>BLOB</code>. When the
2463     * <code>setBinaryStream</code> method is used, the driver may have to do
2464     * extra work to determine whether the parameter data should be sent to the
2465     * server as a <code>LONGVARBINARY</code> or a <code>BLOB</code>
2466     *
2467     * @param parameterIndex index of the first parameter is 1, the second is
2468     * 2, ...
2469     * @param inputStream an object that contains the data to set the parameter
2470     * value to.
2471     * @throws SQLException if a database access error occurs, this method is
2472     * called on a closed <code>PreparedStatement</code> or if
2473     * <code>parameterIndex</code> does not correspond to a parameter
2474     * marker in the SQL statement
2475     */

2476    public void setBlob(int parameterIndex, InputStream JavaDoc inputStream)
2477            throws SQLException JavaDoc {
2478        synchronized (connection_) {
2479            if (agent_.loggingEnabled()) {
2480                agent_.logWriter_.traceEntry(this, "setBlob", parameterIndex,
2481                        inputStream);
2482            }
2483            try {
2484                setBinaryStreamX(parameterIndex, inputStream, -1);
2485            } catch (SqlException se) {
2486                throw se.getSQLException();
2487            }
2488        }
2489    }
2490
2491    /**
2492     * Sets the designated parameter to a InputStream object.
2493     *
2494     * @param parameterIndex index of the first parameter is 1,
2495     * the second is 2, ...
2496     * @param inputStream An object that contains the data to set the parameter
2497     * value to.
2498     * @param length the number of bytes in the parameter data.
2499     * @throws SQLException if parameterIndex does not correspond
2500     * to a parameter marker in the SQL statement, if the length specified
2501     * is less than zero or if the number of bytes in the inputstream does not match
2502     * the specfied length.
2503     *
2504     */

2505    
2506    public void setBlob(int parameterIndex, InputStream JavaDoc inputStream, long length)
2507    throws SQLException JavaDoc{
2508        synchronized (connection_) {
2509            if (agent_.loggingEnabled()) {
2510                agent_.logWriter_.traceEntry(this, "setBlob", parameterIndex,
2511                        inputStream, new Long JavaDoc(length));
2512            }
2513            if(length > Integer.MAX_VALUE)
2514                throw new SqlException(agent_.logWriter_,
2515                    new ClientMessageId(SQLState.BLOB_TOO_LARGE_FOR_CLIENT),
2516                    new Long JavaDoc(length), new Integer JavaDoc(Integer.MAX_VALUE)).getSQLException();
2517            else {
2518                try {
2519                    setBinaryStreamX(parameterIndex, inputStream, (int)length);
2520                } catch(SqlException se){
2521                    throw se.getSQLException();
2522                }
2523            }
2524        }
2525    }
2526 
2527        /*
2528         * Method calls onStatementError occurred on the
2529         * BrokeredConnectionControl class after checking the
2530         * SQLState of the SQLException thrown.
2531         * @param sqle SqlException
2532         * @throws java.sql.SQLException
2533         */

2534        
2535        private void checkStatementValidity(SqlException sqle)
2536                                            throws SQLException JavaDoc {
2537            //check if the statement is already closed
2538
//This might be caused because the connection associated
2539
//with this prepared statement has been closed marking
2540
//its associated prepared statements also as
2541
//closed
2542

2543            if(pooledConnection_!=null && isClosed()){
2544                pooledConnection_.onStatementErrorOccurred(this,
2545                    sqle.getSQLException());
2546            }
2547        }
2548
2549}
2550
Popular Tags