[#4552] Add DSLContext.connection(ConnectionRunnable)
This commit is contained in:
parent
f39fa00c77
commit
be6f0be93f
79
jOOQ/src/main/java/org/jooq/ConnectionCallable.java
Normal file
79
jOOQ/src/main/java/org/jooq/ConnectionCallable.java
Normal file
@ -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<T> {
|
||||
|
||||
/**
|
||||
* Run statements.
|
||||
* <p>
|
||||
* 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 <code>connection</code>,
|
||||
* 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 <code>connection</code> 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;
|
||||
}
|
||||
78
jOOQ/src/main/java/org/jooq/ConnectionRunnable.java
Normal file
78
jOOQ/src/main/java/org/jooq/ConnectionRunnable.java
Normal file
@ -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.
|
||||
* <p>
|
||||
* 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 <code>connection</code>,
|
||||
* 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 <code>connection</code> 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;
|
||||
}
|
||||
@ -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
|
||||
* <code>DSLContext</code>'s underlying {@link #configuration()}'s
|
||||
* {@link Configuration#connectionProvider()}.
|
||||
*
|
||||
* @param runnable The code running statements against the
|
||||
* <code>connection</code>.
|
||||
* @return The outcome of the callable
|
||||
*/
|
||||
<T> T connectionResult(ConnectionCallable<T> callable);
|
||||
|
||||
/**
|
||||
* Run a {@link ConnectionRunnable} in the context of this
|
||||
* <code>DSLContext</code>'s underlying {@link #configuration()}'s
|
||||
* {@link Configuration#connectionProvider()}.
|
||||
*
|
||||
* @param runnable The code running statements against the
|
||||
* <code>connection</code>.
|
||||
*/
|
||||
void connection(ConnectionRunnable runnable);
|
||||
|
||||
/**
|
||||
* Run a {@link MockRunnable} in the context of this <code>DSLContext</code>
|
||||
* 's underlying {@link #configuration()}'s, and of a
|
||||
|
||||
@ -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> T connectionResult(ConnectionCallable<T> 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<Void>() {
|
||||
@Override
|
||||
public Void run(Connection connection) throws Exception {
|
||||
runnable.run(connection);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T mockResult(MockDataProvider provider, MockCallable<T> mockable) {
|
||||
try {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user