From be6f0be93fc4a6d8ef38245b8a55cc97820b2276 Mon Sep 17 00:00:00 2001 From: lukaseder Date: Tue, 15 Sep 2015 08:46:34 +0200 Subject: [PATCH] [#4552] Add DSLContext.connection(ConnectionRunnable) --- .../java/org/jooq/ConnectionCallable.java | 79 +++++++++++++++++++ .../java/org/jooq/ConnectionRunnable.java | 78 ++++++++++++++++++ jOOQ/src/main/java/org/jooq/DSLContext.java | 21 +++++ .../java/org/jooq/impl/DefaultDSLContext.java | 28 +++++++ 4 files changed, 206 insertions(+) create mode 100644 jOOQ/src/main/java/org/jooq/ConnectionCallable.java create mode 100644 jOOQ/src/main/java/org/jooq/ConnectionRunnable.java diff --git a/jOOQ/src/main/java/org/jooq/ConnectionCallable.java b/jOOQ/src/main/java/org/jooq/ConnectionCallable.java new file mode 100644 index 0000000000..116d25e0cf --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/ConnectionCallable.java @@ -0,0 +1,79 @@ +/** + * Copyright (c) 2009-2015, Data Geekery GmbH (http://www.datageekery.com) + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import org.jooq.exception.DataAccessException; + +/** + * An operation that can run code and statements against a provided JDBC + * {@link Connection}. + * + * @author Lukas Eder + */ +/* [java-8] */ +@FunctionalInterface +/* [/java-8] */ +public interface ConnectionCallable { + + /** + * Run statements. + *

+ * Implementations may freely use the argument {@link Connection} to run + * statements against the database. Implementations MUST manage the + * lifecycle of any resources created from this connection, + * such as {@link Statement} or {@link ResultSet}. Implementations MUST NOT + * manage the lifecycle of the {@link Connection}, which is managed by the + * {@link ConnectionProvider} that provided the connection to + * this {@link ConnectionCallable}. + * + * @param connection The connection. + * @return The outcome of the callable. + * @throws Exception Any exception, including {@link SQLException}, that + * will be propagated as an unchecked + * {@link DataAccessException}. + */ + T run(Connection connection) throws Exception; +} diff --git a/jOOQ/src/main/java/org/jooq/ConnectionRunnable.java b/jOOQ/src/main/java/org/jooq/ConnectionRunnable.java new file mode 100644 index 0000000000..7a73f694f1 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/ConnectionRunnable.java @@ -0,0 +1,78 @@ +/** + * Copyright (c) 2009-2015, Data Geekery GmbH (http://www.datageekery.com) + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import org.jooq.exception.DataAccessException; + +/** + * An operation that can run code and statements against a provided JDBC + * {@link Connection}. + * + * @author Lukas Eder + */ +/* [java-8] */ +@FunctionalInterface +/* [/java-8] */ +public interface ConnectionRunnable { + + /** + * Run statements. + *

+ * Implementations may freely use the argument {@link Connection} to run + * statements against the database. Implementations MUST manage the + * lifecycle of any resources created from this connection, + * such as {@link Statement} or {@link ResultSet}. Implementations MUST NOT + * manage the lifecycle of the {@link Connection}, which is managed by the + * {@link ConnectionProvider} that provided the connection to + * this {@link ConnectionRunnable}. + * + * @param connection The connection. + * @throws Exception Any exception, including {@link SQLException}, that + * will be propagated as an unchecked + * {@link DataAccessException}. + */ + void run(Connection connection) throws Exception; +} diff --git a/jOOQ/src/main/java/org/jooq/DSLContext.java b/jOOQ/src/main/java/org/jooq/DSLContext.java index c0042656bd..d7f7395994 100644 --- a/jOOQ/src/main/java/org/jooq/DSLContext.java +++ b/jOOQ/src/main/java/org/jooq/DSLContext.java @@ -212,6 +212,27 @@ public interface DSLContext extends Scope /* [java-8] */, AutoCloseable /* [/jav */ void transaction(TransactionalRunnable transactional); + /** + * Run a {@link ConnectionCallable} in the context of this + * DSLContext's underlying {@link #configuration()}'s + * {@link Configuration#connectionProvider()}. + * + * @param runnable The code running statements against the + * connection. + * @return The outcome of the callable + */ + T connectionResult(ConnectionCallable callable); + + /** + * Run a {@link ConnectionRunnable} in the context of this + * DSLContext's underlying {@link #configuration()}'s + * {@link Configuration#connectionProvider()}. + * + * @param runnable The code running statements against the + * connection. + */ + void connection(ConnectionRunnable runnable); + /** * Run a {@link MockRunnable} in the context of this DSLContext * 's underlying {@link #configuration()}'s, and of a diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java index ea4dd4d742..11d2f92bb0 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java @@ -84,7 +84,9 @@ import org.jooq.BindContext; import org.jooq.CommonTableExpression; import org.jooq.Condition; import org.jooq.Configuration; +import org.jooq.ConnectionCallable; import org.jooq.ConnectionProvider; +import org.jooq.ConnectionRunnable; import org.jooq.CreateIndexStep; import org.jooq.CreateSequenceFinalStep; import org.jooq.CreateTableAsStep; @@ -372,6 +374,32 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri }); } + @Override + public T connectionResult(ConnectionCallable callable) { + final Connection connection = configuration().connectionProvider().acquire(); + + try { + return callable.run(connection); + } + catch (Exception e) { + throw new DataAccessException("Error while running ConnectionCallable", e); + } + finally { + configuration().connectionProvider().release(connection); + } + } + + @Override + public void connection(ConnectionRunnable runnable) { + connectionResult(new ConnectionCallable() { + @Override + public Void run(Connection connection) throws Exception { + runnable.run(connection); + return null; + } + }); + } + @Override public T mockResult(MockDataProvider provider, MockCallable mockable) { try {