~maxb/connectorj/5.0

« back to all changes in this revision

Viewing changes to connector-j/src/com/mysql/jdbc/Statement.java

  • Committer: mmatthews
  • Date: 2007-10-11 20:04:05 UTC
  • Revision ID: svn-v3-trunk0:bce1ec22-edf6-0310-a851-a6aae2aa6c29:branches%2Fbranch_5_0:6637
Changed layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 Copyright (C) 2002-2007 MySQL AB
3
 
 
4
 
 This program is free software; you can redistribute it and/or modify
5
 
 it under the terms of version 2 of the GNU General Public License as 
6
 
 published by the Free Software Foundation.
7
 
 
8
 
 There are special exceptions to the terms and conditions of the GPL 
9
 
 as it is applied to this software. View the full text of the 
10
 
 exception in file EXCEPTIONS-CONNECTOR-J in the directory of this 
11
 
 software distribution.
12
 
 
13
 
 This program is distributed in the hope that it will be useful,
14
 
 but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
 GNU General Public License for more details.
17
 
 
18
 
 You should have received a copy of the GNU General Public License
19
 
 along with this program; if not, write to the Free Software
20
 
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
 
 
22
 
 
23
 
 
24
 
 */
25
 
package com.mysql.jdbc;
26
 
 
27
 
import com.mysql.jdbc.exceptions.MySQLTimeoutException;
28
 
import com.mysql.jdbc.profiler.ProfileEventSink;
29
 
import com.mysql.jdbc.profiler.ProfilerEvent;
30
 
import com.mysql.jdbc.util.LRUCache;
31
 
 
32
 
import java.sql.DataTruncation;
33
 
import java.sql.SQLException;
34
 
import java.sql.SQLWarning;
35
 
import java.sql.Types;
36
 
 
37
 
import java.util.ArrayList;
38
 
import java.util.Calendar;
39
 
import java.util.GregorianCalendar;
40
 
import java.util.HashMap;
41
 
import java.util.Iterator;
42
 
import java.util.List;
43
 
import java.util.Locale;
44
 
import java.util.TimerTask;
45
 
 
46
 
/**
47
 
 * A Statement object is used for executing a static SQL statement and obtaining
48
 
 * the results produced by it.
49
 
 * 
50
 
 * <p>
51
 
 * Only one ResultSet per Statement can be open at any point in time. Therefore,
52
 
 * if the reading of one ResultSet is interleaved with the reading of another,
53
 
 * each must have been generated by different Statements. All statement execute
54
 
 * methods implicitly close a statement's current ResultSet if an open one
55
 
 * exists.
56
 
 * </p>
57
 
 * 
58
 
 * @author Mark Matthews
59
 
 * @version $Id: Statement.java 4624 2005-11-28 14:24:29 -0600 (Mon, 28 Nov
60
 
 *          2005) mmatthews $
61
 
 * 
62
 
 * @see java.sql.Statement
63
 
 * @see ResultSet
64
 
 */
65
 
public class Statement implements java.sql.Statement {
66
 
        protected static final String PING_MARKER = "/* ping */";
67
 
 
68
 
        /**
69
 
         * Thread used to implement query timeouts...Eventually we could be more
70
 
         * efficient and have one thread with timers, but this is a straightforward
71
 
         * and simple way to implement a feature that isn't used all that often.
72
 
         */
73
 
        class CancelTask extends TimerTask {
74
 
 
75
 
                long connectionId = 0;
76
 
                SQLException caughtWhileCancelling = null;
77
 
                
78
 
                CancelTask() throws SQLException {
79
 
                        connectionId = connection.getIO().getThreadId();
80
 
                }
81
 
 
82
 
                public void run() {
83
 
 
84
 
                        Thread cancelThread = new Thread() {
85
 
 
86
 
                                public void run() {
87
 
                                        Connection cancelConn = null;
88
 
                                        java.sql.Statement cancelStmt = null;
89
 
 
90
 
                                        try {
91
 
                                                synchronized (cancelTimeoutMutex) {
92
 
                                                        cancelConn = connection.duplicate();
93
 
                                                        cancelStmt = cancelConn.createStatement();
94
 
                                                        cancelStmt.execute("KILL QUERY " + connectionId);
95
 
                                                        wasCancelled = true;
96
 
                                                }
97
 
                                        } catch (SQLException sqlEx) {
98
 
                                                caughtWhileCancelling = sqlEx;
99
 
                                        } catch (NullPointerException npe) {
100
 
                                                // Case when connection closed while starting to cancel
101
 
                                                // We can't easily synchronize this, because then one thread
102
 
                                                // can't cancel() a running query
103
 
                                                
104
 
                                                // ignore, we shouldn't re-throw this, because the connection's
105
 
                                                // already closed, so the statement has been timed out.
106
 
                                        } finally {
107
 
                                                if (cancelStmt != null) {
108
 
                                                        try {
109
 
                                                                cancelStmt.close();
110
 
                                                        } catch (SQLException sqlEx) {
111
 
                                                                throw new RuntimeException(sqlEx.toString());
112
 
                                                        }
113
 
                                                }
114
 
 
115
 
                                                if (cancelConn != null) {
116
 
                                                        try {
117
 
                                                                cancelConn.close();
118
 
                                                        } catch (SQLException sqlEx) {
119
 
                                                                throw new RuntimeException(sqlEx.toString());
120
 
                                                        }
121
 
                                                }
122
 
                                        }
123
 
                                }
124
 
                        };
125
 
 
126
 
                        cancelThread.start();
127
 
                }
128
 
        }
129
 
        
130
 
        /** Mutex to prevent race between returning query results and noticing
131
 
    that we're timed-out or cancelled. */
132
 
 
133
 
        protected Object cancelTimeoutMutex = new Object();
134
 
 
135
 
        /** Used to generate IDs when profiling. */
136
 
        protected static int statementCounter = 1;
137
 
 
138
 
        public final static byte USES_VARIABLES_FALSE = 0;
139
 
 
140
 
        public final static byte USES_VARIABLES_TRUE = 1;
141
 
 
142
 
        public final static byte USES_VARIABLES_UNKNOWN = -1;
143
 
 
144
 
        protected boolean wasCancelled = false;
145
 
 
146
 
        /** Holds batched commands */
147
 
        protected List batchedArgs;
148
 
 
149
 
        /** The character converter to use (if available) */
150
 
        protected SingleByteCharsetConverter charConverter = null;
151
 
 
152
 
        /** The character encoding to use (if available) */
153
 
        protected String charEncoding = null;
154
 
 
155
 
        /** The connection that created us */
156
 
        protected Connection connection = null;
157
 
        
158
 
        protected long connectionId = 0;
159
 
 
160
 
        /** The catalog in use */
161
 
        protected String currentCatalog = null;
162
 
 
163
 
        /** Should we process escape codes? */
164
 
        protected boolean doEscapeProcessing = true;
165
 
 
166
 
        /** If we're profiling, where should events go to? */
167
 
        protected ProfileEventSink eventSink = null;
168
 
 
169
 
        /** The number of rows to fetch at a time (currently ignored) */
170
 
        private int fetchSize = 0;
171
 
 
172
 
        /** Has this statement been closed? */
173
 
        protected boolean isClosed = false;
174
 
 
175
 
        /** The auto_increment value for the last insert */
176
 
        protected long lastInsertId = -1;
177
 
 
178
 
        /** The max field size for this statement */
179
 
        protected int maxFieldSize = MysqlIO.getMaxBuf();
180
 
 
181
 
        /**
182
 
         * The maximum number of rows to return for this statement (-1 means _all_
183
 
         * rows)
184
 
         */
185
 
        protected int maxRows = -1;
186
 
 
187
 
        /** Has someone changed this for this statement? */
188
 
        protected boolean maxRowsChanged = false;
189
 
 
190
 
        /** List of currently-open ResultSets */
191
 
        protected List openResults = new ArrayList();
192
 
 
193
 
        /** Are we in pedantic mode? */
194
 
        protected boolean pedantic = false;
195
 
 
196
 
        /**
197
 
         * Where this statement was created, only used if profileSql or
198
 
         * useUsageAdvisor set to true.
199
 
         */
200
 
        protected Throwable pointOfOrigin;
201
 
 
202
 
        /** Should we profile? */
203
 
        protected boolean profileSQL = false;
204
 
 
205
 
        /** The current results */
206
 
        protected ResultSet results = null;
207
 
 
208
 
        /** The concurrency for this result set (updatable or not) */
209
 
        protected int resultSetConcurrency = 0;
210
 
 
211
 
        /** The type of this result set (scroll sensitive or in-sensitive) */
212
 
        protected int resultSetType = 0;
213
 
 
214
 
        /** Used to identify this statement when profiling. */
215
 
        protected int statementId;
216
 
 
217
 
        /** The timeout for a query */
218
 
        protected int timeoutInMillis = 0;
219
 
 
220
 
        /** The update count for this statement */
221
 
        protected long updateCount = -1;
222
 
 
223
 
        /** Should we use the usage advisor? */
224
 
        protected boolean useUsageAdvisor = false;
225
 
 
226
 
        /** The warnings chain. */
227
 
        protected SQLWarning warningChain = null;
228
 
 
229
 
        /**
230
 
         * Should this statement hold results open over .close() irregardless of
231
 
         * connection's setting?
232
 
         */
233
 
        protected boolean holdResultsOpenOverClose = false;
234
 
 
235
 
        protected ArrayList batchedGeneratedKeys = null;
236
 
 
237
 
        protected boolean retrieveGeneratedKeys = false;
238
 
 
239
 
        protected boolean continueBatchOnError = false;
240
 
        
241
 
        protected PingTarget pingTarget = null;
242
 
        
243
 
        
244
 
        /**
245
 
         * Constructor for a Statement.
246
 
         * 
247
 
         * @param c
248
 
         *            the Connection instantation that creates us
249
 
         * @param catalog
250
 
         *            the database name in use when we were created
251
 
         * 
252
 
         * @throws SQLException
253
 
         *             if an error occurs.
254
 
         */
255
 
        public Statement(Connection c, String catalog) throws SQLException {
256
 
                if ((c == null) || c.isClosed()) {
257
 
                        throw SQLError.createSQLException(
258
 
                                        Messages.getString("Statement.0"), //$NON-NLS-1$
259
 
                                        SQLError.SQL_STATE_CONNECTION_NOT_OPEN); //$NON-NLS-1$ //$NON-NLS-2$
260
 
                }
261
 
 
262
 
                this.connection = c;
263
 
                this.connectionId = this.connection.getId();
264
 
                
265
 
                this.currentCatalog = catalog;
266
 
                this.pedantic = this.connection.getPedantic();
267
 
                this.continueBatchOnError = this.connection.getContinueBatchOnError();
268
 
                
269
 
                if (!this.connection.getDontTrackOpenResources()) {
270
 
                        this.connection.registerStatement(this);
271
 
                }
272
 
 
273
 
                //
274
 
                // Adjust, if we know it
275
 
                //
276
 
 
277
 
                if (this.connection != null) {
278
 
                        this.maxFieldSize = this.connection.getMaxAllowedPacket();
279
 
 
280
 
                        int defaultFetchSize = this.connection.getDefaultFetchSize();
281
 
 
282
 
                        if (defaultFetchSize != 0) {
283
 
                                setFetchSize(defaultFetchSize);
284
 
                        }
285
 
                }
286
 
 
287
 
                if (this.connection.getUseUnicode()) {
288
 
                        this.charEncoding = this.connection.getEncoding();
289
 
 
290
 
                        this.charConverter = this.connection
291
 
                                        .getCharsetConverter(this.charEncoding);
292
 
                }
293
 
 
294
 
                boolean profiling = this.connection.getProfileSql()
295
 
                                || this.connection.getUseUsageAdvisor();
296
 
 
297
 
                if (this.connection.getAutoGenerateTestcaseScript() || profiling) {
298
 
                        this.statementId = statementCounter++;
299
 
                }
300
 
 
301
 
                if (profiling) {
302
 
                        this.pointOfOrigin = new Throwable();
303
 
                        this.profileSQL = this.connection.getProfileSql();
304
 
                        this.useUsageAdvisor = this.connection.getUseUsageAdvisor();
305
 
                        this.eventSink = ProfileEventSink.getInstance(this.connection);
306
 
                }
307
 
 
308
 
                int maxRowsConn = this.connection.getMaxRows();
309
 
 
310
 
                if (maxRowsConn != -1) {
311
 
                        setMaxRows(maxRowsConn);
312
 
                }
313
 
        }
314
 
 
315
 
