001: /*
002:
003: Derby - Class org.apache.derby.iapi.jdbc.BrokeredPreparedStatement
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.iapi.jdbc;
023:
024: import java.io.InputStream;
025: import java.io.Reader;
026: import java.util.Calendar;
027:
028: import java.sql.*;
029: import java.net.URL;
030:
031: /**
032: JDBC 2 brokered PreparedStatement. Forwards calls off to a real prepared statement
033: obtained through the BrokeredStatementControl getRealPreparedStatement method.
034: */
035: public class BrokeredPreparedStatement extends BrokeredStatement
036: implements EnginePreparedStatement {
037:
038: /**
039: SQL used to create me.
040: */
041: final String sql;
042:
043: public BrokeredPreparedStatement(BrokeredStatementControl control,
044: int jdbcLevel, String sql) throws SQLException {
045: super (control, jdbcLevel);
046: this .sql = sql;
047: }
048:
049: /**
050: * Imitate the getParameterMetaData() function in JDBC 3.0
051: *
052: * Retrieves the number, types and properties of this PreparedStatement
053: * object's parameters.
054: *
055: * @return a EngineParameterMetaData object that contains information about the
056: * number, types and properties of this PreparedStatement object's parameters.
057: * @exception SQLException if a database access error occurs
058: */
059: public EngineParameterMetaData getEmbedParameterSetMetaData()
060: throws SQLException {
061: return ((EnginePreparedStatement) getPreparedStatement())
062: .getEmbedParameterSetMetaData();
063: }
064:
065: /**
066: * A prepared SQL query is executed and its ResultSet is returned.
067: *
068: * @return a ResultSet that contains the data produced by the
069: * query; never null
070: * @exception SQLException thrown on failure.
071: */
072: public final ResultSet executeQuery() throws SQLException {
073: return wrapResultSet(getPreparedStatement().executeQuery());
074: }
075:
076: /**
077: * Execute a SQL INSERT, UPDATE or DELETE statement. In addition,
078: * SQL statements that return nothing such as SQL DDL statements
079: * can be executed.
080: *
081: * @return either the row count for INSERT, UPDATE or DELETE; or 0
082: * for SQL statements that return nothing
083: * @exception SQLException thrown on failure.
084: */
085: public final int executeUpdate() throws SQLException {
086: return getPreparedStatement().executeUpdate();
087: }
088:
089: /**
090: * Set a parameter to SQL NULL.
091: *
092: * <P><B>Note:</B> You must specify the parameter's SQL type.
093: *
094: * @param parameterIndex the first parameter is 1, the second is 2, ...
095: * @param sqlType SQL type code defined by java.sql.Types
096: * @exception SQLException thrown on failure.
097: */
098: public final void setNull(int parameterIndex, int sqlType)
099: throws SQLException {
100: getPreparedStatement().setNull(parameterIndex, sqlType);
101: }
102:
103: /**
104: * Set a parameter to SQL NULL.
105: *
106: * <P><B>Note:</B> You must specify the parameter's SQL type.
107: *
108: * @param parameterIndex the first parameter is 1, the second is 2, ...
109: * @param sqlType SQL type code defined by java.sql.Types
110: * @exception SQLException thrown on failure.
111: */
112: public final void setNull(int parameterIndex, int sqlType,
113: String typeName) throws SQLException {
114: getPreparedStatement().setNull(parameterIndex, sqlType,
115: typeName);
116: }
117:
118: /**
119: * Set a parameter to a Java boolean value. According to the JDBC API spec,
120: * the driver converts this to a SQL BIT value when it sends it to the
121: * database. But we don't have to do this, since the database engine
122: * supports a boolean type.
123: *
124: * @param parameterIndex the first parameter is 1, the second is 2, ...
125: * @param x the parameter value
126: * @exception SQLException thrown on failure.
127: */
128: public final void setBoolean(int parameterIndex, boolean x)
129: throws SQLException {
130: getPreparedStatement().setBoolean(parameterIndex, x);
131: }
132:
133: /**
134: * Set a parameter to a Java byte value. The driver converts this
135: * to a SQL TINYINT value when it sends it to the database.
136: *
137: * @param parameterIndex the first parameter is 1, the second is 2, ...
138: * @param x the parameter value
139: * @exception SQLException thrown on failure.
140: */
141: public final void setByte(int parameterIndex, byte x)
142: throws SQLException {
143: getPreparedStatement().setByte(parameterIndex, x);
144: }
145:
146: /**
147: * Set a parameter to a Java short value. The driver converts this
148: * to a SQL SMALLINT value when it sends it to the database.
149: *
150: * @param parameterIndex the first parameter is 1, the second is 2, ...
151: * @param x the parameter value
152: * @exception SQLException thrown on failure.
153: */
154: public final void setShort(int parameterIndex, short x)
155: throws SQLException {
156: getPreparedStatement().setShort(parameterIndex, x);
157: }
158:
159: /**
160: * Set a parameter to a Java int value. The driver converts this
161: * to a SQL INTEGER value when it sends it to the database.
162: *
163: * @param parameterIndex the first parameter is 1, the second is 2, ...
164: * @param x the parameter value
165: * @exception SQLException thrown on failure.
166: */
167: public final void setInt(int parameterIndex, int x)
168: throws SQLException {
169: getPreparedStatement().setInt(parameterIndex, x);
170: }
171:
172: /**
173: * Set a parameter to a Java long value. The driver converts this
174: * to a SQL BIGINT value when it sends it to the database.
175: *
176: * @param parameterIndex the first parameter is 1, the second is 2, ...
177: * @param x the parameter value
178: * @exception SQLException thrown on failure.
179: */
180: public final void setLong(int parameterIndex, long x)
181: throws SQLException {
182: getPreparedStatement().setLong(parameterIndex, x);
183: }
184:
185: /**
186: * Set a parameter to a Java float value. The driver converts this
187: * to a SQL FLOAT value when it sends it to the database.
188: *
189: * @param parameterIndex the first parameter is 1, the second is 2, ...
190: * @param x the parameter value
191: * @exception SQLException thrown on failure.
192: */
193: public final void setFloat(int parameterIndex, float x)
194: throws SQLException {
195: getPreparedStatement().setFloat(parameterIndex, x);
196: }
197:
198: /**
199: * Set a parameter to a Java double value. The driver converts this
200: * to a SQL DOUBLE value when it sends it to the database.
201: *
202: * @param parameterIndex the first parameter is 1, the second is 2, ...
203: * @param x the parameter value
204: * @exception SQLException thrown on failure.
205: */
206: public final void setDouble(int parameterIndex, double x)
207: throws SQLException {
208: getPreparedStatement().setDouble(parameterIndex, x);
209: }
210:
211: /**
212: * Set a parameter to a java.math.BigDecimal value.
213: * The driver converts this to a SQL NUMERIC value when
214: * it sends it to the database.
215: *
216: * @param parameterIndex the first parameter is 1, the second is 2, ...
217: * @param x the parameter value
218: * @exception SQLException thrown on failure.
219: */
220: public final void setBigDecimal(int parameterIndex,
221: java.math.BigDecimal x) throws SQLException {
222: getPreparedStatement().setBigDecimal(parameterIndex, x);
223: }
224:
225: /**
226: * Set a parameter to a Java String value. The driver converts this
227: * to a SQL VARCHAR or LONGVARCHAR value (depending on the arguments
228: * size relative to the driver's limits on VARCHARs) when it sends
229: * it to the database.
230: *
231: * @param parameterIndex the first parameter is 1, the second is 2, ...
232: * @param x the parameter value
233: * @exception SQLException thrown on failure.
234: */
235: public final void setString(int parameterIndex, String x)
236: throws SQLException {
237: getPreparedStatement().setString(parameterIndex, x);
238: }
239:
240: /**
241: * Set a parameter to a Java array of bytes. The driver converts
242: * this to a SQL VARBINARY or LONGVARBINARY (depending on the
243: * argument's size relative to the driver's limits on VARBINARYs)
244: * when it sends it to the database.
245: *
246: * @param parameterIndex the first parameter is 1, the second is 2, ...
247: * @param x the parameter value
248: * @exception SQLException thrown on failure.
249: */
250: public final void setBytes(int parameterIndex, byte[] x)
251: throws SQLException {
252: getPreparedStatement().setBytes(parameterIndex, x);
253: }
254:
255: /**
256: * Set a parameter to a java.sql.Date value. The driver converts this
257: * to a SQL DATE value when it sends it to the database.
258: *
259: * @param parameterIndex the first parameter is 1, the second is 2, ...
260: * @param x the parameter value
261: * @exception SQLException thrown on failure.
262: */
263: public final void setDate(int parameterIndex, Date x)
264: throws SQLException {
265: getPreparedStatement().setDate(parameterIndex, x);
266: }
267:
268: /**
269: * Set a parameter to a java.sql.Time value. The driver converts this
270: * to a SQL TIME value when it sends it to the database.
271: *
272: * @param parameterIndex the first parameter is 1, the second is 2, ...
273: * @param x the parameter value
274: * @exception SQLException thrown on failure.
275: */
276: public final void setTime(int parameterIndex, Time x)
277: throws SQLException {
278: getPreparedStatement().setTime(parameterIndex, x);
279: }
280:
281: /**
282: * Set a parameter to a java.sql.Timestamp value. The driver
283: * converts this to a SQL TIMESTAMP value when it sends it to the
284: * database.
285: *
286: * @param parameterIndex the first parameter is 1, the second is 2, ...
287: * @param x the parameter value
288: * @exception SQLException thrown on failure.
289: */
290: public final void setTimestamp(int parameterIndex, Timestamp x)
291: throws SQLException {
292: getPreparedStatement().setTimestamp(parameterIndex, x);
293: }
294:
295: /**
296: * We do this inefficiently and read it all in here. The target type
297: * is assumed to be a String.
298: *
299: * @param parameterIndex the first parameter is 1, the second is 2, ...
300: * @param x the java input stream which contains the ASCII parameter value
301: * @param length the number of bytes in the stream
302: * @exception SQLException thrown on failure.
303: */
304: public final void setAsciiStream(int parameterIndex, InputStream x,
305: int length) throws SQLException {
306: getPreparedStatement()
307: .setAsciiStream(parameterIndex, x, length);
308: }
309:
310: /**
311: * We do this inefficiently and read it all in here. The target type
312: * is assumed to be a String. The unicode source is assumed to be
313: * in char[]. RESOLVE: might it be in UTF, instead? that'd be faster!
314: *
315: * @param parameterIndex the first parameter is 1, the second is 2, ...
316: * @param x the java input stream which contains the
317: * UNICODE parameter value
318: * @param length the number of bytes in the stream
319: * @exception SQLException thrown on failure.
320: */
321: public final void setUnicodeStream(int parameterIndex,
322: InputStream x, int length) throws SQLException {
323: getPreparedStatement().setUnicodeStream(parameterIndex, x,
324: length);
325: }
326:
327: /**
328: * @param parameterIndex the first parameter is 1, the second is 2, ...
329: * @param x the java input stream which contains the binary parameter value
330: * @param length the number of bytes in the stream
331: * @exception SQLException thrown on failure.
332: */
333: public final void setBinaryStream(int parameterIndex,
334: InputStream x, int length) throws SQLException {
335: getPreparedStatement().setBinaryStream(parameterIndex, x,
336: length);
337: }
338:
339: /**
340: * JDBC 2.0
341: *
342: * Add a set of parameters to the batch.
343: *
344: * @exception SQLException if a database-access error occurs.
345: */
346: public final void addBatch() throws SQLException {
347: getPreparedStatement().addBatch();
348: }
349:
350: /**
351: * <P>In general, parameter values remain in force for repeated use of a
352: * Statement. Setting a parameter value automatically clears its
353: * previous value. However, in some cases it is useful to immediately
354: * release the resources used by the current parameter values; this can
355: * be done by calling clearParameters.
356: * @exception SQLException thrown on failure.
357: */
358: public final void clearParameters() throws SQLException {
359: getPreparedStatement().clearParameters();
360: }
361:
362: /**
363: * JDBC 2.0
364: *
365: * The number, types and properties of a ResultSet's columns
366: * are provided by the getMetaData method.
367: *
368: * @return the description of a ResultSet's columns
369: * @exception SQLException Feature not implemented for now.
370: */
371: public final java.sql.ResultSetMetaData getMetaData()
372: throws SQLException {
373: return getPreparedStatement().getMetaData();
374: }
375:
376: /**
377: * The interface says that the type of the Object parameter must
378: * be compatible with the type of the targetSqlType. We check that,
379: * and if it flies, we expect the underlying engine to do the
380: * required conversion once we pass in the value using its type.
381: * So, an Integer converting to a CHAR is done via setInteger()
382: * support on the underlying CHAR type.
383: *
384: * <p>If x is null, it won't tell us its type, so we pass it on to setNull
385: *
386: * @param parameterIndex The first parameter is 1, the second is 2, ...
387: * @param x The object containing the input parameter value
388: * @param targetSqlType The SQL type (as defined in java.sql.Types) to be
389: * sent to the database. The scale argument may further qualify this type.
390: * @param scale For java.sql.Types.DECIMAL or java.sql.Types.NUMERIC types
391: * this is the number of digits after the decimal. For all other
392: * types this value will be ignored,
393: * @exception SQLException thrown on failure.
394: */
395: public final void setObject(int parameterIndex, Object x,
396: int targetSqlType, int scale) throws SQLException {
397: getPreparedStatement().setObject(parameterIndex, x,
398: targetSqlType, scale);
399: }
400:
401: /**
402: * This method is like setObject above, but assumes a scale of zero.
403: * @exception SQLException thrown on failure.
404: */
405: public final void setObject(int parameterIndex, Object x,
406: int targetSqlType) throws SQLException {
407: getPreparedStatement().setObject(parameterIndex, x,
408: targetSqlType);
409: }
410:
411: /**
412: * <p>Set the value of a parameter using an object; use the
413: * java.lang equivalent objects for integral values.
414: *
415: * <p>The JDBC specification specifies a standard mapping from
416: * Java Object types to SQL types. The given argument java object
417: * will be converted to the corresponding SQL type before being
418: * sent to the database.
419: *
420: * <p>Note that this method may be used to pass datatabase
421: * specific abstract data types, by using a Driver specific Java
422: * type.
423: *
424: * @param parameterIndex The first parameter is 1, the second is 2, ...
425: * @param x The object containing the input parameter value
426: * @exception SQLException thrown on failure.
427: */
428: public final void setObject(int parameterIndex, Object x)
429: throws SQLException {
430: getPreparedStatement().setObject(parameterIndex, x);
431: }
432:
433: /**
434: * @see java.sql.Statement#execute
435: * @exception SQLException thrown on failure.
436: */
437: public final boolean execute() throws SQLException {
438: return getPreparedStatement().execute();
439: }
440:
441: public final void setCharacterStream(int parameterIndex,
442: Reader reader, int length) throws SQLException {
443: getPreparedStatement().setCharacterStream(parameterIndex,
444: reader, length);
445: }
446:
447: public final void setRef(int i, Ref x) throws SQLException {
448: getPreparedStatement().setRef(i, x);
449: }
450:
451: public final void setBlob(int i, Blob x) throws SQLException {
452: getPreparedStatement().setBlob(i, x);
453: }
454:
455: public final void setClob(int i, Clob x) throws SQLException {
456: getPreparedStatement().setClob(i, x);
457: }
458:
459: public final void setArray(int i, Array x) throws SQLException {
460: getPreparedStatement().setArray(i, x);
461: }
462:
463: public final void setDate(int i, Date x, Calendar cal)
464: throws SQLException {
465: getPreparedStatement().setDate(i, x, cal);
466: }
467:
468: public final void setTime(int i, Time x, Calendar cal)
469: throws SQLException {
470: getPreparedStatement().setTime(i, x, cal);
471: }
472:
473: public final void setTimestamp(int i, Timestamp x, Calendar cal)
474: throws SQLException {
475: getPreparedStatement().setTimestamp(i, x, cal);
476: }
477:
478: /*
479: ** Control methods.
480: */
481:
482: /**
483: * Access the underlying PreparedStatement. This method
484: * is package protected to restrict access to the underlying
485: * object to the brokered objects. Allowing the application to
486: * access the underlying object thtough a public method would
487: *
488: */
489: PreparedStatement getPreparedStatement() throws SQLException {
490: return control.getRealPreparedStatement();
491: }
492:
493: /**
494: Override the BrokeredStatement's getStatement() to always return a PreparedStatement.
495: */
496: public final Statement getStatement() throws SQLException {
497: return getPreparedStatement();
498: }
499:
500: /**
501: Create a duplicate PreparedStatement to this, including state, from the passed in Connection.
502: */
503: public PreparedStatement createDuplicateStatement(Connection conn,
504: PreparedStatement oldStatement) throws SQLException {
505:
506: PreparedStatement newStatement = conn.prepareStatement(sql,
507: resultSetType, resultSetConcurrency);
508:
509: setStatementState(oldStatement, newStatement);
510:
511: return newStatement;
512: }
513: }
|