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

« back to all changes in this revision

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

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

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 Copyright (C) 2002-2004 MySQL AB
 
2
 Copyright (C) 2002-2007 MySQL AB
3
3
 
4
4
 This program is free software; you can redistribute it and/or modify
5
5
 it under the terms of version 2 of the GNU General Public License as 
24
24
 */
25
25
package com.mysql.jdbc;
26
26
 
27
 
import com.mysql.jdbc.Statement.CancelTask;
 
27
import com.mysql.jdbc.exceptions.MySQLStatementCancelledException;
28
28
import com.mysql.jdbc.exceptions.MySQLTimeoutException;
29
29
import com.mysql.jdbc.profiler.ProfileEventSink;
30
30
import com.mysql.jdbc.profiler.ProfilerEvent;
31
31
 
32
 
import java.io.ByteArrayInputStream;
33
32
import java.io.IOException;
34
33
import java.io.InputStream;
35
34
import java.io.Reader;
36
35
import java.io.UnsupportedEncodingException;
37
36
 
 
37
import java.lang.reflect.Constructor;
 
38
import java.lang.reflect.InvocationTargetException;
38
39
import java.math.BigDecimal;
39
40
 
40
41
import java.net.URL;
45
46
import java.sql.Date;
46
47
import java.sql.ParameterMetaData;
47
48
import java.sql.Ref;
 
49
import java.sql.ResultSet;
48
50
import java.sql.SQLException;
49
51
import java.sql.Time;
50
52
import java.sql.Timestamp;
51
53
import java.sql.Types;
52
54
 
53
55
import java.util.ArrayList;
54
 
import java.util.BitSet;
55
56
import java.util.Calendar;
56
 
import java.util.Locale;
 
57
import java.util.Properties;
57
58
import java.util.TimeZone;
58
59
 
59
60
/**
64
65
 *          mmatthews Exp $
65
66
 */
66
67
public class ServerPreparedStatement extends PreparedStatement {
 
68
        private static final Constructor JDBC_4_SPS_CTOR;
 
69
        
 
70
        static {
 
71
                if (Util.isJdbc4()) {
 
72
                        try {
 
73
                                JDBC_4_SPS_CTOR = Class.forName("com.mysql.jdbc.JDBC4ServerPreparedStatement")
 
74
                                .getConstructor(
 
75
                                new Class[] { ConnectionImpl.class, String.class, String.class,
 
76
                                                Integer.TYPE, Integer.TYPE});
 
77
                        } catch (SecurityException e) {
 
78
                                throw new RuntimeException(e);
 
79
                        } catch (NoSuchMethodException e) {
 
80
                                throw new RuntimeException(e);
 
81
                        } catch (ClassNotFoundException e) {
 
82
                                throw new RuntimeException(e);
 
83
                        } 
 
84
                } else {
 
85
                        JDBC_4_SPS_CTOR = null;
 
86
                }
 
87
        }
 
88
        
67
89
        protected static final int BLOB_STREAM_READ_BUF_SIZE = 8192;
68
90
 
69
91
        static class BatchedBindValues {
80
102
                }
81
103
        }
82
104
 