        /**
316
 
         * DOCUMENT ME!
317
 
         * 
318
 
         * @param sql
319
 
         *            DOCUMENT ME!
320
 
         * 
321
 
         * @throws SQLException
322
 
         *             DOCUMENT ME!
323
 
         */
324
 
        public synchronized void addBatch(String sql) throws SQLException {
325
 
                if (this.batchedArgs == null) {
326
 
                        this.batchedArgs = new ArrayList();
327
 
                }
328
 
 
329
 
                if (sql != null) {
330
 
                        this.batchedArgs.add(sql);
331
 
                }
332
 
        }
333
 
 
334
 
        /**
335
 
         * Cancels this Statement object if both the DBMS and driver support
336
 
         * aborting an SQL statement. This method can be used by one thread to
337
 
         * cancel a statement that is being executed by another thread.
338
 
         */
339
 
        public void cancel() throws SQLException {
340
 
                if (!this.isClosed &&
341
 
                                this.connection != null && 
342
 
                                this.connection.versionMeetsMinimum(5, 0, 0)) {
343
 
                        Connection cancelConn = null;
344
 
                        java.sql.Statement cancelStmt = null;
345
 
 
346
 
                        try {
347
 
                                synchronized (this.cancelTimeoutMutex) {
348
 
                                        cancelConn = this.connection.duplicate();
349
 
                                        cancelStmt = cancelConn.createStatement();
350
 
                                        cancelStmt.execute("KILL QUERY "
351
 
                                                        + this.connection.getIO().getThreadId());
352
 
                                        this.wasCancelled = true;
353
 
                                }
354
 
                        } catch (NullPointerException npe) {
355
 
                                // Case when connection closed while starting to cancel
356
 
                                // We can't easily synchronize this, because then one thread
357
 
                                // can't cancel() a running query
358
 
                                
359
 
                                throw SQLError.createSQLException(Messages
360
 
                                                .getString("Statement.49"), //$NON-NLS-1$
361
 
                                                SQLError.SQL_STATE_CONNECTION_NOT_OPEN); //$NON-NLS-1$
362
 
                        } finally {
363
 
                                if (cancelStmt != null) {
364
 
                                        cancelStmt.close();
365
 
                                }
366
 
 
367
 
                                if (cancelConn != null) {
368
 
                                        cancelConn.close();
369
 
                                }
370
 
                        }
371
 
 
372
 
                }
373
 
        }
374
 
 
375
 
        // --------------------------JDBC 2.0-----------------------------
376
 
 
377
 
        /**
378
 
         * Checks if closed() has been called, and throws an exception if so
379
 
         * 
380
 
         * @throws SQLException
381
 
         *             if this statement has been closed
382
 
         */
383
 
        protected void checkClosed() throws SQLException {
384
 
                if (this.isClosed) {
385
 
                        throw SQLError.createSQLException(Messages
386
 
                                        .getString("Statement.49"), //$NON-NLS-1$
387
 
                                        SQLError.SQL_STATE_CONNECTION_NOT_OPEN); //$NON-NLS-1$
388
 
                }
389
 
        }
390
 
 
391
 
        /**
392
 
         * Checks if the given SQL query with the given first non-ws char is a DML
393
 
         * statement. Throws an exception if it is.
394
 
         * 
395
 
         * @param sql
396
 
         *            the SQL to check
397
 
         * @param firstStatementChar
398
 
         *            the UC first non-ws char of the statement
399
 
         * 
400
 
         * @throws SQLException
401
 
         *             if the statement contains DML
402
 
         */
403
 
        protected void checkForDml(String sql, char firstStatementChar)
404
 
