[#7097] Expose jOOQ Connection proxies also as DataSource

This commit is contained in:
lukaseder 2018-01-25 12:25:58 +01:00
parent bf0dcea87b
commit c8812d0bc3
5 changed files with 247 additions and 0 deletions

View File

@ -80,6 +80,7 @@ import java.util.function.Function;
import java.util.stream.Stream;
import javax.annotation.Generated;
import javax.sql.DataSource;
import org.jooq.conf.ParamType;
import org.jooq.conf.Settings;
@ -216,6 +217,14 @@ public interface DSLContext extends Scope , AutoCloseable {
*/
Connection parsingConnection();
/**
* A JDBC data source that runs each statement through the {@link #parser()}
* first, prior to re-generating and running the SQL.
* <p>
* This simply wraps the {@link #parsingConnection()} in a {@link DataSource}.
*/
DataSource parsingDataSource();
/**
* A JDBC connection that proxies the underlying connection to run the jOOQ
* Diagnostics Pack on executed queries.
@ -224,6 +233,14 @@ public interface DSLContext extends Scope , AutoCloseable {
*/
Connection diagnosticsConnection();
/**
* A JDBC connection that proxies the underlying connection to run the jOOQ
* Diagnostics Pack on executed queries.
* <p>
* This simply wraps the {@link #diagnosticsConnection()} in a {@link DataSource}.
*/
DataSource diagnosticsDataSource();
/**
* Access the database meta data.
* <p>

View File

@ -0,0 +1,90 @@
/*
* 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.impl;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;
import javax.sql.DataSource;
/**
* @author Lukas Eder
*/
abstract class AbstractDataSource implements DataSource {
@Override
public final PrintWriter getLogWriter() throws SQLException {
throw new UnsupportedOperationException("getLogWriter");
}
@Override
public final void setLogWriter(PrintWriter out) throws SQLException {
throw new UnsupportedOperationException("setLogWriter");
}
@Override
public final void setLoginTimeout(int seconds) throws SQLException {
throw new UnsupportedOperationException("setLoginTimeout");
}
@Override
public final int getLoginTimeout() throws SQLException {
return 0;
}
@Override
public final Logger getParentLogger() throws SQLFeatureNotSupportedException {
return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
}
@SuppressWarnings("unchecked")
@Override
public final <T> T unwrap(Class<T> iface) throws SQLException {
if (iface.isInstance(this)) {
return (T) this;
}
throw new SQLException("DataSource of type [" + getClass().getName() + "] cannot be unwrapped as [" + iface.getName() + "]");
}
@Override
public final boolean isWrapperFor(Class<?> iface) throws SQLException {
return iface.isInstance(this);
}
}

View File

@ -381,11 +381,21 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
return new ParsingConnection(configuration());
}
@Override
public DataSource parsingDataSource() {
return new ParsingDataSource(configuration());
}
@Override
public Connection diagnosticsConnection() {
return new DiagnosticsConnection(configuration());
}
@Override
public DataSource diagnosticsDataSource() {
return new DiagnosticsDataSource(configuration());
}
@Override
public Meta meta() {
return new MetaImpl(configuration());

View File

@ -0,0 +1,65 @@
/*
* 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.impl;
import java.sql.Connection;
import java.sql.SQLException;
import org.jooq.Configuration;
/**
* @author Lukas Eder
*/
final class DiagnosticsDataSource extends AbstractDataSource {
private final Configuration configuration;
DiagnosticsDataSource(Configuration configuration) {
this.configuration = configuration;
}
@Override
public final Connection getConnection() throws SQLException {
return new DiagnosticsConnection(configuration);
}
@Override
public final Connection getConnection(String username, String password) throws SQLException {
return new DiagnosticsConnection(configuration);
}
}

View File

@ -0,0 +1,65 @@
/*
* 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.impl;
import java.sql.Connection;
import java.sql.SQLException;
import org.jooq.Configuration;
/**
* @author Lukas Eder
*/
final class ParsingDataSource extends AbstractDataSource {
private final Configuration configuration;
ParsingDataSource(Configuration configuration) {
this.configuration = configuration;
}
@Override
public final Connection getConnection() throws SQLException {
return new ParsingConnection(configuration);
}
@Override
public final Connection getConnection(String username, String password) throws SQLException {
return new ParsingConnection(configuration);
}
}