[#1373] Add <T> Field<T> Factory.coerce(Field<?>, DataType<T>) and

similar methods, to coerce a field to a given data type (as opposed to
casting it)
This commit is contained in:
Lukas Eder 2013-05-05 12:43:46 +02:00
parent d5439b468a
commit de3458f0d5
7 changed files with 352 additions and 2 deletions

View File

@ -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<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
assertEquals(uuid2, record.getValue(val)[1]);
}
}
@Test
public void testCoercion() throws Exception {
Field<Long> id1 = TBook_ID().coerce(Long.class);
Field<String> id2 = TBook_ID().coerce(String.class);
Result<Record4<Long, String, Short, String>> 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));
}
}

View File

@ -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();

View File

@ -148,7 +148,8 @@ public interface Field<T> extends GroupField {
* Cast this field to a dialect-specific data type.
*
* @param <Z> 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
<Z> Field<Z> cast(DataType<Z> type);
@ -168,6 +169,102 @@ public interface Field<T> extends GroupField {
@Support
<Z> Field<Z> cast(Class<Z> type);
// ------------------------------------------------------------------------
// Type coercion
// ------------------------------------------------------------------------
/**
* Coerce this field to the type of another field.
* <p>
* Unlike with casting, coercing doesn't affect the way the database sees a
* <code>Field</code>'s type. This is how coercing affects your SQL:
* <h3>Bind values</h3> <code><pre>
* // 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);
* </pre></code>
* <h3>Other Field types</h3> <code><pre>
* // 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);
* </pre></code>
*
* @param <Z> 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
<Z> Field<Z> coerce(Field<Z> field);
/**
* Coerce this field to a dialect-specific data type.
* <p>
* Unlike with casting, coercing doesn't affect the way the database sees a
* <code>Field</code>'s type. This is how coercing affects your SQL:
* <h3>Bind values</h3> <code><pre>
* // 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);
* </pre></code>
* <h3>Other Field types</h3> <code><pre>
* // 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);
* </pre></code>
*
* @param <Z> 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
<Z> Field<Z> coerce(DataType<Z> type);
/**
* Coerce this field to another type.
* <p>
* Unlike with casting, coercing doesn't affect the way the database sees a
* <code>Field</code>'s type. This is how coercing affects your SQL:
* <h3>Bind values</h3> <code><pre>
* // 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);
* </pre></code>
* <h3>Other Field types</h3> <code><pre>
* // 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);
* </pre></code>
*
* @param <Z> 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
<Z> Field<Z> coerce(Class<Z> type);
// ------------------------------------------------------------------------
// Conversion of field into a sort field
// ------------------------------------------------------------------------

View File

@ -162,6 +162,33 @@ abstract class AbstractField<T> extends AbstractQueryPart implements Field<T> {
return cast(DefaultDataType.getDataType(null, type));
}
// ------------------------------------------------------------------------
// XXX: Type coercions
// ------------------------------------------------------------------------
@Override
public final <Z> Field<Z> coerce(Field<Z> field) {
return coerce(field.getDataType());
}
@SuppressWarnings("unchecked")
@Override
public final <Z> Field<Z> coerce(DataType<Z> type) {
// [#473] Prevent unnecessary coercions
if (getDataType().equals(type)) {
return (Field<Z>) this;
}
else {
return new Coerce<Z>(this, type);
}
}
@Override
public final <Z> Field<Z> coerce(Class<Z> type) {
return coerce(DefaultDataType.getDataType(null, type));
}
// ------------------------------------------------------------------------
// XXX: Conversion of field into a sort field
// ------------------------------------------------------------------------

View File

@ -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<T> extends AbstractField<T> {
/**
* Generated UID
*/
private static final long serialVersionUID = -6776617606751542856L;
private final Field<?> field;
public Coerce(Field<?> field, DataType<T> 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);
}
}

View File

@ -3377,6 +3377,108 @@ public class DSL {
return new Decode<T, Z>(nullSafe(value), nullSafe(search), nullSafe(result), nullSafe(more));
}
/**
* Coerce this field to the type of another field.
* <p>
* Unlike with casting, coercing doesn't affect the way the database sees a
* <code>Field</code>'s type. This is how coercing affects your SQL:
* <h3>Bind values</h3> <code><pre>
* // 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);
* </pre></code>
* <h3>Other Field types</h3> <code><pre>
* // 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);
* </pre></code>
*
* @param <T> 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 <T> Field<T> coerce(Field<?> field, Field<T> as) {
return nullSafe(field).coerce(as);
}
/**
* Coerce this field to another type.
* <p>
* Unlike with casting, coercing doesn't affect the way the database sees a
* <code>Field</code>'s type. This is how coercing affects your SQL:
* <h3>Bind values</h3> <code><pre>
* // 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);
* </pre></code>
* <h3>Other Field types</h3> <code><pre>
* // 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);
* </pre></code>
*
* @param <T> 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 <T> Field<T> coerce(Field<?> field, Class<T> as) {
return nullSafe(field).coerce(as);
}
/**
* Coerce a field to another type.
* <p>
* Unlike with casting, coercing doesn't affect the way the database sees a
* <code>Field</code>'s type. This is how coercing affects your SQL:
* <h3>Bind values</h3> <code><pre>
* // 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);
* </pre></code>
* <h3>Other Field types</h3> <code><pre>
* // 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);
* </pre></code>
*
* @param <T> 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 <T> Field<T> coerce(Field<?> field, DataType<T> as) {
return nullSafe(field).coerce(as);
}
/**
* Cast a value to the type of another field.
*

View File

@ -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<String> s = val(1).coerce(String.class);
Field<Integer> 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();
}
}