diff --git a/jOOQ-test/src/org/jooq/test/_/testcases/DataTypeTests.java b/jOOQ-test/src/org/jooq/test/_/testcases/DataTypeTests.java index c94b4c1666..19b734401f 100644 --- a/jOOQ-test/src/org/jooq/test/_/testcases/DataTypeTests.java +++ b/jOOQ-test/src/org/jooq/test/_/testcases/DataTypeTests.java @@ -77,8 +77,8 @@ import java.util.Calendar; import java.util.List; import java.util.UUID; -import org.jooq.DSLContext; import org.jooq.Converter; +import org.jooq.DSLContext; import org.jooq.DataType; import org.jooq.Field; import org.jooq.InsertSetMoreStep; @@ -86,6 +86,7 @@ import org.jooq.Record; import org.jooq.Record1; import org.jooq.Record2; import org.jooq.Record3; +import org.jooq.Record4; import org.jooq.Record6; import org.jooq.Result; import org.jooq.ResultQuery; @@ -1614,4 +1615,32 @@ extends BaseTest id1 = TBook_ID().coerce(Long.class); + Field id2 = TBook_ID().coerce(String.class); + + Result> result = + create().select( + id1, + id2, + val("1").coerce(Short.class), + val(2).coerce(String.class)) + .from(TBook()) + .where(id1.in(1L, 2L)) + .and(id2.in("1", "2")) + .orderBy(id1, id2) + .fetch(); + + assertEquals(2, result.size()); + assertEquals(1L, result.getValue(0, 0)); + assertEquals(2L, result.getValue(1, 0)); + assertEquals("1", result.getValue(0, 1)); + assertEquals("2", result.getValue(1, 1)); + assertEquals((short) 1, result.getValue(0, 2)); + assertEquals((short) 1, result.getValue(1, 2)); + assertEquals("2", result.getValue(0, 3)); + assertEquals("2", result.getValue(1, 3)); + } } diff --git a/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java b/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java index b572bf0cb0..04b60c6d17 100644 --- a/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java +++ b/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java @@ -1118,6 +1118,11 @@ public abstract class jOOQAbstractTest< new SelectTests(this).testForUpdateClauses(); } + @Test + public void testCoercion() throws Exception { + new DataTypeTests(this).testCoercion(); + } + @Test public void testCastingToJavaClass() throws Exception { new DataTypeTests(this).testCastingToJavaClass(); diff --git a/jOOQ/src/main/java/org/jooq/Field.java b/jOOQ/src/main/java/org/jooq/Field.java index 5308afdecd..ffe82166bc 100644 --- a/jOOQ/src/main/java/org/jooq/Field.java +++ b/jOOQ/src/main/java/org/jooq/Field.java @@ -148,7 +148,8 @@ public interface Field extends GroupField { * Cast this field to a dialect-specific data type. * * @param The generic type of the cast field - * @param type + * @param type The data type that is used for the cast + * @return The cast field */ @Support Field cast(DataType type); @@ -168,6 +169,102 @@ public interface Field extends GroupField { @Support Field cast(Class type); + // ------------------------------------------------------------------------ + // Type coercion + // ------------------------------------------------------------------------ + + /** + * Coerce this field to the type of another field. + *

+ * Unlike with casting, coercing doesn't affect the way the database sees a + * Field's type. This is how coercing affects your SQL: + *

Bind values

+     * // This binds an int value to a JDBC PreparedStatement
+     * DSL.val(1).coerce(String.class);
+     *
+     * // This binds an int value to a JDBC PreparedStatement
+     * // and casts it to VARCHAR in SQL
+     * DSL.val(1).cast(String.class);
+     * 
+ *

Other Field types

+     * // This fetches a String value for the BOOK.ID field from JDBC
+     * BOOK.ID.coerce(String.class);
+     *
+     * // This fetches a String value for the BOOK.ID field from JDBC
+     * // after casting it to VARCHAR in the database
+     * BOOK.ID.cast(String.class);
+     * 
+ * + * @param The generic type of the coerced field + * @param field The field whose type is used for the coercion + * @return The coerced field + * @see #coerce(DataType) + * @see #cast(Field) + */ + @Support + Field coerce(Field field); + + /** + * Coerce this field to a dialect-specific data type. + *

+ * Unlike with casting, coercing doesn't affect the way the database sees a + * Field's type. This is how coercing affects your SQL: + *

Bind values

+     * // This binds an int value to a JDBC PreparedStatement
+     * DSL.val(1).coerce(String.class);
+     *
+     * // This binds an int value to a JDBC PreparedStatement
+     * // and casts it to VARCHAR in SQL
+     * DSL.val(1).cast(String.class);
+     * 
+ *

Other Field types

+     * // This fetches a String value for the BOOK.ID field from JDBC
+     * BOOK.ID.coerce(String.class);
+     *
+     * // This fetches a String value for the BOOK.ID field from JDBC
+     * // after casting it to VARCHAR in the database
+     * BOOK.ID.cast(String.class);
+     * 
+ * + * @param The generic type of the coerced field + * @param type The data type that is used for the coercion + * @return The coerced field + * @see #cast(DataType) + */ + @Support + Field coerce(DataType type); + + /** + * Coerce this field to another type. + *

+ * Unlike with casting, coercing doesn't affect the way the database sees a + * Field's type. This is how coercing affects your SQL: + *

Bind values

+     * // This binds an int value to a JDBC PreparedStatement
+     * DSL.val(1).coerce(String.class);
+     *
+     * // This binds an int value to a JDBC PreparedStatement
+     * // and casts it to VARCHAR in SQL
+     * DSL.val(1).cast(String.class);
+     * 
+ *

Other Field types

+     * // This fetches a String value for the BOOK.ID field from JDBC
+     * BOOK.ID.coerce(String.class);
+     *
+     * // This fetches a String value for the BOOK.ID field from JDBC
+     * // after casting it to VARCHAR in the database
+     * BOOK.ID.cast(String.class);
+     * 
+ * + * @param The generic type of the coerced field + * @param type The type that is used for the coercion + * @return The coerced field + * @see #coerce(DataType) + * @see #cast(Class) + */ + @Support + Field coerce(Class type); + // ------------------------------------------------------------------------ // Conversion of field into a sort field // ------------------------------------------------------------------------ diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractField.java b/jOOQ/src/main/java/org/jooq/impl/AbstractField.java index 49b2baccb2..3dc8dc85d6 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractField.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractField.java @@ -162,6 +162,33 @@ abstract class AbstractField extends AbstractQueryPart implements Field { return cast(DefaultDataType.getDataType(null, type)); } + // ------------------------------------------------------------------------ + // XXX: Type coercions + // ------------------------------------------------------------------------ + + @Override + public final Field coerce(Field field) { + return coerce(field.getDataType()); + } + + @SuppressWarnings("unchecked") + @Override + public final Field coerce(DataType type) { + + // [#473] Prevent unnecessary coercions + if (getDataType().equals(type)) { + return (Field) this; + } + else { + return new Coerce(this, type); + } + } + + @Override + public final Field coerce(Class type) { + return coerce(DefaultDataType.getDataType(null, type)); + } + // ------------------------------------------------------------------------ // XXX: Conversion of field into a sort field // ------------------------------------------------------------------------ diff --git a/jOOQ/src/main/java/org/jooq/impl/Coerce.java b/jOOQ/src/main/java/org/jooq/impl/Coerce.java new file mode 100644 index 0000000000..512e3cd17c --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/Coerce.java @@ -0,0 +1,70 @@ +/** + * Copyright (c) 2009-2013, Lukas Eder, lukas.eder@gmail.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.impl; + +import org.jooq.BindContext; +import org.jooq.DataType; +import org.jooq.Field; +import org.jooq.RenderContext; + +/** + * @author Lukas Eder + */ +class Coerce extends AbstractField { + + /** + * Generated UID + */ + private static final long serialVersionUID = -6776617606751542856L; + + private final Field field; + + public Coerce(Field field, DataType type) { + super(field.getName(), type); + + this.field = (field instanceof Coerce) ? ((Coerce) field).field : field; + } + + @Override + public final void toSQL(RenderContext context) { + context.sql(field); + } + + @Override + public final void bind(BindContext context) { + context.bind(field); + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 835e1ec25f..c7a0a51b3b 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -3377,6 +3377,108 @@ public class DSL { return new Decode(nullSafe(value), nullSafe(search), nullSafe(result), nullSafe(more)); } + /** + * Coerce this field to the type of another field. + *

+ * Unlike with casting, coercing doesn't affect the way the database sees a + * Field's type. This is how coercing affects your SQL: + *

Bind values

+     * // This binds an int value to a JDBC PreparedStatement
+     * DSL.val(1).coerce(String.class);
+     *
+     * // This binds an int value to a JDBC PreparedStatement
+     * // and casts it to VARCHAR in SQL
+     * DSL.val(1).cast(String.class);
+     * 
+ *

Other Field types

+     * // This fetches a String value for the BOOK.ID field from JDBC
+     * BOOK.ID.coerce(String.class);
+     *
+     * // This fetches a String value for the BOOK.ID field from JDBC
+     * // after casting it to VARCHAR in the database
+     * BOOK.ID.cast(String.class);
+     * 
+ * + * @param The generic type of the coerced field + * @param field The field to be coerced + * @param as The field whose type is used for the coercion + * @return The coerced field + * @see Field#coerce(DataType) + * @see Field#cast(Field) + */ + @Support + public static Field coerce(Field field, Field as) { + return nullSafe(field).coerce(as); + } + + /** + * Coerce this field to another type. + *

+ * Unlike with casting, coercing doesn't affect the way the database sees a + * Field's type. This is how coercing affects your SQL: + *

Bind values

+     * // This binds an int value to a JDBC PreparedStatement
+     * DSL.val(1).coerce(String.class);
+     *
+     * // This binds an int value to a JDBC PreparedStatement
+     * // and casts it to VARCHAR in SQL
+     * DSL.val(1).cast(String.class);
+     * 
+ *

Other Field types

+     * // This fetches a String value for the BOOK.ID field from JDBC
+     * BOOK.ID.coerce(String.class);
+     *
+     * // This fetches a String value for the BOOK.ID field from JDBC
+     * // after casting it to VARCHAR in the database
+     * BOOK.ID.cast(String.class);
+     * 
+ * + * @param The generic type of the coerced field + * @param field The field to be coerced + * @param as The type that is used for the coercion + * @return The coerced field + * @see Field#coerce(DataType) + * @see Field#cast(Class) + */ + @Support + public static Field coerce(Field field, Class as) { + return nullSafe(field).coerce(as); + } + + /** + * Coerce a field to another type. + *

+ * Unlike with casting, coercing doesn't affect the way the database sees a + * Field's type. This is how coercing affects your SQL: + *

Bind values

+     * // This binds an int value to a JDBC PreparedStatement
+     * DSL.val(1).coerce(String.class);
+     *
+     * // This binds an int value to a JDBC PreparedStatement
+     * // and casts it to VARCHAR in SQL
+     * DSL.val(1).cast(String.class);
+     * 
+ *

Other Field types

+     * // This fetches a String value for the BOOK.ID field from JDBC
+     * BOOK.ID.coerce(String.class);
+     *
+     * // This fetches a String value for the BOOK.ID field from JDBC
+     * // after casting it to VARCHAR in the database
+     * BOOK.ID.cast(String.class);
+     * 
+ * + * @param The generic type of the coerced field + * @param field The field to be coerced + * @param as The type that is used for the coercion + * @return The coerced field + * @see Field#coerce(DataType) + * @see Field#cast(DataType) + */ + @Support + public static Field coerce(Field field, DataType as) { + return nullSafe(field).coerce(as); + } + /** * Cast a value to the type of another field. * diff --git a/jOOQ/src/test/java/org/jooq/test/DataTypeTest.java b/jOOQ/src/test/java/org/jooq/test/DataTypeTest.java index 9fa763a61e..0cf939c1ba 100644 --- a/jOOQ/src/test/java/org/jooq/test/DataTypeTest.java +++ b/jOOQ/src/test/java/org/jooq/test/DataTypeTest.java @@ -39,6 +39,7 @@ package org.jooq.test; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertTrue; import static org.jooq.impl.DSL.fieldByName; +import static org.jooq.impl.DSL.val; import java.sql.Timestamp; @@ -282,4 +283,23 @@ public class DataTypeTest extends AbstractTest { m.getSign() * (int) m.getTotalMinutes()); } } + + @Test + public void testCoercion() throws Exception { + Field s = val(1).coerce(String.class); + Field i = val("2").coerce(Integer.class); + + assertEquals("?", r_ref().render(s)); + assertEquals("?", r_ref().render(i)); + assertEquals("1", r_refI().render(s)); + assertEquals("'2'", r_refI().render(i)); + + context.checking(new Expectations() {{ + oneOf(statement).setInt(1, 1); + oneOf(statement).setString(2, "2"); + }}); + + assertEquals(3, b_ref().bind(s).bind(i).peekIndex()); + context.assertIsSatisfied(); + } }