in src/main/protocol-impl/java/com/mysql/cj/protocol/a/NativeProtocol.java [911:1030]
public final <T extends Resultset> T sendQueryPacket(Query callingQuery, NativePacketPayload queryPacket, int maxRows, boolean streamResults,
ColumnDefinition cachedMetadata, ProtocolEntityFactory<T, NativePacketPayload> resultSetFactory) throws IOException {
final long queryStartTime = getCurrentTimeNanosOrMillis();
this.statementExecutionDepth++;
byte[] queryBuf = queryPacket.getByteBuffer();
int oldPacketPosition = queryPacket.getPosition(); // save the packet position
int queryPosition = queryPacket.getTag("QUERY");
LazyString query = new LazyString(queryBuf, queryPosition, oldPacketPosition - queryPosition);
try {
if (this.queryInterceptors != null) {
T interceptedResults = invokeQueryInterceptorsPre(query, callingQuery, false);
if (interceptedResults != null) {
return interceptedResults;
}
}
if (this.autoGenerateTestcaseScript) {
StringBuilder debugBuf = new StringBuilder(query.length() + 32);
generateQueryCommentBlock(debugBuf);
debugBuf.append(query);
debugBuf.append(';');
TestUtils.dumpTestcaseQuery(debugBuf.toString());
}
// Send query command and sql query string
NativePacketPayload resultPacket = sendCommand(queryPacket, false, 0);
final long queryEndTime = getCurrentTimeNanosOrMillis();
final long queryDuration = queryEndTime - queryStartTime;
if (callingQuery != null) {
callingQuery.setExecuteTime(queryDuration);
}
boolean queryWasSlow = this.logSlowQueries && (this.useAutoSlowLog ? this.metricsHolder.checkAbonormallyLongQuery(queryDuration)
: queryDuration > this.propertySet.getIntegerProperty(PropertyKey.slowQueryThresholdMillis).getValue());
long fetchBeginTime = this.profileSQL ? getCurrentTimeNanosOrMillis() : 0L;
T rs = readAllResults(maxRows, streamResults, resultPacket, false, cachedMetadata, resultSetFactory);
if (this.profileSQL || queryWasSlow) {
long fetchEndTime = this.profileSQL ? getCurrentTimeNanosOrMillis() : 0L;
// Extract the actual query from the network packet
boolean truncated = oldPacketPosition - queryPosition > this.maxQuerySizeToLog.getValue();
int extractPosition = truncated ? this.maxQuerySizeToLog.getValue() + queryPosition : oldPacketPosition;
String extractedQuery = StringUtils.toString(queryBuf, queryPosition, extractPosition - queryPosition);
if (truncated) {
extractedQuery += Messages.getString("Protocol.2");
}
ProfilerEventHandler eventSink = this.session.getProfilerEventHandler();
if (this.logSlowQueries) {
if (queryWasSlow) {
eventSink.processEvent(ProfilerEvent.TYPE_SLOW_QUERY, this.session, callingQuery, rs, queryDuration, new Throwable(),
Messages.getString("Protocol.SlowQuery",
new Object[] { this.useAutoSlowLog ? " 95% of all queries " : String.valueOf(this.slowQueryThreshold),
this.queryTimingUnits, Long.valueOf(queryDuration), extractedQuery }));
if (this.propertySet.getBooleanProperty(PropertyKey.explainSlowQueries).getValue()) {
if (oldPacketPosition - queryPosition < MAX_QUERY_SIZE_TO_EXPLAIN) {
queryPacket.setPosition(queryPosition); // skip until the query is located in the packet
explainSlowQuery(query.toString(), extractedQuery);
} else {
this.log.logWarn(Messages.getString("Protocol.3", new Object[] { MAX_QUERY_SIZE_TO_EXPLAIN }));
}
}
}
if (this.serverSession.noGoodIndexUsed()) {
eventSink.processEvent(ProfilerEvent.TYPE_SLOW_QUERY, this.session, callingQuery, rs, queryDuration, new Throwable(),
Messages.getString("Protocol.4") + extractedQuery);
}
if (this.serverSession.noIndexUsed()) {
eventSink.processEvent(ProfilerEvent.TYPE_SLOW_QUERY, this.session, callingQuery, rs, queryDuration, new Throwable(),
Messages.getString("Protocol.5") + extractedQuery);
}
if (this.serverSession.queryWasSlow()) {
eventSink.processEvent(ProfilerEvent.TYPE_SLOW_QUERY, this.session, callingQuery, rs, queryDuration, new Throwable(),
Messages.getString("Protocol.ServerSlowQuery") + extractedQuery);
}
}
if (this.profileSQL) {
eventSink.processEvent(ProfilerEvent.TYPE_QUERY, this.session, callingQuery, rs, queryDuration, new Throwable(), extractedQuery);
eventSink.processEvent(ProfilerEvent.TYPE_FETCH, this.session, callingQuery, rs, fetchEndTime - fetchBeginTime, new Throwable(), null);
}
}
if (this.hadWarnings) {
scanForAndThrowDataTruncation();
}
if (this.queryInterceptors != null) {
rs = invokeQueryInterceptorsPost(query, callingQuery, rs, false);
}
return rs;
} catch (CJException sqlEx) {
if (this.queryInterceptors != null) {
// TODO why doing this?
invokeQueryInterceptorsPost(query, callingQuery, null, false); // we don't do anything with the result set in this case
}
if (callingQuery != null) {
callingQuery.checkCancelTimeout();
}
throw sqlEx;
} finally {
this.statementExecutionDepth--;
}
}