83
 
        static class BindValue {
 
105
        public static class BindValue {
84
106
 
85
107
                long boundBeforeExecutionNum = 0;
86
108
                
87
 
                long bindLength; /* Default length of data */
 
109
                public long bindLength; /* Default length of data */
88
110
 
89
111
                int bufferType; /* buffer type */
90
112
 
96
118
 
97
119
                int intBinding;
98
120
 
99
 
                boolean isLongData; /* long data indicator */
 
121
                public boolean isLongData; /* long data indicator */
100
122
 
101
 
                boolean isNull; /* NULL indicator */
 
123
                public boolean isNull; /* NULL indicator */
102
124
 
103
125
                boolean isSet = false; /* has this parameter been set? */
104
126
 
106
128
 
107
129
                short shortBinding;
108
130
 
109
 
                Object value; /* The value to store */
 
131
                public Object value; /* The value to store */
110
132
 
111
133
                BindValue() {
112
134
                }
272
294
        private boolean serverNeedsResetBeforeEachExecution;
273
295
 
274
296
        /**
 
297
         * Creates a prepared statement instance -- We need to provide factory-style
 
298
         * methods so we can support both JDBC3 (and older) and JDBC4 runtimes,
 
299
         * otherwise the class verifier complains when it tries to load JDBC4-only
 
300
         * interface classes that are present in JDBC4 method signatures.
 
301
         */
 
302
 
 
303
        protected static ServerPreparedStatement getInstance(ConnectionImpl conn,
 
304
                        String sql, String catalog, int resultSetType,
 
305
                        int resultSetConcurrency) throws SQLException {
 
306
                if (!Util.isJdbc4()) {
 
307
                        return new ServerPreparedStatement(conn, sql, catalog,
 
308
                                        resultSetType, resultSetConcurrency);
 
309
                }
 
310
 
 
311
                try {
 
312
                        return (ServerPreparedStatement) JDBC_4_SPS_CTOR.newInstance(new Object[] { conn,
 
313
                                        sql, catalog, Constants.integerValueOf(resultSetType),
 
314
                                        Constants.integerValueOf(resultSetConcurrency) });
 
315
                } catch (IllegalArgumentException e) {
 
316
                        throw new SQLException(e.toString(), SQLError.SQL_STATE_GENERAL_ERROR);
 
317
                } catch (InstantiationException e) {
 
318
                        throw new SQLException(e.toString(), SQLError.SQL_STATE_GENERAL_ERROR);
 
319
                } catch (IllegalAccessException e) {
 
320
                        throw new SQLException(e.toString(), SQLError.SQL_STATE_GENERAL_ERROR);
 
321
                } catch (InvocationTargetException e) {
 
322
                        Throwable target = e.getTargetException(); 
 
323
                        
 
324
                        if (target instanceof SQLException) {
 
325
                                throw (SQLException)target;
 
326
                        }
 
327
                        
 
328
                        throw new SQLException(target.toString(), SQLError.SQL_STATE_GENERAL_ERROR);
 
329
                }
 
330
        }
 
331
 
 
332
        /**
275
333
         * Creates a new ServerPreparedStatement object.
276
334
         * 
277
335
         * @param conn
284
342
         * @throws SQLException
285
343
         *             If an error occurs
286
344
         */
287
 
        public ServerPreparedStatement(Connection conn, String sql, String catalog,
 
345
        protected ServerPreparedStatement(ConnectionImpl conn, String sql, String catalog,
288
346
                        int resultSetType, int resultSetConcurrency)
289
347
                        throws SQLException {
290
348
                super(conn, catalog);
291
349
 
292
350
                checkNullOrEmptyQuery(sql);
293
351
 
294
 
                this.isSelectQuery = StringUtils.startsWithIgnoreCaseAndWs(sql,
295
 
                                "SELECT"); //$NON-NLS-1$
 
352
                int startOfStatement = findStartOfStatement(sql);
 
353
                
 
354
                this.firstCharOfStmt = StringUtils.firstAlphaCharUc(sql, startOfStatement);
 
355
                
 
356
                this.isSelectQuery = 'S' == this.firstCharOfStmt;
296
357
                
297
358
                if (this.connection.versionMeetsMinimum(5, 0, 0)) {
298
359
                        this.serverNeedsResetBeforeEachExecution = 
302
363
                                !this.connection.versionMeetsMinimum(4, 1, 10);
303
364
                }
304
365
                
 
366
                this.useAutoSlowLog = this.connection.getAutoSlowLog();
305
367
                this.useTrueBoolean = this.connection.versionMeetsMinimum(3, 21, 23);
306
368
                this.hasLimitClause = (StringUtils.indexOfIgnoreCase(sql, "LIMIT") != -1); //$NON-NLS-1$
307
 
                this.firstCharOfStmt = StringUtils.firstNonWsCharUc(sql);
308
 
                this.originalSql = sql;
 
369
                
 
370
                String statementComment = this.connection.getStatementComment();
 
371
 
 
372
                this.originalSql = (statementComment == null) ? sql : "/* "
 
373
                                + statementComment + " */ " + sql;
309
374
 
310
375
                if (this.connection.versionMeetsMinimum(4, 1, 2)) {
311
376
                        this.stringTypeCode = MysqlDefs.FIELD_TYPE_VAR_STRING;
328
393
                
329
394
                setResultSetType(resultSetType);
330
395
                setResultSetConcurrency(resultSetConcurrency);
 
396
                
 
397
                this.parameterTypes = new int[this.parameterCount];
331
398
        }
332
399
 
333
400
        /**
336
403
         * @exception SQLException
337
404
         *                if a database-access error occurs.
338
405
         * 
339
 
         * @see Statement#addBatch
 
406
         * @see StatementImpl#addBatch
340
407
         */
341
408
        public synchronized void addBatch() throws SQLException {
342
409
                checkClosed();
357
424
                PreparedStatement pStmtForSub = null;
358
425
 
359
426
                try {
360
 
                        pStmtForSub = new PreparedStatement(this.connection,
 
427
                        pStmtForSub = PreparedStatement.getInstance(this.connection,
361
428
                                        this.originalSql, this.currentCatalog);
362
429
 
363
430
                        int numParameters = pStmtForSub.parameterCount;
460
527
 
461
528
        protected boolean isCached = false;
462
529
 
 
530
        private boolean useAutoSlowLog;
 
531
 
463
532
        protected void setClosed(boolean flag) {
464
533
                this.isClosed = flag;
465
534
        }
634
703
 
635
704
                                                                                while (rs.next()) {
636
705
                                                                                        this.batchedGeneratedKeys
637
 
                                                                                                        .add(new byte[][] { rs
638
 
                                                                                                                        .getBytes(1) });
 
706
                                                                                                        .add(new ByteArrayRow(new byte[][] { rs
 
707
                                                                                                                        .getBytes(1) }));
639
708
                                                                                }
640
709
                                                                        } finally {
641
710
                                                                                if (rs != null) {
682
751
         * @see com.mysql.jdbc.PreparedStatement#executeInternal(int,
683
752
         *      com.mysql.jdbc.Buffer, boolean, boolean)
684
753
         */
685
 
        protected com.mysql.jdbc.ResultSet executeInternal(int maxRowsToRetrieve,
 
754
        protected com.mysql.jdbc.ResultSetInternalMethods executeInternal(int maxRowsToRetrieve,
686
755
                        Buffer sendPacket, boolean createStreamingResultSet,
687
 
                        boolean queryIsSelectOnly, boolean unpackFields, boolean isBatch)
 
756
                        boolean queryIsSelectOnly, Field[] metadataFromCache,
 
757
                        boolean isBatch)
688
758
                        throws SQLException {
689
759
                this.numberOfExecutions++;
690
760
 
691
761
                // We defer to server-side execution
692
762
                try {
693
 
                        return serverExecute(maxRowsToRetrieve, createStreamingResultSet);
 
763
                        return serverExecute(maxRowsToRetrieve, createStreamingResultSet, 
 
764
                                        metadataFromCache);
694
765
                } catch (SQLException sqlEx) {
695
766
                        // don't wrap SQLExceptions
696
767
                        if (this.connection.getEnablePacketDebug()) {
705
776
                                                .append("\n\nQuery being executed when exception was thrown:\n\n");
706
777
                                messageBuf.append(extractedSql);
707
778
 
708
 
                                sqlEx = Connection.appendMessageToException(sqlEx, messageBuf
 
779
                                sqlEx = ConnectionImpl.appendMessageToException(sqlEx, messageBuf
709
780
                                                .toString());
710
781
                        }
711
782
 
726
797
                                                .append("\n\nQuery being executed when exception was thrown:\n\n");
727
798
                                messageBuf.append(extractedSql);
728
799
 
729
 
                                sqlEx = Connection.appendMessageToException(sqlEx, messageBuf
 
800
                                sqlEx = ConnectionImpl.appendMessageToException(sqlEx, messageBuf
730
801
                                                .toString());
731
802
                        }
732
803
 
751
822
                return null; // we don't use this type of packet
752
823
        }
753
824
 
754
 
        private BindValue getBinding(int parameterIndex, boolean forLongData)
 
825
        /**
 
826
         * Returns the structure representing the value that (can be)/(is)
 
827
         * bound at the given parameter index.
 
828
         * 
 
829
         * @param parameterIndex 1-based
 
830
         * @param forLongData is this for a stream?
 
831
         * @return
 
832
         * @throws SQLException
 
833
         */
 
834
        protected BindValue getBinding(int parameterIndex, boolean forLongData)
755
835
                        throws SQLException {
756
836
                checkClosed();
757
837
                
878
958
                        if (this.connection.getAutoGenerateTestcaseScript()) {
879
959
                                dumpCloseForTestcase();
880
960
                        }
881
 
                        
882
 
                        synchronized (this.connection.getMutex()) {
883
 
        
884
 
                                //
885
 
                                // Don't communicate with the server if we're being
886
 
                                // called from the finalizer...
887
 
                                // 
888
 
                                // This will leak server resources, but if we don't do this,
889
 
                                // we'll deadlock (potentially, because there's no guarantee
890
 
                                // when, what order, and what concurrency finalizers will be
891
 
                                // called with). Well-behaved programs won't rely on finalizers
892
 
                                // to clean up their statements.
893
 
                                //
894
 
                                
895
 
                                SQLException exceptionDuringClose = null;
896
 
                                
897
 
 
898
 
                                if (calledExplicitly) {
 
961
 
 
962
                        //
 
963
                        // Don't communicate with the server if we're being
 
964
                        // called from the finalizer...
 
965
                        // 
 
966
                        // This will leak server resources, but if we don't do this,
 
967
                        // we'll deadlock (potentially, because there's no guarantee
 
968
                        // when, what order, and what concurrency finalizers will be
 
969
                        // called with). Well-behaved programs won't rely on finalizers
 
970
                        // to clean up their statements.
 
971
                        //
 
972
 
 
973
                        SQLException exceptionDuringClose = null;
 
974
 
 
975
                        if (calledExplicitly && !this.connection.isClosed()) {
 
976
                                synchronized (this.connection.getMutex()) {
899
977
                                        try {
900
 
                                                
 
978
 
901
979
                                                MysqlIO mysql = this.connection.getIO();
902
 
                                                
 
980
 
903
981
                                                Buffer packet = mysql.getSharedSendPacket();
904
 
                                                
 
982
 
905
983
                                                packet.writeByte((byte) MysqlDefs.COM_CLOSE_STATEMENT);
906
984
                                                packet.writeLong(this.serverStatementId);
907
 
                                                
 
985
 
908
986
                                                mysql.sendCommand(MysqlDefs.COM_CLOSE_STATEMENT, null,
909
987
                                                                packet, true, null);
910
988
                                        } catch (SQLException sqlEx) {
911
989
                                                exceptionDuringClose = sqlEx;
912
990
                                        }
913
 
                                        
914
 
                                }
915
 
 
916
 
                                super.realClose(calledExplicitly, closeOpenResults);
917
 
 
918
 
                                clearParametersInternal(false);
919
 
                                this.parameterBindings = null;
920
 
                                
921
 
                                this.parameterFields = null;
922
 
                                this.resultFields = null;
923
 
                                
924
 
                                if (exceptionDuringClose != null) {
925
 
                                        throw exceptionDuringClose;
926
 
                                }
 
991
                                }
 
992
                        }
 
993
 
 
994
                        super.realClose(calledExplicitly, closeOpenResults);
 
995
 
 
996
                        clearParametersInternal(false);
 
997
                        this.parameterBindings = null;
 
998
 
 
999
                        this.parameterFields = null;
 
1000
                        this.resultFields = null;
 
1001
 
 
1002
                        if (exceptionDuringClose != null) {
 
1003
                                throw exceptionDuringClose;
927
1004
                        }
928
1005
                }
929
1006
        }
1009
1086
         * 
1010
1087
         * @throws SQLException
1011
1088
         */
1012
 
        private com.mysql.jdbc.ResultSet serverExecute(int maxRowsToRetrieve,
1013
 
                        boolean createStreamingResultSet) throws SQLException {
 
1089
        private com.mysql.jdbc.ResultSetInternalMethods serverExecute(int maxRowsToRetrieve,
 
1090
                        boolean createStreamingResultSet, 
 
1091
                        Field[] metadataFromCache) throws SQLException {
1014
1092
                synchronized (this.connection.getMutex()) {
1015
1093
                        if (this.detectedLongParameterSwitch) {
1016
1094
                                // Check when values were bound
1148
1226
 
1149
1227
                        long begin = 0;
1150
1228
 
1151
 
                        if (this.connection.getProfileSql()
1152
 
                                        || this.connection.getLogSlowQueries()
1153
 
                                        || this.connection.getGatherPerformanceMetrics()) {
1154
 
                                begin = System.currentTimeMillis();
 
1229
                        boolean logSlowQueries = this.connection.getLogSlowQueries();
 
1230
                        boolean gatherPerformanceMetrics = this.connection
 
1231
                                        .getGatherPerformanceMetrics();
 
1232
 
 
1233
                        if (this.profileSQL || logSlowQueries || gatherPerformanceMetrics) {
 
1234
                                begin = mysql.getCurrentTimeNanosOrMillis();
1155
1235
                        }
1156
1236
 
1157
 
                        this.wasCancelled = false;
 
1237
                        resetCancelledState();
1158
1238
                        
1159
1239
                        CancelTask timeoutTask = null;
1160
1240
 
1161
1241
                        try {
1162
 
                                if (this.timeoutInMillis != 0
 
1242
                                if (this.connection.getEnableQueryTimeouts() &&
 
1243
                                                this.timeoutInMillis != 0
1163
1244
                                                && this.connection.versionMeetsMinimum(5, 0, 0)) {
1164
 
                                        timeoutTask = new CancelTask();
 
1245
                                        timeoutTask = new CancelTask(this);
1165
1246
                                        this.connection.getCancelTimer().schedule(timeoutTask, 
1166
1247
                                                        this.timeoutInMillis);
1167
1248
                                }
1169
1250
                                Buffer resultPacket = mysql.sendCommand(MysqlDefs.COM_EXECUTE,
1170
1251
                                        null, packet, false, null);
1171
1252
                                
 
1253
                                long queryEndTime = 0L;
 
1254
                                
 
1255
                                if (logSlowQueries || gatherPerformanceMetrics || this.profileSQL) {
 
1256
                                        queryEndTime = mysql.getCurrentTimeNanosOrMillis();
 
1257
                                }
 
1258
                                
1172
1259
                                if (timeoutTask != null) {
1173
1260
                                        timeoutTask.cancel();
 
1261
                                        
 
1262
                                        if (timeoutTask.caughtWhileCancelling != null) {
 
1263
                                                throw timeoutTask.caughtWhileCancelling;
 
1264
                                        }
 
1265
                                        
1174
1266
                                        timeoutTask = null;
1175
1267
                                }
1176
1268
                                
1177
 
                                if (this.wasCancelled) {
1178
 
                                        this.wasCancelled = false;
1179
 
                                        throw new MySQLTimeoutException();
1180
 
                                }
1181
 
                        
1182
 
                                this.connection.incrementNumberOfPreparedExecutes();
1183
 
        
1184
 
                                if (this.connection.getProfileSql()) {
1185
 
                                        this.eventSink = ProfileEventSink.getInstance(this.connection);
1186
 
        
1187
 
                                        this.eventSink.consumeEvent(new ProfilerEvent(
1188
 
                                                        ProfilerEvent.TYPE_EXECUTE, "", this.currentCatalog, //$NON-NLS-1$
1189
 
                                                        this.connectionId, this.statementId, -1, System
1190
 
                                                                        .currentTimeMillis(), (int) (System
1191
 
                                                                        .currentTimeMillis() - begin), null,
1192
 
                                                        new Throwable(), truncateQueryToLog(asSql(true))));
1193
 
                                }
1194
 
        
1195
 
                                com.mysql.jdbc.ResultSet rs = mysql.readAllResults(this,
1196
 
                                                maxRowsToRetrieve, this.resultSetType,
1197
 
                                                this.resultSetConcurrency, createStreamingResultSet,
1198
 
                                                this.currentCatalog, resultPacket, true, this.fieldCount,
1199
 
                                                true);
1200
 
        
 
1269
                                synchronized (this.cancelTimeoutMutex) {
 
1270
                                        if (this.wasCancelled) {
 
1271
                                                SQLException cause = null;
 
1272
                                                
 
1273
                                                if (this.wasCancelledByTimeout) {
 
1274
                                                        cause = new MySQLTimeoutException();
 
1275
                                                } else {
 
1276
                                                        cause = new MySQLStatementCancelledException();
 
1277
                                                }
 
1278
                                                
 
1279
                                                resetCancelledState();
 
1280
                                                
 
1281
                                                throw cause;
 
1282
                                        }
 
1283
                                }
 
1284
 
 
1285
                                boolean queryWasSlow = false;
1201
1286
                                
1202
 
                                if (!createStreamingResultSet && 
1203
 
                                                this.serverNeedsResetBeforeEachExecution) {
1204
 
                                        serverResetStatement(); // clear any long data...
1205
 
                                }
1206
 
        
1207
 
                                this.sendTypesToServer = false;
1208
 
                                this.results = rs;
1209
 
        
1210
 
                                if (this.connection.getLogSlowQueries()
1211
 
                                                || this.connection.getGatherPerformanceMetrics()) {
1212
 
                                        long elapsedTime = System.currentTimeMillis() - begin;
1213
 
        
1214
 
                                        if (this.connection.getLogSlowQueries()
1215
 
                                                        && (elapsedTime >= this.connection
1216
 
                                                                        .getSlowQueryThresholdMillis())) {
 
1287
                                if (logSlowQueries || gatherPerformanceMetrics) {
 
1288
                                        long elapsedTime = queryEndTime - begin;
 
1289
 
 
1290
                                        if (logSlowQueries) {
 
1291
                                        if (this.useAutoSlowLog) {
 
1292
                                                queryWasSlow = elapsedTime > this.connection.getSlowQueryThresholdMillis();
 
1293
                                        } else {
 
1294
                                                queryWasSlow = this.connection.isAbonormallyLongQuery(elapsedTime);
 
1295
                                                
 
1296
                                                this.connection.reportQueryTime(elapsedTime);
 
1297
                                        }
 
1298
                                        }
 
1299
 
 
1300
                                        if (queryWasSlow) {
 
1301
                                                
1217
1302
                                                StringBuffer mesgBuf = new StringBuffer(
1218
1303
                                                                48 + this.originalSql.length());
1219
1304
                                                mesgBuf.append(Messages
1220
1305
                                                                .getString("ServerPreparedStatement.15")); //$NON-NLS-1$
1221
 
                                                mesgBuf.append(this.connection
1222
 
                                                                .getSlowQueryThresholdMillis());
 
1306
                                                mesgBuf.append(mysql.getSlowQueryThreshold());
1223
1307
                                                mesgBuf.append(Messages
1224
1308
                                                                .getString("ServerPreparedStatement.15a")); //$NON-NLS-1$
1225
1309
                                                mesgBuf.append(elapsedTime);
1226
1310
                                                mesgBuf.append(Messages
1227
1311
                                                                .getString("ServerPreparedStatement.16")); //$NON-NLS-1$
1228
 
                                                
 
1312
 
1229
1313
                                                mesgBuf.append("as prepared: ");
1230
1314
                                                mesgBuf.append(this.originalSql);
1231
1315
                                                mesgBuf.append("\n\n with parameters bound:\n\n");
1232
1316
                                                mesgBuf.append(asSql(true));
1233
 
        
1234
 
                                                this.connection.getLog().logWarn(mesgBuf.toString());
1235
 
        
1236
 
                                                if (this.connection.getExplainSlowQueries()) {
1237
 
                                                        String queryAsString = asSql(true);
1238
 
        
1239
 
                                                        mysql.explainSlowQuery(queryAsString.getBytes(),
1240
 
                                                                        queryAsString);
1241
 
                                                }
 
1317
 
 
1318
                                                this.eventSink
 
1319
                                                                .consumeEvent(new ProfilerEvent(
 
1320
                                                                                ProfilerEvent.TYPE_SLOW_QUERY,
 
1321
                                                                                "", this.currentCatalog, this.connection.getId(), //$NON-NLS-1$
 
1322
                                                                                getId(), 0, System.currentTimeMillis(),
 
1323
                                                                                elapsedTime, mysql
 
1324
                                                                                                .getQueryTimingUnits(), null,
 
1325
                                                                                new Throwable(), mesgBuf.toString()));
1242
1326
                                        }
1243
 
        
1244
 
                                        if (this.connection.getGatherPerformanceMetrics()) {
 
1327
 
 
1328
                                        if (gatherPerformanceMetrics) {
1245
1329
                                                this.connection.registerQueryExecutionTime(elapsedTime);
1246
1330
                                        }
1247
1331
                                }
1248
 
                                
 
1332
 
 
1333
                                this.connection.incrementNumberOfPreparedExecutes();
 
1334
 
 
1335
                                if (this.profileSQL) {
 
1336
                                        this.eventSink = ProfileEventSink
 
1337
                                                        .getInstance(this.connection);
 
1338
 
 
1339
                                        this.eventSink.consumeEvent(new ProfilerEvent(
 
1340
                                                        ProfilerEvent.TYPE_EXECUTE,
 
1341
                                                        "", this.currentCatalog, //$NON-NLS-1$
 
1342
                                                        this.connectionId, this.statementId, -1, System
 
1343
                                                                        .currentTimeMillis(), (int) (mysql
 
1344
                                                                        .getCurrentTimeNanosOrMillis() - begin),
 
1345
                                                        mysql.getQueryTimingUnits(), null, new Throwable(),
 
1346
                                                        truncateQueryToLog(asSql(true))));
 
1347
                                }
 
1348
        
 
1349
                                com.mysql.jdbc.ResultSetInternalMethods rs = mysql.readAllResults(this,
 
1350
                                                maxRowsToRetrieve, this.resultSetType,
 
1351
                                                this.resultSetConcurrency, createStreamingResultSet,
 
1352
                                                this.currentCatalog, resultPacket, true, this.fieldCount,
 
1353
                                                metadataFromCache);
 
1354
                                
 
1355
                                if (this.profileSQL) {
 
1356
                                        long fetchEndTime = mysql.getCurrentTimeNanosOrMillis();
 
1357
 
 
1358
                                        this.eventSink.consumeEvent(new ProfilerEvent(
 
1359
                                                        ProfilerEvent.TYPE_FETCH,
 
1360
                                                        "", this.currentCatalog, this.connection.getId(), //$NON-NLS-1$
 
1361
                                                        getId(), 0 /* FIXME rs.resultId */, System.currentTimeMillis(),
 
1362
                                                        (fetchEndTime - queryEndTime), mysql
 
1363
                                                                        .getQueryTimingUnits(), null,
 
1364
                                                        new Throwable(), null));
 
1365
                                }
 
1366
        
 
1367
                                if (queryWasSlow && this.connection.getExplainSlowQueries()) {
 
1368
                                        String queryAsString = asSql(true);
 
1369
 
 
1370
                                        mysql.explainSlowQuery(queryAsString.getBytes(),
 
1371
                                                        queryAsString);
 
1372
                                }
 
1373
                                
 
1374
                                if (!createStreamingResultSet && 
 
1375
                                                this.serverNeedsResetBeforeEachExecution) {
 
1376
                                        serverResetStatement(); // clear any long data...
 
1377
                                }
 
1378
        
 
1379
                                
 
1380
                                this.sendTypesToServer = false;
 
1381
                                this.results = rs;
 
1382
        
1249
1383
                                if (mysql.hadWarnings()) {
1250
1384
                            mysql.scanForAndThrowDataTruncation();
1251
1385
                        }
1259
1393
                }
1260
1394
        }
1261
1395
 
1262
 
 
1263
1396
        /**
1264
1397
         * Sends stream-type data parameters to the server.
1265
1398
         * 
1378
1511
 
1379
1512
                                this.connection.incrementNumberOfPrepares();
1380
1513
 
1381
 
                                if (this.connection.getProfileSql()) {
1382
 
                                        this.eventSink = ProfileEventSink
1383
 
                                                        .getInstance(this.connection);
1384
 
 
 
1514
                                if (this.profileSQL) {
1385
1515
                                        this.eventSink.consumeEvent(new ProfilerEvent(
1386
1516
                                                        ProfilerEvent.TYPE_PREPARE,
1387
1517
                                                        "", this.currentCatalog, //$NON-NLS-1$
1388
1518
                                                        this.connectionId, this.statementId, -1,
1389
 
                                                        System.currentTimeMillis(), (int) (System
1390
 
                                                                        .currentTimeMillis() - begin), null,
 
1519
                                                        System.currentTimeMillis(), 
 
1520
                                                        mysql.getCurrentTimeNanosOrMillis() - begin, 
 
1521
                                                        mysql.getQueryTimingUnits(), null,
1391
1522
                                                        new Throwable(), truncateQueryToLog(sql)));
1392
1523
                                }
1393
1524
 
1432
1563
                                                        .append("\n\nQuery being prepared when exception was thrown:\n\n");
1433
1564
                                        messageBuf.append(this.originalSql);
1434
1565
 
1435
 
                                        sqlEx = Connection.appendMessageToException(sqlEx,
 
1566
                                        sqlEx = ConnectionImpl.appendMessageToException(sqlEx,
1436
1567
                                                        messageBuf.toString());
1437
1568
                                }
1438
1569
 
1540
1671
                                setType(binding, this.stringTypeCode);
1541
1672
                        }
1542
1673
 
1543
 
                        binding.value = StringUtils.fixDecimalExponent(x.toString());
 
1674
                        binding.value = StringUtils
 
1675
                                .fixDecimalExponent(StringUtils.consistentToString(x));
1544
1676
                        binding.isNull = false;
1545
1677
                        binding.isLongData = false;
1546
1678
                }
1896
2028
         */
1897
2029
        public void setTime(int parameterIndex, java.sql.Time x)
1898
2030
                        throws SQLException {
1899
 
                setTimeInternal(parameterIndex, x, null, TimeZone.getDefault(), false);
 
2031
                setTimeInternal(parameterIndex, x, null, this.connection.getDefaultTimeZone(), false);
1900
2032
        }
1901
2033
 
1902
2034
        /**
1973
2105
         */
1974
2106
        public void setTimestamp(int parameterIndex, java.sql.Timestamp x)
1975
2107
                        throws SQLException {
1976
 
                setTimestampInternal(parameterIndex, x, null, TimeZone.getDefault(), false);
 
2108
                setTimestampInternal(parameterIndex, x, null, this.connection.getDefaultTimeZone(), false);
1977
2109
        }
1978
2110
 
1979
2111
        /**
2023
2155
                }
2024
2156
        }
2025
2157
 
2026
 
        private void setType(BindValue oldValue, int bufferType) {
 
2158
        protected void setType(BindValue oldValue, int bufferType) {
2027
2159
                if (oldValue.bufferType != bufferType) {
2028
2160
                        this.sendTypesToServer = true;
2029
2161
                }
2217
2349
 
2218
2350
                                byte length = (byte) 7;
2219
2351
 
2220
 
                                intoBuf.ensureCapacity(length);
2221
 
 
2222
2352
                                if (dt instanceof java.sql.Timestamp) {
2223
2353
                                        length = (byte) 11;
2224
2354
                                }
2225
2355
 
 
2356
                                intoBuf.ensureCapacity(length);
 
2357
                                
2226
2358
                                intoBuf.writeByte(length); // length
2227
2359
 
2228
2360
                                int year = sessionCalendar.get(Calendar.YEAR);
2247
2379
                                }
2248
2380
 
2249
2381
                                if (length == 11) {
2250
 
                                        intoBuf.writeLong(((java.sql.Timestamp) dt).getNanos());
 
2382
                                        //      MySQL expects microseconds, not nanos
 
2383
                                        intoBuf.writeLong(((java.sql.Timestamp) dt).getNanos() / 1000);
2251
2384
                                }
2252
2385
                        
2253
2386
                        } finally {
2442
2575
        protected long getServerStatementId() {
2443
2576
                return serverStatementId;
2444
2577
        }
 
2578
 
 
2579
 
2445
2580
}