~ubuntu-branches/ubuntu/maverick/hsqldb/maverick

« back to all changes in this revision

Viewing changes to src/org/hsqldb/jdbc/jdbcPreparedStatement.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-09-26 11:47:49 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060926114749-3jd0utm7w21x1iqt
Tags: 1.8.0.5-2ubuntu1
* Synchronise with Debian unstable; remaining changes:
  - build using java-gcj-compat.
* libhsqldb-java: Add gij as alternative dependency.
* Build a libhsqldb-java-gcj package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 2001-2005, The HSQL Development Group
2
 
 * All rights reserved.
3
 
 *
4
 
 * Redistribution and use in source and binary forms, with or without
5
 
 * modification, are permitted provided that the following conditions are met:
6
 
 *
7
 
 * Redistributions of source code must retain the above copyright notice, this
8
 
 * list of conditions and the following disclaimer.
9
 
 *
10
 
 * Redistributions in binary form must reproduce the above copyright notice,
11
 
 * this list of conditions and the following disclaimer in the documentation
12
 
 * and/or other materials provided with the distribution.
13
 
 *
14
 
 * Neither the name of the HSQL Development Group nor the names of its
15
 
 * contributors may be used to endorse or promote products derived from this
16
 
 * software without specific prior written permission.
17
 
 *
18
 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22
 
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23
 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26
 
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28
 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 
 */
30
 
 
31
 
 
 
1
/* Copyright (c) 2001-2005, The HSQL Development Group
 
2
 * All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 *
 
7
 * Redistributions of source code must retain the above copyright notice, this
 
8
 * list of conditions and the following disclaimer.
 
9
 *
 
10
 * Redistributions in binary form must reproduce the above copyright notice,
 
11
 * this list of conditions and the following disclaimer in the documentation
 
12
 * and/or other materials provided with the distribution.
 
13
 *
 
14
 * Neither the name of the HSQL Development Group nor the names of its
 
15
 * contributors may be used to endorse or promote products derived from this
 
16
 * software without specific prior written permission.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
19
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
21
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
 
22
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
23
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
24
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
25
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 
26
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
28
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 */
 
30
 
 
31
 
32
32
package org.hsqldb.jdbc;
33
33
 
34
34
import java.io.IOException;
82
82
// boucherb@users 20030801 - patch 1.7.2 - updated some setXXX methods
83
83
// boucherb@users 200403/4xx - doc 1.7.2 - javadoc updates toward 1.7.2 final
84
84
// boucherb@users 200403/4xx - patch 1.7.2 - eliminate eager buffer allocation from setXXXStream/Blob/Clob
 
85
// fredt@users 20060215 - patch 1.8.0 - check for unset parameters
85
86
 
86
87
/**
87
88
 * <!-- start generic documentation -->
121
122
 * preference to a jdbcStatement object, if a short-running SQL statement is
122
123
 * to be executed more than a small number of times. <p>
123
124
 *
124
 
 * Please note, however, that 1.7.2 does not yet provide a sophisticated
125
 
 * internal statement pooling facility.  For this reason, the observation
126
 
 * above is guaranteed to apply only under certain use patterns. <p>
127
 
 *
128
 
 * Specifically, when it can be otherwise avoided, it is to be considered poor
 
125
 * When it can be otherwise avoided, it is to be considered poor
129
126
 * practice to fully prepare (construct), parameterize, execute, fetch and
130
127
 * close a jdbcPreparedStatement object for each execution cycle. Indeed, under
131
 
 * HSQLDB 1.7.2, this practice is likely to be noticably <em>less</em>
 
128
 * HSQLDB 1.8.0, this practice is likely to be noticably <em>less</em>
132
129
 * performant for short-running statements than the equivalent process using
133
130
 * jdbcStatement objects, albeit far more convenient, less error prone and
134
131
 * certainly much less resource-intensive, especially when large binary and
199
196
 *
200
197
 * @author boucherb@users
201
198
 * @author fredt@users
202
 
 * @version 1.7.2
 
199
 * @version 1.8.0
203
200
 * @see jdbcConnection#prepareStatement
204
201
 * @see jdbcResultSet
205
202
 */
209
206
    /** The parameter values for the next non-batch execution. */
210
207
    protected Object[] parameterValues;
211
208
 
 
209
    /** Flags for bound variables. */
 
210
    protected boolean[] parameterSet;
 
211
 
 
212
    /** Flags for bound stream variables. */
 
