From f736280b86f123d9acc29e2f478cf120b7c5ed3a Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Fri, 24 Feb 2012 07:35:39 +0000 Subject: [PATCH] [#1157] Add SQL / JDBC tracing capabilities in addition to logging - Fixed batchSQL contents --- .../jooq/test/_/TestStatisticsListener.java | 60 +++++++++++++++ .../_/testcases/ExecuteListenerTests.java | 73 ++++++++++++++++++ .../src/org/jooq/test/jOOQAbstractTest.java | 21 ++++- .../main/java/org/jooq/ExecuteContext.java | 40 ---------- jOOQ/src/main/java/org/jooq/ExecuteType.java | 76 +++++++++++++++++++ 5 files changed, 229 insertions(+), 41 deletions(-) create mode 100644 jOOQ-test/src/org/jooq/test/_/TestStatisticsListener.java create mode 100644 jOOQ-test/src/org/jooq/test/_/testcases/ExecuteListenerTests.java create mode 100644 jOOQ/src/main/java/org/jooq/ExecuteType.java diff --git a/jOOQ-test/src/org/jooq/test/_/TestStatisticsListener.java b/jOOQ-test/src/org/jooq/test/_/TestStatisticsListener.java new file mode 100644 index 0000000000..37d5b87344 --- /dev/null +++ b/jOOQ-test/src/org/jooq/test/_/TestStatisticsListener.java @@ -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 STATISTICS = new HashMap(); + + @Override + public void start(ExecuteContext ctx) { + Integer count = STATISTICS.get(ctx.type()); + + if (count == null) { + count = 0; + } + + STATISTICS.put(ctx.type(), count + 1); + } + +} diff --git a/jOOQ-test/src/org/jooq/test/_/testcases/ExecuteListenerTests.java b/jOOQ-test/src/org/jooq/test/_/testcases/ExecuteListenerTests.java new file mode 100644 index 0000000000..7a1cd10c6c --- /dev/null +++ b/jOOQ-test/src/org/jooq/test/_/testcases/ExecuteListenerTests.java @@ -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, + B extends UpdatableRecord, + S extends UpdatableRecord, + B2S extends UpdatableRecord, + BS extends UpdatableRecord, + L extends TableRecord, + X extends TableRecord, + DATE extends UpdatableRecord, + D extends UpdatableRecord, + T extends UpdatableRecord, + U extends TableRecord, + I extends TableRecord, + IPK extends UpdatableRecord, + T658 extends TableRecord, + T725 extends UpdatableRecord, + T639 extends UpdatableRecord, + T785 extends TableRecord> +extends BaseTest { + + public ExecuteListenerTests(jOOQAbstractTest delegate) { + super(delegate); + } + + @Test + public void testExecuteListener() throws Exception { + + } +} diff --git a/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java b/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java index 6a37004e66..2e38386966 100644 --- a/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java +++ b/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java @@ -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 scripts = new HashMap(); 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(); diff --git a/jOOQ/src/main/java/org/jooq/ExecuteContext.java b/jOOQ/src/main/java/org/jooq/ExecuteContext.java index bd8653e59e..a6a508b521 100644 --- a/jOOQ/src/main/java/org/jooq/ExecuteContext.java +++ b/jOOQ/src/main/java/org/jooq/ExecuteContext.java @@ -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 SELECT query is being executed - */ - READ, - - /** - * An INSERT, UPDATE, DELETE, - * MERGE 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 - *

- * Currently, this only applies to TRUNCATE statements - */ - DDL, - - /** - * An other (unknown) type of database interaction is being executed - */ - OTHER, - } } diff --git a/jOOQ/src/main/java/org/jooq/ExecuteType.java b/jOOQ/src/main/java/org/jooq/ExecuteType.java new file mode 100644 index 0000000000..cab4a38b9d --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/ExecuteType.java @@ -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 SELECT query is being executed + */ + READ, + + /** + * An INSERT, UPDATE, DELETE, + * MERGE 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 + *

+ * Currently, this only applies to TRUNCATE statements + */ + DDL, + + /** + * An other (unknown) type of database interaction is being executed + */ + OTHER, +} \ No newline at end of file