📄 mysqlio.java
字号:
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 = Buffer.allocateNew(packLength, this.useNewIo); } 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()); } } else if (command == MysqlDefs.PROCESS_KILL) { long id = new Long(extraData).longValue(); this.sendPacket.writeLong(id); } send(this.sendPacket); } else { this.packetSequence = -1; send(queryPacket); // 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) { if (this.useNewIo) { this.sendPacket = Buffer.allocateDirect(packLength, this.useNewIo); } else { this.sendPacket = Buffer.allocateNew(packLength, false); } } 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()); } 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()); } } } else { this.sendPacket.writeStringNoNull(query); } queryPacket = this.sendPacket; } byte[] queryBuf = null; int oldPacketPosition = 0; if (this.profileSql || this.logSlowQueries) { queryBuf = queryPacket.getByteBuffer(); // save the packet position oldPacketPosition = queryPacket.getPosition(); queryStartTime = System.currentTimeMillis(); } // 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 = System.currentTimeMillis(); boolean shouldExtractQuery = false; if (this.profileSql) { shouldExtractQuery = true; } else if (this.logSlowQueries && ((queryEndTime - queryStartTime) > this.connection.getSlowQueryThresholdMillis())) { shouldExtractQuery = true; queryWasSlow = true; } if (shouldExtractQuery) { // Extract the actual query from the network packet boolean truncated = false; int extractPosition = oldPacketPosition; if (oldPacketPosition > MAX_QUERY_SIZE_TO_LOG) { extractPosition = MAX_QUERY_SIZE_TO_LOG; truncated = true; } profileQueryToLog = new String(queryBuf, 5, (extractPosition - 5)); if (truncated) { profileQueryToLog += Messages.getString("MysqlIO.25"); //$NON-NLS-1$ } } fetchBeginTime = queryEndTime; } ResultSet rs = readAllResults(callingStatement, maxRows, resultSetType, resultSetConcurrency, streamResults, catalog, resultPacket, false, -1L, unpackFieldInfo); if (queryWasSlow) { StringBuffer mesgBuf = new StringBuffer(48 + profileQueryToLog.length()); mesgBuf.append(Messages.getString("MysqlIO.26")); //$NON-NLS-1$ mesgBuf.append(this.connection.getSlowQueryThresholdMillis()); mesgBuf.append(Messages.getString("MysqlIO.27")); //$NON-NLS-1$ mesgBuf.append(profileQueryToLog); this.connection.getLog().logWarn(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 = System.currentTimeMillis(); 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(), (int) (queryEndTime - queryStartTime), 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(), (int) (fetchEndTime - fetchBeginTime), null, new Throwable(), null)); if (this.queryBadIndexUsed) { eventSink.consumeEvent(new ProfilerEvent( ProfilerEvent.TYPE_WARN, "", catalog, //$NON-NLS-1$ this.connection.getId(), (callingStatement != null) ? callingStatement.getId() : 999, rs.resultId, System.currentTimeMillis(), (int) (queryEndTime - queryStartTime), null, new Throwable(), Messages.getString("MysqlIO.33") //$NON-NLS-1$ +profileQueryToLog)); } if (this.queryNoIndexUsed) { eventSink.consumeEvent(new ProfilerEvent( ProfilerEvent.TYPE_WARN, "", catalog, //$NON-NLS-1$ this.connection.getId(), (callingStatement != null) ? callingStatement.getId() : 999, rs.resultId, System.currentTimeMillis(), (int) (queryEndTime - queryStartTime), null, new Throwable(), Messages.getString("MysqlIO.35") //$NON-NLS-1$ +profileQueryToLog)); } } if (this.hadWarnings) { scanForAndThrowDataTruncation(); } return rs; } /** * Returns the host this IO is connected to * * @return DOCUMENT ME! */ String getHost() { return this.host; } /** * Is the version of the MySQL server we are connected to the given * version? * * @param major the major version * @param minor the minor version * @param subminor the subminor version * * @return true if the version of the MySQL server we are connected is the * given version */ boolean isVersion(int major, int minor, int subminor) { return ((major == getServerMajorVersion()) && (minor == getServerMinorVersion()) && (subminor == getServerSubMinorVersion())); } /** * Does the version of the MySQL server we are connected to meet the given * minimums? * * @param major DOCUMENT ME! * @param minor DOCUMENT ME! * @param subminor DOCUMENT ME! * * @return DOCUMENT ME! */ boolean versionMeetsMinimum(int major, int minor, int subminor) { if (getServerMajorVersion() >= major) { if (getServerMajorVersion() == major) { if (getServerMinorVersion() >= minor) { if (getServerMinorVersion() == minor) { return (getServerSubMinorVersion() >= subminor); } // newer than major.minor return true; } // older than major.minor return false; } // newer than major return true; } return false; } /** * Returns the hex dump of the given packet, truncated to * MAX_PACKET_DUMP_LENGTH if packetLength exceeds that value. * * @param packetToDump the packet to dump in hex * @param packetLength the number of bytes to dump * * @return the hex dump of the given packet */ private final static String getPacketDumpToLog(Buffer packetToDump, int packetLength) { if (packetLength < MAX_PACKET_DUMP_LENGTH) { return packetToDump.dump(packetLength); } StringBuffer packetDumpBuf = new StringBuffer(MAX_PACKET_DUMP_LENGTH * 4); packetDumpBuf.append(packetToDump.dump(MAX_PACKET_DUMP_LENGTH)); packetDumpBuf.append(Messages.getString("MysqlIO.36")); //$NON-NLS-1$ packetDumpBuf.append(MAX_PACKET_DUMP_LENGTH); packetDumpBuf.append(Messages.getString("MysqlIO.37")); //$NON-NLS-1$ return packetDumpBuf.toStrin
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -