📄 mysqlio.java
字号:
maxRows, resultSetType, resultSetConcurrency, streamResults, catalog, fieldPacket, isBinaryEncoded, preSentColumnCount, unpackFieldInfo, metadataFromCache); currentResultSet.setNextResultSet(newResultSet); currentResultSet = newResultSet; moreRowSetsExist = (this.serverStatus & SERVER_MORE_RESULTS_EXISTS) != 0; } if (!streamResults) { clearInputStream(); } reclaimLargeReusablePacket(); return topLevelResultSet; } /** * Sets the buffer size to max-buf */ void resetMaxBuf() { this.maxAllowedPacket = this.connection.getMaxAllowedPacket(); } /** * Send a command to the MySQL server If data is to be sent with command, * it should be put in extraData. * * Raw packets can be sent by setting queryPacket to something other * than null. * * @param command the MySQL protocol 'command' from MysqlDefs * @param extraData any 'string' data for the command * @param queryPacket a packet pre-loaded with data for the protocol (i.e. * from a client-side prepared statement). * @param skipCheck do not call checkErrorPacket() if true * @param extraDataCharEncoding the character encoding of the extraData * parameter. * * @return the response packet from the server * * @throws SQLException if an I/O error or SQL error occurs */ final Buffer sendCommand(int command, String extraData, Buffer queryPacket, boolean skipCheck, String extraDataCharEncoding) throws SQLException { // // We cache these locally, per-command, as the checks // for them are in very 'hot' sections of the I/O code // and we save 10-15% in overall performance by doing this... // this.enablePacketDebug = this.connection.getEnablePacketDebug(); this.traceProtocol = this.connection.getTraceProtocol(); this.readPacketSequence = 0; try { checkForOutstandingStreamingData(); // Clear serverStatus...this value is guarded by an // external mutex, as you can only ever be processing // one command at a time this.serverStatus = 0; this.hadWarnings = false; this.warningCount = 0; this.queryNoIndexUsed = false; this.queryBadIndexUsed = false; // // Compressed input stream needs cleared at beginning // of each command execution... // if (this.useCompression) { int bytesLeft = this.mysqlInput.available(); if (bytesLeft > 0) { this.mysqlInput.skip(bytesLeft); } } try { clearInputStream(); // // PreparedStatements construct their own packets, // for efficiency's sake. // // If this is a generic query, we need to re-use // the sending packet. // if (queryPacket == null) { int packLength = HEADER_LENGTH + COMP_HEADER_LENGTH + 1 + ((extraData != null) ? extraData.length() : 0) + 2; if (this.sendPacket == null) { this.sendPacket = new Buffer(packLength); } this.packetSequence = -1; this.readPacketSequence = 0; this.checkPacketSequence = true; this.sendPacket.clear(); this.sendPacket.writeByte((byte) command); if ((command == MysqlDefs.INIT_DB) || (command == MysqlDefs.CREATE_DB) || (command == MysqlDefs.DROP_DB) || (command == MysqlDefs.QUERY) || (command == MysqlDefs.COM_PREPARE)) { if (extraDataCharEncoding == null) { this.sendPacket.writeStringNoNull(extraData); } else { this.sendPacket.writeStringNoNull(extraData, extraDataCharEncoding, this.connection.getServerCharacterEncoding(), this.connection.parserKnowsUnicode(), this.connection); } } else if (command == MysqlDefs.PROCESS_KILL) { long id = new Long(extraData).longValue(); this.sendPacket.writeLong(id); } send(this.sendPacket, this.sendPacket.getPosition()); } else { this.packetSequence = -1; send(queryPacket, queryPacket.getPosition()); // packet passed by PreparedStatement } } catch (SQLException sqlEx) { // don't wrap SQLExceptions throw sqlEx; } catch (Exception ex) { throw new CommunicationsException(this.connection, this.lastPacketSentTimeMs, ex); } Buffer returnPacket = null; if (!skipCheck) { if ((command == MysqlDefs.COM_EXECUTE) || (command == MysqlDefs.COM_RESET_STMT)) { this.readPacketSequence = 0; this.packetSequenceReset = true; } returnPacket = checkErrorPacket(command); } return returnPacket; } catch (IOException ioEx) { throw new CommunicationsException(this.connection, this.lastPacketSentTimeMs, ioEx); } } /** * Send a query stored in a packet directly to the server. * * @param callingStatement DOCUMENT ME! * @param resultSetConcurrency DOCUMENT ME! * @param characterEncoding DOCUMENT ME! * @param queryPacket DOCUMENT ME! * @param maxRows DOCUMENT ME! * @param conn DOCUMENT ME! * @param resultSetType DOCUMENT ME! * @param resultSetConcurrency DOCUMENT ME! * @param streamResults DOCUMENT ME! * @param catalog DOCUMENT ME! * @param unpackFieldInfo should we read MYSQL_FIELD info (if available)? * * @return DOCUMENT ME! * * @throws Exception DOCUMENT ME! */ final ResultSet sqlQueryDirect(Statement callingStatement, String query, String characterEncoding, Buffer queryPacket, int maxRows, Connection conn, int resultSetType, int resultSetConcurrency, boolean streamResults, String catalog, boolean unpackFieldInfo) throws Exception { long queryStartTime = 0; long queryEndTime = 0; if (query != null) { // We don't know exactly how many bytes we're going to get // from the query. Since we're dealing with Unicode, the // max is 2, so pad it (2 * query) + space for headers int packLength = HEADER_LENGTH + 1 + (query.length() * 2) + 2; if (this.sendPacket == null) { this.sendPacket = new Buffer(packLength); } else { this.sendPacket.clear(); } this.sendPacket.writeByte((byte) MysqlDefs.QUERY); if (characterEncoding != null) { if (this.platformDbCharsetMatches) { this.sendPacket.writeStringNoNull(query, characterEncoding, this.connection.getServerCharacterEncoding(), this.connection.parserKnowsUnicode(), this.connection); } else { if (StringUtils.startsWithIgnoreCaseAndWs(query, "LOAD DATA")) { //$NON-NLS-1$ this.sendPacket.writeBytesNoNull(query.getBytes()); } else { this.sendPacket.writeStringNoNull(query, characterEncoding, this.connection.getServerCharacterEncoding(), this.connection.parserKnowsUnicode(), this.connection); } } } else { this.sendPacket.writeStringNoNull(query); } queryPacket = this.sendPacket; } byte[] queryBuf = null; int oldPacketPosition = 0; if (needToGrabQueryFromPacket) { queryBuf = queryPacket.getByteBuffer(); // save the packet position oldPacketPosition = queryPacket.getPosition(); queryStartTime = getCurrentTimeNanosOrMillis(); } // Send query command and sql query string Buffer resultPacket = sendCommand(MysqlDefs.QUERY, null, queryPacket, false, null); long fetchBeginTime = 0; long fetchEndTime = 0; String profileQueryToLog = null; boolean queryWasSlow = false; if (this.profileSql || this.logSlowQueries) { queryEndTime = getCurrentTimeNanosOrMillis(); boolean shouldExtractQuery = false; if (this.profileSql) { shouldExtractQuery = true; } else if (this.logSlowQueries) { if ((queryEndTime - queryStartTime) > this.slowQueryThreshold) { shouldExtractQuery = true; queryWasSlow = true; } } if (shouldExtractQuery) { // Extract the actual query from the network packet boolean truncated = false; int extractPosition = oldPacketPosition; if (oldPacketPosition > this.connection.getMaxQuerySizeToLog()) { extractPosition = this.connection.getMaxQuerySizeToLog() + 5; truncated = true; } profileQueryToLog = new String(queryBuf, 5, (extractPosition - 5)); if (truncated) { profileQueryToLog += Messages.getString("MysqlIO.25"); //$NON-NLS-1$ } } fetchBeginTime = queryEndTime; } if (this.autoGenerateTestcaseScript) { String testcaseQuery = null; if (query != null) { testcaseQuery = query; } else { testcaseQuery = new String(queryBuf, 5, (oldPacketPosition - 5)); } StringBuffer debugBuf = new StringBuffer(testcaseQuery.length() + 32); this.connection.generateConnectionCommentBlock(debugBuf); debugBuf.append(testcaseQuery); debugBuf.append(';'); this.connection.dumpTestcaseQuery(debugBuf.toString()); } ResultSet rs = readAllResults(callingStatement, maxRows, resultSetType, resultSetConcurrency, streamResults, catalog, resultPacket, false, -1L, unpackFieldInfo, null /* we don't need metadata for cached MD in this case */); if (queryWasSlow) { StringBuffer mesgBuf = new StringBuffer(48 + profileQueryToLog.length()); mesgBuf.append(Messages.getString("MysqlIO.SlowQuery", new Object[] {new Long(this.slowQueryThreshold), queryTimingUnits, new Long(queryEndTime - queryStartTime)})); mesgBuf.append(profileQueryToLog); ProfileEventSink eventSink = ProfileEventSink.getInstance(this.connection); eventSink.consumeEvent(new ProfilerEvent(ProfilerEvent.TYPE_SLOW_QUERY, "", catalog, this.connection.getId(), //$NON-NLS-1$ (callingStatement != null) ? callingStatement.getId() : 999, rs.resultId, System.currentTimeMillis(), (int) (queryEndTime - queryStartTime), queryTimingUnits, null, new Throwable(), mesgBuf.toString())); if (this.connection.getExplainSlowQueries()) { if (oldPacketPosition < MAX_QUERY_SIZE_TO_EXPLAIN) { explainSlowQuery(queryPacket.getBytes(5, (oldPacketPosition - 5)), profileQueryToLog); } else { this.connection.getLog().logWarn(Messages.getString( "MysqlIO.28") //$NON-NLS-1$ +MAX_QUERY_SIZE_TO_EXPLAIN + Messages.getString("MysqlIO.29")); //$NON-NLS-1$ } } } if (this.profileSql) { fetchEndTime = getCurrentTimeNanosOrMillis(); ProfileEventSink eventSink = ProfileEventSink.getInstance(this.connection); eventSink.consumeEvent(new ProfilerEvent(ProfilerEvent.TYPE_QUERY, "", catalog, this.connection.getId(), //$NON-NLS-1$ (callingStatement != null) ? callingStatement.getId() : 999, rs.resultId, System.currentTimeMillis(), (queryEndTime - queryStartTime), this.queryTimingUnits, null, new Throwable(), profileQueryToLog)); eventSink.consumeEvent(new ProfilerEvent(ProfilerEvent.TYPE_FETCH, "", catalog, this.connection.getId(), //$NON-NLS-1$ (callingStatement != null) ? callingStatement.getId() : 999, rs.resultId, System.currentTimeMillis(), (fetchEndTime - fetchBeginTime), this.queryTimingUnits, null, new Throwable(), null)); if (this.queryBadIndexUsed) { eventSink.consumeEvent(new ProfilerEvent( ProfilerEvent.TYPE_SLOW_QUERY, "", catalog, //$NON-NLS-1$ this.connection.getId(), (callingStatement != null) ? callingStatement.getId()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -