[#1472] Move org.jooq.tools.debug API from jOOQ Console to core - Added

QueryOrigin property to QueryLog
This commit is contained in:
Lukas Eder 2012-09-16 23:37:41 +02:00
parent 6cd084e77f
commit 337b158648
4 changed files with 127 additions and 24 deletions

View File

@ -148,6 +148,8 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
Matcher d1m1 = d1.newMatcher();
Matcher d2m1 = d2.newMatcher();
d1m1.newLogger().listener(new LListener(null));
Processor d1m1p1 = d1m1.newProcessor();
Processor d2m1p1 = d2m1.newProcessor();

View File

@ -61,15 +61,17 @@ public final class QueryLog implements Serializable {
private final StackTraceElement[] stackTrace;
private final Query query;
private final QueryOrigin origin;
private final long prepareTime;
private final long bindTime;
private final long executeTime;
public QueryLog(Query query, long prepareTime, long bindTime, long executeTime) {
public QueryLog(Query query, QueryOrigin origin, long prepareTime, long bindTime, long executeTime) {
this.id = NEXT_ID.getAndIncrement();
this.stackTrace = Thread.currentThread().getStackTrace();
this.query = query;
this.origin = origin;
this.prepareTime = prepareTime;
this.bindTime = bindTime;
this.executeTime = executeTime;
@ -79,6 +81,10 @@ public final class QueryLog implements Serializable {
return query;
}
public final QueryOrigin getOrigin() {
return origin;
}
public final int getId() {
return id;
}
@ -111,7 +117,9 @@ public final class QueryLog implements Serializable {
sb.append(StopWatch.format(executeTime));
sb.append(", query=[");
sb.append(query);
sb.append("]]");
sb.append(", origin=");
sb.append(origin);
sb.append("]");
return sb.toString();
}

View File

@ -0,0 +1,74 @@
/**
* Copyright (c) 2009-2012, Lukas Eder, lukas.eder@gmail.com
* Christopher Deckers, chrriis@gmail.com
* All rights reserved.
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOQ" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.jooq.tools.debug;
/**
* The query origin indicates what entity generated a query that winds up in the
* {@link QueryLog}
*
* @author Christopher Deckers
* @author Lukas Eder
*/
public enum QueryOrigin {
/**
* A regular, application-originating query.
*/
APPLICATION,
/**
* A {@link Processor#before()}-originating query.
*/
PROCESSOR_BEFORE,
/**
* A {@link Processor#after()}-originating query.
*/
PROCESSOR_AFTER,
/**
* A {@link Breakpoint}-originating query from
* {@link BreakpointListener#before(HitContext)}
*/
BREAKPOINT_BEFORE,
/**
* A {@link Breakpoint}-originating query from
* {@link BreakpointListener#after(HitContext)}
*/
BREAKPOINT_AFTER,
}

View File

@ -38,6 +38,11 @@ package org.jooq.tools.debug.impl;
import static org.jooq.conf.ExecuteDebugging.SERVER;
import static org.jooq.tools.StringUtils.defaultIfNull;
import static org.jooq.tools.debug.QueryOrigin.APPLICATION;
import static org.jooq.tools.debug.QueryOrigin.BREAKPOINT_AFTER;
import static org.jooq.tools.debug.QueryOrigin.BREAKPOINT_BEFORE;
import static org.jooq.tools.debug.QueryOrigin.PROCESSOR_AFTER;
import static org.jooq.tools.debug.QueryOrigin.PROCESSOR_BEFORE;
import static org.jooq.tools.debug.Step.STEP;
import java.util.ArrayList;
@ -62,6 +67,7 @@ import org.jooq.tools.debug.Matcher;
import org.jooq.tools.debug.Processor;
import org.jooq.tools.debug.QueryExecutor;
import org.jooq.tools.debug.QueryLog;
import org.jooq.tools.debug.QueryOrigin;
import org.jooq.tools.debug.ResultLog;
import org.jooq.tools.debug.Step;
import org.jooq.tools.debug.impl.LocalDebugger.DebuggerRegistry;
@ -74,25 +80,25 @@ import org.jooq.tools.debug.impl.LocalDebugger.DebuggerRegistry;
*/
public class DebugListener extends DefaultExecuteListener {
private static final ThreadLocal<Object> RECURSION_LOCK = new ThreadLocal<Object>();
static final ThreadLocal<QueryExecutor> BREAKPOINT_EXECUTORS = new ThreadLocal<QueryExecutor>();
private static final ThreadLocal<QueryOrigin> RECURSION_LOCK = new ThreadLocal<QueryOrigin>();
static final ThreadLocal<QueryExecutor> BREAKPOINT_EXECUTORS = new ThreadLocal<QueryExecutor>();
private boolean hasDebuggers;
private boolean hasDebuggers;
private long startPrepareTime;
private long endPrepareTime;
private long startPrepareTime;
private long endPrepareTime;
private long startBindTime;
private long endBindTime;
private long startBindTime;
private long endBindTime;
private long startExecuteTime;
private long endExecuteTime;
private long startExecuteTime;
private long endExecuteTime;
private long startFetchTime;
private long endFetchTime;
private long startFetchTime;
private long endFetchTime;
private List<Matcher> matchers;
private QueryLog log;
private List<Matcher> matchers;
private QueryLog log;
@Override
public void start(ExecuteContext ctx) {
@ -111,8 +117,8 @@ public class DebugListener extends DefaultExecuteListener {
startExecuteTime = 0;
endExecuteTime = 0;
// Avoid recursion for processors and breakpoints.
recursionSafe(new Runnable() {
// Avoid recursion for processors.
recursionSafe(PROCESSOR_BEFORE, new Runnable() {
@Override
public void run() {
Factory create = create(ctx);
@ -126,11 +132,15 @@ public class DebugListener extends DefaultExecuteListener {
}
}
}
}
});
// Process breakpoints
// Avoid recursion for breakpoints.
recursionSafe(BREAKPOINT_BEFORE, new Runnable() {
@Override
public void run() {
breakpoints(true, ctx);
}
});
}
@ -182,8 +192,8 @@ public class DebugListener extends DefaultExecuteListener {
}
}
// Avoid recursion for processors and breakpoints.
recursionSafe(new Runnable() {
// Avoid recursion for processors.
recursionSafe(PROCESSOR_AFTER, new Runnable() {
@Override
public void run() {
Factory create = create(ctx);
@ -196,8 +206,14 @@ public class DebugListener extends DefaultExecuteListener {
}
}
}
}
});
// Process breakpoints
// Avoid recursion for breakpoints.
recursionSafe(BREAKPOINT_AFTER, new Runnable() {
@Override
public void run() {
breakpoints(false, ctx);
}
});
@ -298,9 +314,9 @@ public class DebugListener extends DefaultExecuteListener {
}
}
private void recursionSafe(Runnable runnable) {
private void recursionSafe(QueryOrigin origin, Runnable runnable) {
if (RECURSION_LOCK.get() == null) {
RECURSION_LOCK.set(new Object());
RECURSION_LOCK.set(origin);
try {
runnable.run();
@ -328,9 +344,12 @@ public class DebugListener extends DefaultExecuteListener {
}
private QueryLog getLog(ExecuteContext ctx) {
QueryOrigin origin = defaultIfNull(RECURSION_LOCK.get(), APPLICATION);
if (log == null) {
log = new QueryLog(
ctx.query(),
origin,
endPrepareTime - startPrepareTime,
endBindTime - startBindTime,
endExecuteTime - startExecuteTime