[#894] Move functions from Field<?> to new org.jooq.impl.SQL and make them static - moved coalesce

This commit is contained in:
Lukas Eder 2011-11-07 22:40:16 +00:00
parent 2d3662e028
commit 5d672b666a
6 changed files with 91 additions and 72 deletions

View File

@ -67,6 +67,7 @@ import static org.jooq.impl.Factory.cast;
import static org.jooq.impl.Factory.castNull;
import static org.jooq.impl.Factory.ceil;
import static org.jooq.impl.Factory.charLength;
import static org.jooq.impl.Factory.coalesce;
import static org.jooq.impl.Factory.concat;
import static org.jooq.impl.Factory.cos;
import static org.jooq.impl.Factory.cosh;
@ -5723,29 +5724,29 @@ public abstract class jOOQAbstractTest<
// ---------------------------------------------------------------------
// COALESCE
// ---------------------------------------------------------------------
assertEquals(null, create().select(sNull.coalesce(sNull)).fetchOne(0));
assertEquals(Integer.valueOf(1), create().select(iNull.coalesce(1)).fetchOne(0));
assertEquals(Integer.valueOf(1), create().select(iNull.coalesce(iNull, val(1))).fetchOne(0));
assertEquals(Integer.valueOf(1), create().select(iNull.coalesce(iNull, iNull, val(1))).fetchOne(0));
assertEquals(null, create().select(coalesce(sNull, sNull)).fetchOne(0));
assertEquals(Integer.valueOf(1), create().select(coalesce(iNull, val(1))).fetchOne(0));
assertEquals(Integer.valueOf(1), create().select(coalesce(iNull, iNull, val(1))).fetchOne(0));
assertEquals(Integer.valueOf(1), create().select(coalesce(iNull, iNull, iNull, val(1))).fetchOne(0));
assertEquals("1", create().select(sNull.coalesce("1")).fetchOne(0));
assertEquals("1", create().select(sNull.coalesce(sNull, val("1"))).fetchOne(0));
assertEquals("1", create().select(sNull.coalesce(sNull, sNull, val("1"))).fetchOne(0));
assertEquals("1", create().select(coalesce(sNull, val("1"))).fetchOne(0));
assertEquals("1", create().select(coalesce(sNull, sNull, val("1"))).fetchOne(0));
assertEquals("1", create().select(coalesce(sNull, sNull, sNull, val("1"))).fetchOne(0));
assertEquals(Integer.valueOf(2), create().select(val(2).coalesce(1)).fetchOne(0));
assertEquals(Integer.valueOf(2), create().select(val(2).coalesce(1, 1)).fetchOne(0));
assertEquals(Integer.valueOf(2), create().select(val(2).coalesce(1, 1, 1)).fetchOne(0));
assertEquals(Integer.valueOf(2), create().select(coalesce(2, 1)).fetchOne(0));
assertEquals(Integer.valueOf(2), create().select(coalesce(2, 1, 1)).fetchOne(0));
assertEquals(Integer.valueOf(2), create().select(coalesce(2, 1, 1, 1)).fetchOne(0));
assertEquals("2", create().select(val("2").coalesce("1")).fetchOne(0));
assertEquals("2", create().select(val("2").coalesce("1", "1")).fetchOne(0));
assertEquals("2", create().select(val("2").coalesce("1", "1", "1")).fetchOne(0));
assertEquals("2", create().select(coalesce("2", "1")).fetchOne(0));
assertEquals("2", create().select(coalesce("2", "1", "1")).fetchOne(0));
assertEquals("2", create().select(coalesce("2", "1", "1", "1")).fetchOne(0));
assertTrue(("" + create()
.select(TBook_CONTENT_TEXT().cast(String.class).coalesce(sNull, val("abc")))
.select(coalesce(TBook_CONTENT_TEXT().cast(String.class), sNull, val("abc")))
.from(TBook())
.where(TBook_ID().equal(1)).fetchOne(0)).startsWith("To know and"));
assertEquals("abc", create()
.select(TBook_CONTENT_TEXT().cast(String.class).coalesce(sNull, val("abc")))
.select(coalesce(TBook_CONTENT_TEXT().cast(String.class), sNull, val("abc")))
.from(TBook())
.where(TBook_ID().equal(2)).fetchOne(0));

View File

@ -438,27 +438,6 @@ public interface Field<T> extends NamedTypeProviderQueryPart<T>, AliasProvider<F
*/
<Z> Field<Z> decode(Field<T> search, Field<Z> result, Field<?>... more);
/**
* Gets the Oracle-style <code>COALESCE(expr1, expr2, ... , exprn)</code>
* function
*
* @see #coalesce(Field, Field...)
*/
Field<T> coalesce(T option, T... options);
/**
* Gets the Oracle-style <code>COALESCE(expr1, expr2, ... , exprn)</code>
* function
* <p>
* Returns the dialect's equivalent to COALESCE:
* <ul>
* <li>Oracle <a
* href="http://www.techonthenet.com/oracle/functions/coalesce.php">COALESCE</a>
* </li>
* </ul>
*/
Field<T> coalesce(Field<T> option, Field<?>... options);
// ------------------------------------------------------------------------
// Conditions created from this field
// ------------------------------------------------------------------------

View File

@ -40,7 +40,6 @@ import static org.jooq.impl.ExpressionOperator.DIVIDE;
import static org.jooq.impl.ExpressionOperator.MULTIPLY;
import static org.jooq.impl.ExpressionOperator.SUBTRACT;
import static org.jooq.impl.Factory.falseCondition;
import static org.jooq.impl.Factory.function;
import static org.jooq.impl.Factory.nullSafe;
import static org.jooq.impl.Factory.trueCondition;
import static org.jooq.impl.Factory.val;
@ -333,16 +332,6 @@ abstract class AbstractField<T> extends AbstractNamedTypeProviderQueryPart<T> im
return new Decode<T, Z>(this, nullSafe(search), nullSafe(result), nullSafe(more));
}
@Override
public final Field<T> coalesce(T option, T... options) {
return coalesce(val(option), vals(options).toArray(new Field<?>[0]));
}
@Override
public final Field<T> coalesce(Field<T> option, Field<?>... options) {
return function("coalesce", getDataType(), nullSafe(combine(this, option, options)));
}
// ------------------------------------------------------------------------
// Conditions created from this field
// ------------------------------------------------------------------------

