[#1157] Add SQL / JDBC tracing capabilities in addition to logging - Fixed batchSQL contents

This commit is contained in:
Lukas Eder 2012-02-24 07:35:39 +00:00
parent 0de72e5b47
commit f736280b86
5 changed files with 229 additions and 41 deletions

View File

@ -0,0 +1,60 @@
/**
* Copyright (c) 2009-2012, Lukas Eder, lukas.eder@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.test._;
import java.util.HashMap;
import java.util.Map;
import org.jooq.ExecuteContext;
import org.jooq.ExecuteType;
import org.jooq.impl.DefaultExecuteListener;
public class TestStatisticsListener extends DefaultExecuteListener {
public static Map<ExecuteType, Integer> STATISTICS = new HashMap<ExecuteType, Integer>();
@Override
public void start(ExecuteContext ctx) {
Integer count = STATISTICS.get(ctx.type());
if (count == null) {
count = 0;
}
STATISTICS.put(ctx.type(), count + 1);
}
}

View File

@ -0,0 +1,73 @@
/**
* Copyright (c) 2009-2012, Lukas Eder, lukas.eder@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.test._.testcases;
import org.jooq.TableRecord;
import org.jooq.UpdatableRecord;
import org.jooq.test.BaseTest;
import org.jooq.test.jOOQAbstractTest;
import org.junit.Test;
public class ExecuteListenerTests<
A extends UpdatableRecord<A>,
B extends UpdatableRecord<B>,
S extends UpdatableRecord<S>,
B2S extends UpdatableRecord<B2S>,
BS extends UpdatableRecord<BS>,
L extends TableRecord<L>,
X extends TableRecord<X>,
DATE extends UpdatableRecord<DATE>,
D extends UpdatableRecord<D>,
T extends UpdatableRecord<T>,
U extends TableRecord<U>,
I extends TableRecord<I>,
IPK extends UpdatableRecord<IPK>,
T658 extends TableRecord<T658>,
T725 extends UpdatableRecord<T725>,
T639 extends UpdatableRecord<T639>,
T785 extends TableRecord<T785>>
extends BaseTest<A, B, S, B2S, BS, L, X, DATE, D, T, U, I, IPK, T658, T725, T639, T785> {
public ExecuteListenerTests(jOOQAbstractTest<A, B, S, B2S, BS, L, X, DATE, D, T, U, I, IPK, T658, T725, T639, T785> delegate) {
super(delegate);
}
@Test
public void testExecuteListener() throws Exception {
}
}

View File

@ -55,6 +55,7 @@ import java.util.Properties;
import org.jooq.ArrayRecord;
import org.jooq.DataType;
import org.jooq.ExecuteType;
import org.jooq.Field;
import org.jooq.QueryPart;
import org.jooq.QueryPartInternal;
@ -72,10 +73,12 @@ import org.jooq.conf.Settings;
import org.jooq.debugger.SqlQueryDebuggerExecuteListener;
import org.jooq.debugger.console.remote.SqlRemoteQueryDebuggerServer;
import org.jooq.impl.Factory;
import org.jooq.test._.TestStatisticsListener;
import org.jooq.test._.testcases.AggregateWindowFunctionTests;
import org.jooq.test._.testcases.CRUDTests;
import org.jooq.test._.testcases.DataTypeTests;
import org.jooq.test._.testcases.EnumTests;
import org.jooq.test._.testcases.ExecuteListenerTests;
import org.jooq.test._.testcases.ExoticTests;
import org.jooq.test._.testcases.FetchTests;
import org.jooq.test._.testcases.FormatTests;
@ -198,6 +201,7 @@ public abstract class jOOQAbstractTest<
public static Map<String, String> scripts = new HashMap<String, String>();
private static SqlRemoteQueryDebuggerServer SERVER;
private static TestStatisticsListener STATISTICS;
protected void execute(String script) throws Exception {
Statement stmt = null;
@ -374,6 +378,13 @@ public abstract class jOOQAbstractTest<
}
connection.close();
log.info("TEST STATISTICS");
log.info("---------------");
for (ExecuteType type : ExecuteType.values()) {
log.info(type.name(), TestStatisticsListener.STATISTICS.get(type) + " executions");
}
}
public final Connection getConnection() {
@ -616,7 +627,10 @@ public abstract class jOOQAbstractTest<
}
protected final Factory create() {
Settings settings = new Settings().withExecuteListeners(SqlQueryDebuggerExecuteListener.class.getName());
Settings settings = new Settings().withExecuteListeners(
TestStatisticsListener.class.getName(),
SqlQueryDebuggerExecuteListener.class.getName());
return create(settings);
}
@ -1323,6 +1337,11 @@ public abstract class jOOQAbstractTest<
new ExoticTests(this).testPivotClause();
}
@Test
public void testExecuteListener() throws Exception {
new ExecuteListenerTests(this).testExecuteListener();
}
@Test
public void testLoader() throws Exception {
new LoaderTests(this).testLoader();

View File

@ -191,44 +191,4 @@ public interface ExecuteContext extends Configuration {
* Calling this has no effect. It is being used by jOOQ internally.
*/
void result(Result<?> result);
/**
* The type of database interaction that is being executed with this
* context.
*/
enum ExecuteType {
/**
* A <code>SELECT</code> query is being executed
*/
READ,
/**
* An <code>INSERT</code>, <code>UPDATE</code>, <code>DELETE</code>,
* <code>MERGE</code> query is being executed
*/
WRITE,
/**
* A routine (stored procedure or function) is being executed
*/
ROUTINE,
/**
* A batch statement is being executed (not yet supported)
*/
BATCH,
/**
* A DDL statement is being executed
* <p>
* Currently, this only applies to <code>TRUNCATE</code> statements
*/
DDL,
/**
* An other (unknown) type of database interaction is being executed
*/
OTHER,
}
}

View File

@ -0,0 +1,76 @@
/**
* Copyright (c) 2009-2012, Lukas Eder, lukas.eder@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;
/**
* The type of database interaction that is being executed with this
* context.
*/
public enum ExecuteType {
/**
* A <code>SELECT</code> query is being executed
*/
READ,
/**
* An <code>INSERT</code>, <code>UPDATE</code>, <code>DELETE</code>,
* <code>MERGE</code> query is being executed
*/
WRITE,
/**
* A routine (stored procedure or function) is being executed
*/
ROUTINE,
/**
* A batch statement is being executed (not yet supported)
*/
BATCH,
/**
* A DDL statement is being executed
* <p>
* Currently, this only applies to <code>TRUNCATE</code> statements
*/
DDL,
/**
* An other (unknown) type of database interaction is being executed
*/
OTHER,
}