From a42de9aafb766dbce83ade6e1f16bc7fab383a7e Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Mon, 7 Nov 2011 23:00:30 +0000 Subject: [PATCH] [#894] Move functions from Field to new org.jooq.impl.SQL and make them static - moved greatest/least functions --- .../src/org/jooq/test/jOOQAbstractTest.java | 106 +----------------- jOOQ/src/main/java/org/jooq/Field.java | 48 -------- .../java/org/jooq/impl/AbstractField.java | 25 ----- jOOQ/src/main/java/org/jooq/impl/Factory.java | 64 ++++++++++- .../src/main/java/org/jooq/impl/Greatest.java | 11 +- jOOQ/src/main/java/org/jooq/impl/Least.java | 9 +- .../src/test/java/org/jooq/test/jOOQTest.java | 30 ++++- 7 files changed, 98 insertions(+), 195 deletions(-) diff --git a/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java b/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java index ee85f1f787..8f3316d865 100644 --- a/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java +++ b/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java @@ -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)); diff --git a/jOOQ/src/main/java/org/jooq/Field.java b/jOOQ/src/main/java/org/jooq/Field.java index 9bfdb0db5f..fca548d346 100644 --- a/jOOQ/src/main/java/org/jooq/Field.java +++ b/jOOQ/src/main/java/org/jooq/Field.java @@ -336,54 +336,6 @@ public interface Field extends NamedTypeProviderQueryPart, AliasProvider - * This function has no equivalent in Adaptive Server, Derby, SQL Server and - * Sybase SQL Anywhere. Its current simulation implementation has - * O(2^n) complexity and should be avoided for - * n > 5! Better implementation suggestions are very - * welcome. - * - * @see #greatest(Field...) - */ - Field greatest(T... others); - - /** - * Find the greatest among all values - *

- * This function has no equivalent in Adaptive Server, Derby, SQL Server and - * Sybase SQL Anywhere. Its current simulation implementation has - * O(2^n) complexity and should be avoided for - * n > 5! Better implementation suggestions are very - * welcome. - */ - Field greatest(Field... others); - - /** - * Find the least among all values - *

- * This function has no equivalent in Adaptive Server, Derby, SQL Server and - * Sybase SQL Anywhere. Its current simulation implementation has - * O(2^n) complexity and should be avoided for - * n > 5! Better implementation suggestions are very - * welcome. - * - * @see #least(Field...) - */ - Field least(T... others); - - /** - * Find the least among all values - *

- * This function has no equivalent in Adaptive Server, Derby, SQL Server and - * Sybase SQL Anywhere. Its current simulation implementation has - * O(2^n) complexity and should be avoided for - * n > 5! Better implementation suggestions are very - * welcome. - */ - Field least(Field... others); - /** * Gets the Oracle-style * DECODE(expression, search, result[, search , result]... [, default]) diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractField.java b/jOOQ/src/main/java/org/jooq/impl/AbstractField.java index 4e77c9e867..c2005a0982 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractField.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractField.java @@ -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 extends AbstractNamedTypeProviderQueryPart im return new Mod(this, nullSafe(value)); } - // ------------------------------------------------------------------------ - // Functions created from this field - // ------------------------------------------------------------------------ - - @Override - public final Field greatest(T... others) { - return greatest(vals(others).toArray(new Field[0])); - } - - @Override - public final Field greatest(Field... others) { - return new Greatest(getDataType(), nullSafe(combine(this, others))); - } - - @Override - public final Field least(T... others) { - return least(vals(others).toArray(new Field[0])); - } - - @Override - public final Field least(Field... others) { - return new Least(getDataType(), nullSafe(combine(this, others))); - } - // ------------------------------------------------------------------------ // Other functions created from this field // ------------------------------------------------------------------------ diff --git a/jOOQ/src/main/java/org/jooq/impl/Factory.java b/jOOQ/src/main/java/org/jooq/impl/Factory.java index 330e49267b..2a6ec9eab2 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Factory.java +++ b/jOOQ/src/main/java/org/jooq/impl/Factory.java @@ -1304,8 +1304,8 @@ public class Factory implements FactoryOperations { * * @see #coalesce(Field, Field, Field...) */ - public static Field coalesce(T value1, T value2, T... values) { - return coalesce(val(value1), val(value2), vals(values).toArray(new Field[0])); + public static Field coalesce(T value, T... values) { + return coalesce(val(value), vals(values).toArray(new Field[0])); } /** @@ -1319,8 +1319,8 @@ public class Factory implements FactoryOperations { * * */ - public static Field coalesce(Field field1, Field field2, Field... fields) { - return function("coalesce", nullSafeDataType(field1), nullSafe(combine(field1, field2, fields))); + public static Field coalesce(Field 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 + *

+ * This function has no equivalent in Adaptive Server, Derby, SQL Server and + * Sybase SQL Anywhere. Its current simulation implementation has + * O(2^n) complexity and should be avoided for + * n > 5! Better implementation suggestions are very + * welcome. + * + * @see #greatest(Field...) + */ + public static Field greatest(T value, T... values) { + return greatest(val(value), vals(values).toArray(new Field[0])); + } + + /** + * Find the greatest among all values + *

+ * This function has no equivalent in Adaptive Server, Derby, SQL Server and + * Sybase SQL Anywhere. Its current simulation implementation has + * O(2^n) complexity and should be avoided for + * n > 5! Better implementation suggestions are very + * welcome. + */ + public static Field greatest(Field field, Field... others) { + return new Greatest(nullSafeDataType(field), nullSafe(combine(field, others))); + } + + /** + * Find the least among all values + *

+ * This function has no equivalent in Adaptive Server, Derby, SQL Server and + * Sybase SQL Anywhere. Its current simulation implementation has + * O(2^n) complexity and should be avoided for + * n > 5! Better implementation suggestions are very + * welcome. + * + * @see #least(Field...) + */ + public static Field least(T value, T... values) { + return least(val(value), vals(values).toArray(new Field[0])); + } + + /** + * Find the least among all values + *

+ * This function has no equivalent in Adaptive Server, Derby, SQL Server and + * Sybase SQL Anywhere. Its current simulation implementation has + * O(2^n) complexity and should be avoided for + * n > 5! Better implementation suggestions are very + * welcome. + */ + public static Field least(Field field, Field... others) { + return new Least(nullSafeDataType(field), nullSafe(combine(field, others))); + } + /** * Get the sign of a numeric field: sign(field) * diff --git a/jOOQ/src/main/java/org/jooq/impl/Greatest.java b/jOOQ/src/main/java/org/jooq/impl/Greatest.java index 10a14ad751..1b42252788 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Greatest.java +++ b/jOOQ/src/main/java/org/jooq/impl/Greatest.java @@ -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 extends AbstractFunction { Field other = (Field) 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); } } diff --git a/jOOQ/src/main/java/org/jooq/impl/Least.java b/jOOQ/src/main/java/org/jooq/impl/Least.java index 21716c0ffb..c54edf42fd 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Least.java +++ b/jOOQ/src/main/java/org/jooq/impl/Least.java @@ -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 extends AbstractFunction { 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); } } diff --git a/jOOQ/src/test/java/org/jooq/test/jOOQTest.java b/jOOQ/src/test/java/org/jooq/test/jOOQTest.java index 3863531f73..65a3af3a7f 100644 --- a/jOOQ/src/test/java/org/jooq/test/jOOQTest.java +++ b/jOOQ/src/test/java/org/jooq/test/jOOQTest.java @@ -209,15 +209,9 @@ public class jOOQTest { assertEquals( FIELD_ID1.greaterThan((Integer) null), FIELD_ID1.greaterThan((Field) null)); - assertEquals( - FIELD_ID1.greatest((Integer) null), - FIELD_ID1.greatest((Field) null)); assertEquals( FIELD_ID1.in((Integer) null), FIELD_ID1.in((Field) null)); - assertEquals( - FIELD_ID1.least((Integer) null), - FIELD_ID1.least((Field) null)); assertEquals( FIELD_ID1.lessOrEqual((Integer) null), FIELD_ID1.lessOrEqual((Field) null)); @@ -371,12 +365,36 @@ public class jOOQTest { assertEquals( Factory.floor((Integer) null), Factory.floor((Field) null)); + assertEquals( + Factory.greatest((Integer) null), + Factory.greatest((Field) null)); + assertEquals( + Factory.greatest((Integer) null, (Integer[]) null), + Factory.greatest((Field) null, (Field[]) null)); + assertEquals( + Factory.greatest((Integer) null, (Integer) null, (Integer) null), + Factory.greatest((Field) null, (Field) null, (Field) null)); + assertEquals( + Factory.greatest((Integer) null, (Integer) null), + Factory.greatest((Field) null, (Field) null)); assertEquals( Factory.lag((Field) null, 1, (Integer) null), Factory.lag((Field) null, 1, (Field) null)); assertEquals( Factory.lead((Field) null, 1, (Integer) null), Factory.lead((Field) null, 1, (Field) null)); + assertEquals( + Factory.least((Integer) null), + Factory.least((Field) null)); + assertEquals( + Factory.least((Integer) null, (Integer) null), + Factory.least((Field) null, (Field) null)); + assertEquals( + Factory.least((Integer) null, (Integer[]) null), + Factory.least((Field) null, (Field[]) null)); + assertEquals( + Factory.least((Integer) null, (Integer) null, (Integer) null), + Factory.least((Field) null, (Field) null, (Field) null)); assertEquals( Factory.length((String) null), Factory.length((Field) null));