View File

@ -36,6 +36,8 @@
package org.jooq.impl;
import static org.jooq.impl.JooqUtil.combine;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
@ -1296,6 +1298,31 @@ public class Factory implements FactoryOperations {
return (Field<T>[]) castFields;
}
/**
* Gets the Oracle-style <code>COALESCE(value1, value2, ... , value n)</code>
* function
*
* @see #coalesce(Field, Field, Field...)
*/
public static <T> Field<T> coalesce(T value1, T value2, T... values) {
return coalesce(val(value1), val(value2), vals(values).toArray(new Field[0]));
}
/**
* Gets the Oracle-style <code>COALESCE(field1, field2, ... , field n)</code>
* function
* <p>
* Returns the dialect's equivalent to COALESCE:
* <ul>
* <li>Oracle <a
* href="http://www.techonthenet.com/oracle/functions/coalesce.php">COALESCE</a>
* </li>
* </ul>
*/
public static <T> Field<T> coalesce(Field<T> field1, Field<T> field2, Field<?>... fields) {
return function("coalesce", nullSafeDataType(field1), nullSafe(combine(field1, field2, fields)));
}
/**
* Gets the Oracle-style NVL(value, defaultValue) function
*
@ -3492,12 +3519,9 @@ public class Factory implements FactoryOperations {
* Get a list of values and fields
*/
public static List<Field<?>> vals(Object... values) {
if (values == null) {
throw new IllegalArgumentException("Cannot create a list of constants for null");
}
else {
FieldList result = new FieldList();
FieldList result = new FieldList();
if (values != null) {
for (Object value : values) {
// Fields can be mixed with constant values
@ -3508,9 +3532,9 @@ public class Factory implements FactoryOperations {
result.add(val(value));
}
}
return result;
}
return result;
}
// -------------------------------------------------------------------------

View File

@ -203,33 +203,50 @@ final class JooqUtil {
* Combine a field with an array of fields
*/
static Field<?>[] combine(Field<?> field, Field<?>... fields) {
Field<?>[] result = new Field<?>[fields.length + 1];
result[0] = field;
System.arraycopy(fields, 0, result, 1, fields.length);
return result;
if (fields == null) {
return new Field[] { field };
}
else {
Field<?>[] result = new Field<?>[fields.length + 1];
result[0] = field;
System.arraycopy(fields, 0, result, 1, fields.length);
return result;
}
}
/**
* Combine a field with an array of fields
*/
static Field<?>[] combine(Field<?> field1, Field<?> field2, Field<?>... fields) {
Field<?>[] result = new Field<?>[fields.length + 2];
result[0] = field1;
result[1] = field2;
System.arraycopy(fields, 0, result, 2, fields.length);
return result;
if (fields == null) {
return new Field[] { field1, field2 };
}
else {
Field<?>[] result = new Field<?>[fields.length + 2];
result[0] = field1;
result[1] = field2;
System.arraycopy(fields, 0, result, 2, fields.length);
return result;
}
}
/**
* Combine a field with an array of fields
*/
static Field<?>[] combine(Field<?> field1, Field<?> field2, Field<?> field3, Field<?>... fields) {
Field<?>[] result = new Field<?>[fields.length + 3];
result[0] = field1;
result[1] = field2;
result[2] = field3;
System.arraycopy(fields, 0, result, 3, fields.length);
return result;
if (fields == null) {
return new Field[] { field1, field2, field3 };
}
else {
Field<?>[] result = new Field<?>[fields.length + 3];
result[0] = field1;
result[1] = field2;
result[2] = field3;
System.arraycopy(fields, 0, result, 3, fields.length);
return result;
}
}
/**

View File

@ -194,9 +194,6 @@ public class jOOQTest {
assertEquals(
FIELD_ID1.between((Integer) null, null),
FIELD_ID1.between((Field<Integer>) null, null));
assertEquals(
FIELD_ID1.coalesce((Integer) null),
FIELD_ID1.coalesce((Field<Integer>) null));
assertEquals(
FIELD_ID1.decode((Integer) null, null),
FIELD_ID1.decode((Field<Integer>) null, null));
@ -332,6 +329,18 @@ public class jOOQTest {
assertEquals(
Factory.charLength((String) null),
Factory.charLength((Field<String>) null));
assertEquals(
Factory.coalesce((Integer) null, (Integer) null),
Factory.coalesce((Field<Integer>) null, (Field<Integer>) null));
assertEquals(
Factory.coalesce((Integer) null, (Integer) null, (Integer) null),
Factory.coalesce((Field<Integer>) null, (Field<Integer>) null, (Field<Integer>) null));
assertEquals(
Factory.coalesce((Integer) null, (Integer) null, (Integer[]) null),
Factory.coalesce((Field<Integer>) null, (Field<Integer>) null, (Field<?>[]) null));
assertEquals(
Factory.coalesce((Integer) null, (Integer) null, (Integer) null, (Integer) null),
Factory.coalesce((Field<Integer>) null, (Field<Integer>) null, (Field<Integer>) null, (Field<Integer>) null));
assertEquals(
Factory.concat((String) null, (String) null),
Factory.concat((Field<String>) null, (Field<String>) null));