source: Ballon/out/artifacts/geisa_artifact/WEB-INF/lib/mysql-connector-java-5.1.21/src/com/mysql/jdbc/JDBC4UpdatableResultSet.java @ 848

Last change on this file since 848 was 766, checked in by npipsl, 11 years ago
File size: 19.9 KB
Line 
1/*
2 Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
3 
4
5  The MySQL Connector/J is licensed under the terms of the GPLv2
6  <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors.
7  There are special exceptions to the terms and conditions of the GPLv2 as it is applied to
8  this software, see the FLOSS License Exception
9  <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
10
11  This program is free software; you can redistribute it and/or modify it under the terms
12  of the GNU General Public License as published by the Free Software Foundation; version 2
13  of the License.
14
15  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
16  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17  See the GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License along with this
20  program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth
21  Floor, Boston, MA 02110-1301  USA
22
23 */
24
25package com.mysql.jdbc;
26
27import java.io.InputStream;
28import java.io.Reader;
29import java.io.UnsupportedEncodingException;
30import java.sql.NClob;
31import java.sql.RowId;
32import java.sql.SQLException;
33import java.sql.SQLXML;
34
35import com.mysql.jdbc.Connection;
36import com.mysql.jdbc.Field;
37import com.mysql.jdbc.NotUpdatable;
38import com.mysql.jdbc.RowData;
39import com.mysql.jdbc.Statement;
40import com.mysql.jdbc.StringUtils;
41import com.mysql.jdbc.UpdatableResultSet;
42
43
44
45public class JDBC4UpdatableResultSet extends UpdatableResultSet {
46        public JDBC4UpdatableResultSet(String catalog, Field[] fields, RowData tuples, 
47                        MySQLConnection conn, StatementImpl creatorStmt) throws SQLException {
48                super(catalog, fields, tuples, conn, creatorStmt);
49        }
50
51        public void updateAsciiStream(int columnIndex, InputStream x) throws SQLException {
52                throw new NotUpdatable();
53               
54        }
55
56        public void updateAsciiStream(int columnIndex, InputStream x, long length) throws SQLException {
57                throw new NotUpdatable();
58               
59        }
60       
61        public void updateBinaryStream(int columnIndex, InputStream x) throws SQLException {
62                throw new NotUpdatable();
63               
64        }
65
66        public void updateBinaryStream(int columnIndex, InputStream x, long length) throws SQLException {
67                throw new NotUpdatable();
68               
69        }
70
71        public void updateBlob(int columnIndex, InputStream inputStream) throws SQLException {
72                throw new NotUpdatable();
73        }
74
75        public void updateBlob(int columnIndex, InputStream inputStream, long length) throws SQLException {
76                throw new NotUpdatable();
77               
78        }
79
80        public void updateCharacterStream(int columnIndex, Reader x) throws SQLException {
81                throw new NotUpdatable();
82               
83        }
84
85
86        public void updateCharacterStream(int columnIndex, Reader x, long length) throws SQLException {
87                throw new NotUpdatable();
88               
89        }
90
91        public void updateClob(int columnIndex, Reader reader) throws SQLException {
92                throw new NotUpdatable();
93               
94        }
95
96        public void updateClob(int columnIndex, Reader reader, long length) throws SQLException {
97                throw new NotUpdatable();
98               
99        }
100
101        public void updateNCharacterStream(int columnIndex, Reader x) throws SQLException {
102                throw new NotUpdatable();
103               
104        }
105
106        public void updateNCharacterStream(int columnIndex, Reader x, long length) throws SQLException {
107                updateNCharacterStream(columnIndex, x, (int) length);
108               
109        }
110       
111
112        public void updateNClob(int columnIndex, Reader reader) throws SQLException {
113                throw new NotUpdatable();
114               
115        }
116
117        public void updateNClob(int columnIndex, Reader reader, long length) throws SQLException {
118                throw new NotUpdatable();
119        }
120
121        public void updateSQLXML(int columnIndex, SQLXML xmlObject) throws SQLException {
122                throw new NotUpdatable();
123               
124        }
125       
126        public void updateRowId(int columnIndex, RowId x) throws SQLException {
127                throw new NotUpdatable();
128        }
129
130        public void updateAsciiStream(String columnLabel, InputStream x) throws SQLException {
131                updateAsciiStream(findColumn(columnLabel), x); 
132        }
133
134        public void updateAsciiStream(String columnLabel, InputStream x, long length) throws SQLException {
135                updateAsciiStream(findColumn(columnLabel), x, length);
136        }
137
138        public void updateBinaryStream(String columnLabel, InputStream x) throws SQLException {
139                updateBinaryStream(findColumn(columnLabel), x);
140        }
141
142        public void updateBinaryStream(String columnLabel, InputStream x, long length) throws SQLException {
143                updateBinaryStream(findColumn(columnLabel), x, length);
144        }
145
146        public void updateBlob(String columnLabel, InputStream inputStream) throws SQLException {
147                updateBlob(findColumn(columnLabel), inputStream);
148        }
149
150        public void updateBlob(String columnLabel, InputStream inputStream, long length) throws SQLException {
151                updateBlob(findColumn(columnLabel), inputStream, length);
152        }
153
154        public void updateCharacterStream(String columnLabel, Reader reader) throws SQLException {
155                updateCharacterStream(findColumn(columnLabel), reader);
156        }
157
158        public void updateCharacterStream(String columnLabel, Reader reader, long length) throws SQLException {
159                updateCharacterStream(findColumn(columnLabel), reader, length);
160        }
161       
162        public void updateClob(String columnLabel, Reader reader) throws SQLException {
163                updateClob(findColumn(columnLabel), reader);
164        }
165
166        public void updateClob(String columnLabel, Reader reader, long length) throws SQLException {
167                updateClob(findColumn(columnLabel), reader, length);
168        }
169       
170        public void updateNCharacterStream(String columnLabel, Reader reader) throws SQLException {
171                updateNCharacterStream(findColumn(columnLabel), reader);
172               
173        }
174
175        public void updateNCharacterStream(String columnLabel, Reader reader, long length) throws SQLException {
176                updateNCharacterStream(findColumn(columnLabel), reader, length);
177        }
178
179
180        public void updateNClob(String columnLabel, Reader reader) throws SQLException {
181                updateNClob(findColumn(columnLabel), reader);
182               
183        }
184
185        public void updateNClob(String columnLabel, Reader reader, long length) throws SQLException {
186                updateNClob(findColumn(columnLabel), reader, length);
187        }
188
189        public void updateSQLXML(String columnLabel, SQLXML xmlObject) throws SQLException {
190                updateSQLXML(findColumn(columnLabel), xmlObject);
191               
192        }
193
194        /**
195         * JDBC 4.0 Update a column with a character stream value. The updateXXX()
196         * methods are used to update column values in the current row, or the
197         * insert row. The updateXXX() methods do not update the underlying
198         * database, instead the updateRow() or insertRow() methods are called to
199         * update the database.
200         *
201         * @param columnIndex
202         *            the first column is 1, the second is 2, ...
203         * @param x
204         *            the new column value
205         * @param length
206         *            the length of the stream
207         *
208         * @exception SQLException
209         *                if a database-access error occurs
210         */
211        public synchronized void updateNCharacterStream(int columnIndex,
212                java.io.Reader x, int length) throws SQLException {
213            String fieldEncoding = this.fields[columnIndex - 1].getCharacterSet();
214            if (fieldEncoding == null || !fieldEncoding.equals("UTF-8")) {
215                throw new SQLException(
216                        "Can not call updateNCharacterStream() when field's character set isn't UTF-8");
217            }
218           
219            if (!this.onInsertRow) {
220                if (!this.doingUpdates) {
221                    this.doingUpdates = true;
222                    syncUpdate();
223                }
224       
225                ((com.mysql.jdbc.JDBC4PreparedStatement)this.updater).setNCharacterStream(columnIndex, x, length);
226            } else {
227                ((com.mysql.jdbc.JDBC4PreparedStatement)this.inserter).setNCharacterStream(columnIndex, x, length);
228       
229                if (x == null) {
230                    this.thisRow.setColumnValue(columnIndex - 1, null);
231                } else {
232                        this.thisRow.setColumnValue(columnIndex - 1, STREAM_DATA_MARKER);
233                }
234            }
235        }
236
237        /**
238         * JDBC 4.0 Update a column with a character stream value. The updateXXX()
239         * methods are used to update column values in the current row, or the
240         * insert row. The updateXXX() methods do not update the underlying
241         * database, instead the updateRow() or insertRow() methods are called to
242         * update the database.
243         *
244         * @param columnName
245         *            the name of the column
246         * @param reader
247         *            the new column value
248         * @param length
249         *            of the stream
250         *
251         * @exception SQLException
252         *                if a database-access error occurs
253         */
254        public synchronized void updateNCharacterStream(String columnName,
255                java.io.Reader reader, int length) throws SQLException {
256            updateNCharacterStream(findColumn(columnName), reader, length);
257        }
258
259        /**
260         * @see ResultSet#updateNClob(int, NClob)
261         */
262        public void updateNClob(int columnIndex, java.sql.NClob nClob)
263                throws SQLException {
264            String fieldEncoding = this.fields[columnIndex - 1].getCharacterSet();
265            if (fieldEncoding == null || !fieldEncoding.equals("UTF-8")) {
266                throw new SQLException("Can not call updateNClob() when field's character set isn't UTF-8");
267            }
268           
269            if (nClob == null) {
270                updateNull(columnIndex);
271            } else {
272                updateNCharacterStream(columnIndex, nClob.getCharacterStream(),
273                        (int) nClob.length());
274            }
275        }
276
277        /**
278         * @see ResultSet#updateClob(int, Clob)
279         */
280        public void updateNClob(String columnName, java.sql.NClob nClob)
281                throws SQLException {
282            updateNClob(findColumn(columnName), nClob);
283        }
284
285        /**
286         * JDBC 4.0 Update a column with NATIONAL CHARACTER. The updateXXX() methods
287         * are used to update column values in the current row, or the insert row.
288         * The updateXXX() methods do not update the underlying database, instead
289         * the updateRow() or insertRow() methods are called to update the database.
290         *
291         * @param columnIndex
292         *            the first column is 1, the second is 2, ...
293         * @param x
294         *            the new column value
295         *
296         * @exception SQLException
297         *                if a database-access error occurs
298         */
299        public synchronized void updateNString(int columnIndex, String x)
300                throws SQLException {
301            String fieldEncoding = this.fields[columnIndex - 1].getCharacterSet();
302            if (fieldEncoding == null || !fieldEncoding.equals("UTF-8")) {
303                throw new SQLException("Can not call updateNString() when field's character set isn't UTF-8");
304            }
305           
306            if (!this.onInsertRow) {
307                if (!this.doingUpdates) {
308                    this.doingUpdates = true;
309                    syncUpdate();
310                }
311       
312                ((com.mysql.jdbc.JDBC4PreparedStatement)this.updater).setNString(columnIndex, x);
313            } else {
314                ((com.mysql.jdbc.JDBC4PreparedStatement)this.inserter).setNString(columnIndex, x);
315       
316                if (x == null) {
317                         this.thisRow.setColumnValue(columnIndex - 1, null);
318                } else {
319                         this.thisRow.setColumnValue(columnIndex - 1, StringUtils.getBytes(x,
320                                this.charConverter, fieldEncoding,
321                                this.connection.getServerCharacterEncoding(),
322                                this.connection.parserKnowsUnicode(), getExceptionInterceptor()));
323                }
324            }
325        }
326
327        /**
328         * JDBC 4.0 Update a column with NATIONAL CHARACTER. The updateXXX() methods
329         * are used to update column values in the current row, or the insert row.
330         * The updateXXX() methods do not update the underlying database, instead
331         * the updateRow() or insertRow() methods are called to update the database.
332         *
333         * @param columnName
334         *            the name of the column
335         * @param x
336         *            the new column value
337         *
338         * @exception SQLException
339         *                if a database-access error occurs
340         */
341        public synchronized void updateNString(String columnName, String x)
342                throws SQLException {
343            updateNString(findColumn(columnName), x);
344        }
345
346        public int getHoldability() throws SQLException {
347                throw SQLError.notImplemented();
348        }
349
350        /**
351         * JDBC 4.0 Get a NCLOB column.
352         *
353         * @param columnIndex
354         *            the first column is 1, the second is 2, ...
355         *
356         * @return an object representing a NCLOB
357         *
358         * @throws SQLException
359         *             if an error occurs
360         */
361        protected java.sql.NClob getNativeNClob(int columnIndex)
362                        throws SQLException {
363                String stringVal = getStringForNClob(columnIndex);
364       
365                if (stringVal == null) {
366                        return null;
367                }
368       
369                return getNClobFromString(stringVal, columnIndex);
370        }
371
372        /**
373         * JDBC 4.0
374         *
375         * <p>
376         * Get the value of a column in the current row as a java.io.Reader.
377         * </p>
378         *
379         * @param columnIndex
380         *            the column to get the value from
381         *
382         * @return the value in the column as a java.io.Reader.
383         *
384         * @throws SQLException
385         *             if an error occurs
386         */
387        public Reader getNCharacterStream(int columnIndex) throws SQLException {
388                String fieldEncoding = this.fields[columnIndex - 1].getCharacterSet();
389                if (fieldEncoding == null || !fieldEncoding.equals("UTF-8")) {
390                        throw new SQLException(
391                                        "Can not call getNCharacterStream() when field's charset isn't UTF-8");
392                }
393               
394                return getCharacterStream(columnIndex);
395        }
396
397        /**
398         * JDBC 4.0
399         *
400         * <p>
401         * Get the value of a column in the current row as a java.io.Reader.
402         * </p>
403         *
404         * @param columnName
405         *            the column name to retrieve the value from
406         *
407         * @return the value as a java.io.Reader
408         *
409         * @throws SQLException
410         *             if an error occurs
411         */
412        public Reader getNCharacterStream(String columnName) throws SQLException {
413                return getNCharacterStream(findColumn(columnName));
414        }
415
416        /**
417         * JDBC 4.0 Get a NCLOB column.
418         *
419         * @param i
420         *            the first column is 1, the second is 2, ...
421         *
422         * @return an object representing a NCLOB
423         *
424         * @throws SQLException
425         *             if an error occurs
426         */
427        public NClob getNClob(int columnIndex) throws SQLException {
428                String fieldEncoding = this.fields[columnIndex - 1].getCharacterSet();
429               
430                if (fieldEncoding == null || !fieldEncoding.equals("UTF-8")) {
431                        throw new SQLException(
432                                        "Can not call getNClob() when field's charset isn't UTF-8");
433                }
434               
435                if (!this.isBinaryEncoded) {
436                        String asString = getStringForNClob(columnIndex);
437       
438                        if (asString == null) {
439                                return null;
440                        }
441       
442                        return new com.mysql.jdbc.JDBC4NClob(asString, getExceptionInterceptor());
443                }
444       
445                return getNativeNClob(columnIndex);
446        }
447
448        /**
449         * JDBC 4.0 Get a NCLOB column.
450         *
451         * @param colName
452         *            the column name
453         *
454         * @return an object representing a NCLOB
455         *
456         * @throws SQLException
457         *             if an error occurs
458         */
459        public NClob getNClob(String columnName) throws SQLException {
460                return getNClob(findColumn(columnName));
461        }
462
463        private final java.sql.NClob getNClobFromString(String stringVal,
464                        int columnIndex) throws SQLException {
465                return new com.mysql.jdbc.JDBC4NClob(stringVal, getExceptionInterceptor());
466        }
467
468        /**
469         * JDBC 4.0
470         *
471         * Get the value of a column in the current row as a Java String
472         *
473         * @param columnIndex
474         *            the first column is 1, the second is 2...
475         *
476         * @return the column value, null for SQL NULL
477         *
478         * @exception SQLException
479         *                if a database access error occurs
480         */
481        public String getNString(int columnIndex) throws SQLException {
482                String fieldEncoding = this.fields[columnIndex - 1].getCharacterSet();
483               
484                if (fieldEncoding == null || !fieldEncoding.equals("UTF-8")) {
485                        throw new SQLException(
486                                        "Can not call getNString() when field's charset isn't UTF-8");
487                }
488               
489                return getString(columnIndex);
490        }
491
492        /**
493         * JDBC 4.0
494         *
495         * The following routines simply convert the columnName into a columnIndex
496         * and then call the appropriate routine above.
497         *
498         * @param columnName
499         *            is the SQL name of the column
500         *
501         * @return the column value
502         *
503         * @exception SQLException
504         *                if a database access error occurs
505         */
506        public String getNString(String columnName) throws SQLException {
507                return getNString(findColumn(columnName));
508        }
509
510        public RowId getRowId(int columnIndex) throws SQLException {
511                throw SQLError.notImplemented();
512        }
513
514        public RowId getRowId(String columnLabel) throws SQLException {
515                return getRowId(findColumn(columnLabel));
516        }
517
518        public SQLXML getSQLXML(int columnIndex) throws SQLException {
519                return new JDBC4MysqlSQLXML(this, columnIndex, getExceptionInterceptor());
520        }
521
522        public SQLXML getSQLXML(String columnLabel) throws SQLException {
523                return getSQLXML(findColumn(columnLabel));
524        }
525
526        private String getStringForNClob(int columnIndex) throws SQLException {
527                String asString = null;
528       
529                String forcedEncoding = "UTF-8";
530       
531                try {
532                        byte[] asBytes = null;
533       
534                        if (!this.isBinaryEncoded) {
535                                asBytes = getBytes(columnIndex);
536                        } else {
537                                asBytes = getNativeBytes(columnIndex, true);
538                        }
539       
540                        if (asBytes != null) {
541                                asString = new String(asBytes, forcedEncoding);
542                        }
543                } catch (UnsupportedEncodingException uee) {
544                        throw SQLError.createSQLException("Unsupported character encoding "
545                                        + forcedEncoding, SQLError.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
546                }
547       
548                return asString;
549        }
550
551        public synchronized boolean isClosed() throws SQLException {
552                return this.isClosed;
553        }
554
555        /**
556         * Returns true if this either implements the interface argument or is
557         * directly or indirectly a wrapper for an object that does. Returns false
558         * otherwise. If this implements the interface then return true, else if
559         * this is a wrapper then return the result of recursively calling
560         * <code>isWrapperFor</code> on the wrapped object. If this does not
561         * implement the interface and is not a wrapper, return false. This method
562         * should be implemented as a low-cost operation compared to
563         * <code>unwrap</code> so that callers can use this method to avoid
564         * expensive <code>unwrap</code> calls that may fail. If this method
565         * returns true then calling <code>unwrap</code> with the same argument
566         * should succeed.
567         *
568         * @param interfaces
569         *            a Class defining an interface.
570         * @return true if this implements the interface or directly or indirectly
571         *         wraps an object that does.
572         * @throws java.sql.SQLException
573         *             if an error occurs while determining whether this is a
574         *             wrapper for an object with the given interface.
575         * @since 1.6
576         */
577        public boolean isWrapperFor(Class<?> iface) throws SQLException {
578                checkClosed();
579               
580                // This works for classes that aren't actually wrapping
581                // anything
582                return iface.isInstance(this);
583        }
584
585    /**
586         * Returns an object that implements the given interface to allow access to
587         * non-standard methods, or standard methods not exposed by the proxy. The
588         * result may be either the object found to implement the interface or a
589         * proxy for that object. If the receiver implements the interface then that
590         * is the object. If the receiver is a wrapper and the wrapped object
591         * implements the interface then that is the object. Otherwise the object is
592         * the result of calling <code>unwrap</code> recursively on the wrapped
593         * object. If the receiver is not a wrapper and does not implement the
594         * interface, then an <code>SQLException</code> is thrown.
595         *
596         * @param iface
597         *            A Class defining an interface that the result must implement.
598         * @return an object that implements the interface. May be a proxy for the
599         *         actual implementing object.
600         * @throws java.sql.SQLException
601         *             If no object found that implements the interface
602         * @since 1.6
603         */
604    public <T> T unwrap(java.lang.Class<T> iface) throws java.sql.SQLException {
605        try {
606                // This works for classes that aren't actually wrapping
607                // anything
608            return iface.cast(this);
609        } catch (ClassCastException cce) {
610            throw SQLError.createSQLException("Unable to unwrap to " + iface.toString(), 
611                        SQLError.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
612        }
613    }
614}
Note: See TracBrowser for help on using the repository browser.