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

This commit is contained in:
Lukas Eder 2011-11-07 23:00:30 +00:00
parent 5d672b666a
commit a42de9aafb
7 changed files with 98 additions and 195 deletions

View File

@ -47,103 +47,7 @@ import static org.jooq.SQLDialect.DB2;
import static org.jooq.SQLDialect.MYSQL;
import static org.jooq.SQLDialect.SQLSERVER;
import static org.jooq.SQLDialect.SYBASE;
import static org.jooq.impl.Factory.abs;
import static org.jooq.impl.Factory.acos;
import static org.jooq.impl.Factory.ascii;
import static org.jooq.impl.Factory.asin;
import static org.jooq.impl.Factory.atan;
import static org.jooq.impl.Factory.atan2;
import static org.jooq.impl.Factory.avg;
import static org.jooq.impl.Factory.bitAnd;
import static org.jooq.impl.Factory.bitCount;
import static org.jooq.impl.Factory.bitLength;
import static org.jooq.impl.Factory.bitNand;
import static org.jooq.impl.Factory.bitNor;
import static org.jooq.impl.Factory.bitNot;
import static org.jooq.impl.Factory.bitOr;
import static org.jooq.impl.Factory.bitXNor;
import static org.jooq.impl.Factory.bitXor;
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;
import static org.jooq.impl.Factory.cot;
import static org.jooq.impl.Factory.coth;
import static org.jooq.impl.Factory.count;
import static org.jooq.impl.Factory.countDistinct;
import static org.jooq.impl.Factory.cube;
import static org.jooq.impl.Factory.cumeDist;
import static org.jooq.impl.Factory.currentDate;
import static org.jooq.impl.Factory.currentTime;
import static org.jooq.impl.Factory.currentTimestamp;
import static org.jooq.impl.Factory.currentUser;
import static org.jooq.impl.Factory.decode;
import static org.jooq.impl.Factory.deg;
import static org.jooq.impl.Factory.denseRank;
import static org.jooq.impl.Factory.e;
import static org.jooq.impl.Factory.exp;
import static org.jooq.impl.Factory.extract;
import static org.jooq.impl.Factory.falseCondition;
import static org.jooq.impl.Factory.field;
import static org.jooq.impl.Factory.firstValue;
import static org.jooq.impl.Factory.floor;
import static org.jooq.impl.Factory.function;
import static org.jooq.impl.Factory.grouping;
import static org.jooq.impl.Factory.groupingId;
import static org.jooq.impl.Factory.groupingSets;
import static org.jooq.impl.Factory.lag;
import static org.jooq.impl.Factory.lead;
import static org.jooq.impl.Factory.length;
import static org.jooq.impl.Factory.ln;
import static org.jooq.impl.Factory.log;
import static org.jooq.impl.Factory.lower;
import static org.jooq.impl.Factory.lpad;
import static org.jooq.impl.Factory.max;
import static org.jooq.impl.Factory.median;
import static org.jooq.impl.Factory.min;
import static org.jooq.impl.Factory.nullif;
import static org.jooq.impl.Factory.nvl;
import static org.jooq.impl.Factory.nvl2;
import static org.jooq.impl.Factory.octetLength;
import static org.jooq.impl.Factory.one;
import static org.jooq.impl.Factory.percentRank;
import static org.jooq.impl.Factory.pi;
import static org.jooq.impl.Factory.position;
import static org.jooq.impl.Factory.power;
import static org.jooq.impl.Factory.rad;
import static org.jooq.impl.Factory.rand;
import static org.jooq.impl.Factory.rank;
import static org.jooq.impl.Factory.repeat;
import static org.jooq.impl.Factory.replace;
import static org.jooq.impl.Factory.rollup;
import static org.jooq.impl.Factory.round;
import static org.jooq.impl.Factory.rowNumber;
import static org.jooq.impl.Factory.rpad;
import static org.jooq.impl.Factory.shl;
import static org.jooq.impl.Factory.shr;
import static org.jooq.impl.Factory.sign;
import static org.jooq.impl.Factory.sin;
import static org.jooq.impl.Factory.sinh;
import static org.jooq.impl.Factory.sqrt;
import static org.jooq.impl.Factory.stddevPop;
import static org.jooq.impl.Factory.stddevSamp;
import static org.jooq.impl.Factory.substring;
import static org.jooq.impl.Factory.sum;
import static org.jooq.impl.Factory.table;
import static org.jooq.impl.Factory.tan;
import static org.jooq.impl.Factory.tanh;
import static org.jooq.impl.Factory.trim;
import static org.jooq.impl.Factory.trueCondition;
import static org.jooq.impl.Factory.two;
import static org.jooq.impl.Factory.val;
import static org.jooq.impl.Factory.vals;
import static org.jooq.impl.Factory.varPop;
import static org.jooq.impl.Factory.varSamp;
import static org.jooq.impl.Factory.zero;
import static org.jooq.impl.Factory.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@ -5368,10 +5272,10 @@ public abstract class jOOQAbstractTest<
// Greatest and least
record = create().select(
val(1).greatest(2, 3, 4),
val(1).least(2, 3),
val("1").greatest("2", "3", "4"),
val("1").least("2", "3")).fetchOne();
greatest(1, 2, 3, 4),
least(1, 2, 3),
greatest("1", "2", "3", "4"),
least("1", "2", "3")).fetchOne();
assertEquals(Integer.valueOf(4), record.getValue(0));
assertEquals(Integer.valueOf(1), record.getValue(1));

