From c8812d0bc312b4e0f1779b2675cf139874563e98 Mon Sep 17 00:00:00 2001 From: lukaseder Date: Thu, 25 Jan 2018 12:25:58 +0100 Subject: [PATCH] [#7097] Expose jOOQ Connection proxies also as DataSource --- jOOQ/src/main/java/org/jooq/DSLContext.java | 17 ++++ .../org/jooq/impl/AbstractDataSource.java | 90 +++++++++++++++++++ .../java/org/jooq/impl/DefaultDSLContext.java | 10 +++ .../org/jooq/impl/DiagnosticsDataSource.java | 65 ++++++++++++++ .../java/org/jooq/impl/ParsingDataSource.java | 65 ++++++++++++++ 5 files changed, 247 insertions(+) create mode 100644 jOOQ/src/main/java/org/jooq/impl/AbstractDataSource.java create mode 100644 jOOQ/src/main/java/org/jooq/impl/DiagnosticsDataSource.java create mode 100644 jOOQ/src/main/java/org/jooq/impl/ParsingDataSource.java diff --git a/jOOQ/src/main/java/org/jooq/DSLContext.java b/jOOQ/src/main/java/org/jooq/DSLContext.java index aaf8655162..c8a7fedc07 100644 --- a/jOOQ/src/main/java/org/jooq/DSLContext.java +++ b/jOOQ/src/main/java/org/jooq/DSLContext.java @@ -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. + *

+ * 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. + *

+ * This simply wraps the {@link #diagnosticsConnection()} in a {@link DataSource}. + */ + DataSource diagnosticsDataSource(); + /** * Access the database meta data. *

diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractDataSource.java b/jOOQ/src/main/java/org/jooq/impl/AbstractDataSource.java new file mode 100644 index 0000000000..fcd6f32c31 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractDataSource.java @@ -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 unwrap(Class 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); + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java index 7a84641248..cfddc81916 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java @@ -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()); diff --git a/jOOQ/src/main/java/org/jooq/impl/DiagnosticsDataSource.java b/jOOQ/src/main/java/org/jooq/impl/DiagnosticsDataSource.java new file mode 100644 index 0000000000..827aa648ca --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/DiagnosticsDataSource.java @@ -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); + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/ParsingDataSource.java b/jOOQ/src/main/java/org/jooq/impl/ParsingDataSource.java new file mode 100644 index 0000000000..6e2adb1a72 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/ParsingDataSource.java @@ -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); + } +}