                        throws SQLException {
405
 
                if ((firstStatementChar == 'I') || (firstStatementChar == 'U')
406
 
                                || (firstStatementChar == 'D') || (firstStatementChar == 'A')
407
 
                                || (firstStatementChar == 'C')) {
408
 
                        String noCommentSql = StringUtils.stripComments(sql,
409
 
                                        "'\"", "'\"", true, false, true, true);
410
 
                        
411
 
                        if (StringUtils.startsWithIgnoreCaseAndWs(noCommentSql, "INSERT") //$NON-NLS-1$
412
 
                                        || StringUtils.startsWithIgnoreCaseAndWs(noCommentSql, "UPDATE") //$NON-NLS-1$
413
 
                                        || StringUtils.startsWithIgnoreCaseAndWs(noCommentSql, "DELETE") //$NON-NLS-1$
414
 
                                        || StringUtils.startsWithIgnoreCaseAndWs(noCommentSql, "DROP") //$NON-NLS-1$
415
 
                                        || StringUtils.startsWithIgnoreCaseAndWs(noCommentSql, "CREATE") //$NON-NLS-1$
416
 
                                        || StringUtils.startsWithIgnoreCaseAndWs(noCommentSql, "ALTER")) { //$NON-NLS-1$
417
 
                                throw SQLError.createSQLException(Messages
418
 
                                                .getString("Statement.57"), //$NON-NLS-1$
419
 
                                                SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$
420
 
                        }
421
 
                }
422
 
        }
423
 
 
424
 
        /**
425
 
         * Method checkNullOrEmptyQuery.
426
 
         * 
427
 
         * @param sql
428
 
         *            the SQL to check
429
 
         * 
430
 
         * @throws SQLException
431
 
         *             if query is null or empty.
432
 
         */
433
 
        protected void checkNullOrEmptyQuery(String sql) throws SQLException {
434
 
                if (sql == null) {
435
 
                        throw SQLError.createSQLException(Messages
436
 
                                        .getString("Statement.59"), //$NON-NLS-1$
437
 
                                        SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$ //$NON-NLS-2$
438
 
                }
439
 
 
440
 
                if (sql.length() == 0) {
441
 
                        throw SQLError.createSQLException(Messages
442
 
                                        .getString("Statement.61"), //$NON-NLS-1$
443
 
                                        SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$ //$NON-NLS-2$
444
 
                }
445
 
        }
446
 
 
447
 
        /**
448
 
         * JDBC 2.0 Make the set of commands in the current batch empty. This method
449
 
         * is optional.
450
 
         * 
451
 
         * @exception SQLException
452
 
         *                if a database-access error occurs, or the driver does not
453
 
         *                support batch statements
454
 
         */
455
 
        public synchronized void clearBatch() throws SQLException {
456
 
                if (this.batchedArgs != null) {
457
 
                        this.batchedArgs.clear();
458
 
                }
459
 
        }
460
 
 
461
 
        /**
462
 
         * After this call, getWarnings returns null until a new warning is reported
463
 
         * for this Statement.
464
 
         * 
465
 
         * @exception SQLException
466
 
         *                if a database access error occurs (why?)
467
 
         */
468
 
        public void clearWarnings() throws SQLException {
469
 
                this.warningChain = null;
470
 
        }
471
 
 
472
 
        /**
473
 
         * In many cases, it is desirable to immediately release a Statement's
474
 
         * database and JDBC resources instead of waiting for this to happen when it
475
 
         * is automatically closed. The close method provides this immediate
476
 
         * release.
477
 
         * 
478
 
         * <p>
479
 
         * <B>Note:</B> A Statement is automatically closed when it is garbage
480
 
         * collected. When a Statement is closed, its current ResultSet, if one
481
 
         * exists, is also closed.
482
 
         * </p>
483
 
         * 
484
 
         * @exception SQLException
485
 
         *                if a database access error occurs
486
 
         */
487
 
        public void close() throws SQLException {
488
 
                realClose(true, true);
489
 
        }
490
 
 
491
 
        /**
492
 
         * Close any open result sets that have been 'held open'
493
 
         */
494
 
        protected void closeAllOpenResults() {
495
 
                if (this.openResults != null) {
496
 
                        for (Iterator iter = this.openResults.iterator(); iter.hasNext();) {
497
 
                                ResultSet element = (ResultSet) iter.next();
498
 
 
499
 
                                try {
500
 
                                        element.realClose(false);
501
 
                                } catch (SQLException sqlEx) {
502
 
                                        AssertionFailedException.shouldNotHappen(sqlEx);
503
 
                                }
504
 
                        }
505
 
 
506
 
                        this.openResults.clear();
507
 
                }
508
 
        }
509
 
 
510
 
        /**
511
 
         * @param sql
512
 
         * @return
513
 
         */
514
 
        private ResultSet createResultSetUsingServerFetch(String sql)
515
 
                        throws SQLException {
516
 
                java.sql.PreparedStatement pStmt = this.connection.prepareStatement(
517
 
                                sql, this.resultSetType, this.resultSetConcurrency);
518
 
 
519
 
                pStmt.setFetchSize(this.fetchSize);
520
 
                
521
 
                if (this.maxRows > -1) {
522
 
                        pStmt.setMaxRows(this.maxRows);
523
 
                }
524
 
                
525
 
                pStmt.execute();
526
 
 
527
 
                //
528
 
                // Need to be able to get resultset irrespective if we issued DML or
529
 
                // not to make this work.
530
 
                //
531
 
                ResultSet rs = ((com.mysql.jdbc.Statement) pStmt)
532
 
                                .getResultSetInternal();
533
 
 
534
 
                rs
535
 
                                .setStatementUsedForFetchingRows((com.mysql.jdbc.PreparedStatement) pStmt);
536
 
 
537
 
                this.results = rs;
538
 
 
539
 
                return rs;
540
 
        }
541
 
 
542
 
        /**
543
 
         * We only stream result sets when they are forward-only, read-only, and the
544
 
         * fetch size has been set to Integer.MIN_VALUE
545
 
         * 
546
 
         * @return true if this result set should be streamed row at-a-time, rather
547
 
         *         than read all at once.
548
 
         */
549
 
        protected boolean createStreamingResultSet() {
550
 
                return ((this.resultSetType == java.sql.ResultSet.TYPE_FORWARD_ONLY)
551
 
                                && (this.resultSetConcurrency == java.sql.ResultSet.CONCUR_READ_ONLY) && (this.fetchSize == Integer.MIN_VALUE));
552
 
        }
553
 
 
554
 
        /**
555
 
         * Workaround for containers that 'check' for sane values of
556
 
         * Statement.setFetchSize().
557
 
         * 
558
 
         * @throws SQLException
559
 
         */
560
 
        public void enableStreamingResults() throws SQLException {
561
 
                setFetchSize(Integer.MIN_VALUE);
562
 
                setResultSetType(ResultSet.TYPE_FORWARD_ONLY);
563
 
        }
564
 
 
565
 
        /**
566
 
         * Execute a SQL statement that may return multiple results. We don't have
567
 
         * to worry about this since we do not support multiple ResultSets. You can
568
 
         * use getResultSet or getUpdateCount to retrieve the result.
569
 
         * 
570
 
         * @param sql
571
 
         *            any SQL statement
572
 
         * 
573
 
         * @return true if the next result is a ResulSet, false if it is an update
574
 
         *         count or there are no more results
575
 
         * 
576
 
         * @exception SQLException
577
 
         *                if a database access error occurs
578
 
         */
579
 
        public boolean execute(String sql) throws SQLException {
580
 
                checkClosed();
581
 
                
582
 
                Connection locallyScopedConn = this.connection;
583
 
                
584
 
                synchronized (locallyScopedConn.getMutex()) {
585
 
                        synchronized (this.cancelTimeoutMutex) {
586
 
                                this.wasCancelled = false;
587
 
                        }
588
 
        
589
 
                        checkNullOrEmptyQuery(sql);
590
 
        
591
 
                        checkClosed();
592
 
        
593
 
                        char firstNonWsChar = StringUtils.firstNonWsCharUc(sql);
594
 
 
595
 
                        boolean isSelect = true;
596
 
        
597
 
                        if (firstNonWsChar != 'S') {
598
 
                                isSelect = false;
599
 
        
600
 
                                if (locallyScopedConn.isReadOnly()) {
601
 
                                        throw SQLError.createSQLException(Messages
602
 
                                                        .getString("Statement.27") //$NON-NLS-1$
603
 
                                                        + Messages.getString("Statement.28"), //$NON-NLS-1$
604
 
                                                        SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$
605
 
                                }
606
 
                        }
607
 
        
608
 
                        if (this.doEscapeProcessing) {
609
 
                                Object escapedSqlResult = EscapeProcessor.escapeSQL(sql,
610
 
                                                locallyScopedConn.serverSupportsConvertFn(), locallyScopedConn);
611
 
        
612
 
                                if (escapedSqlResult instanceof String) {
613
 
                                        sql = (String) escapedSqlResult;
614
 
                                } else {
615
 
                                        sql = ((EscapeProcessorResult) escapedSqlResult).escapedSql;
616
 
                                }
617
 
                        }
618
 
        
619
 
                        if (this.results != null) {
620
 
                                if (!locallyScopedConn.getHoldResultsOpenOverStatementClose()) {
621
 
                                        this.results.realClose(false);
622
 
                                }
623
 
                        }
624
 
        
625
 
                        if (firstNonWsChar == '/') {
626
 
                                if (sql.startsWith(PING_MARKER)) {
627
 
                                        doPingInstead();
628
 
                                
629
 
                                        return true;
630
 
                                }
631
 
                        }
632
 
                        
633
 
                        CachedResultSetMetaData cachedMetaData = null;
634
 
        
635
 
                        ResultSet rs = null;
636
 
        
637
 
                        // If there isn't a limit clause in the SQL
638
 
                        // then limit the number of rows to return in
639
 
                        // an efficient manner. Only do this if
640
 
                        // setMaxRows() hasn't been used on any Statements
641
 
                        // generated from the current Connection (saves
642
 
                        // a query, and network traffic).
643
 
                        
644
 
                        this.batchedGeneratedKeys = null;
645
 
                        
646
 
                        if (useServerFetch()) {
647
 
                                rs = createResultSetUsingServerFetch(sql);
648
 
                        } else {
649
 
                                CancelTask timeoutTask = null;
650
 
                                
651
 
                                String oldCatalog = null;
652
 
                                
653
 
                                try {
654
 
                                        if (locallyScopedConn.getEnableQueryTimeouts() &&
655
 
                                                        this.timeoutInMillis != 0
656
 
                                                        && locallyScopedConn.versionMeetsMinimum(5, 0, 0)) {
657
 
                                                timeoutTask = new CancelTask();
658
 
                                                Connection.getCancelTimer().schedule(timeoutTask, 
659
 
                                                                this.timeoutInMillis);
660
 
                                        }
661
 
 
662
 
                                        
663
 
 
664
 
                                        if (!locallyScopedConn.getCatalog().equals(
665
 
                                                        this.currentCatalog)) {
666
 
                                                oldCatalog = locallyScopedConn.getCatalog();
667
 
                                                locallyScopedConn.setCatalog(this.currentCatalog);
668
 
                                        }
669
 
 
670
 
                                        //
671
 
                                        // Check if we have cached metadata for this query...
672
 
                                        //
673
 
                                        if (locallyScopedConn.getCacheResultSetMetadata()) {
674
 
                                                cachedMetaData = locallyScopedConn.getCachedMetaData(sql);
675
 
                                        }
676
 
 
677
 
                                        //
678
 
                                        // Only apply max_rows to selects
679
 
                                        //
680
 
                                        if (locallyScopedConn.useMaxRows()) {
681
 
                                                int rowLimit = -1;
682
 
 
683
 
                                                if (isSelect) {
684
 
                                                        if (StringUtils.indexOfIgnoreCase(sql, "LIMIT") != -1) { //$NON-NLS-1$
685
 
                                                                rowLimit = this.maxRows;
686
 
                                                        } else {
687
 
                                                                if (this.maxRows <= 0) {
688
 
                                                                        locallyScopedConn
689
 
                                                                                        .execSQL(
690
 
                                                                                                        this,
691
 
                                                                                                        "SET OPTION SQL_SELECT_LIMIT=DEFAULT", -1, //$NON-NLS-1$
692
 
                                                                                                        null,
693
 
                                                                                                        java.sql.ResultSet.TYPE_FORWARD_ONLY,
694
 
                                                                                                        java.sql.ResultSet.CONCUR_READ_ONLY,
695
 
                                                                                                        false, 
696
 
                                                                                                        this.currentCatalog, true); //$NON-NLS-1$
697
 
                                                                } else {
698
 
                                                                        locallyScopedConn
699
 
                                                                                        .execSQL(
700
 
                                                                                                        this,
701
 
                                                                                                        "SET OPTION SQL_SELECT_LIMIT=" + this.maxRows, //$NON-NLS-1$
702
 
                                                                                                        -1,
703
 
                                                                                                        null,
704
 
                                                                                                        java.sql.ResultSet.TYPE_FORWARD_ONLY,
705
 
                                                                                                        java.sql.ResultSet.CONCUR_READ_ONLY,
706
 
                                                                                                        false, 
707
 
                                                                                                        this.currentCatalog, true); //$NON-NLS-1$
708
 
                                                                }
709
 
                                                        }
710
 
                                                } else {
711
 
                                                        locallyScopedConn
712
 
                                                                        .execSQL(
713
 
                                                                                        this,
714
 
                                                                                        "SET OPTION SQL_SELECT_LIMIT=DEFAULT", -1, null, //$NON-NLS-1$
715
 
                                                                                        java.sql.ResultSet.TYPE_FORWARD_ONLY,
716
 
                                                                                        java.sql.ResultSet.CONCUR_READ_ONLY,
717
 
                                                                                        false, this.currentCatalog,
718
 
                                                                                        true); //$NON-NLS-1$
719
 
                                                }
720
 
 
721
 
                                                // Finally, execute the query
722
 
                                                rs = locallyScopedConn.execSQL(this, sql, rowLimit, null,
723
 
                                                                this.resultSetType, this.resultSetConcurrency,
724
 
                                                                createStreamingResultSet(), 
725
 
                                                                this.currentCatalog, (cachedMetaData == null));
726
 
                                        } else {
727
 
                                                rs = locallyScopedConn.execSQL(this, sql, -1, null,
728
 
                                                                this.resultSetType, this.resultSetConcurrency,
729
 
                                                                createStreamingResultSet(), 
730
 
                                                                this.currentCatalog, (cachedMetaData == null));
731
 
                                        }
732
 
                                        
733
 
                                        if (timeoutTask != null) {
734
 
                                                if (timeoutTask.caughtWhileCancelling != null) {
735
 
                                                        throw timeoutTask.caughtWhileCancelling;
736
 
                                                }
737
 
                                                
738
 
                                                timeoutTask.cancel();
739
 
                                                timeoutTask = null;
740
 
                                        }
741
 
 
742
 
                                        synchronized (this.cancelTimeoutMutex) {
743
 
                                                if (this.wasCancelled) {
744
 
                                                        this.wasCancelled = false;
745
 
                                                        throw new MySQLTimeoutException();
746
 
                                                }
747
 
                                        }
748
 
                                } finally {                                     
749
 
                                        if (timeoutTask != null) {
750
 
                                                timeoutTask.cancel();
751
 
                                        }
752
 
                                        
753
 
                                        if (oldCatalog != null) {
754
 
                                                locallyScopedConn.setCatalog(oldCatalog);
755
 
                                        }
756
 
                                }
757
 
                        }
758
 
 
759
 
                        this.lastInsertId = rs.getUpdateID();
760
 
 
761
 
                        if (rs != null) {
762
 
                                this.results = rs;
763
 
 
764
 
                                rs.setFirstCharOfQuery(firstNonWsChar);
765
 
 
766
 
                                if (rs.reallyResult()) {
767
 
                                        if (cachedMetaData != null) {
768
 
                                                locallyScopedConn.initializeResultsMetadataFromCache(sql, cachedMetaData,
769
 
                                                                this.results);
770
 
                                        } else {
771
 
                                                if (this.connection.getCacheResultSetMetadata()) {
772
 
                                                        locallyScopedConn.initializeResultsMetadataFromCache(sql,
773
 
                                                                        null /* will be created */, this.results);
774
 
                                                }
775
 
                                        }
776
 
                                }
777
 
                        }
778
 
 
779
 
                        return ((rs != null) && rs.reallyResult());
780
 
                }
781
 
        }
782
 
 
783
 
        /**
784
 
         * @see Statement#execute(String, int)
785
 
         */
786
 
        public boolean execute(String sql, int returnGeneratedKeys)
787
 
                        throws SQLException {
788
 
                
789
 
                
790
 
                if (returnGeneratedKeys == java.sql.Statement.RETURN_GENERATED_KEYS) {
791
 
                        checkClosed();
792
 
                        
793
 
                        Connection locallyScopedConn = this.connection;
794
 
                        
795
 
                        synchronized (locallyScopedConn.getMutex()) {
796
 
                                // If this is a 'REPLACE' query, we need to be able to parse
797
 
                                // the 'info' message returned from the server to determine
798
 
                                // the actual number of keys generated.
799
 
                                boolean readInfoMsgState = this.connection
800
 
                                                .isReadInfoMsgEnabled();
801
 
                                locallyScopedConn.setReadInfoMsgEnabled(true);
802
 
 
803
 
                                try {
804
 
                                        return execute(sql);
805
 
                                } finally {
806
 
                                        locallyScopedConn.setReadInfoMsgEnabled(readInfoMsgState);
807
 
                                }
808
 
                        }
809
 
                }
810
 
 
811
 
                return execute(sql);
812
 
        }
813
 
 
814
 
        /**
815
 
         * @see Statement#execute(String, int[])
816
 
         */
817
 
        public boolean execute(String sql, int[] generatedKeyIndices)
818
 
                        throws SQLException {
819
 
                if ((generatedKeyIndices != null) && (generatedKeyIndices.length > 0)) {
820
 
                        checkClosed();
821
 
                        
822
 
                        Connection locallyScopedConn = this.connection;
823
 
                        
824
 
                        synchronized (locallyScopedConn.getMutex()) {
825
 
                                // If this is a 'REPLACE' query, we need to be able to parse
826
 
                                // the 'info' message returned from the server to determine
827
 
                                // the actual number of keys generated.
828
 
                                boolean readInfoMsgState = locallyScopedConn
829
 
                                                .isReadInfoMsgEnabled();
830
 
                                locallyScopedConn.setReadInfoMsgEnabled(true);
831
 
 
832
 
                                try {
833
 
                                        return execute(sql);
834
 
                                } finally {
835
 
                                        locallyScopedConn.setReadInfoMsgEnabled(readInfoMsgState);
836
 
                                }
837
 
                        }
838
 
                }
839
 
 
840
 
                return execute(sql);
841
 
        }
842
 
 
843
 
        /**
844
 
         * @see Statement#execute(String, String[])
845
 
         */
846
 
        public boolean execute(String sql, String[] generatedKeyNames)
847
 
                        throws SQLException {
848
 
                if ((generatedKeyNames != null) && (generatedKeyNames.length > 0)) {
849
 
                        checkClosed();
850
 
 
851
 
                        Connection locallyScopedConn = this.connection;
852
 
                        
853
 
                        synchronized (locallyScopedConn.getMutex()) {
854
 
                                // If this is a 'REPLACE' query, we need to be able to parse
855
 
                                // the 'info' message returned from the server to determine
856
 
                                // the actual number of keys generated.
857
 
                                boolean readInfoMsgState = this.connection
858
 
                                                .isReadInfoMsgEnabled();
859
 
                                locallyScopedConn.setReadInfoMsgEnabled(true);
860
 
 
861
 
                                try {
862
 
                                        return execute(sql);
863
 
                                } finally {
864
 
                                        locallyScopedConn.setReadInfoMsgEnabled(readInfoMsgState);
865
 
                                }
866
 
                        }
867
 
                }
868
 
 
869
 
                return execute(sql);
870
 
        }
871
 
 
872
 
        /**
873
 
         * JDBC 2.0 Submit a batch of commands to the database for execution. This
874
 
         * method is optional.
875
 
         * 
876
 
         * @return an array of update counts containing one element for each command
877
 
         *         in the batch. The array is ordered according to the order in
878
 
         *         which commands were inserted into the batch
879
 
         * 
880
 
         * @exception SQLException
881
 
         *                if a database-access error occurs, or the driver does not
882
 
         *                support batch statements
883
 
         * @throws java.sql.BatchUpdateException
884
 
         *             DOCUMENT ME!
885
 
         */
886
 
        public synchronized int[] executeBatch() throws SQLException {
887
 
                checkClosed();
888
 
                
889
 
                Connection locallyScopedConn = this.connection;
890
 
                
891
 
                if (locallyScopedConn.isReadOnly()) {
892
 
                        throw SQLError.createSQLException(Messages
893
 
                                        .getString("Statement.34") //$NON-NLS-1$
894
 
                                        + Messages.getString("Statement.35"), //$NON-NLS-1$
895
 
                                        SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$
896
 
                }
897
 
 
898
 
                if (this.results != null) {
899
 
                        if (!locallyScopedConn.getHoldResultsOpenOverStatementClose()) {
900
 
                                this.results.realClose(false);
901
 
                        }
902
 
                }
903
 
 
904
 
                synchronized (locallyScopedConn.getMutex()) {
905
 
                    if (this.batchedArgs == null || this.batchedArgs.size() == 0) {
906
 
                        return new int[0];
907
 
                    }
908
 
                    
909
 
                        try {
910
 
                                this.retrieveGeneratedKeys = true;
911
 
                                
912
 
                                int[] updateCounts = null;
913
 
 
914
 
                                if (this.batchedArgs != null) {
915
 
                                        int nbrCommands = this.batchedArgs.size();
916
 
 
917
 
                                        this.batchedGeneratedKeys = new ArrayList(this.batchedArgs.size());
918
 
                                        
919
 
                                        boolean multiQueriesEnabled = locallyScopedConn.getAllowMultiQueries();
920
 
                                        
921
 
                                        if (locallyScopedConn.versionMeetsMinimum(4, 1, 1) && 
922
 
                                                        (multiQueriesEnabled || 
923
 
                                                        (locallyScopedConn.getRewriteBatchedStatements() && 
924
 
                                                                        nbrCommands > 4))) {
925
 
                                                return executeBatchUsingMultiQueries(multiQueriesEnabled, nbrCommands);
926
 
                                        }
927
 
                                        
928
 
                                        updateCounts = new int[nbrCommands];
929
 
 
930
 
                                        for (int i = 0; i < nbrCommands; i++) {
931
 
                                                updateCounts[i] = -3;
932
 
                                        }
933
 
 
934
 
                                        SQLException sqlEx = null;
935
 
 
936
 
                                        int commandIndex = 0;
937
 
 
938
 
                                        for (commandIndex = 0; commandIndex < nbrCommands; commandIndex++) {
939
 
                                                try {
940
 
                                                        updateCounts[commandIndex] = executeUpdate((String) this.batchedArgs
941
 
                                                                        .get(commandIndex), true);
942
 
                                                        getBatchedGeneratedKeys();
943
 
                                                } catch (SQLException ex) {
944
 
                                                        updateCounts[commandIndex] = EXECUTE_FAILED;
945
 
 
946
 
                                                        if (this.continueBatchOnError) {
947
 
                                                                sqlEx = ex;
948
 
                                                        } else {
949
 
                                                                int[] newUpdateCounts = new int[commandIndex];
950
 
                                                                System.arraycopy(updateCounts, 0,
951
 
                                                                                newUpdateCounts, 0, commandIndex);
952
 
 
953
 
                                                                throw new java.sql.BatchUpdateException(ex
954
 
                                                                                .getMessage(), ex.getSQLState(), ex
955
 
                                                                                .getErrorCode(), newUpdateCounts);
956
 
                                                        }
957
 
                                                }
958
 
                                        }
959
 
 
960
 
                                        if (sqlEx != null) {
961
 
                                                throw new java.sql.BatchUpdateException(sqlEx
962
 
                                                                .getMessage(), sqlEx.getSQLState(), sqlEx
963
 
                                                                .getErrorCode(), updateCounts);
964
 
                                        }
965
 
                                }
966
 
 
967
 
                                return (updateCounts != null) ? updateCounts : new int[0];
968
 
                        } finally {
969
 
                                this.retrieveGeneratedKeys = false;
970
 
                                
971
 
                                clearBatch();
972
 
                        }
973
 
                }
974
 
        }
975
 
 
976
 
        /**
977
 
         * Rewrites batch into a single query to send to the server. This method
978
 
         * will constrain each batch to be shorter than max_allowed_packet on the
979
 
         * server.
980
 
         * 
981
 
         * @return update counts in the same manner as executeBatch()
982
 
         * @throws SQLException
983
 
         */
984
 
        private int[] executeBatchUsingMultiQueries(boolean multiQueriesEnabled,
985
 
                        int nbrCommands) throws SQLException {
986
 
 
987
 
                Connection locallyScopedConn = this.connection;
988
 
                
989
 
                if (!multiQueriesEnabled) {
990
 
                        locallyScopedConn.getIO().enableMultiQueries();
991
 
                }
992
 
 
993
 
                java.sql.Statement batchStmt = null;
994
 
                
995
 
                try {
996
 
                        int[] updateCounts = new int[nbrCommands];
997
 
 
998
 
                        for (int i = 0; i < nbrCommands; i++) {
999
 
                                updateCounts[i] = -3;
1000
 
                        }
1001
 
 
1002
 
                        int commandIndex = 0;
1003
 
 
1004
 
                        StringBuffer queryBuf = new StringBuffer();
1005
 
 
1006
 
                        
1007
 
                        
1008
 
                        batchStmt = locallyScopedConn.createStatement();
1009
 
                        
1010
 
 
1011
 
                        int counter = 0;
1012
 
 
1013
 
                        int numberOfBytesPerChar = 1;
1014
 
 
1015
 
                        String connectionEncoding = locallyScopedConn.getEncoding();
1016
 
 
1017
 
                        if (StringUtils.startsWithIgnoreCase(connectionEncoding, "utf")) {
1018
 
                                numberOfBytesPerChar = 3;
1019
 
                        } else if (CharsetMapping.isMultibyteCharset(connectionEncoding)) {
1020
 
                                numberOfBytesPerChar = 2;
1021
 
                        }
1022
 
 
1023
 
                        int escapeAdjust = 1;
1024
 
                        
1025
 
                        if (this.doEscapeProcessing) {
1026
 
                                escapeAdjust = 2; /* We assume packet _could_ grow by this amount, as we're not
1027
 
                                                     sure how big statement will end up after
1028
 
                                                     escape processing */
1029
 
                        }
1030
 
                        
1031
 
                        for (commandIndex = 0; commandIndex < nbrCommands; commandIndex++) {
1032
 
                                String nextQuery = (String) this.batchedArgs.get(commandIndex);
1033
 
 
1034
 
                                if (((((queryBuf.length() + nextQuery.length())
1035
 
                                                * numberOfBytesPerChar) + 1 /* for semicolon */ 
1036
 
                                                + MysqlIO.HEADER_LENGTH) * escapeAdjust)  + 32 > this.connection
1037
 
                                                .getMaxAllowedPacket()) {
1038
 
                                        batchStmt.execute(queryBuf.toString());
1039
 
 
1040
 
                                        updateCounts[counter++] = batchStmt.getUpdateCount();
1041
 
                                        long generatedKeyStart = ((com.mysql.jdbc.Statement)batchStmt).getLastInsertID();
1042
 
                                        byte[][] row = new byte[1][];
1043
 
                                        row[0] = Long.toString(generatedKeyStart++).getBytes();
1044
 
                                        this.batchedGeneratedKeys.add(row);
1045
 
 
1046
 
                                        while (batchStmt.getMoreResults()
1047
 
                                                        || batchStmt.getUpdateCount() != -1) {
1048
 
                                                updateCounts[counter++] = batchStmt.getUpdateCount();
1049
 
                                                row = new byte[1][];
1050
 
                                                row[0] = Long.toString(generatedKeyStart++).getBytes();
1051
 
                                                this.batchedGeneratedKeys.add(row);
1052
 
                                        }
1053
 
 
1054
 
                                        queryBuf = new StringBuffer();
1055
 
                                }
1056
 
 
1057
 
                                queryBuf.append(nextQuery);
1058
 
                                queryBuf.append(";");
1059
 
                        }
1060
 
 
1061
 
                        if (queryBuf.length() > 0) {
1062
 
                                batchStmt.execute(queryBuf.toString());
1063
 
 
1064
 
                                long generatedKeyStart = ((com.mysql.jdbc.Statement)batchStmt).getLastInsertID();
1065
 
                                byte[][] row = new byte[1][];
1066
 
                                row[0] = Long.toString(generatedKeyStart++).getBytes();
1067
 
                                this.batchedGeneratedKeys.add(row);
1068
 
                                
1069
 
                                updateCounts[counter++] = batchStmt.getUpdateCount();
1070
 
 
1071
 
                                while (batchStmt.getMoreResults()
1072
 
                                                || batchStmt.getUpdateCount() != -1) {
1073
 
                                        updateCounts[counter++] = batchStmt.getUpdateCount();
1074
 
                                        row = new byte[1][];
1075
 
                                        row[0] = Long.toString(generatedKeyStart++).getBytes();
1076
 
                                        this.batchedGeneratedKeys.add(row);
1077
 
                                }
1078
 
                        }
1079
 
 
1080
 
                        return (updateCounts != null) ? updateCounts : new int[0];
1081
 
                } finally {
1082
 
                        try {
1083
 
                                if (batchStmt != null) {
1084
 
                                        batchStmt.close();
1085
 
                                }
1086
 
                        } finally {
1087
 
                                if (!multiQueriesEnabled) {
1088
 
                                        locallyScopedConn.getIO().disableMultiQueries();
1089
 
                                }
1090
 
                        }
1091
 
                }
1092
 
        }
1093
 
        
1094
 
        /**
1095
 
         * Execute a SQL statement that retruns a single ResultSet
1096
 
         * 
1097
 
         * @param sql
1098
 
         *            typically a static SQL SELECT statement
1099
 
         * 
1100
 
         * @return a ResulSet that contains the data produced by the query
1101
 
         * 
1102
 
         * @exception SQLException
1103
 
         *                if a database access error occurs
1104
 
         */
1105
 
        public java.sql.ResultSet executeQuery(String sql)
1106
 
                        throws SQLException {
1107
 
                checkClosed();
1108
 
                
1109
 
                Connection locallyScopedConn = this.connection;
1110
 
                
1111
 
                synchronized (locallyScopedConn.getMutex()) {
1112
 
                        synchronized (this.cancelTimeoutMutex) {
1113
 
                                this.wasCancelled = false;
1114
 
                        }
1115
 
        
1116
 
                        checkNullOrEmptyQuery(sql);
1117
 
 
1118
 
                        if (this.doEscapeProcessing) {
1119
 
                                Object escapedSqlResult = EscapeProcessor.escapeSQL(sql,
1120
 
                                                locallyScopedConn.serverSupportsConvertFn(), this.connection);
1121
 
        
1122
 
                                if (escapedSqlResult instanceof String) {
1123
 
                                        sql = (String) escapedSqlResult;
1124
 
                                } else {
1125
 
                                        sql = ((EscapeProcessorResult) escapedSqlResult).escapedSql;
1126
 
                                }
1127
 
                        }
1128
 
        
1129
 
                        char firstStatementChar = StringUtils.firstNonWsCharUc(sql, 
1130
 
                                        findStartOfStatement(sql));
1131
 
        
1132
 
                        if (sql.charAt(0) == '/') {
1133
 
                                if (sql.startsWith(PING_MARKER)) {
1134
 
                                        doPingInstead();
1135
 
                                
1136
 
                                        return this.results;
1137
 
                                }
1138
 
                        }
1139
 
                        
1140
 
                        checkForDml(sql, firstStatementChar);
1141
 
        
1142
 
                        if (this.results != null) {
1143
 
                                if (!locallyScopedConn.getHoldResultsOpenOverStatementClose()) {
1144
 
                                        this.results.realClose(false);
1145
 
                                }
1146
 
                        }
1147
 
        
1148
 
                        CachedResultSetMetaData cachedMetaData = null;
1149
 
        
1150
 
                        // If there isn't a limit clause in the SQL
1151
 
                        // then limit the number of rows to return in
1152
 
                        // an efficient manner. Only do this if
1153
 
                        // setMaxRows() hasn't been used on any Statements
1154
 
                        // generated from the current Connection (saves
1155
 
                        // a query, and network traffic).
1156
 
                        
1157
 
                        if (useServerFetch()) {
1158
 
                                this.results = createResultSetUsingServerFetch(sql);
1159
 
 
1160
 
                                return this.results;
1161
 
                        }
1162
 
 
1163
 
                        CancelTask timeoutTask = null;
1164
 
                        
1165
 
                        String oldCatalog = null;
1166
 
                        
1167
 
                        try {
1168
 
                                if (locallyScopedConn.getEnableQueryTimeouts() &&
1169
 
                                                this.timeoutInMillis != 0
1170
 
                                                && locallyScopedConn.versionMeetsMinimum(5, 0, 0)) {
1171
 
                                        timeoutTask = new CancelTask();
1172
 
                                        Connection.getCancelTimer().schedule(timeoutTask, 
1173
 
                                                        this.timeoutInMillis);
1174
 
                                }
1175
 
 
1176
 
                                if (!locallyScopedConn.getCatalog().equals(this.currentCatalog)) {
1177
 
                                        oldCatalog = locallyScopedConn.getCatalog();
1178
 
                                        locallyScopedConn.setCatalog(this.currentCatalog);
1179
 
                                }
1180
 
 
1181
 
                                //
1182
 
                                // Check if we have cached metadata for this query...
1183
 
                                //
1184
 
                                if (locallyScopedConn.getCacheResultSetMetadata()) {
1185
 
                                        cachedMetaData = locallyScopedConn.getCachedMetaData(sql);
1186
 
                                }
1187
 
 
1188
 
                                if (locallyScopedConn.useMaxRows()) {
1189
 
                                        // We need to execute this all together
1190
 
                                        // So synchronize on the Connection's mutex (because
1191
 
                                        // even queries going through there synchronize
1192
 
                                        // on the connection
1193
 
                                        if (StringUtils.indexOfIgnoreCase(sql, "LIMIT") != -1) { //$NON-NLS-1$
1194
 
                                                this.results = locallyScopedConn.execSQL(this, sql,
1195
 
                                                                this.maxRows, null, this.resultSetType,
1196
 
                                                                this.resultSetConcurrency,
1197
 
                                                                createStreamingResultSet(),
1198
 
                                                                this.currentCatalog, (cachedMetaData == null));
1199
 
                                        } else {
1200
 
                                                if (this.maxRows <= 0) {
1201
 
                                                        locallyScopedConn
1202
 
                                                                        .execSQL(
1203
 
                                                                                        this,
1204
 
                                                                                        "SET OPTION SQL_SELECT_LIMIT=DEFAULT", -1, null, //$NON-NLS-1$
1205
 
                                                                                        java.sql.ResultSet.TYPE_FORWARD_ONLY,
1206
 
                                                                                        java.sql.ResultSet.CONCUR_READ_ONLY,
1207
 
                                                                                        false, this.currentCatalog,
1208
 
                                                                                        true); //$NON-NLS-1$
1209
 
                                                } else {
1210
 
                                                        locallyScopedConn
1211
 
                                                                        .execSQL(
1212
 
                                                                                        this,
1213
 
                                                                                        "SET OPTION SQL_SELECT_LIMIT=" + this.maxRows, -1, //$NON-NLS-1$
1214
 
                                                                                        null,
1215
 
                                                                                        java.sql.ResultSet.TYPE_FORWARD_ONLY,
1216
 
                                                                                        java.sql.ResultSet.CONCUR_READ_ONLY,
1217
 
                                                                                        false, this.currentCatalog,
1218
 
                                                                                        true); //$NON-NLS-1$
1219
 
                                                }
1220
 
 
1221
 
                                                this.results = locallyScopedConn.execSQL(this, sql, -1,
1222
 
                                                                null, this.resultSetType,
1223
 
                                                                this.resultSetConcurrency,
1224
 
                                                                createStreamingResultSet(),
1225
 
                                                                this.currentCatalog, (cachedMetaData == null));
1226
 
 
1227
 
                                                if (oldCatalog != null) {
1228
 
                                                        locallyScopedConn.setCatalog(oldCatalog);
1229
 
                                                }
1230
 
                                        }
1231
 
                                } else {
1232
 
                                        this.results = locallyScopedConn.execSQL(this, sql, -1, null,
1233
 
                                                        this.resultSetType, this.resultSetConcurrency,
1234
 
                                                        createStreamingResultSet(),
1235
 
                                                        this.currentCatalog, (cachedMetaData == null));
1236
 
                                }
1237
 
 
1238
 
                                if (timeoutTask != null) {
1239
 
                                        if (timeoutTask.caughtWhileCancelling != null) {
1240
 
                                                throw timeoutTask.caughtWhileCancelling;
1241
 
                                        }
1242
 
                                        
1243
 
                                        timeoutTask.cancel();
1244
 
                                        timeoutTask = null;
1245
 
                                }
1246
 
                                
1247
 
                                synchronized (this.cancelTimeoutMutex) {
1248
 
                                        if (this.wasCancelled) {
1249
 
                                                this.wasCancelled = false;
1250
 
                                                throw new MySQLTimeoutException();
1251
 
                                        }
1252
 
                                }
1253
 
                        } finally {
1254
 
                                if (timeoutTask != null) {
1255
 
                                        timeoutTask.cancel();
1256
 
                                }
1257
 
                                
1258
 
                                if (oldCatalog != null) {
1259
 
                                        locallyScopedConn.setCatalog(oldCatalog);
1260
 
                                }
1261
 
                        }
1262
 
 
1263
 
                        this.lastInsertId = this.results.getUpdateID();
1264
 
 
1265
 
                        if (cachedMetaData != null) {
1266
 
                                locallyScopedConn.initializeResultsMetadataFromCache(sql, cachedMetaData,
1267
 
                                                this.results);
1268
 
                        } else {
1269
 
                                if (this.connection.getCacheResultSetMetadata()) {
1270
 
                                        locallyScopedConn.initializeResultsMetadataFromCache(sql,
1271
 
                                                        null /* will be created */, this.results);
1272
 
                                }
1273
 
                        }
1274
 
                        
1275
 
                        return this.results;
1276
 
                }
1277
 
        }
1278
 
 
1279
 
        protected void doPingInstead() throws SQLException {
1280
 
                if (this.pingTarget != null) {
1281
 
                        this.pingTarget.doPing();
1282
 
                } else {
1283
 
                        this.connection.ping();
1284
 
                }
1285
 
 
1286
 
                ResultSet fakeSelectOneResultSet = generatePingResultSet();
1287
 
                this.results = fakeSelectOneResultSet;
1288
 
        }
1289
 
 
1290
 
        protected ResultSet generatePingResultSet() throws SQLException {
1291
 
                Field[] fields = { new Field(null, "1", Types.BIGINT, 1) };
1292
 
                ArrayList rows = new ArrayList();
1293
 
                byte[] colVal = new byte[] { (byte) '1' };
1294
 
 
1295
 
                rows.add(new byte[][] { colVal });
1296
 
 
1297
 
                return (ResultSet) DatabaseMetaData.buildResultSet(fields, rows,
1298
 
                                this.connection);
1299
 
        }
1300
 
 
1301
 
        /**
1302
 
         * Execute a SQL INSERT, UPDATE or DELETE statement. In addition SQL
1303
 
         * statements that return nothing such as SQL DDL statements can be executed
1304
 
         * Any IDs generated for AUTO_INCREMENT fields can be retrieved by casting
1305
 
         * this Statement to org.gjt.mm.mysql.Statement and calling the
1306
 
         * getLastInsertID() method.
1307
 
         * 
1308
 
         * @param sql
1309
 
         *            a SQL statement
1310
 
         * 
1311
 
         * @return either a row count, or 0 for SQL commands
1312
 
         * 
1313
 
         * @exception SQLException
1314
 
         *                if a database access error occurs
1315
 
         */
1316
 
        public int executeUpdate(String sql) throws SQLException {
1317
 
                return executeUpdate(sql, false);
1318
 
        }
1319
 
 
1320
 
        protected int executeUpdate(String sql, boolean isBatch)
1321
 
                        throws SQLException {
1322
 
                checkClosed();
1323
 
                
1324
 
                Connection locallyScopedConn = this.connection;
1325
 
                
1326
 
                char firstStatementChar = StringUtils.firstNonWsCharUc(sql,
1327
 
                                findStartOfStatement(sql));
1328
 
 
1329
 
                ResultSet rs = null;
1330
 
 
1331
 
                synchronized (locallyScopedConn.getMutex()) {
1332
 
                        synchronized (this.cancelTimeoutMutex) {
1333
 
                                this.wasCancelled = false;
1334
 
                        }
1335
 
        
1336
 
                        checkNullOrEmptyQuery(sql);
1337
 
 
1338
 
                        if (this.doEscapeProcessing) {
1339
 
                                Object escapedSqlResult = EscapeProcessor.escapeSQL(sql,
1340
 
                                                this.connection.serverSupportsConvertFn(), this.connection);
1341
 
 
1342
 
                                if (escapedSqlResult instanceof String) {
1343
 
                                        sql = (String) escapedSqlResult;
1344
 
                                } else {
1345
 
                                        sql = ((EscapeProcessorResult) escapedSqlResult).escapedSql;
1346
 
                                }
1347
 
                        }
1348
 
                        
1349
 
                        if (locallyScopedConn.isReadOnly()) {
1350
 
                                throw SQLError.createSQLException(Messages
1351
 
                                                .getString("Statement.42") //$NON-NLS-1$
1352
 
                                                + Messages.getString("Statement.43"), //$NON-NLS-1$
1353
 
                                                SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$
1354
 
                        }
1355
 
        
1356
 
                        if (StringUtils.startsWithIgnoreCaseAndWs(sql, "select")) { //$NON-NLS-1$
1357
 
                                throw SQLError.createSQLException(Messages
1358
 
                                                .getString("Statement.46"), //$NON-NLS-1$
1359
 
                                                "01S03"); //$NON-NLS-1$
1360
 
                        }
1361
 
        
1362
 
                        if (this.results != null) {
1363
 
                                if (!locallyScopedConn.getHoldResultsOpenOverStatementClose()) {
1364
 
                                        this.results.realClose(false);
1365
 
                                }
1366
 
                        }
1367
 
        
1368
 
                        // The checking and changing of catalogs
1369
 
                        // must happen in sequence, so synchronize
1370
 
                        // on the same mutex that _conn is using
1371
 
                
1372
 
                        CancelTask timeoutTask = null;
1373
 
                        
1374
 
                        String oldCatalog = null;
1375
 
                        
1376
 
                        try {
1377
 
                                if (locallyScopedConn.getEnableQueryTimeouts() &&
1378
 
                                                this.timeoutInMillis != 0
1379
 
                                                && locallyScopedConn.versionMeetsMinimum(5, 0, 0)) {
1380
 
                                        timeoutTask = new CancelTask();
1381
 
                                        Connection.getCancelTimer().schedule(timeoutTask, 
1382
 
                                                        this.timeoutInMillis);
1383
 
                                }
1384
 
 
1385
 
                                if (!locallyScopedConn.getCatalog().equals(this.currentCatalog)) {
1386
 
                                        oldCatalog = locallyScopedConn.getCatalog();
1387
 
                                        locallyScopedConn.setCatalog(this.currentCatalog);
1388
 
                                }
1389
 
 
1390
 
                                //
1391
 
                                // Only apply max_rows to selects
1392
 
                                //
1393
 
                                if (locallyScopedConn.useMaxRows()) {
1394
 
                                        locallyScopedConn.execSQL(
1395
 
                                                        this,
1396
 
                                                        "SET OPTION SQL_SELECT_LIMIT=DEFAULT", //$NON-NLS-1$
1397
 
                                                        -1, null, java.sql.ResultSet.TYPE_FORWARD_ONLY,
1398
 
                                                        java.sql.ResultSet.CONCUR_READ_ONLY, false,
1399
 
                                                        this.currentCatalog, true);
1400
 
                                }
1401
 
 
1402
 
                                rs = locallyScopedConn.execSQL(this, sql, -1, null,
1403
 
                                                java.sql.ResultSet.TYPE_FORWARD_ONLY,
1404
 
                                                java.sql.ResultSet.CONCUR_READ_ONLY, false,
1405
 
                                                this.currentCatalog,
1406
 
                                                true /* force read of field info on DML */,
1407
 
                                                isBatch);
1408
 
                                
1409
 
                                if (timeoutTask != null) {
1410
 
                                        if (timeoutTask.caughtWhileCancelling != null) {
1411
 
                                                throw timeoutTask.caughtWhileCancelling;
1412
 
                                        }
1413
 
                                        
1414
 
                                        timeoutTask.cancel();
1415
 
                                        timeoutTask = null;
1416
 
                                }
1417
 
 
1418
 
                                synchronized (this.cancelTimeoutMutex) {
1419
 
                                        if (this.wasCancelled) {
1420
 
                                                this.wasCancelled = false;
1421
 
                                                throw new MySQLTimeoutException();
1422
 
                                        }
1423
 
                                }
1424
 
                        } finally {
1425
 
                                if (timeoutTask != null) {
1426
 
                                        timeoutTask.cancel();
1427
 
                                }
1428
 
                                
1429
 
                                if (oldCatalog != null) {
1430
 
                                        locallyScopedConn.setCatalog(oldCatalog);
1431
 
                                }
1432
 
                        }
1433
 
                }
1434
 
 
1435
 
                this.results = rs;
1436
 
 
1437
 
                rs.setFirstCharOfQuery(firstStatementChar);
1438
 
 
1439
 
                this.updateCount = rs.getUpdateCount();
1440
 
 
1441
 
                int truncatedUpdateCount = 0;
1442
 
 
1443
 
                if (this.updateCount > Integer.MAX_VALUE) {
1444
 
                        truncatedUpdateCount = Integer.MAX_VALUE;
1445
 
                } else {
1446
 
                        truncatedUpdateCount = (int) this.updateCount;
1447
 
                }
1448
 
 
1449
 
                this.lastInsertId = rs.getUpdateID();
1450
 
 
1451
 
                return truncatedUpdateCount;
1452
 
        }
1453
 
 
1454
 
        /**
1455
 
         * @see Statement#executeUpdate(String, int)
1456
 
         */
1457
 
        public int executeUpdate(String sql, int returnGeneratedKeys)
1458
 
                        throws SQLException {
1459
 
                if (returnGeneratedKeys == java.sql.Statement.RETURN_GENERATED_KEYS) {
1460
 
                        checkClosed();
1461
 
 
1462
 
                        Connection locallyScopedConn = this.connection;
1463
 
                        
1464
 
                        synchronized (locallyScopedConn.getMutex()) {
1465
 
                                // If this is a 'REPLACE' query, we need to be able to parse
1466
 
                                // the 'info' message returned from the server to determine
1467
 
                                // the actual number of keys generated.
1468
 
                                boolean readInfoMsgState = locallyScopedConn
1469
 
                                                .isReadInfoMsgEnabled();
1470
 
                                locallyScopedConn.setReadInfoMsgEnabled(true);
1471
 
 
1472
 
                                try {
1473
 
                                        return executeUpdate(sql);
1474
 
                                } finally {
1475
 
                                        locallyScopedConn.setReadInfoMsgEnabled(readInfoMsgState);
1476
 
                                }
1477
 
                        }
1478
 
                }
1479
 
 
1480
 
                return executeUpdate(sql);
1481
 
        }
1482
 
 
1483
 
        /**
1484
 
         * @see Statement#executeUpdate(String, int[])
1485
 
         */
1486
 
        public int executeUpdate(String sql, int[] generatedKeyIndices)
1487
 
                        throws SQLException {
1488
 
                if ((generatedKeyIndices != null) && (generatedKeyIndices.length > 0)) {
1489
 
                        checkClosed();
1490
 
                        
1491
 
                        Connection locallyScopedConn = this.connection;
1492
 
                        
1493
 
                        synchronized (locallyScopedConn.getMutex()) {
1494
 
                                // If this is a 'REPLACE' query, we need to be able to parse
1495
 
                                // the 'info' message returned from the server to determine
1496
 
                                // the actual number of keys generated.
1497
 
                                boolean readInfoMsgState = locallyScopedConn
1498
 
                                                .isReadInfoMsgEnabled();
1499
 
                                locallyScopedConn.setReadInfoMsgEnabled(true);
1500
 
 
1501
 
                                try {
1502
 
                                        return executeUpdate(sql);
1503
 
                                } finally {
1504
 
                                        locallyScopedConn.setReadInfoMsgEnabled(readInfoMsgState);
1505
 
                                }
1506
 
                        }
1507
 
                }
1508
 
 
1509
 
                return executeUpdate(sql);
1510
 
        }
1511
 
 
1512
 
        /**
1513
 
         * @see Statement#executeUpdate(String, String[])
1514
 
         */
1515
 
        public int executeUpdate(String sql, String[] generatedKeyNames)
1516
 
                        throws SQLException {
1517
 
                if ((generatedKeyNames != null) && (generatedKeyNames.length > 0)) {
1518
 
                        checkClosed();
1519
 
 
1520
 
                        Connection locallyScopedConn = this.connection;
1521
 
                        
1522
 
                        synchronized (locallyScopedConn.getMutex()) {
1523
 
                                // If this is a 'REPLACE' query, we need to be able to parse
1524
 
                                // the 'info' message returned from the server to determine
1525
 
                                // the actual number of keys generated.
1526
 
                                boolean readInfoMsgState = this.connection
1527
 
                                                .isReadInfoMsgEnabled();
1528
 
                                locallyScopedConn.setReadInfoMsgEnabled(true);
1529
 
 
1530
 
                                try {
1531
 
                                        return executeUpdate(sql);
1532
 
                                } finally {
1533
 
                                        locallyScopedConn.setReadInfoMsgEnabled(readInfoMsgState);
1534
 
                                }
1535
 
                        }
1536
 
                }
1537
 
 
1538
 
                return executeUpdate(sql);
1539
 
        }
1540
 
 
1541
 
 
1542
 
 
1543
 
        /**
1544
 
         * Optimization to only use one calendar per-session, or calculate it for
1545
 
         * each call, depending on user configuration
1546
 
         */
1547
 
        protected Calendar getCalendarInstanceForSessionOrNew() {
1548
 
                if (this.connection != null) {
1549
 
                        return this.connection.getCalendarInstanceForSessionOrNew();
1550
 
                } else {
1551
 
                        // punt, no connection around
1552
 
                        return new GregorianCalendar();
1553
 
                }
1554
 
        }
1555
 
 
1556
 
        /**
1557
 
         * JDBC 2.0 Return the Connection that produced the Statement.
1558
 
         * 
1559
 
         * @return the Connection that produced the Statement
1560
 
         * 
1561
 
         * @throws SQLException
1562
 
         *             if an error occurs
1563
 
         */
1564
 
        public java.sql.Connection getConnection() throws SQLException {
1565
 
                return this.connection;
1566
 
        }
1567
 
 
1568
 
        /**
1569
 
         * JDBC 2.0 Determine the fetch direction.
1570
 
         * 
1571
 
         * @return the default fetch direction
1572
 
         * 
1573
 
         * @exception SQLException
1574
 
         *                if a database-access error occurs
1575
 
         */
1576
 
        public int getFetchDirection() throws SQLException {
1577
 
                return java.sql.ResultSet.FETCH_FORWARD;
1578
 
        }
1579
 
 
1580
 
        /**
1581
 
         * JDBC 2.0 Determine the default fetch size.
1582
 
         * 
1583
 
         * @return the number of rows to fetch at a time
1584
 
         * 
1585
 
         * @throws SQLException
1586
 
         *             if an error occurs
1587
 
         */
1588
 
        public int getFetchSize() throws SQLException {
1589
 
                return this.fetchSize;
1590
 
        }
1591
 
 
1592
 
        /**
1593
 
         * DOCUMENT ME!
1594
 
         * 
1595
 
         * @return DOCUMENT ME!
1596
 
         * 
1597
 
         * @throws SQLException
1598
 
         *             DOCUMENT ME!
1599
 
         */
1600
 
        public java.sql.ResultSet getGeneratedKeys()
1601
 
                        throws SQLException {
1602
 
                if (this.batchedGeneratedKeys == null) {
1603
 
                        return getGeneratedKeysInternal();
1604
 
                }
1605
 
 
1606
 
                Field[] fields = new Field[1];
1607
 
                fields[0] = new Field("", "GENERATED_KEY", Types.BIGINT, 17); //$NON-NLS-1$ //$NON-NLS-2$
1608
 
                fields[0].setConnection(this.connection);
1609
 
 
1610
 
                return new com.mysql.jdbc.ResultSet(this.currentCatalog, fields,
1611
 
                                new RowDataStatic(this.batchedGeneratedKeys), this.connection,
1612
 
                                this);
1613
 
        }
1614
 
        
1615
 
        /*
1616
 
         * Needed because there's no concept of super.super to get to this
1617
 
         * implementation from ServerPreparedStatement when dealing with batched
1618
 
         * updates.
1619
 
         */
1620
 
        protected java.sql.ResultSet getGeneratedKeysInternal()
1621
 
                        throws SQLException {
1622
 
                Field[] fields = new Field[1];
1623
 
                fields[0] = new Field("", "GENERATED_KEY", Types.BIGINT, 17); //$NON-NLS-1$ //$NON-NLS-2$
1624
 
                fields[0].setConnection(this.connection);
1625
 
 
1626
 
                ArrayList rowSet = new ArrayList();
1627
 
 
1628
 
                long beginAt = getLastInsertID();
1629
 
                int numKeys = getUpdateCount();
1630
 
 
1631
 
                if (this.results != null) {
1632
 
                        String serverInfo = this.results.getServerInfo();
1633
 
        
1634
 
                        // 
1635
 
                        // Only parse server info messages for 'REPLACE'
1636
 
                        // queries
1637
 
                        //
1638
 
                        if ((numKeys > 0) && (this.results.getFirstCharOfQuery() == 'R')
1639
 
                                        && (serverInfo != null) && (serverInfo.length() > 0)) {
1640
 
                                numKeys = getRecordCountFromInfo(serverInfo);
1641
 
                        }
1642
 
        
1643
 
                        if ((beginAt > 0) && (numKeys > 0)) {
1644
 
                                for (int i = 0; i < numKeys; i++) {
1645
 
                                        byte[][] row = new byte[1][];
1646
 
                                        row[0] = Long.toString(beginAt++).getBytes();
1647
 
                                        rowSet.add(row);
1648
 
                                }
1649
 
                        }
1650
 
                }
1651
 
 
1652
 
                return new com.mysql.jdbc.ResultSet(this.currentCatalog, fields,
1653
 
                                new RowDataStatic(rowSet), this.connection, this);
1654
 
        }
1655
 
 
1656
 
        /**
1657
 
         * Returns the id used when profiling
1658
 
         * 
1659
 
         * @return the id used when profiling.
1660
 
         */
1661
 
        protected int getId() {
1662
 
                return this.statementId;
1663
 
        }
1664
 
 
1665
 
        /**
1666
 
         * getLastInsertID returns the value of the auto_incremented key after an
1667
 
         * executeQuery() or excute() call.
1668
 
         * 
1669
 
         * <p>
1670
 
         * This gets around the un-threadsafe behavior of "select LAST_INSERT_ID()"
1671
 
         * which is tied to the Connection that created this Statement, and
1672
 
         * therefore could have had many INSERTS performed before one gets a chance
1673
 
         * to call "select LAST_INSERT_ID()".
1674
 
         * </p>
1675
 
         * 
1676
 
         * @return the last update ID.
1677
 
         */
1678
 
        public long getLastInsertID() {
1679
 
                return this.lastInsertId;
1680
 
        }
1681
 
 
1682
 
        /**
1683
 
         * getLongUpdateCount returns the current result as an update count, if the
1684
 
         * result is a ResultSet or there are no more results, -1 is returned. It
1685
 
         * should only be called once per result.
1686
 
         * 
1687
 
         * <p>
1688
 
         * This method returns longs as MySQL server versions newer than 3.22.4
1689
 
         * return 64-bit values for update counts
1690
 
         * </p>
1691
 
         * 
1692
 
         * @return the current update count.
1693
 
         */
1694
 
        public long getLongUpdateCount() {
1695
 
                if (this.results == null) {
1696
 
                        return -1;
1697
 
                }
1698
 
 
1699
 
                if (this.results.reallyResult()) {
1700
 
                        return -1;
1701
 
                }
1702
 
 
1703
 
                return this.updateCount;
1704
 
        }
1705
 
 
1706
 
        /**
1707
 
         * The maxFieldSize limit (in bytes) is the maximum amount of data returned
1708
 
         * for any column value; it only applies to BINARY, VARBINARY,
1709
 
         * LONGVARBINARY, CHAR, VARCHAR and LONGVARCHAR columns. If the limit is
1710
 
         * exceeded, the excess data is silently discarded.
1711
 
         * 
1712
 
         * @return the current max column size limit; zero means unlimited
1713
 
         * 
1714
 
         * @exception SQLException
1715
 
         *                if a database access error occurs
1716
 
         */
1717
 
        public int getMaxFieldSize() throws SQLException {
1718
 
                return this.maxFieldSize;
1719
 
        }
1720
 
 
1721
 
        /**
1722
 
         * The maxRows limit is set to limit the number of rows that any ResultSet
1723
 
         * can contain. If the limit is exceeded, the excess rows are silently
1724
 
         * dropped.
1725
 
         * 
1726
 
         * @return the current maximum row limit; zero means unlimited
1727
 
         * 
1728
 
         * @exception SQLException
1729
 
         *                if a database access error occurs
1730
 
         */
1731
 
        public int getMaxRows() throws SQLException {
1732
 
                if (this.maxRows <= 0) {
1733
 
                        return 0;
1734
 
                }
1735
 
 
1736
 
                return this.maxRows;
1737
 
        }
1738
 
 
1739
 
        /**
1740
 
         * getMoreResults moves to a Statement's next result. If it returns true,
1741
 
         * this result is a ResulSet.
1742
 
         * 
1743
 
         * @return true if the next ResultSet is valid
1744
 
         * 
1745
 
         * @exception SQLException
1746
 
         *                if a database access error occurs
1747
 
         */
1748
 
        public boolean getMoreResults() throws SQLException {
1749
 
                return getMoreResults(CLOSE_CURRENT_RESULT);
1750
 
        }
1751
 
 
1752
 
        /**
1753
 
         * @see Statement#getMoreResults(int)
1754
 
         */
1755
 
        public boolean getMoreResults(int current) throws SQLException {
1756
 
 
1757
 
                if (this.results == null) {
1758
 
                        return false;
1759
 
                }
1760
 
 
1761
 
                ResultSet nextResultSet = this.results.getNextResultSet();
1762
 
 
1763
 
                switch (current) {
1764
 
                case java.sql.Statement.CLOSE_CURRENT_RESULT:
1765
 
 
1766
 
                        if (this.results != null) {
1767
 
                                this.results.close();
1768
 
                                this.results.clearNextResult();
1769
 
                        }
1770
 
 
1771
 
                        break;
1772
 
 
1773
 
                case java.sql.Statement.CLOSE_ALL_RESULTS:
1774
 
 
1775
 
                        if (this.results != null) {
1776
 
                                this.results.close();
1777
 
                                this.results.clearNextResult();
1778
 
                        }
1779
 
 
1780
 
                        closeAllOpenResults();
1781
 
 
1782
 
                        break;
1783
 
 
1784
 
                case java.sql.Statement.KEEP_CURRENT_RESULT:
1785
 
                        if (!this.connection.getDontTrackOpenResources()) {
1786
 
                                this.openResults.add(this.results);
1787
 
                        }
1788
 
 
1789
 
                        this.results.clearNextResult(); // nobody besides us should
1790
 
                        // ever need this value...
1791
 
                        break;
1792
 
 
1793
 
                default:
1794
 
                        throw SQLError.createSQLException(Messages
1795
 
                                        .getString("Statement.19"), //$NON-NLS-1$
1796
 
                                        SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$
1797
 
                }
1798
 
 
1799
 
                this.results = nextResultSet;
1800
 
 
1801
 
                if (this.results == null) {
1802
 
                        this.updateCount = -1;
1803
 
                        this.lastInsertId = -1;
1804
 
                } else if (this.results.reallyResult()) {
1805
 
                        this.updateCount = -1;
1806
 
                        this.lastInsertId = -1;
1807
 
                } else {
1808
 
                        this.updateCount = this.results.getUpdateCount();
1809
 
                        this.lastInsertId = this.results.getUpdateID();
1810
 
                }
1811
 
 
1812
 
                return ((this.results != null) && this.results.reallyResult()) ? true
1813
 
                                : false;
1814
 
        }
1815
 
 
1816
 
        /**
1817
 
         * The queryTimeout limit is the number of seconds the driver will wait for
1818
 
         * a Statement to execute. If the limit is exceeded, a SQLException is
1819
 
         * thrown.
1820
 
         * 
1821
 
         * @return the current query timeout limit in seconds; 0 = unlimited
1822
 
         * 
1823
 
         * @exception SQLException
1824
 
         *                if a database access error occurs
1825
 
         */
1826
 
        public int getQueryTimeout() throws SQLException {
1827
 
                return this.timeoutInMillis / 1000;
1828
 
        }
1829
 
 
1830
 
        /**
1831
 
         * Parses actual record count from 'info' message
1832
 
         * 
1833
 
         * @param serverInfo
1834
 
         *            DOCUMENT ME!
1835
 
         * 
1836
 
         * @return DOCUMENT ME!
1837
 
         */
1838
 
        private int getRecordCountFromInfo(String serverInfo) {
1839
 
                StringBuffer recordsBuf = new StringBuffer();
1840
 
                int recordsCount = 0;
1841
 
                int duplicatesCount = 0;
1842
 
 
1843
 
                char c = (char) 0;
1844
 
 
1845
 
                int length = serverInfo.length();
1846
 
                int i = 0;
1847
 
 
1848
 
                for (; i < length; i++) {
1849
 
                        c = serverInfo.charAt(i);
1850
 
 
1851
 
                        if (Character.isDigit(c)) {
1852
 
                                break;
1853
 
                        }
1854
 
                }
1855
 
 
1856
 
                recordsBuf.append(c);
1857
 
                i++;
1858
 
 
1859
 
                for (; i < length; i++) {
1860
 
                        c = serverInfo.charAt(i);
1861
 
 
1862
 
                        if (!Character.isDigit(c)) {
1863
 
                                break;
1864
 
                        }
1865
 
 
1866
 
                        recordsBuf.append(c);
1867
 
                }
1868
 
 
1869
 
                recordsCount = Integer.parseInt(recordsBuf.toString());
1870
 
 
1871
 
                StringBuffer duplicatesBuf = new StringBuffer();
1872
 
 
1873
 
                for (; i < length; i++) {
1874
 
                        c = serverInfo.charAt(i);
1875
 
 
1876
 
                        if (Character.isDigit(c)) {
1877
 
                                break;
1878
 
                        }
1879
 
                }
1880
 
 
1881
 
                duplicatesBuf.append(c);
1882
 
                i++;
1883
 
 
1884
 
                for (; i < length; i++) {
1885
 
                        c = serverInfo.charAt(i);
1886
 
 
1887
 
                        if (!Character.isDigit(c)) {
1888
 
                                break;
1889
 
                        }
1890
 
 
1891
 
                        duplicatesBuf.append(c);
1892
 
                }
1893
 
 
1894
 
                duplicatesCount = Integer.parseInt(duplicatesBuf.toString());
1895
 
 
1896
 
                return recordsCount - duplicatesCount;
1897
 
        }
1898
 
 
1899
 
        /**
1900
 
         * getResultSet returns the current result as a ResultSet. It should only be
1901
 
         * called once per result.
1902
 
         * 
1903
 
         * @return the current result set; null if there are no more
1904
 
         * 
1905
 
         * @exception SQLException
1906
 
         *                if a database access error occurs (why?)
1907
 
         */
1908
 
        public java.sql.ResultSet getResultSet() throws SQLException {
1909
 
                return ((this.results != null) && this.results.reallyResult()) ? (java.sql.ResultSet) this.results
1910
 
                                : null;
1911
 
        }
1912
 
 
1913
 
        /**
1914
 
         * JDBC 2.0 Determine the result set concurrency.
1915
 
         * 
1916
 
         * @return CONCUR_UPDATABLE or CONCUR_READONLY
1917
 
         * 
1918
 
         * @throws SQLException
1919
 
         *             if an error occurs
1920
 
         */
1921
 
        public int getResultSetConcurrency() throws SQLException {
1922
 
                return this.resultSetConcurrency;
1923
 
        }
1924
 
 
1925
 
        /**
1926
 
         * @see Statement#getResultSetHoldability()
1927
 
         */
1928
 
        public int getResultSetHoldability() throws SQLException {
1929
 
                return java.sql.ResultSet.HOLD_CURSORS_OVER_COMMIT;
1930
 
        }
1931
 
 
1932
 
        protected ResultSet getResultSetInternal() {
1933
 
                return this.results;
1934
 
        }
1935
 
 
1936
 
        /**
1937
 
         * JDBC 2.0 Determine the result set type.
1938
 
         * 
1939
 
         * @return the ResultSet type (SCROLL_SENSITIVE or SCROLL_INSENSITIVE)
1940
 
         * 
1941
 
         * @throws SQLException
1942
 
         *             if an error occurs.
1943
 
         */
1944
 
        public int getResultSetType() throws SQLException {
1945
 
                return this.resultSetType;
1946
 
        }
1947
 
 
1948
 
        /**
1949
 
         * getUpdateCount returns the current result as an update count, if the
1950
 
         * result is a ResultSet or there are no more results, -1 is returned. It
1951
 
         * should only be called once per result.
1952
 
         * 
1953
 
         * @return the current result as an update count.
1954
 
         * 
1955
 
         * @exception SQLException
1956
 
         *                if a database access error occurs
1957
 
         */
1958
 
        public int getUpdateCount() throws SQLException {
1959
 
                if (this.results == null) {
1960
 
                        return -1;
1961
 
                }
1962
 
 
1963
 
                if (this.results.reallyResult()) {
1964
 
                        return -1;
1965
 
                }
1966
 
 
1967
 
                int truncatedUpdateCount = 0;
1968
 
 
1969
 
                if (this.results.getUpdateCount() > Integer.MAX_VALUE) {
1970
 
                        truncatedUpdateCount = Integer.MAX_VALUE;
1971
 
                } else {
1972
 
                        truncatedUpdateCount = (int) this.results.getUpdateCount();
1973
 
                }
1974
 
 
1975
 
                return truncatedUpdateCount;
1976
 
        }
1977
 
 
1978
 
        /**
1979
 
         * The first warning reported by calls on this Statement is returned. A
1980
 
         * Statement's execute methods clear its java.sql.SQLWarning chain.
1981
 
         * Subsequent Statement warnings will be chained to this
1982
 
         * java.sql.SQLWarning.
1983
 
         * 
1984
 
         * <p>
1985
 
         * The Warning chain is automatically cleared each time a statement is
1986
 
         * (re)executed.
1987
 
         * </p>
1988
 
         * 
1989
 
         * <p>
1990
 
         * <B>Note:</B> If you are processing a ResultSet then any warnings
1991
 
         * associated with ResultSet reads will be chained on the ResultSet object.
1992
 
         * </p>
1993
 
         * 
1994
 
         * @return the first java.sql.SQLWarning or null
1995
 
         * 
1996
 
         * @exception SQLException
1997
 
         *                if a database access error occurs
1998
 
         */
1999
 
        public java.sql.SQLWarning getWarnings() throws SQLException {
2000
 
                checkClosed();
2001
 
 
2002
 
                if (this.connection != null && !this.connection.isClosed()
2003
 
                                && this.connection.versionMeetsMinimum(4, 1, 0)) {
2004
 
                        SQLWarning pendingWarningsFromServer = SQLError
2005
 
                                        .convertShowWarningsToSQLWarnings(this.connection);
2006
 
 
2007
 
                        if (this.warningChain != null) {
2008
 
                                this.warningChain.setNextWarning(pendingWarningsFromServer);
2009
 
                        } else {
2010
 
                                this.warningChain = pendingWarningsFromServer;
2011
 
                        }
2012
 
 
2013
 
                        return this.warningChain;
2014
 
                }
2015
 
 
2016
 
                return this.warningChain;
2017
 
        }
2018
 
 
2019
 
 
2020
 
 
2021
 
        /**
2022
 
         * Closes this statement, and frees resources.
2023
 
         * 
2024
 
         * @param calledExplicitly
2025
 
         *            was this called from close()?
2026
 
         * 
2027
 
         * @throws SQLException
2028
 
         *             if an error occurs
2029
 
         */
2030
 
        protected void realClose(boolean calledExplicitly, boolean closeOpenResults)
2031
 
                        throws SQLException {
2032
 
                if (this.isClosed) {
2033
 
                        return;
2034
 
                }
2035
 
 
2036
 
                if (this.useUsageAdvisor) {
2037
 
                        if (!calledExplicitly) {
2038
 
                                String message = Messages.getString("Statement.63") //$NON-NLS-1$
2039
 
                                                + Messages.getString("Statement.64"); //$NON-NLS-1$
2040
 
 
2041
 
                                this.eventSink.consumeEvent(new ProfilerEvent(
2042
 
                                                ProfilerEvent.TYPE_WARN,
2043
 
                                                "", //$NON-NLS-1$
2044
 
                                                this.currentCatalog, this.connectionId, this.getId(),
2045
 
                                                -1, System.currentTimeMillis(), 0,
2046
 
                                                Constants.MILLIS_I18N, null, this.pointOfOrigin,
2047
 
                                                message));
2048
 
                        }
2049
 
                }
2050
 
 
2051
 
                if (this.results != null) {
2052
 
                        if (closeOpenResults) {
2053
 
                                closeOpenResults = !this.holdResultsOpenOverClose;
2054
 
                        }
2055
 
 
2056
 
                        if (closeOpenResults && this.connection != null
2057
 
                                        && !this.connection.getHoldResultsOpenOverStatementClose()) {
2058
 
                                try {
2059
 
                                        this.results.close();
2060
 
                                } catch (Exception ex) {
2061
 
                                        ;
2062
 
                                }
2063
 
 
2064
 
                                this.closeAllOpenResults();
2065
 
                        }
2066
 
                }
2067
 
 
2068
 
                if (this.connection != null) {
2069
 
                        if (this.maxRowsChanged) {
2070
 
                                this.connection.unsetMaxRows(this);
2071
 
                        }
2072
 
 
2073
 
                        if (!this.connection.getDontTrackOpenResources()) {
2074
 
                                this.connection.unregisterStatement(this);
2075
 
                        }
2076
 
                }
2077
 
 
2078
 
                this.isClosed = true;
2079
 
                
2080
 
                this.results = null;
2081
 
                this.connection = null;
2082
 
                this.warningChain = null;
2083
 
                this.openResults = null;
2084
 
                this.batchedGeneratedKeys = null;
2085
 
                this.cancelTimeoutMutex = null;
2086
 
                this.pingTarget = null;
2087
 
        }
2088
 
 
2089
 
        /**
2090
 
         * setCursorName defines the SQL cursor name that will be used by subsequent
2091
 
         * execute methods. This name can then be used in SQL positioned
2092
 
         * update/delete statements to identify the current row in the ResultSet
2093
 
         * generated by this statement. If a database doesn't support positioned
2094
 
         * update/delete, this method is a no-op.
2095
 
         * 
2096
 
         * <p>
2097
 
         * <b>Note:</b> This MySQL driver does not support cursors.
2098
 
         * </p>
2099
 
         * 
2100
 
         * @param name
2101
 
         *            the new cursor name
2102
 
         * 
2103
 
         * @exception SQLException
2104
 
         *                if a database access error occurs
2105
 
         */
2106
 
        public void setCursorName(String name) throws SQLException {
2107
 
                // No-op
2108
 
        }
2109
 
 
2110
 
        /**
2111
 
         * If escape scanning is on (the default), the driver will do escape
2112
 
         * substitution before sending the SQL to the database.
2113
 
         * 
2114
 
         * @param enable
2115
 
         *            true to enable; false to disable
2116
 
         * 
2117
 
         * @exception SQLException
2118
 
         *                if a database access error occurs
2119
 
         */
2120
 
        public void setEscapeProcessing(boolean enable)
2121
 
                        throws SQLException {
2122
 
                this.doEscapeProcessing = enable;
2123
 
        }
2124
 
 
2125
 
        /**
2126
 
         * JDBC 2.0 Give a hint as to the direction in which the rows in a result
2127
 
         * set will be processed. The hint applies only to result sets created using
2128
 
         * this Statement object. The default value is ResultSet.FETCH_FORWARD.
2129
 
         * 
2130
 
         * @param direction
2131
 
         *            the initial direction for processing rows
2132
 
         * 
2133
 
         * @exception SQLException
2134
 
         *                if a database-access error occurs or direction is not one
2135
 
         *                of ResultSet.FETCH_FORWARD, ResultSet.FETCH_REVERSE, or
2136
 
         *                ResultSet.FETCH_UNKNOWN
2137
 
         */
2138
 
        public void setFetchDirection(int direction) throws SQLException {
2139
 
                switch (direction) {
2140
 
                case java.sql.ResultSet.FETCH_FORWARD:
2141
 
                case java.sql.ResultSet.FETCH_REVERSE:
2142
 
                case java.sql.ResultSet.FETCH_UNKNOWN:
2143
 
                        break;
2144
 
 
2145
 
                default:
2146
 
                        throw SQLError.createSQLException(
2147
 
                                        Messages.getString("Statement.5"), //$NON-NLS-1$
2148
 
                                        SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$
2149
 
                }
2150
 
        }
2151
 
 
2152
 
        /**
2153
 
         * JDBC 2.0 Give the JDBC driver a hint as to the number of rows that should
2154
 
         * be fetched from the database when more rows are needed. The number of
2155
 
         * rows specified only affects result sets created using this statement. If
2156
 
         * the value specified is zero, then the hint is ignored. The default value
2157
 
         * is zero.
2158
 
         * 
2159
 
         * @param rows
2160
 
         *            the number of rows to fetch
2161
 
         * 
2162
 
         * @exception SQLException
2163
 
         *                if a database-access error occurs, or the condition 0
2164
 
         *                &lt;= rows &lt;= this.getMaxRows() is not satisfied.
2165
 
         */
2166
 
        public void setFetchSize(int rows) throws SQLException {
2167
 
                if (((rows < 0) && (rows != Integer.MIN_VALUE))
2168
 
                                || ((this.maxRows != 0) && (this.maxRows != -1) && (rows > this
2169
 
                                                .getMaxRows()))) {
2170
 
                        throw SQLError.createSQLException(
2171
 
                                        Messages.getString("Statement.7"), //$NON-NLS-1$
2172
 
                                        SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$ //$NON-NLS-2$
2173
 
                }
2174
 
 
2175
 
                this.fetchSize = rows;
2176
 
        }
2177
 
 
2178
 
        protected void setHoldResultsOpenOverClose(boolean holdResultsOpenOverClose) {
2179
 
                this.holdResultsOpenOverClose = holdResultsOpenOverClose;
2180
 
        }
2181
 
 
2182
 
        /**
2183
 
         * Sets the maxFieldSize
2184
 
         * 
2185
 
         * @param max
2186
 
         *            the new max column size limit; zero means unlimited
2187
 
         * 
2188
 
         * @exception SQLException
2189
 
         *                if size exceeds buffer size
2190
 
         */
2191
 
        public void setMaxFieldSize(int max) throws SQLException {
2192
 
                if (max < 0) {
2193
 
                        throw SQLError.createSQLException(Messages
2194
 
                                        .getString("Statement.11"), //$NON-NLS-1$
2195
 
                                        SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$
2196
 
                }
2197
 
 
2198
 
                int maxBuf = (this.connection != null) ? this.connection
2199
 
                                .getMaxAllowedPacket() : MysqlIO.getMaxBuf();
2200
 
 
2201
 
                if (max > maxBuf) {
2202
 
                        throw SQLError.createSQLException(Messages.getString(
2203
 
                                        "Statement.13", //$NON-NLS-1$
2204
 
                                        new Object[] { new Long(maxBuf) }), //$NON-NLS-1$
2205
 
                                        SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$
2206
 
                }
2207
 
 
2208
 
                this.maxFieldSize = max;
2209
 
        }
2210
 
 
2211
 
        /**
2212
 
         * Set the maximum number of rows
2213
 
         * 
2214
 
         * @param max
2215
 
         *            the new max rows limit; zero means unlimited
2216
 
         * 
2217
 
         * @exception SQLException
2218
 
         *                if a database access error occurs
2219
 
         * 
2220
 
         * @see getMaxRows
2221
 
         */
2222
 
        public void setMaxRows(int max) throws SQLException {
2223
 
                if ((max > MysqlDefs.MAX_ROWS) || (max < 0)) {
2224
 
                        throw SQLError
2225
 
                                        .createSQLException(
2226
 
                                                        Messages.getString("Statement.15") + max //$NON-NLS-1$
2227
 
                                                                        + " > " //$NON-NLS-1$ //$NON-NLS-2$
2228
 
                                                                        + MysqlDefs.MAX_ROWS + ".", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$ //$NON-NLS-2$
2229
 
                }
2230
 
 
2231
 
                if (max == 0) {
2232
 
                        max = -1;
2233
 
                }
2234
 
 
2235
 
                this.maxRows = max;
2236
 
                this.maxRowsChanged = true;
2237
 
 
2238
 
                if (this.maxRows == -1) {
2239
 
                        this.connection.unsetMaxRows(this);
2240
 
                        this.maxRowsChanged = false;
2241
 
                } else {
2242
 
                        // Most people don't use setMaxRows()
2243
 
                        // so don't penalize them
2244
 
                        // with the extra query it takes
2245
 
                        // to do it efficiently unless we need
2246
 
                        // to.
2247
 
                        this.connection.maxRowsChanged(this);
2248
 
                }
2249
 
        }
2250
 
 
2251
 
        /**
2252
 
         * Sets the queryTimeout limit
2253
 
         * 
2254
 
         * @param seconds -
2255
 
         *            the new query timeout limit in seconds
2256
 
         * 
2257
 
         * @exception SQLException
2258
 
         *                if a database access error occurs
2259
 
         */
2260
 
        public void setQueryTimeout(int seconds) throws SQLException {
2261
 
                if (seconds < 0) {
2262
 
                        throw SQLError.createSQLException(Messages
2263
 
                                        .getString("Statement.21"), //$NON-NLS-1$
2264
 
                                        SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$
2265
 
                }
2266
 
 
2267
 
                this.timeoutInMillis = seconds * 1000;
2268
 
        }
2269
 
 
2270
 
        /**
2271
 
         * Sets the concurrency for result sets generated by this statement
2272
 
         * 
2273
 
         * @param concurrencyFlag
2274
 
         *            DOCUMENT ME!
2275
 
         */
2276
 
        void setResultSetConcurrency(int concurrencyFlag) {
2277
 
                this.resultSetConcurrency = concurrencyFlag;
2278
 
        }
2279
 
 
2280
 
        /**
2281
 
         * Sets the result set type for result sets generated by this statement
2282
 
         * 
2283
 
         * @param typeFlag
2284
 
         *            DOCUMENT ME!
2285
 
         */
2286
 
        void setResultSetType(int typeFlag) {
2287
 
                this.resultSetType = typeFlag;
2288
 
        }
2289
 
 
2290
 
        protected void getBatchedGeneratedKeys(java.sql.Statement batchedStatement) throws SQLException {
2291
 
                if (this.retrieveGeneratedKeys) {
2292
 
                        java.sql.ResultSet rs = null;
2293
 
        
2294
 
                        try {
2295
 
                                rs = batchedStatement.getGeneratedKeys();
2296
 
        
2297
 
                                while (rs.next()) {
2298
 
                                        this.batchedGeneratedKeys
2299
 
                                                        .add(new byte[][] { rs.getBytes(1) });
2300
 
                                }
2301
 
                        } finally {
2302
 
                                if (rs != null) {
2303
 
                                        rs.close();
2304
 
                                }
2305
 
                        }
2306
 
                }
2307
 
        }
2308
 
        
2309
 
        protected void getBatchedGeneratedKeys() throws SQLException {
2310
 
                if (this.retrieveGeneratedKeys) {
2311
 
                        java.sql.ResultSet rs = null;
2312
 
        
2313
 
                        try {
2314
 
                                rs = getGeneratedKeysInternal();
2315
 
        
2316
 
                                while (rs.next()) {
2317
 
                                        this.batchedGeneratedKeys
2318
 
                                                        .add(new byte[][] { rs.getBytes(1) });
2319
 
                                }
2320
 
                        } finally {
2321
 
                                if (rs != null) {
2322
 
                                        rs.close();
2323
 
                                }
2324
 
                        }
2325
 
                }
2326
 
        }
2327
 
        
2328
 
        /**
2329
 
         * @return
2330
 
         */
2331
 
        private boolean useServerFetch() throws SQLException {
2332
 
 
2333
 
                return this.connection.isCursorFetchEnabled() && this.fetchSize > 0
2334
 
                                && this.resultSetConcurrency == ResultSet.CONCUR_READ_ONLY
2335
 
                                && this.resultSetType == ResultSet.TYPE_FORWARD_ONLY;
2336
 
        }
2337
 
 
2338
 
        protected int findStartOfStatement(String sql) {
2339
 
                int statementStartPos = 0;
2340
 
                
2341
 
                if (StringUtils.startsWithIgnoreCaseAndWs(sql, "/*")) {
2342
 
                        statementStartPos = sql.indexOf("*/");
2343
 
                        
2344
 
                        if (statementStartPos == -1) {
2345
 
                                statementStartPos = 0;
2346
 
                        } else {
2347
 
                                statementStartPos += 2;
2348
 
                        }
2349
 
                } else if (StringUtils.startsWithIgnoreCaseAndWs(sql, "--")
2350
 
                        || StringUtils.startsWithIgnoreCaseAndWs(sql, "#")) {
2351
 
                        statementStartPos = sql.indexOf('\n');
2352
 
                        
2353
 
                        if (statementStartPos == -1) {
2354
 
                                statementStartPos = sql.indexOf('\r');
2355
 
                                
2356
 
                                if (statementStartPos == -1) {
2357
 
                                        statementStartPos = 0;
2358
 
                                }
2359
 
                        }
2360
 
                }
2361
 
                
2362
 
                return statementStartPos;
2363
 
        }
2364
 
 
2365
 
        protected synchronized void setPingTarget(PingTarget pingTarget) {
2366
 
                this.pingTarget = pingTarget;
2367
 
        }
2368
 
}