diff --git a/jOOQ/src/main/java/org/jooq/impl/BlobBinding.java b/jOOQ/src/main/java/org/jooq/impl/BlobBinding.java new file mode 100644 index 0000000000..d58734aab6 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/BlobBinding.java @@ -0,0 +1,159 @@ +/** + * Copyright (c) 2009-2015, Data Geekery GmbH (http://www.datageekery.com) + * All rights reserved. + * + * This work is dual-licensed + * - under the Apache Software License 2.0 (the "ASL") + * - under the jOOQ License and Maintenance Agreement (the "jOOQ License") + * ============================================================================= + * You may choose which license applies to you: + * + * - If you're using this work with Open Source databases, you may choose + * either ASL or jOOQ License. + * - If you're using this work with at least one commercial database, you must + * choose jOOQ License + * + * For more information, please visit http://www.jooq.org/licenses + * + * Apache Software License 2.0: + * ----------------------------------------------------------------------------- + * 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. + * + * jOOQ License and Maintenance Agreement: + * ----------------------------------------------------------------------------- + * Data Geekery grants the Customer the non-exclusive, timely limited and + * non-transferable license to install and use the Software under the terms of + * the jOOQ License and Maintenance Agreement. + * + * This library is distributed with a LIMITED WARRANTY. See the jOOQ License + * and Maintenance Agreement for more details: http://www.jooq.org/licensing + */ +package org.jooq.impl; + +import static org.jooq.tools.reflect.Reflect.on; + +import java.sql.Blob; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Types; + +import org.jooq.Binding; +import org.jooq.BindingGetResultSetContext; +import org.jooq.BindingGetSQLInputContext; +import org.jooq.BindingGetStatementContext; +import org.jooq.BindingRegisterContext; +import org.jooq.BindingSQLContext; +import org.jooq.BindingSetSQLOutputContext; +import org.jooq.BindingSetStatementContext; +import org.jooq.Configuration; +import org.jooq.Converter; +import org.jooq.Converters; + +/** + * A binding that takes binary values but binds them as {@link Blob} to at the + * JDBC level. + *

+ * This is useful for workarounds for bugs in Oracle, like ORA-01461: can bind a + * LONG value only for insert into a LONG column (see [#4091]) + * + * @author Lukas Eder + */ +public class BlobBinding implements Binding { + + /** + * Generated UID + */ + private static final long serialVersionUID = 358789452467943117L; + + @Override + public final Converter converter() { + return Converters.identity(byte[].class); + } + + @Override + public final void sql(BindingSQLContext ctx) throws SQLException { + ctx.render().sql("?"); + } + + @Override + public final void register(BindingRegisterContext ctx) throws SQLException { + ctx.statement().registerOutParameter(ctx.index(), Types.BLOB); + } + + @Override + public final void set(BindingSetStatementContext ctx) throws SQLException { + Blob blob = newBlob(ctx.configuration(), ctx.value()); + // [#4205] register blob for free() + ctx.statement().setBlob(ctx.index(), blob); + } + + @Override + public final void set(BindingSetSQLOutputContext ctx) throws SQLException { + Blob blob = newBlob(ctx.configuration(), ctx.value()); + // [#4205] register blob for free() + ctx.output().writeBlob(blob); + } + + @Override + public final void get(BindingGetResultSetContext ctx) throws SQLException { + Blob blob = ctx.resultSet().getBlob(ctx.index()); + // [#4205] register blob for free() + ctx.value(blob == null ? null : blob.getBytes(1, (int) blob.length())); + } + + @Override + public final void get(BindingGetStatementContext ctx) throws SQLException { + Blob blob = ctx.statement().getBlob(ctx.index()); + // [#4205] register blob for free() + ctx.value(blob == null ? null : blob.getBytes(1, (int) blob.length())); + } + + @Override + public final void get(BindingGetSQLInputContext ctx) throws SQLException { + Blob blob = ctx.input().readBlob(); + // [#4205] register blob for free() + ctx.value(blob == null ? null : blob.getBytes(1, (int) blob.length())); + } + + private final Blob newBlob(Configuration configuration, byte[] bytes) throws SQLException { + Connection c = configuration.connectionProvider().acquire(); + + try { + Blob blob = null; + + switch (configuration.family()) { + /* [pro] xx + xx xxxxxxxx xxx x xxx xxxx xxxxxxx xxxxx xxxxx xxxxxxxxx + xx xxxxxxxxxxxxxxxxxxxxxxxxxx + xxxx xxxxxxx x + xxxx x xxxxxxxxxxxxxxxxxxxxx + xxxxxxxxxxxxxxxxxxxxxxxx xx xxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxx + xxxxxx + x + xx [/pro] */ + + default: { + blob = c.createBlob(); + break; + } + } + + blob.setBytes(1, bytes); + return blob; + } + finally { + configuration.connectionProvider().release(c); + } + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/ClobBinding.java b/jOOQ/src/main/java/org/jooq/impl/ClobBinding.java new file mode 100644 index 0000000000..e1b6c46099 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/ClobBinding.java @@ -0,0 +1,159 @@ +/** + * Copyright (c) 2009-2015, Data Geekery GmbH (http://www.datageekery.com) + * All rights reserved. + * + * This work is dual-licensed + * - under the Apache Software License 2.0 (the "ASL") + * - under the jOOQ License and Maintenance Agreement (the "jOOQ License") + * ============================================================================= + * You may choose which license applies to you: + * + * - If you're using this work with Open Source databases, you may choose + * either ASL or jOOQ License. + * - If you're using this work with at least one commercial database, you must + * choose jOOQ License + * + * For more information, please visit http://www.jooq.org/licenses + * + * Apache Software License 2.0: + * ----------------------------------------------------------------------------- + * 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. + * + * jOOQ License and Maintenance Agreement: + * ----------------------------------------------------------------------------- + * Data Geekery grants the Customer the non-exclusive, timely limited and + * non-transferable license to install and use the Software under the terms of + * the jOOQ License and Maintenance Agreement. + * + * This library is distributed with a LIMITED WARRANTY. See the jOOQ License + * and Maintenance Agreement for more details: http://www.jooq.org/licensing + */ +package org.jooq.impl; + +import static org.jooq.tools.reflect.Reflect.on; + +import java.sql.Clob; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Types; + +import org.jooq.Binding; +import org.jooq.BindingGetResultSetContext; +import org.jooq.BindingGetSQLInputContext; +import org.jooq.BindingGetStatementContext; +import org.jooq.BindingRegisterContext; +import org.jooq.BindingSQLContext; +import org.jooq.BindingSetSQLOutputContext; +import org.jooq.BindingSetStatementContext; +import org.jooq.Configuration; +import org.jooq.Converter; +import org.jooq.Converters; + +/** + * A binding that takes binary values but binds them as {@link Clob} to at the + * JDBC level. + *

+ * This is useful for workarounds for bugs in Oracle, like ORA-01461: can bind a + * LONG value only for insert into a LONG column (see [#4091]) + * + * @author Lukas Eder + */ +public class ClobBinding implements Binding { + + /** + * Generated UID + */ + private static final long serialVersionUID = 358789452467943117L; + + @Override + public final Converter converter() { + return Converters.identity(String.class); + } + + @Override + public final void sql(BindingSQLContext ctx) throws SQLException { + ctx.render().sql("?"); + } + + @Override + public final void register(BindingRegisterContext ctx) throws SQLException { + ctx.statement().registerOutParameter(ctx.index(), Types.CLOB); + } + + @Override + public final void set(BindingSetStatementContext ctx) throws SQLException { + Clob clob = newClob(ctx.configuration(), ctx.value()); + // [#4205] register clob for free() + ctx.statement().setClob(ctx.index(), clob); + } + + @Override + public final void set(BindingSetSQLOutputContext ctx) throws SQLException { + Clob clob = newClob(ctx.configuration(), ctx.value()); + // [#4205] register clob for free() + ctx.output().writeClob(clob); + } + + @Override + public final void get(BindingGetResultSetContext ctx) throws SQLException { + Clob clob = ctx.resultSet().getClob(ctx.index()); + // [#4205] register blob for free() + ctx.value(clob == null ? null : clob.getSubString(1, (int) clob.length())); + } + + @Override + public final void get(BindingGetStatementContext ctx) throws SQLException { + Clob clob = ctx.statement().getClob(ctx.index()); + // [#4205] register clob for free() + ctx.value(clob == null ? null : clob.getSubString(1, (int) clob.length())); + } + + @Override + public final void get(BindingGetSQLInputContext ctx) throws SQLException { + Clob clob = ctx.input().readClob(); + // [#4205] register clob for free() + ctx.value(clob == null ? null : clob.getSubString(1, (int) clob.length())); + } + + private final Clob newClob(Configuration configuration, String string) throws SQLException { + Connection c = configuration.connectionProvider().acquire(); + + try { + Clob clob = null; + + switch (configuration.family()) { + /* [pro] xx + xx xxxxxxxx xxx x xxx xxxx xxxxxxx xxxxx xxxxx xxxxxxxxx + xx xxxxxxxxxxxxxxxxxxxxxxxxxx + xxxx xxxxxxx x + xxxx x xxxxxxxxxxxxxxxxxxxxx + xxxxxxxxxxxxxxxxxxxxxxxx xx xxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxx + xxxxxx + x + xx [/pro] */ + + default: { + clob = c.createClob(); + break; + } + } + + clob.setString(1, string); + return clob; + } + finally { + configuration.connectionProvider().release(c); + } + } +}