213
    protected boolean[] parameterStream;
 
214
 
212
215
    /** The SQL types of the parameters. */
213
216
    protected int[] parameterTypes;
214
217
 
310
313
     * <div class="ReleaseSpecificDocumentation">
311
314
     * <h3>HSQLDB-Specific Information:</h3> <p>
312
315
     *
313
 
     * Including 1.7.2, prepared statements do not generate
 
316
     * Including 1.8.0, prepared statements do not generate
314
317
     * multiple fetchable results. <p>
315
318
     *
316
 
     * Following 1.7.2, it will be possible that statements
 
319
     * In future versions, it will be possible that statements
317
320
     * generate multiple fetchable results under certain conditions.
318
321
     * </div>
319
322
     *
367
370
        checkClosed();
368
371
        connection.clearWarningsNoCheck();
369
372
        checkIsRowCount(false);
 
373
        checkParametersSet();
370
374
 
371
375
        resultIn = null;
372
376
 
411
415
        checkClosed();
412
416
        connection.clearWarningsNoCheck();
413
417
        checkIsRowCount(true);
 
418
        checkParametersSet();
414
419
 
415
420
        resultIn = null;
416
421
 
835
840
    public void setAsciiStream(int parameterIndex, java.io.InputStream x,
836
841
                               int length) throws SQLException {
837
842
 
838
 
        checkSetParameterIndex(parameterIndex);
 
843
        checkSetParameterIndex(parameterIndex, true);
839
844
 
840
845
        String s;
841
846
 
894
899
    public void setUnicodeStream(int parameterIndex, java.io.InputStream x,
895
900
                                 int length) throws SQLException {
896
901
 
897
 
        checkSetParameterIndex(parameterIndex);
 
902
        checkSetParameterIndex(parameterIndex, true);
898
903
 
899
904
        String msg = null;
900
905
 
969
974
    public void setBinaryStream(int parameterIndex, java.io.InputStream x,
970
975
                                int length) throws SQLException {
971
976
 
972
 
        checkSetParameterIndex(parameterIndex);
 
977
        checkSetParameterIndex(parameterIndex, true);
973
978
 
974
979
        if (x == null) {
975
980
            throw Util.sqlException(Trace.error(Trace.INVALID_JDBC_ARGUMENT,
999
1004
 
1000
1005
            setParameter(parameterIndex, out.toByteArray());
1001
1006
        } catch (IOException e) {
1002
 
            throw Util.sqlException(Trace.INPUTSTREAM_ERROR, e.getMessage());
 
1007
            throw Util.sqlException(Trace.INPUTSTREAM_ERROR, e.toString());
1003
1008
        } finally {
1004
1009
            if (out != null) {
1005
1010
                try {
1023
1028
     * @exception SQLException if a database access error occurs
1024
1029
     */
1025
1030
    public void clearParameters() throws SQLException {
 
1031
 
1026
1032
        checkClosed();
1027
1033
        ArrayUtil.fillArray(parameterValues, null);
 
1034
        ArrayUtil.clearArray(ArrayUtil.CLASS_CODE_BOOLEAN, parameterSet, 0,
 
1035
                             parameterSet.length);
 
1036
 
 
1037
        if (parameterStream != null) {
 
1038
            ArrayUtil.clearArray(ArrayUtil.CLASS_CODE_BOOLEAN,
 
1039
                                 parameterStream, 0, parameterStream.length);
 
1040
        }
1028
1041
    }
1029
1042
 
1030
1043
    //----------------------------------------------------------------------
1191
1204
        int      len      = parameterValues.length;
1192
1205
        Object[] bpValues = new Object[len];
1193
1206
 
 
1207
        checkParametersSet();
1194
1208
        System.arraycopy(parameterValues, 0, bpValues, 0, len);
1195
1209
 
1196
1210
        if (batchResultOut == null) {
1240
1254
    public void setCharacterStream(int parameterIndex, java.io.Reader reader,
1241
1255
                                   int length) throws SQLException {
1242
1256
 
1243
 
        checkSetParameterIndex(parameterIndex);
 
1257
        checkSetParameterIndex(parameterIndex, true);
1244
1258
 
1245
1259
        if (reader == null) {
1246
1260
            String msg = "reader is null";
1296
1310
     * jdbcPreparedStatement)
1297
1311
     */
1298
1312
    public void setRef(int i, Ref x) throws SQLException {
1299
 
        throw Util.notSupported;
 
1313
        throw Util.notSupported();
1300
1314
    }
1301
1315
 
1302
1316
    /**
1344
1358
            return;
1345
1359
        }
1346
1360
 
1347
 
        checkSetParameterIndex(i);
 
1361
        checkSetParameterIndex(i, false);
1348
1362
 
1349
1363
        final long length = x.length();
1350
1364
 
1379
1393
 
1380
1394
            setParameter(i, out.toByteArray());
1381
1395
        } catch (IOException e) {
1382
 
            throw Util.sqlException(Trace.INPUTSTREAM_ERROR, e.getMessage());
 
1396
            throw Util.sqlException(Trace.INPUTSTREAM_ERROR, e.toString());
1383
1397
        } finally {
1384
1398
            if (out != null) {
1385
1399
                try {
1434
1448
            return;
1435
1449
        }
1436
1450
 
1437
 
        checkSetParameterIndex(i);
 
1451
        checkSetParameterIndex(i, false);
1438
1452
 
1439
1453
        final long length = x.length();
1440
1454
 
1496
1510
     *   jdbcPreparedStatement)
1497
1511
     */
1498
1512
    public void setArray(int i, Array x) throws SQLException {
1499
 
        throw Util.notSupported;
 
1513
        throw Util.notSupported();
1500
1514
    }
1501
1515
 
1502
1516
    /**
1584
1598
        try {
1585
1599
            s = HsqlDateTime.getDateString(x, cal);
1586
1600
        } catch (Exception e) {
1587
 
            throw Util.sqlException(Trace.INVALID_ESCAPE, e.getMessage());
 
1601
            throw Util.sqlException(Trace.INVALID_ESCAPE, e.toString());
1588
1602
        }
1589
1603
 
1590
1604
        setParameter(parameterIndex, s);
1622
1636
        try {
1623
1637
            s = HsqlDateTime.getTimeString(x, cal);
1624
1638
        } catch (Exception e) {
1625
 
            throw Util.sqlException(Trace.INVALID_ESCAPE, e.getMessage());
 
1639
            throw Util.sqlException(Trace.INVALID_ESCAPE, e.toString());
1626
1640
        }
1627
1641
 
1628
1642
        setParameter(parameterIndex, s);
1654
1668
    public void setTimestamp(int parameterIndex, Timestamp x,
1655
1669
                             Calendar cal) throws SQLException {
1656
1670
 
1657
 
        checkSetParameterIndex(parameterIndex);
 
1671
        checkSetParameterIndex(parameterIndex, false);
1658
1672
 
1659
1673
        if (cal != null && x != null) {
1660
1674
            int ns = x.getNanos();
1736
1750
//#ifdef JDBC3
1737
1751
    public void setURL(int parameterIndex,
1738
1752
                       java.net.URL x) throws SQLException {
1739
 
        throw Util.notSupported;
 
1753
        throw Util.notSupported();
1740
1754
    }
1741
1755
 
1742
1756
//#endif JDBC3
1864
1878
            pmdDescriptor   = (Result) row[0];
1865
1879
            parameterTypes  = pmdDescriptor.metaData.getParameterTypes();
1866
1880
            parameterValues = new Object[parameterTypes.length];
 
1881
            parameterSet    = new boolean[parameterTypes.length];
1867
1882
            parameterModes  = pmdDescriptor.metaData.paramMode;
1868
1883
        } catch (Exception e) {
1869
1884
            throw Trace.error(Trace.GENERAL_ERROR, e.toString());
1902
1917
     * @param i The parameter index to check
1903
1918
     * @throws SQLException if the specified parameter index is invalid
1904
1919
     */
1905
 
    protected void checkSetParameterIndex(int i) throws SQLException {
 
1920
    protected void checkSetParameterIndex(int i,
 
1921
                                          boolean isStream)
 
1922
                                          throws SQLException {
1906
1923
 
1907
1924
        int    mode;
1908
1925
        String msg;
1914
1931
 
1915
1932
            throw Util.sqlException(Trace.INVALID_JDBC_ARGUMENT, msg);
1916
1933
        }
 
1934
 
 
1935
        if (isStream) {
 
1936
            if (parameterStream == null) {
 
1937
                parameterStream = new boolean[parameterTypes.length];
 
1938
            }
 
1939
 
 
1940
            parameterStream[i - 1] = true;
 
1941
            parameterSet[i - 1]    = false;
 
1942
        } else {
 
1943
            parameterSet[i - 1] = true;
 
1944
        }
1917
1945
/*
1918
1946
        mode = parameterModes[i - 1];
1919
1947
 
1932
1960
    }
1933
1961
 
1934
1962
    /**
 
1963
     * Called just before execution or adding to batch, this ensures all the
 
1964
     * parameters have been set.<p>
 
1965
     *
 
1966
     * If a parameter has been set using a stream method, it should be set
 
1967
     * again for the next reuse. When set using other methods, the parameter
 
1968
     * setting is retained for the next use.
 
1969
     */
 
1970
    private void checkParametersSet() throws SQLException {
 
1971
        ;
 
1972
    }
 
1973
 
 
1974
/*
 
1975
    // do not check for compatibility with 1.8.0.x
 
1976
    private void checkParametersSet() throws SQLException {
 
1977
 
 
1978
        if (parameterStream == null) {
 
1979
            for (int i = 0; i < parameterSet.length; i++) {
 
1980
                if (!parameterSet[i]) {
 
1981
                    throw Util.sqlException(Trace.JDBC_PARAMETER_NOT_SET);
 
1982
                }
 
1983
            }
 
1984
        } else {
 
1985
            for (int i = 0; i < parameterSet.length; i++) {
 
1986
                if (!parameterSet[i] &&!parameterStream[i]) {
 
1987
                    throw Util.sqlException(Trace.JDBC_PARAMETER_NOT_SET);
 
1988
                }
 
1989
            }
 
1990
 
 
1991
            ArrayUtil.clearArray(ArrayUtil.CLASS_CODE_BOOLEAN,
 
1992
                                 parameterStream, 0, parameterStream.length);
 
1993
        }
 
1994
    }
 
1995
*/
 
1996
 
 
1997
    /**
1935
1998
     * The internal parameter value setter always converts the parameter to
1936
1999
     * the Java type required for data transmission. Target BINARY and OTHER
1937
2000
     * types are converted directly. All other target types are converted
1943
2006
     */
1944
2007
    private void setParameter(int i, Object o) throws SQLException {
1945
2008
 
1946
 
        checkSetParameterIndex(i);
 
2009
        checkSetParameterIndex(i, false);
1947
2010
 
1948
2011
        i--;
1949
2012
 
2027
2090
     */
2028
2091
    private void setIntParameter(int i, int value) throws SQLException {
2029
2092
 
2030
 
        checkSetParameterIndex(i);
 
2093
        checkSetParameterIndex(i, false);
2031
2094
 
2032
2095
        int outType = parameterTypes[i - 1];
2033
2096
 
2056
2119
     */
2057
2120
    private void setLongParameter(int i, long value) throws SQLException {
2058
2121
 
2059
 
        checkSetParameterIndex(i);
 
2122
        checkSetParameterIndex(i, false);
2060
2123
 
2061
2124
        int outType = parameterTypes[i - 1];
2062
2125
 
2085
2148
     * @throws SQLException always
2086
2149
     */
2087
2150
    public void addBatch(String sql) throws SQLException {
2088
 
        throw Util.notSupported;
 
2151
        throw Util.notSupported();
2089
2152
    }
2090
2153
 
2091
2154
    /**
2097
2160
     * @return nothing
2098
2161
     */
2099
2162
    public ResultSet executeQuery(String sql) throws SQLException {
2100
 
        throw Util.notSupported;
 
2163
        throw Util.notSupported();
2101
2164
    }
2102
2165
 
2103
2166
    /**
2109
2172
     * @return nothing
2110
2173
     */
2111
2174
    public boolean execute(String sql) throws SQLException {
2112
 
        throw Util.notSupported;
 
2175
        throw Util.notSupported();
2113
2176
    }
2114
2177
 
2115
2178
    /**
2121
2184
     * @return nothing
2122
2185
     */
2123
2186
    public int executeUpdate(String sql) throws SQLException {
2124
 
        throw Util.notSupported;
 
2187
        throw Util.notSupported();
2125
2188
    }
2126
2189
 
2127
2190
    /**
2152
2215
        }
2153
2216
 
2154
2217
        parameterValues = null;
 
2218
        parameterSet    = null;
 
2219
        parameterStream = null;
2155
2220
        parameterTypes  = null;
2156
2221
        parameterModes  = null;
2157
2222
        rsmdDescriptor  = null;