View File

@ -336,54 +336,6 @@ public interface Field<T> extends NamedTypeProviderQueryPart<T>, AliasProvider<F
// General functions created from this field
// ------------------------------------------------------------------------
/**
* Find the greatest among all values
* <p>
* This function has no equivalent in Adaptive Server, Derby, SQL Server and
* Sybase SQL Anywhere. Its current simulation implementation has
* <code>O(2^n)</code> complexity and should be avoided for
* <code>n &gt; 5</code>! Better implementation suggestions are very
* welcome.
*
* @see #greatest(Field...)
*/
Field<T> greatest(T... others);
/**
* Find the greatest among all values
* <p>
* This function has no equivalent in Adaptive Server, Derby, SQL Server and
* Sybase SQL Anywhere. Its current simulation implementation has
* <code>O(2^n)</code> complexity and should be avoided for
* <code>n &gt; 5</code>! Better implementation suggestions are very
* welcome.
*/
Field<T> greatest(Field<?>... others);
/**
* Find the least among all values
* <p>
* This function has no equivalent in Adaptive Server, Derby, SQL Server and
* Sybase SQL Anywhere. Its current simulation implementation has
* <code>O(2^n)</code> complexity and should be avoided for
* <code>n &gt; 5</code>! Better implementation suggestions are very
* welcome.
*
* @see #least(Field...)
*/
Field<T> least(T... others);
/**
* Find the least among all values
* <p>
* This function has no equivalent in Adaptive Server, Derby, SQL Server and
* Sybase SQL Anywhere. Its current simulation implementation has
* <code>O(2^n)</code> complexity and should be avoided for
* <code>n &gt; 5</code>! Better implementation suggestions are very
* welcome.
*/
Field<T> least(Field<?>... others);
/**
* Gets the Oracle-style
* <code>DECODE(expression, search, result[, search , result]... [, default])</code>

View File

@ -44,7 +44,6 @@ import static org.jooq.impl.Factory.nullSafe;
import static org.jooq.impl.Factory.trueCondition;
import static org.jooq.impl.Factory.val;
import static org.jooq.impl.Factory.vals;
import static org.jooq.impl.JooqUtil.combine;
import java.util.ArrayList;
import java.util.Arrays;
@ -281,30 +280,6 @@ abstract class AbstractField<T> extends AbstractNamedTypeProviderQueryPart<T> im
return new Mod<T>(this, nullSafe(value));
}
// ------------------------------------------------------------------------
// Functions created from this field
// ------------------------------------------------------------------------
@Override
public final Field<T> greatest(T... others) {
return greatest(vals(others).toArray(new Field<?>[0]));
}
@Override
public final Field<T> greatest(Field<?>... others) {
return new Greatest<T>(getDataType(), nullSafe(combine(this, others)));
}
@Override
public final Field<T> least(T... others) {
return least(vals(others).toArray(new Field<?>[0]));
}
@Override
public final Field<T> least(Field<?>... others) {
return new Least<T>(getDataType(), nullSafe(combine(this, others)));
}
// ------------------------------------------------------------------------
// Other functions created from this field
// ------------------------------------------------------------------------

View File

@ -1304,8 +1304,8 @@ public class Factory implements FactoryOperations {
*
* @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]));
public static <T> Field<T> coalesce(T value, T... values) {
return coalesce(val(value), vals(values).toArray(new Field[0]));
}
/**
@ -1319,8 +1319,8 @@ public class Factory implements FactoryOperations {
* </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)));
public static <T> Field<T> coalesce(Field<T> field, Field<?>... fields) {
return function("coalesce", nullSafeDataType(field), nullSafe(combine(field, fields)));
}
/**
@ -2552,6 +2552,62 @@ public class Factory implements FactoryOperations {
// Mathematical functions
// ------------------------------------------------------------------------
/**
* Find the greatest among all values
* <p>
* This function has no equivalent in Adaptive Server, Derby, SQL Server and
* Sybase SQL Anywhere. Its current simulation implementation has
* <code>O(2^n)</code> complexity and should be avoided for
* <code>n &gt; 5</code>! Better implementation suggestions are very
* welcome.
*
* @see #greatest(Field...)
*/
public static <T> Field<T> greatest(T value, T... values) {
return greatest(val(value), vals(values).toArray(new Field[0]));
}
/**
* Find the greatest among all values
* <p>
* This function has no equivalent in Adaptive Server, Derby, SQL Server and
* Sybase SQL Anywhere. Its current simulation implementation has
* <code>O(2^n)</code> complexity and should be avoided for
* <code>n &gt; 5</code>! Better implementation suggestions are very
* welcome.
*/
public static <T> Field<T> greatest(Field<T> field, Field<?>... others) {
return new Greatest<T>(nullSafeDataType(field), nullSafe(combine(field, others)));
}
/**
* Find the least among all values
* <p>
* This function has no equivalent in Adaptive Server, Derby, SQL Server and
* Sybase SQL Anywhere. Its current simulation implementation has
* <code>O(2^n)</code> complexity and should be avoided for
* <code>n &gt; 5</code>! Better implementation suggestions are very
* welcome.
*
* @see #least(Field...)
*/
public static <T> Field<T> least(T value, T... values) {
return least(val(value), vals(values).toArray(new Field[0]));
}
/**
* Find the least among all values
* <p>
* This function has no equivalent in Adaptive Server, Derby, SQL Server and
* Sybase SQL Anywhere. Its current simulation implementation has
* <code>O(2^n)</code> complexity and should be avoided for
* <code>n &gt; 5</code>! Better implementation suggestions are very
* welcome.
*/
public static <T> Field<T> least(Field<T> field, Field<?>... others) {
return new Least<T>(nullSafeDataType(field), nullSafe(combine(field, others)));
}
/**
* Get the sign of a numeric field: sign(field)
*

View File

@ -36,6 +36,7 @@
package org.jooq.impl;
import static org.jooq.impl.Factory.function;
import static org.jooq.impl.Factory.greatest;
import org.jooq.Configuration;
import org.jooq.DataType;
@ -76,18 +77,16 @@ class Greatest<T> extends AbstractFunction<T> {
Field<T> other = (Field<T>) getArguments()[1];
if (getArguments().length > 2) {
Field<?>[] remaining = new Field<?>[getArguments().length - 2];
Field<?>[] remaining = new Field[getArguments().length - 2];
System.arraycopy(getArguments(), 2, remaining, 0, remaining.length);
return Factory.decode()
.when(first.greaterThan(other),
first.greatest(remaining))
.otherwise(other.greatest(remaining));
.when(first.greaterThan(other), greatest(first, remaining))
.otherwise(greatest(other, remaining));
}
else {
return Factory.decode()
.when(first.greaterThan(other),
first)
.when(first.greaterThan(other), first)
.otherwise(other);
}
}

View File

@ -36,6 +36,7 @@
package org.jooq.impl;
import static org.jooq.impl.Factory.function;
import static org.jooq.impl.Factory.least;
import org.jooq.Configuration;
import org.jooq.DataType;
@ -80,14 +81,12 @@ class Least<T> extends AbstractFunction<T> {
System.arraycopy(getArguments(), 2, remaining, 0, remaining.length);
return Factory.decode()
.when(first.lessThan(other),
first.least(remaining))
.otherwise(other.least(remaining));
.when(first.lessThan(other), least(first, remaining))
.otherwise(least(other, remaining));
}
else {
return Factory.decode()
.when(first.lessThan(other),
first)
.when(first.lessThan(other), first)
.otherwise(other);
}
}

View File

@ -209,15 +209,9 @@ public class jOOQTest {
assertEquals(
FIELD_ID1.greaterThan((Integer) null),
FIELD_ID1.greaterThan((Field<Integer>) null));
assertEquals(
FIELD_ID1.greatest((Integer) null),
FIELD_ID1.greatest((Field<Integer>) null));
assertEquals(
FIELD_ID1.in((Integer) null),
FIELD_ID1.in((Field<Integer>) null));
assertEquals(
FIELD_ID1.least((Integer) null),
FIELD_ID1.least((Field<Integer>) null));
assertEquals(
FIELD_ID1.lessOrEqual((Integer) null),
FIELD_ID1.lessOrEqual((Field<Integer>) null));
@ -371,12 +365,36 @@ public class jOOQTest {
assertEquals(
Factory.floor((Integer) null),
Factory.floor((Field<Integer>) null));
assertEquals(
Factory.greatest((Integer) null),
Factory.greatest((Field<Integer>) null));
assertEquals(
Factory.greatest((Integer) null, (Integer[]) null),
Factory.greatest((Field<Integer>) null, (Field[]) null));
assertEquals(
Factory.greatest((Integer) null, (Integer) null, (Integer) null),
Factory.greatest((Field<Integer>) null, (Field<Integer>) null, (Field<Integer>) null));
assertEquals(
Factory.greatest((Integer) null, (Integer) null),
Factory.greatest((Field<Integer>) null, (Field<Integer>) null));
assertEquals(
Factory.lag((Field<Integer>) null, 1, (Integer) null),
Factory.lag((Field<Integer>) null, 1, (Field<Integer>) null));
assertEquals(
Factory.lead((Field<Integer>) null, 1, (Integer) null),
Factory.lead((Field<Integer>) null, 1, (Field<Integer>) null));
assertEquals(
Factory.least((Integer) null),
Factory.least((Field<Integer>) null));
assertEquals(
Factory.least((Integer) null, (Integer) null),
Factory.least((Field<Integer>) null, (Field<Integer>) null));
assertEquals(
Factory.least((Integer) null, (Integer[]) null),
Factory.least((Field<Integer>) null, (Field[]) null));
assertEquals(
Factory.least((Integer) null, (Integer) null, (Integer) null),
Factory.least((Field<Integer>) null, (Field<Integer>) null, (Field<Integer>) null));
assertEquals(
Factory.length((String) null),
Factory.length((Field<String>) null));