~ubuntu-branches/ubuntu/oneiric/mysql-connector-java/oneiric

« back to all changes in this revision

Viewing changes to src/com/mysql/jdbc/RowDataDynamic.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2007-11-30 10:34:13 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20071130103413-mxm4lpfrr4fnjbuj
Tags: 5.1.5+dfsg-1
* New upstream release. Closes: #450718.
* Add Homepage field to debian/control.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 Copyright (C) 2002-2004 MySQL AB
 
2
 Copyright (C) 2002-2007 MySQL AB
3
3
 
4
4
 This program is free software; you can redistribute it and/or modify
5
5
 it under the terms of version 2 of the GNU General Public License as 
33
33
 * Allows streaming of MySQL data.
34
34
 * 
35
35
 * @author dgan
36
 
 * @version $Id: RowDataDynamic.java 4891 2006-02-03 19:10:02Z mmatthews $
 
36
 * @version $Id: RowDataDynamic.java 6454 2007-06-21 17:03:53Z mmatthews $
37
37
 */
38
38
public class RowDataDynamic implements RowData {
39
 
        // ~ Instance fields
40
 
        // --------------------------------------------------------
41
39
 
42
40
        class OperationNotSupportedException extends SQLException {
43
41
                OperationNotSupportedException() {
48
46
 
49
47
        private int columnCount;
50
48
 
51
 
        private Field[] fields;
 
49
        private Field[] metadata;
52
50
 
53
51
        private int index = -1;
54
52
 
60
58
 
61
59
        private boolean isBinaryEncoded = false;
62
60
 
63
 
        private Object[] nextRow;
 
61
        private ResultSetRow nextRow;
64
62
 
65
 
        private ResultSet owner;
 
63
        private ResultSetImpl owner;
66
64
 
67
65
        private boolean streamerClosed = false;
 
66
        
 
67
        private boolean wasEmpty = false; // we don't know until we attempt to traverse
68
68
 
69
 
        // ~ Methods
70
 
        // ----------------------------------------------------------------
 
69
        private boolean useBufferRowExplicit;
71
70
 
72
71
        /**
73
72
         * Creates a new RowDataDynamic object.
74
73
         * 
75
74
         * @param io
76
75
         *            the connection to MySQL that this data is coming from
77
 
         * @param fields
78
 
         *            the fields that describe this data
 
76
         * @param metadata
 
77
         *            the metadata that describe this data
79
78
         * @param isBinaryEncoded
80
79
         *            is this data in native format?
81
80
         * @param colCount
88
87
                this.io = io;
89
88
                this.columnCount = colCount;
90
89
                this.isBinaryEncoded = isBinaryEncoded;
91
 
                this.fields = fields;
92
 
                nextRecord();
 
90
                this.metadata = fields;
 
91
                
 
92
                this.useBufferRowExplicit = MysqlIO.useBufferRowExplicit(this.metadata);
93
93
        }
94
94
 
95
95
        /**
100
100
         * @throws SQLException
101
101
         *             if a database error occurs
102
102
         */
103
 
        public void addRow(byte[][] row) throws SQLException {
 
103
        public void addRow(ResultSetRow row) throws SQLException {
104
104
                notSupported();
105
105
        }
106
106
 
141
141
         *             if a database error occurs
142
142
         */
143
143
        public void close() throws SQLException {
 
144
                // Belt and suspenders here - if we don't
 
145
                // have a reference to the connection
 
146
                // it's more than likely dead/gone and we
 
147
                // won't be able to consume rows anyway
 
148
 
 
149
                Object mutex = this;
 
150
 
 
151
                ConnectionImpl conn = null;
 
152
 
 
153
                if (this.owner != null) {
 
154
                        conn = this.owner.connection;
 
155
 
 
156
                        if (conn != null) {
 
157
                                mutex = conn.getMutex();
 
158
                        }
 
159
                }
144
160
 
145
161
                boolean hadMore = false;
146
162
                int howMuchMore = 0;
147
163
 
148
 
                // drain the rest of the records.
149
 
                while (this.hasNext()) {
150
 
                        this.next();
151
 
                        hadMore = true;
152
 
                        howMuchMore++;
153
 
 
154
 
                        if (howMuchMore % 100 == 0) {
155
 
                                Thread.yield();
156
 
                        }
157
 
                }
158
 
 
159
 
                if (this.owner != null) {
160
 
                        Connection conn = this.owner.connection;
161
 
 
162
 
                        if (conn != null && conn.getUseUsageAdvisor()) {
163
 
                                if (hadMore) {
164
 
 
165
 
                                        ProfileEventSink eventSink = ProfileEventSink
166
 
                                                        .getInstance(conn);
167
 
 
168
 
                                        eventSink
169
 
                                                        .consumeEvent(new ProfilerEvent(
170
 
                                                                        ProfilerEvent.TYPE_WARN,
171
 
                                                                        "", //$NON-NLS-1$
172
 
                                                                        this.owner.owningStatement == null ? "N/A" : this.owner.owningStatement.currentCatalog, //$NON-NLS-1$
173
 
                                                                        this.owner.connectionId,
174
 
                                                                        this.owner.owningStatement == null ? -1
175
 
                                                                                        : this.owner.owningStatement
176
 
                                                                                                        .getId(),
177
 
                                                                        -1,
178
 
                                                                        System.currentTimeMillis(),
179
 
                                                                        0,
180
 
                                                                        null,
181
 
                                                                        null,
182
 
                                                                        Messages.getString("RowDataDynamic.2") //$NON-NLS-1$
183
 
                                                                                        + howMuchMore
184
 
                                                                                        + Messages
185
 
                                                                                                        .getString("RowDataDynamic.3") //$NON-NLS-1$
186
 
                                                                                        + Messages
187
 
                                                                                                        .getString("RowDataDynamic.4") //$NON-NLS-1$
188
 
                                                                                        + Messages
189
 
                                                                                                        .getString("RowDataDynamic.5") //$NON-NLS-1$
190
 
                                                                                        + Messages
191
 
                                                                                                        .getString("RowDataDynamic.6") //$NON-NLS-1$
192
 
                                                                                        + this.owner.pointOfOrigin));
193
 
                                }
194
 
                        }
195
 
                }
196
 
 
197
 
                this.fields = null;
 
164
                synchronized (mutex) {
 
165
                        // drain the rest of the records.
 
166
                        while (next() != null) {
 
167
                                hadMore = true;
 
168
                                howMuchMore++;
 
169
 
 
170
                                if (howMuchMore % 100 == 0) {
 
171
                                        Thread.yield();
 
172
                                }
 
173
                        }
 
174
 
 
175
                        if (conn != null) {
 
176
                                if (!conn.getClobberStreamingResults() && 
 
177
                                                conn.getNetTimeoutForStreamingResults() > 0) {
 
178
                                        String oldValue = conn
 
179
                                        .getServerVariable("net_write_timeout");
 
180
 
 
181
                                        if (oldValue == null || oldValue.length() == 0) {
 
182
                                                oldValue = "60"; // the current default
 
183
                                        }
 
184
 
 
185
                                        this.io.clearInputStream();
 
186
                                        
 
187
                                        java.sql.Statement stmt = null;
 
188
                                        
 
189
                                        try {
 
190
                                                stmt = conn.createStatement();
 
191
                                                stmt.executeUpdate("SET net_write_timeout=" + oldValue);
 
192
                                        } finally {
 
193
                                                if (stmt != null) {
 
194
                                                        stmt.close();
 
195
                                                }
 
196
                                        }
 
197
                                }
 
198
                                
 
199
                                if (conn.getUseUsageAdvisor()) {
 
200
                                        if (hadMore) {
 
201
 
 
202
                                                ProfileEventSink eventSink = ProfileEventSink
 
203
                                                .getInstance(conn);
 
204
 
 
205
                                                eventSink
 
206
                                                .consumeEvent(new ProfilerEvent(
 
207
                                                                ProfilerEvent.TYPE_WARN,
 
208
                                                                "", //$NON-NLS-1$
 
209
                                                                this.owner.owningStatement == null ? "N/A" : this.owner.owningStatement.currentCatalog, //$NON-NLS-1$
 
210
                                                                this.owner.connectionId,
 
211
                                                                this.owner.owningStatement == null ? -1
 
212
                                                                                : this.owner.owningStatement
 
213
                                                                                                .getId(),
 
214
                                                                -1,
 
215
                                                                System.currentTimeMillis(),
 
216
                                                                0,
 
217
                                                                Constants.MILLIS_I18N,
 
218
                                                                null,
 
219
                                                                null,
 
220
                                                                Messages.getString("RowDataDynamic.2") //$NON-NLS-1$
 
221
                                                                                + howMuchMore
 
222
                                                                                + Messages
 
223
                                                                                                .getString("RowDataDynamic.3") //$NON-NLS-1$
 
224
                                                                                + Messages
 
225
                                                                                                .getString("RowDataDynamic.4") //$NON-NLS-1$
 
226
                                                                                + Messages
 
227
                                                                                                .getString("RowDataDynamic.5") //$NON-NLS-1$
 
228
                                                                                + Messages
 
229
                                                                                                .getString("RowDataDynamic.6") //$NON-NLS-1$
 
230
                                                                                + this.owner.pointOfOrigin));
 
231
                                        }
 
232
                                }
 
233
                        }
 
234
                }
 
235
 
 
236
                this.metadata = null;
198
237
                this.owner = null;
199
238
        }
200
239
 
207
246
         * @throws SQLException
208
247
         *             if a database error occurs
209
248
         */
210
 
        public Object[] getAt(int ind) throws SQLException {
 
249
        public ResultSetRow getAt(int ind) throws SQLException {
211
250
                notSupported();
212
251
 
213
252
                return null;
229
268
        /**
230
269
         * @see com.mysql.jdbc.RowData#getOwner()
231
270
         */
232
 
        public ResultSet getOwner() {
 
271
        public ResultSetInternalMethods getOwner() {
233
272
                return this.owner;
234
273
        }
235
274
 
343
382
         * @throws SQLException
344
383
         *             if a database error occurs
345
384
         */
346
 
        public Object[] next() throws SQLException {
347
 
                if (this.index != Integer.MAX_VALUE) {
348
 
                        this.index++;
349
 
                }
 
385
        public ResultSetRow next() throws SQLException {
 
386
                
350
387
 
351
 
                Object[] ret = this.nextRow;
352
388
                nextRecord();
353
389
 
354
 
                return ret;
 
390
                if (this.nextRow == null && !this.streamerClosed) {
 
391
                        this.io.closeStreamer(this);
 
392
                        this.streamerClosed = true;
 
393
                }
 
394
                
 
395
                if (this.nextRow != null) {
 
396
                        if (this.index != Integer.MAX_VALUE) {
 
397
                                this.index++;
 
398
                        }
 
399
                }
 
400
                
 
401
                return this.nextRow;
355
402
        }
356
403
 
 
404
 
357
405
        private void nextRecord() throws SQLException {
358
406
 
359
407
                try {
360
 
                        if (!this.isAtEnd) {
361
 
 
362
 
                                this.nextRow = this.io.nextRow(this.fields, this.columnCount,
 
408
                        if (!this.isAtEnd) {                            
 
409
                                this.nextRow = this.io.nextRow(this.metadata, this.columnCount,
363
410
                                                this.isBinaryEncoded,
364
 
                                                java.sql.ResultSet.CONCUR_READ_ONLY);
 
411
                                                java.sql.ResultSet.CONCUR_READ_ONLY, true, 
 
412
                                                this.useBufferRowExplicit, true, null);
365
413
 
366
414
                                if (this.nextRow == null) {
367
415
                                        this.isAtEnd = true;
 
416
                                        
 
417
                                        if (this.index == -1) {
 
418
                                                this.wasEmpty = true;
 
419
                                        }
368
420
                                }
369
421
                        } else {
370
422
                                this.isAfterEnd = true;
371
423
                        }
372
424
                } catch (SQLException sqlEx) {
 
425
                        if (sqlEx instanceof StreamingNotifiable) {
 
426
                                ((StreamingNotifiable)sqlEx).setWasStreamingResults();
 
427
                        }
 
428
                        
373
429
                        // don't wrap SQLExceptions
374
430
                        throw sqlEx;
375
431
                } catch (Exception ex) {
402
458
                notSupported();
403
459
        }
404
460
 
405
 
        // ~ Inner Classes
406
 
        // ----------------------------------------------------------
407
 
 
408
461
        /**
409
462
         * Moves the current position in the result set to the given row number.
410
463
         * 
418
471
        }
419
472
 
420
473
        /**
421
 
         * @see com.mysql.jdbc.RowData#setOwner(com.mysql.jdbc.ResultSet)
 
474
         * @see com.mysql.jdbc.RowData#setOwner(com.mysql.jdbc.ResultSetInternalMethods)
422
475
         */
423
 
        public void setOwner(ResultSet rs) {
 
476
        public void setOwner(ResultSetImpl rs) {
424
477
                this.owner = rs;
425
478
        }
426
479
 
433
486
                return RESULT_SET_SIZE_UNKNOWN;
434
487
        }
435
488
 
 
489
        public boolean wasEmpty() {
 
490
                return this.wasEmpty;
 
491
        }
 
492
 
 
493
        public void setMetadata(Field[] metadata) {
 
494
                this.metadata = metadata;
 
495
        }
436
496
}