[#894] Move functions from Field<?> to Factory and make them static - moved math functions

This commit is contained in:
Lukas Eder 2011-11-04 11:02:45 +00:00
parent 0c4a09451e
commit da9b69ea98
19 changed files with 761 additions and 501 deletions

View File

@ -37,6 +37,7 @@ package org.jooq.test.h2;
import static org.jooq.impl.Factory.count;
import static org.jooq.impl.Factory.max;
import static org.jooq.impl.Factory.sign;
import static org.jooq.impl.SQLDataType.INTEGER;
import static org.jooq.test.h2.generatedclasses.tables.TAuthor.T_AUTHOR;
import static org.jooq.test.h2.generatedclasses.tables.TBook.T_BOOK;
@ -98,7 +99,7 @@ public class F {
H2Factory create = create(connection);
Integer result =
create.select(count().sign())
create.select(sign(count()))
.from(T_AUTHOR)
.where(TAuthor.FIRST_NAME.equal(authorName))
.or(TAuthor.LAST_NAME.equal(authorName))

View File

@ -47,9 +47,19 @@ 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.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.cast;
import static org.jooq.impl.Factory.castNull;
import static org.jooq.impl.Factory.ceil;
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.countOver;
@ -60,28 +70,42 @@ 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.denseRankOver;
import static org.jooq.impl.Factory.e;
import static org.jooq.impl.Factory.exp;
import static org.jooq.impl.Factory.falseCondition;
import static org.jooq.impl.Factory.field;
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.ln;
import static org.jooq.impl.Factory.log;
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.one;
import static org.jooq.impl.Factory.percentRankOver;
import static org.jooq.impl.Factory.pi;
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.rankOver;
import static org.jooq.impl.Factory.rollup;
import static org.jooq.impl.Factory.round;
import static org.jooq.impl.Factory.rowNumberOver;
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.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.trueCondition;
import static org.jooq.impl.Factory.two;
import static org.jooq.impl.Factory.val;
@ -5233,23 +5257,23 @@ public abstract class jOOQAbstractTest<
assertNotNull(rand);
// Some rounding functions
Field<Float> f1a = val(1.111f).round();
Field<Float> f2a = val(1.111f).round(2);
Field<Float> f3a = val(1.111f).floor();
Field<Float> f4a = val(1.111f).ceil();
Field<Double> f1b = val(-1.111).round();
Field<Double> f2b = val(-1.111).round(2);
Field<Double> f3b = val(-1.111).floor();
Field<Double> f4b = val(-1.111).ceil();
Field<Float> f1a = round(1.111f);
Field<Float> f2a = round(1.111f, 2);
Field<Float> f3a = floor(1.111f);
Field<Float> f4a = ceil(1.111f);
Field<Double> f1b = round(-1.111);
Field<Double> f2b = round(-1.111, 2);
Field<Double> f3b = floor(-1.111);
Field<Double> f4b = ceil(-1.111);
Field<Float> f1c = val(2.0f).round();
Field<Float> f2c = val(2.0f).round(2);
Field<Float> f3c = val(2.0f).floor();
Field<Float> f4c = val(2.0f).ceil();
Field<Double> f1d = val(-2.0).round();
Field<Double> f2d = val(-2.0).round(2);
Field<Double> f3d = val(-2.0).floor();
Field<Double> f4d = val(-2.0).ceil();
Field<Float> f1c = round(2.0f);
Field<Float> f2c = round(2.0f, 2);
Field<Float> f3c = floor(2.0f);
Field<Float> f4c = ceil(2.0f);
Field<Double> f1d = round(-2.0);
Field<Double> f2d = round(-2.0, 2);
Field<Double> f3d = floor(-2.0);
Field<Double> f4d = ceil(-2.0);
// Some arbitrary checks on having multiple select clauses
Record record =
@ -5302,15 +5326,15 @@ public abstract class jOOQAbstractTest<
default: {
// Exponentials, logarithms and roots
// ----------------------------------
Field<BigDecimal> m1 = val(2).sqrt();
Field<BigDecimal> m2 = val(4).sqrt().round();
Field<BigDecimal> m3 = val(2).exp();
Field<BigDecimal> m4 = val(0).exp().round();
Field<BigDecimal> m5 = val(-2).exp();
Field<BigDecimal> m6 = val(2).ln();
Field<BigDecimal> m7 = val(16).log(4).round();
Field<BigDecimal> m8 = val(2).power(4).round();
Field<BigDecimal> m9 = val(2).sqrt().power(2).sqrt().power(2).round();
Field<BigDecimal> m1 = sqrt(2);
Field<BigDecimal> m2 = round(sqrt(4));
Field<BigDecimal> m3 = exp(2);
Field<BigDecimal> m4 = round(exp(0));
Field<BigDecimal> m5 = exp(-2);
Field<BigDecimal> m6 = ln(2);
Field<BigDecimal> m7 = round(log(16, 4));
Field<BigDecimal> m8 = round(power(2, 4));
Field<BigDecimal> m9 = round(power(sqrt(power(sqrt(2), 2)), 2));
record = create().select(m1, m2, m3, m4, m5, m6, m7, m8, m9).fetchOne();
@ -5328,22 +5352,22 @@ public abstract class jOOQAbstractTest<
// Trigonometry
// ------------
Field<BigDecimal> t1 = val(Math.PI / 6 + 0.00001).sin();
Field<BigDecimal> t2 = val(Math.PI / 6).cos();
Field<BigDecimal> t3 = val(Math.PI / 6).tan();
Field<BigDecimal> t4 = val(Math.PI / 6).cot();
Field<BigDecimal> t6 = val(1.1).deg().rad();
Field<BigDecimal> t7 = val(Math.PI / 6).asin();
Field<BigDecimal> t8 = val(Math.PI / 6).acos();
Field<BigDecimal> t9 = val(Math.PI / 6).atan();
Field<BigDecimal> ta = val(1).atan2(1).deg().round();
Field<BigDecimal> t1 = sin(Math.PI / 6 + 0.00001);
Field<BigDecimal> t2 = cos(Math.PI / 6);
Field<BigDecimal> t3 = tan(Math.PI / 6);
Field<BigDecimal> t4 = cot(Math.PI / 6);
Field<BigDecimal> t6 = rad(deg(1.1));
Field<BigDecimal> t7 = asin(Math.PI / 6);
Field<BigDecimal> t8 = acos(Math.PI / 6);
Field<BigDecimal> t9 = atan(Math.PI / 6);
Field<BigDecimal> ta = round(deg(atan2(1, 1)));
// Hyperbolic functions
// --------------------
Field<BigDecimal> tb = val(1.0).sinh()
.div(val(1.0).cosh())
.mul(val(1.0).tanh())
.mul(val(1.0).coth().power(2).add(0.1));
Field<BigDecimal> tb = sinh(1.0)
.div(cosh(1.0))
.mul(tanh(1.0))
.mul(power(coth(1.0), 2).add(0.1));
record = create().select(t1, t2, t3, t4, t6, t7, t8, t9, ta, tb).fetchOne();
@ -5366,11 +5390,11 @@ public abstract class jOOQAbstractTest<
// The sign function
record = create().select(
val(2).sign(),
val(1).sign(),
val(0).sign(),
val(-1).sign(),
val(-2).sign()).fetchOne();
sign(2),
sign(1),
sign(0),
sign(-1),
sign(-2)).fetchOne();
assertNotNull(record);
assertEquals(Integer.valueOf(1), record.getValue(0));
@ -5381,11 +5405,11 @@ public abstract class jOOQAbstractTest<
// The abs function
record = create().select(
val(2).abs(),
val(1).abs(),
val(0).abs(),
val(-1).abs(),
val(-2).abs()).fetchOne();
abs(2),
abs(1),
abs(0),
abs(-1),
abs(-2)).fetchOne();
assertNotNull(record);
assertEquals(Integer.valueOf(2), record.getValue(0));

View File

@ -560,262 +560,6 @@ public interface Field<T> extends NamedTypeProviderQueryPart<T>, AliasProvider<F
*/
Field<T> shr(Field<? extends Number> value);
// ------------------------------------------------------------------------
// Mathematical functions created from this field
// ------------------------------------------------------------------------
/**
* Get the sign of a numeric field: sign(field)
* <p>
* This renders the sign function where available:
* <code><pre>sign([this])</pre></code>
* ... or simulates it elsewhere (without bind variables on values -1, 0, 1):
* <code><pre>
* CASE WHEN [this] > 0 THEN 1
* WHEN [this] < 0 THEN -1
* ELSE 0
* END
*/
Field<Integer> sign();
/**
* Get the absolute value of a numeric field: abs(field)
* <p>
* This renders the same on all dialects:
* <code><pre>abs([this])</pre></code>
*/
Field<T> abs();
/**
* Get rounded value of a numeric field: round(field)
* <p>
* This renders the round function where available:
* <code><pre>round([this]) or
* round([this], 0)</pre></code>
* ... or simulates it elsewhere using floor and ceil
*/
Field<T> round();
/**
* Get rounded value of a numeric field: round(field, decimals)
* <p>
* This renders the round function where available:
* <code><pre>round([this], [decimals])</pre></code>
* ... or simulates it elsewhere using floor and ceil
*/
Field<T> round(int decimals);
/**
* Get the largest integer value not greater than [this]
* <p>
* This renders the floor function where available:
* <code><pre>floor([this])</pre></code>
* ... or simulates it elsewhere using round:
* <code><pre>round([this] - 0.499999999999999)</pre></code>
*/
Field<T> floor();
/**
* Get the smallest integer value not less than [this]
* <p>
* This renders the ceil or ceiling function where available:
* <code><pre>ceil([this]) or
* ceiling([this])</pre></code>
* ... or simulates it elsewhere using round:
* <code><pre>round([this] + 0.499999999999999)</pre></code>
*/
Field<T> ceil();
/**
* Get the sqrt(field) function
* <p>
* This renders the sqrt function where available:
* <code><pre>sqrt([this])</pre></code> ... or simulates it elsewhere using
* power (which in turn may also be simulated using ln and exp functions):
* <code><pre>power([this], 0.5)</pre></code>
*/
Field<BigDecimal> sqrt();
/**
* Get the exp(field) function, taking this field as the power of e
* <p>
* This renders the same on all dialects:
* <code><pre>exp([this])</pre></code>
*/
Field<BigDecimal> exp();
/**
* Get the ln(field) function, taking the natural logarithm of this field
* <p>
* This renders the ln or log function where available:
* <code><pre>ln([this]) or
* log([this])</pre></code>
*/
Field<BigDecimal> ln();
/**
* Get the log(field, base) function
* <p>
* This renders the log function where available:
* <code><pre>log([this])</pre></code> ... or simulates it elsewhere (in
* most RDBMS) using the natural logarithm:
* <code><pre>ln([this]) / ln([base])</pre></code>
*/
Field<BigDecimal> log(int base);
/**
* Get the power(field, exponent) function
* <p>
* This renders the power function where available:
* <code><pre>power([this], [exponent])</pre></code> ... or simulates it
* elsewhere using ln and exp:
* <code><pre>exp(ln([this]) * [exponent])</pre></code>
*/
Field<BigDecimal> power(Number exponent);
/**
* Get the power(field, exponent) function
* <p>
* This renders the power function where available:
* <code><pre>power([this], [exponent])</pre></code> ... or simulates it
* elsewhere using ln and exp:
* <code><pre>exp(ln([this]) * [exponent])</pre></code>
*/
Field<BigDecimal> power(Field<? extends Number> exponent);
/**
* Get the arc cosine(field) function
* <p>
* This renders the acos function where available:
* <code><pre>acos([this])</pre></code>
*/
Field<BigDecimal> acos();
/**
* Get the arc sine(field) function
* <p>
* This renders the asin function where available:
* <code><pre>asin([this])</pre></code>
*/
Field<BigDecimal> asin();
/**
* Get the arc tangent(field) function
* <p>
* This renders the atan function where available:
* <code><pre>atan([this])</pre></code>
*/
Field<BigDecimal> atan();
/**
* Get the arc tangent 2(field, y) function
* <p>
* This renders the atan2 or atn2 function where available:
* <code><pre>atan2([this]) or
* atn2([this])</pre></code>
*/
Field<BigDecimal> atan2(Number y);
/**
* Get the arc tangent 2(field, y) function
* <p>
* This renders the atan2 or atn2 function where available:
* <code><pre>atan2([this]) or
* atn2([this])</pre></code>
*/
Field<BigDecimal> atan2(Field<? extends Number> y);
/**
* Get the cosine(field) function
* <p>
* This renders the cos function where available:
* <code><pre>cos([this])</pre></code>
*/
Field<BigDecimal> cos();
/**
* Get the sine(field) function
* <p>
* This renders the sin function where available:
* <code><pre>sin([this])</pre></code>
*/
Field<BigDecimal> sin();
/**
* Get the tangent(field) function
* <p>
* This renders the tan function where available:
* <code><pre>tan([this])</pre></code>
*/
Field<BigDecimal> tan();
/**
* Get the cotangent(field) function
* <p>
* This renders the cot function where available:
* <code><pre>cot([this])</pre></code> ... or simulates it elsewhere using
* sin and cos: <code><pre>cos([this]) / sin([this])</pre></code>
*/
Field<BigDecimal> cot();
/**
* Get the hyperbolic sine function: sinh(field)
* <p>
* This renders the sinh function where available:
* <code><pre>sinh([this])</pre></code> ... or simulates it elsewhere using
* exp: <code><pre>(exp([this] * 2) - 1) / (exp([this] * 2))</pre></code>
*/
Field<BigDecimal> sinh();
/**
* Get the hyperbolic cosine function: cosh(field)
* <p>
* This renders the cosh function where available:
* <code><pre>cosh([this])</pre></code> ... or simulates it elsewhere using
* exp: <code><pre>(exp([this] * 2) + 1) / (exp([this] * 2))</pre></code>
*/
Field<BigDecimal> cosh();
/**
* Get the hyperbolic tangent function: tanh(field)
* <p>
* This renders the tanh function where available:
* <code><pre>tanh([this])</pre></code> ... or simulates it elsewhere using
* exp:
* <code><pre>(exp([this] * 2) - 1) / (exp([this] * 2) + 1)</pre></code>
*/
Field<BigDecimal> tanh();
/**
* Get the hyperbolic cotangent function: coth(field)
* <p>
* This is not supported by any RDBMS, but simulated using exp exp:
* <code><pre>(exp([this] * 2) + 1) / (exp([this] * 2) - 1)</pre></code>
*/
Field<BigDecimal> coth();
/**
* Calculate degrees from radians from this field
* <p>
* This renders the degrees function where available:
* <code><pre>degrees([this])</pre></code> ... or simulates it elsewhere:
* <code><pre>[this] * 180 / PI</pre></code>
*/
Field<BigDecimal> deg();
/**
* Calculate radians from degrees from this field
* <p>
* This renders the degrees function where available:
* <code><pre>degrees([this])</pre></code> ... or simulates it elsewhere:
* <code><pre>[this] * PI / 180</pre></code>
*/
Field<BigDecimal> rad();
// ------------------------------------------------------------------------
// Aggregate functions created from this field
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// Analytic (or window) functions created from this field
// ------------------------------------------------------------------------

View File

@ -491,40 +491,6 @@ abstract class AbstractField<T> extends AbstractNamedTypeProviderQueryPart<T> im
// Functions created from this field
// ------------------------------------------------------------------------
@Override
public final Field<Integer> sign() {
return new Sign(this);
}
@Override
public final Field<T> abs() {
return function("abs", getDataType(), this);
}
@Override
public final Field<T> round() {
return new Round<T>(this);
}
@Override
public final Field<T> round(int decimals) {
return new Round<T>(this, decimals);
}
@Override
public final Field<T> floor() {
return new Floor<T>(this);
}
@Override
public final Field<T> ceil() {
return new Ceil<T>(this);
}
// ------------------------------------------------------------------------
// Mathematical functions created from this field
// ------------------------------------------------------------------------
@Override
public final Field<T> greatest(T... others) {
return greatest(vals(others).toArray(new Field<?>[0]));
@ -545,115 +511,6 @@ abstract class AbstractField<T> extends AbstractNamedTypeProviderQueryPart<T> im
return new Least<T>(getDataType(), JooqUtil.combine(this, others));
}
@Override
public final Field<BigDecimal> sqrt() {
return new Sqrt(this);
}
@Override
public final Field<BigDecimal> exp() {
return function("exp", SQLDataType.NUMERIC, this);
}
@Override
public final Field<BigDecimal> ln() {
return new Ln(this);
}
@Override
public final Field<BigDecimal> log(int base) {
return new Ln(this, base);
}
@Override
public final Field<BigDecimal> power(Number exponent) {
return power(val(exponent));
}
@Override
public Field<BigDecimal> power(Field<? extends Number> exponent) {
return new Power(this, exponent);
}
@Override
public final Field<BigDecimal> acos() {
return function("acos", SQLDataType.NUMERIC, this);
}
@Override
public final Field<BigDecimal> asin() {
return function("asin", SQLDataType.NUMERIC, this);
}
@Override
public final Field<BigDecimal> atan() {
return function("atan", SQLDataType.NUMERIC, this);
}
@Override
public final Field<BigDecimal> atan2(Number y) {
return atan2(val(y));
}
@Override
public final Field<BigDecimal> atan2(Field<? extends Number> y) {
if (y == null) {
return atan2((Number) null);
}
return new Function<BigDecimal>(Term.ATAN2, SQLDataType.NUMERIC, this, y);
}
@Override
public final Field<BigDecimal> cos() {
return function("cos", SQLDataType.NUMERIC, this);
}
@Override
public final Field<BigDecimal> sin() {
return function("sin", SQLDataType.NUMERIC, this);
}
@Override
public final Field<BigDecimal> tan() {
return function("tan", SQLDataType.NUMERIC, this);
}
@Override
public final Field<BigDecimal> cot() {
return new Cot(this);
}
@Override
public final Field<BigDecimal> sinh() {
return new Sinh(this);
}
@Override
public final Field<BigDecimal> cosh() {
return new Cosh(this);
}
@Override
public final Field<BigDecimal> tanh() {
return new Tanh(this);
}
@Override
public final Field<BigDecimal> coth() {
return mul(2).exp().add(1).div(mul(2).exp().sub(1));
}
@Override
public final Field<BigDecimal> deg() {
return new Degrees(this);
}
@Override
public final Field<BigDecimal> rad() {
return new Radians(this);
}
// ------------------------------------------------------------------------
// Other functions created from this field
// ------------------------------------------------------------------------

View File

@ -36,6 +36,7 @@
package org.jooq.impl;
import static org.jooq.impl.Factory.function;
import static org.jooq.impl.Factory.round;
import org.jooq.Configuration;
import org.jooq.Field;
@ -43,7 +44,7 @@ import org.jooq.Field;
/**
* @author Lukas Eder
*/
class Ceil<T> extends AbstractFunction<T> {
class Ceil<T extends Number> extends AbstractFunction<T> {
/**
* Generated UID
@ -64,7 +65,7 @@ class Ceil<T> extends AbstractFunction<T> {
// evaluate "ceil" if unavailable
case SQLITE:
return argument.add(0.499999999999999).round();
return round(argument.add(0.499999999999999));
case ASE:
case H2:

View File

@ -35,6 +35,7 @@
*/
package org.jooq.impl;
import static org.jooq.impl.Factory.exp;
import static org.jooq.impl.Factory.function;
import static org.jooq.impl.Factory.one;
import static org.jooq.impl.Factory.two;
@ -52,11 +53,11 @@ class Cosh extends AbstractFunction<BigDecimal> {
/**
* Generated UID
*/
private static final long serialVersionUID = -7273879239726265322L;
private static final long serialVersionUID = -7273879239726265322L;
private final Field<?> argument;
private final Field<? extends Number> argument;
Cosh(Field<?> argument) {
Cosh(Field<? extends Number> argument) {
super("cosh", SQLDataType.NUMERIC, argument);
this.argument = argument;
@ -72,7 +73,7 @@ class Cosh extends AbstractFunction<BigDecimal> {
case POSTGRES:
case SQLSERVER:
case SYBASE:
return argument.mul(two()).exp().add(one()).div(argument.exp().mul(two()));
return exp(argument.mul(two())).add(one()).div(exp(argument).mul(two()));
default:
return function("cosh", SQLDataType.NUMERIC, argument);

View File

@ -35,7 +35,9 @@
*/
package org.jooq.impl;
import static org.jooq.impl.Factory.cos;
import static org.jooq.impl.Factory.function;
import static org.jooq.impl.Factory.sin;
import java.math.BigDecimal;
@ -50,11 +52,11 @@ class Cot extends AbstractFunction<BigDecimal> {
/**
* Generated UID
*/
private static final long serialVersionUID = -7273879239726265322L;
private static final long serialVersionUID = -7273879239726265322L;
private final Field<?> argument;
private final Field<? extends Number> argument;
Cot(Field<?> argument) {
Cot(Field<? extends Number> argument) {
super("cot", SQLDataType.NUMERIC, argument);
this.argument = argument;
@ -65,7 +67,7 @@ class Cot extends AbstractFunction<BigDecimal> {
switch (configuration.getDialect()) {
case INGRES:
case ORACLE:
return argument.cos().div(argument.sin());
return cos(argument).div(sin(argument));
default:
return function("cot", SQLDataType.NUMERIC, argument);

View File

@ -35,6 +35,7 @@
*/
package org.jooq.impl;
import static org.jooq.impl.Factory.exp;
import static org.jooq.impl.Factory.function;
import static org.jooq.impl.Factory.literal;
import static org.jooq.impl.Factory.one;
@ -72,7 +73,7 @@ class Euler extends AbstractFunction<BigDecimal> {
case POSTGRES:
case SQLSERVER:
case SYBASE:
return one().exp();
return exp(one());
case SQLITE:
return literal(Math.E, BigDecimal.class);

View File

@ -56,6 +56,7 @@ import static org.jooq.impl.ExpressionOperator.SHL;
import static org.jooq.impl.ExpressionOperator.SHR;
import static org.jooq.impl.Factory.function;
import static org.jooq.impl.Factory.literal;
import static org.jooq.impl.Factory.power;
import java.sql.SQLException;
import java.util.Arrays;
@ -136,10 +137,10 @@ class Expression<T> extends AbstractFunction<T> {
// Many dialects don't support shifts. Use multiplication/division instead
else if (SHL == operator && asList(ASE, DB2, H2, HSQLDB, INGRES, ORACLE, SQLSERVER, SYBASE).contains(dialect)) {
return lhs.mul(literal(2).power(rhsAsNumber()));
return lhs.mul(power(literal(2), rhsAsNumber()));
}
else if (SHR == operator && asList(ASE, DB2, H2, HSQLDB, INGRES, ORACLE, SQLSERVER, SYBASE).contains(dialect)) {
return lhs.div(literal(2).power(rhsAsNumber()));
return lhs.div(power(literal(2), rhsAsNumber()));
}
// These operators are not supported in any dialect

View File

@ -1759,6 +1759,10 @@ public class Factory implements Configuration {
return (Field<T>[]) castFields;
}
// ------------------------------------------------------------------------
// Construction of special grouping functions
// ------------------------------------------------------------------------
/**
* Create a ROLLUP(field1, field2, .., fieldn) grouping field
* <p>
@ -1950,6 +1954,619 @@ public class Factory implements Configuration {
return function("grouping_id", Integer.class, fields);
}
// ------------------------------------------------------------------------
// Mathematical functions
// ------------------------------------------------------------------------
/**
* Get the sign of a numeric field: sign(field)
* <p>
* This renders the sign function where available:
* <code><pre>sign([this])</pre></code>
* ... or simulates it elsewhere (without bind variables on values -1, 0, 1):
* <code><pre>
* CASE WHEN [this] > 0 THEN 1
* WHEN [this] < 0 THEN -1
* ELSE 0
* END
*/
public static Field<Integer> sign(Number value) {
return sign(val(value));
}
/**
* Get the sign of a numeric field: sign(field)
* <p>
* This renders the sign function where available:
* <code><pre>sign([this])</pre></code>
* ... or simulates it elsewhere (without bind variables on values -1, 0, 1):
* <code><pre>
* CASE WHEN [this] > 0 THEN 1
* WHEN [this] < 0 THEN -1
* ELSE 0
* END
*/
public static Field<Integer> sign(Field<? extends Number> field) {
return new Sign(field);
}
/**
* Get the absolute value of a numeric field: abs(field)
* <p>
* This renders the same on all dialects:
* <code><pre>abs([this])</pre></code>
*/
public static <T extends Number> Field<T> abs(T value) {
return abs(val(value));
}
/**
* Get the absolute value of a numeric field: abs(field)
* <p>
* This renders the same on all dialects:
* <code><pre>abs([this])</pre></code>
*/
public static <T extends Number> Field<T> abs(Field<T> field) {
return function("abs", field.getDataType(), field);
}
/**
* Get rounded value of a numeric field: round(field)
* <p>
* This renders the round function where available:
* <code><pre>round([this]) or
* round([this], 0)</pre></code>
* ... or simulates it elsewhere using floor and ceil
*/
public static <T extends Number> Field<T> round(T value) {
return round(val(value));
}
/**
* Get rounded value of a numeric field: round(field)
* <p>
* This renders the round function where available:
* <code><pre>round([this]) or
* round([this], 0)</pre></code>
* ... or simulates it elsewhere using floor and ceil
*/
public static <T extends Number> Field<T> round(Field<T> field) {
return new Round<T>(field);
}
/**
* Get rounded value of a numeric field: round(field, decimals)
* <p>
* This renders the round function where available:
* <code><pre>round([this], [decimals])</pre></code>
* ... or simulates it elsewhere using floor and ceil
*/
public static <T extends Number> Field<T> round(T value, int decimals) {
return round(val(value), decimals);
}
/**
* Get rounded value of a numeric field: round(field, decimals)
* <p>
* This renders the round function where available:
* <code><pre>round([this], [decimals])</pre></code>
* ... or simulates it elsewhere using floor and ceil
*/
public static <T extends Number> Field<T> round(Field<T> field, int decimals) {
return new Round<T>(field, decimals);
}
/**
* Get the largest integer value not greater than [this]
* <p>
* This renders the floor function where available:
* <code><pre>floor([this])</pre></code>
* ... or simulates it elsewhere using round:
* <code><pre>round([this] - 0.499999999999999)</pre></code>
*/
public static <T extends Number> Field<T> floor(T value) {
return floor(val(value));
}
/**
* Get the largest integer value not greater than [this]
* <p>
* This renders the floor function where available:
* <code><pre>floor([this])</pre></code>
* ... or simulates it elsewhere using round:
* <code><pre>round([this] - 0.499999999999999)</pre></code>
*/
public static <T extends Number> Field<T> floor(Field<T> field) {
return new Floor<T>(field);
}
/**
* Get the smallest integer value not less than [this]
* <p>
* This renders the ceil or ceiling function where available:
* <code><pre>ceil([this]) or
* ceiling([this])</pre></code>
* ... or simulates it elsewhere using round:
* <code><pre>round([this] + 0.499999999999999)</pre></code>
*/
public static <T extends Number> Field<T> ceil(T value) {
return ceil(val(value));
}
/**
* Get the smallest integer value not less than [this]
* <p>
* This renders the ceil or ceiling function where available:
* <code><pre>ceil([this]) or
* ceiling([this])</pre></code>
* ... or simulates it elsewhere using round:
* <code><pre>round([this] + 0.499999999999999)</pre></code>
*/
public static <T extends Number> Field<T> ceil(Field<T> field) {
return new Ceil<T>(field);
}
/**
* Get the sqrt(field) function
* <p>
* This renders the sqrt function where available:
* <code><pre>sqrt([this])</pre></code> ... or simulates it elsewhere using
* power (which in turn may also be simulated using ln and exp functions):
* <code><pre>power([this], 0.5)</pre></code>
*/
public static Field<BigDecimal> sqrt(Number value) {
return sqrt(val(value));
}
/**
* Get the sqrt(field) function
* <p>
* This renders the sqrt function where available:
* <code><pre>sqrt([this])</pre></code> ... or simulates it elsewhere using
* power (which in turn may also be simulated using ln and exp functions):
* <code><pre>power([this], 0.5)</pre></code>
*/
public static Field<BigDecimal> sqrt(Field<? extends Number> field) {
return new Sqrt(field);
}
/**
* Get the exp(field) function, taking this field as the power of e
* <p>
* This renders the same on all dialects:
* <code><pre>exp([this])</pre></code>
*/
public static Field<BigDecimal> exp(Number value) {
return exp(val(value));
}
/**
* Get the exp(field) function, taking this field as the power of e
* <p>
* This renders the same on all dialects:
* <code><pre>exp([this])</pre></code>
*/
public static Field<BigDecimal> exp(Field<? extends Number> field) {
return function("exp", SQLDataType.NUMERIC, field);
}
/**
* Get the ln(field) function, taking the natural logarithm of this field
* <p>
* This renders the ln or log function where available:
* <code><pre>ln([this]) or
* log([this])</pre></code>
*/
public static Field<BigDecimal> ln(Number value) {
return ln(val(value));
}
/**
* Get the ln(field) function, taking the natural logarithm of this field
* <p>
* This renders the ln or log function where available:
* <code><pre>ln([this]) or
* log([this])</pre></code>
*/
public static Field<BigDecimal> ln(Field<? extends Number> field) {
return new Ln(field);
}
/**
* Get the log(field, base) function
* <p>
* This renders the log function where available:
* <code><pre>log([this])</pre></code> ... or simulates it elsewhere (in
* most RDBMS) using the natural logarithm:
* <code><pre>ln([this]) / ln([base])</pre></code>
*/
public static Field<BigDecimal> log(Number value, int base) {
return log(val(value), base);
}
/**
* Get the log(field, base) function
* <p>
* This renders the log function where available:
* <code><pre>log([this])</pre></code> ... or simulates it elsewhere (in
* most RDBMS) using the natural logarithm:
* <code><pre>ln([this]) / ln([base])</pre></code>
*/
public static Field<BigDecimal> log(Field<? extends Number> field, int base) {
return new Ln(field, base);
}
/**
* Get the power(field, exponent) function
* <p>
* This renders the power function where available:
* <code><pre>power([this], [exponent])</pre></code> ... or simulates it
* elsewhere using ln and exp:
* <code><pre>exp(ln([this]) * [exponent])</pre></code>
*/
public static Field<BigDecimal> power(Number value, Number exponent) {
return power(val(value), val(exponent));
}
/**
* Get the power(field, exponent) function
* <p>
* This renders the power function where available:
* <code><pre>power([this], [exponent])</pre></code> ... or simulates it
* elsewhere using ln and exp:
* <code><pre>exp(ln([this]) * [exponent])</pre></code>
*/
public static Field<BigDecimal> power(Field<? extends Number> field, Number exponent) {
return power(field, val(exponent));
}
/**
* Get the power(field, exponent) function
* <p>
* This renders the power function where available:
* <code><pre>power([this], [exponent])</pre></code> ... or simulates it
* elsewhere using ln and exp:
* <code><pre>exp(ln([this]) * [exponent])</pre></code>
*/
public static Field<BigDecimal> power(Number value, Field<? extends Number> exponent) {
return power(val(value), exponent);
}
/**
* Get the power(field, exponent) function
* <p>
* This renders the power function where available:
* <code><pre>power([this], [exponent])</pre></code> ... or simulates it
* elsewhere using ln and exp:
* <code><pre>exp(ln([this]) * [exponent])</pre></code>
*/
public static Field<BigDecimal> power(Field<? extends Number> field, Field<? extends Number> exponent) {
return new Power(field, exponent);
}
/**
* Get the arc cosine(field) function
* <p>
* This renders the acos function where available:
* <code><pre>acos([this])</pre></code>
*/
public static Field<BigDecimal> acos(Number value) {
return acos(val(value));
}
/**
* Get the arc cosine(field) function
* <p>
* This renders the acos function where available:
* <code><pre>acos([this])</pre></code>
*/
public static Field<BigDecimal> acos(Field<? extends Number> field) {
return function("acos", SQLDataType.NUMERIC, field);
}
/**
* Get the arc sine(field) function
* <p>
* This renders the asin function where available:
* <code><pre>asin([this])</pre></code>
*/
public static Field<BigDecimal> asin(Number value) {
return asin(val(value));
}
/**
* Get the arc sine(field) function
* <p>
* This renders the asin function where available:
* <code><pre>asin([this])</pre></code>
*/
public static Field<BigDecimal> asin(Field<? extends Number> field) {
return function("asin", SQLDataType.NUMERIC, field);
}
/**
* Get the arc tangent(field) function
* <p>
* This renders the atan function where available:
* <code><pre>atan([this])</pre></code>
*/
public static Field<BigDecimal> atan(Number value) {
return atan(val(value));
}
/**
* Get the arc tangent(field) function
* <p>
* This renders the atan function where available:
* <code><pre>atan([this])</pre></code>
*/
public static Field<BigDecimal> atan(Field<? extends Number> field) {
return function("atan", SQLDataType.NUMERIC, field);
}
/**
* Get the arc tangent 2(field, y) function
* <p>
* This renders the atan2 or atn2 function where available:
* <code><pre>atan2([this]) or
* atn2([this])</pre></code>
*/
public static Field<BigDecimal> atan2(Number x, Number y) {
return atan2(val(x), val(y));
}
/**
* Get the arc tangent 2(field, y) function
* <p>
* This renders the atan2 or atn2 function where available:
* <code><pre>atan2([this]) or
* atn2([this])</pre></code>
*/
public static Field<BigDecimal> atan2(Field<? extends Number> x, Number y) {
return atan2(x, val(y));
}
/**
* Get the arc tangent 2(field, y) function
* <p>
* This renders the atan2 or atn2 function where available:
* <code><pre>atan2([this]) or
* atn2([this])</pre></code>
*/
public static Field<BigDecimal> atan2(Number x, Field<? extends Number> y) {
return atan2(val(x), y);
}
/**
* Get the arc tangent 2(field, y) function
* <p>
* This renders the atan2 or atn2 function where available:
* <code><pre>atan2([this]) or
* atn2([this])</pre></code>
*/
public static Field<BigDecimal> atan2(Field<? extends Number> x, Field<? extends Number> y) {
if (y == null) {
return atan2(x, (Number) null);
}
return new Function<BigDecimal>(Term.ATAN2, SQLDataType.NUMERIC, x, y);
}
/**
* Get the cosine(field) function
* <p>
* This renders the cos function where available:
* <code><pre>cos([this])</pre></code>
*/
public static Field<BigDecimal> cos(Number value) {
return cos(val(value));
}
/**
* Get the cosine(field) function
* <p>
* This renders the cos function where available:
* <code><pre>cos([this])</pre></code>
*/
public static Field<BigDecimal> cos(Field<? extends Number> field) {
return function("cos", SQLDataType.NUMERIC, field);
}
/**
* Get the sine(field) function
* <p>
* This renders the sin function where available:
* <code><pre>sin([this])</pre></code>
*/
public static Field<BigDecimal> sin(Number value) {
return sin(val(value));
}
/**
* Get the sine(field) function
* <p>
* This renders the sin function where available:
* <code><pre>sin([this])</pre></code>
*/
public static Field<BigDecimal> sin(Field<? extends Number> field) {
return function("sin", SQLDataType.NUMERIC, field);
}
/**
* Get the tangent(field) function
* <p>
* This renders the tan function where available:
* <code><pre>tan([this])</pre></code>
*/
public static Field<BigDecimal> tan(Number value) {
return tan(val(value));
}
/**
* Get the tangent(field) function
* <p>
* This renders the tan function where available:
* <code><pre>tan([this])</pre></code>
*/
public static Field<BigDecimal> tan(Field<? extends Number> field) {
return function("tan", SQLDataType.NUMERIC, field);
}
/**
* Get the cotangent(field) function
* <p>
* This renders the cot function where available:
* <code><pre>cot([this])</pre></code> ... or simulates it elsewhere using
* sin and cos: <code><pre>cos([this]) / sin([this])</pre></code>
*/
public static Field<BigDecimal> cot(Number value) {
return cot(val(value));
}
/**
* Get the cotangent(field) function
* <p>
* This renders the cot function where available:
* <code><pre>cot([this])</pre></code> ... or simulates it elsewhere using
* sin and cos: <code><pre>cos([this]) / sin([this])</pre></code>
*/
public static Field<BigDecimal> cot(Field<? extends Number> field) {
return new Cot(field);
}
/**
* Get the hyperbolic sine function: sinh(field)
* <p>
* This renders the sinh function where available:
* <code><pre>sinh([this])</pre></code> ... or simulates it elsewhere using
* exp: <code><pre>(exp([this] * 2) - 1) / (exp([this] * 2))</pre></code>
*/
public static Field<BigDecimal> sinh(Number value) {
return sinh(val(value));
}
/**
* Get the hyperbolic sine function: sinh(field)
* <p>
* This renders the sinh function where available:
* <code><pre>sinh([this])</pre></code> ... or simulates it elsewhere using
* exp: <code><pre>(exp([this] * 2) - 1) / (exp([this] * 2))</pre></code>
*/
public static Field<BigDecimal> sinh(Field<? extends Number> field) {
return new Sinh(field);
}
/**
* Get the hyperbolic cosine function: cosh(field)
* <p>
* This renders the cosh function where available:
* <code><pre>cosh([this])</pre></code> ... or simulates it elsewhere using
* exp: <code><pre>(exp([this] * 2) + 1) / (exp([this] * 2))</pre></code>
*/
public static Field<BigDecimal> cosh(Number value) {
return cosh(val(value));
}
/**
* Get the hyperbolic cosine function: cosh(field)
* <p>
* This renders the cosh function where available:
* <code><pre>cosh([this])</pre></code> ... or simulates it elsewhere using
* exp: <code><pre>(exp([this] * 2) + 1) / (exp([this] * 2))</pre></code>
*/
public static Field<BigDecimal> cosh(Field<? extends Number> field) {
return new Cosh(field);
}
/**
* Get the hyperbolic tangent function: tanh(field)
* <p>
* This renders the tanh function where available:
* <code><pre>tanh([this])</pre></code> ... or simulates it elsewhere using
* exp:
* <code><pre>(exp([this] * 2) - 1) / (exp([this] * 2) + 1)</pre></code>
*/
public static Field<BigDecimal> tanh(Number value) {
return tanh(val(value));
}
/**
* Get the hyperbolic tangent function: tanh(field)
* <p>
* This renders the tanh function where available:
* <code><pre>tanh([this])</pre></code> ... or simulates it elsewhere using
* exp:
* <code><pre>(exp([this] * 2) - 1) / (exp([this] * 2) + 1)</pre></code>
*/
public static Field<BigDecimal> tanh(Field<? extends Number> field) {
return new Tanh(field);
}
/**
* Get the hyperbolic cotangent function: coth(field)
* <p>
* This is not supported by any RDBMS, but simulated using exp exp:
* <code><pre>(exp([this] * 2) + 1) / (exp([this] * 2) - 1)</pre></code>
*/
public static Field<BigDecimal> coth(Number value) {
return coth(val(value));
}
/**
* Get the hyperbolic cotangent function: coth(field)
* <p>
* This is not supported by any RDBMS, but simulated using exp exp:
* <code><pre>(exp([this] * 2) + 1) / (exp([this] * 2) - 1)</pre></code>
*/
public static Field<BigDecimal> coth(Field<? extends Number> field) {
return exp(field.mul(2)).add(1).div(exp(field.mul(2)).sub(1));
}
/**
* Calculate degrees from radians from this field
* <p>
* This renders the degrees function where available:
* <code><pre>degrees([this])</pre></code> ... or simulates it elsewhere:
* <code><pre>[this] * 180 / PI</pre></code>
*/
public static Field<BigDecimal> deg(Number value) {
return deg(val(value));
}
/**
* Calculate degrees from radians from this field
* <p>
* This renders the degrees function where available:
* <code><pre>degrees([this])</pre></code> ... or simulates it elsewhere:
* <code><pre>[this] * 180 / PI</pre></code>
*/
public static Field<BigDecimal> deg(Field<? extends Number> field) {
return new Degrees(field);
}
/**
* Calculate radians from degrees from this field
* <p>
* This renders the degrees function where available:
* <code><pre>degrees([this])</pre></code> ... or simulates it elsewhere:
* <code><pre>[this] * PI / 180</pre></code>
*/
public static Field<BigDecimal> rad(Number value) {
return rad(val(value));
}
/**
* Calculate radians from degrees from this field
* <p>
* This renders the degrees function where available:
* <code><pre>degrees([this])</pre></code> ... or simulates it elsewhere:
* <code><pre>[this] * PI / 180</pre></code>
*/
public static Field<BigDecimal> rad(Field<? extends Number> field) {
return new Radians(field);
}
// -------------------------------------------------------------------------
// Aggregate functions
// -------------------------------------------------------------------------
@ -1992,14 +2609,14 @@ public class Factory implements Configuration {
/**
* Get the sum over a numeric field: sum(field)
*/
public static Field<BigDecimal> sum(Field<?> field) {
public static Field<BigDecimal> sum(Field<? extends Number> field) {
return function("sum", SQLDataType.NUMERIC, field);
}
/**
* Get the average over a numeric field: avg(field)
*/
public static Field<BigDecimal> avg(Field<?> field) {
public static Field<BigDecimal> avg(Field<? extends Number> field) {
return function("avg", SQLDataType.NUMERIC, field);
}
@ -2013,7 +2630,7 @@ public class Factory implements Configuration {
* <li>Sybase SQL Anywhere</li>
* </ul>
*/
public static Field<BigDecimal> median(Field<?> field) {
public static Field<BigDecimal> median(Field<? extends Number> field) {
return function("median", SQLDataType.NUMERIC, field);
}
@ -2034,7 +2651,7 @@ public class Factory implements Configuration {
* <li>Sybase SQL Anywhere</li>
* </ul>
*/
public static Field<BigDecimal> stddevPop(Field<?> field) {
public static Field<BigDecimal> stddevPop(Field<? extends Number> field) {
return new Function<BigDecimal>(Term.STDDEV_POP, SQLDataType.NUMERIC, field);
}
@ -2055,7 +2672,7 @@ public class Factory implements Configuration {
* <li>Sybase SQL Anywhere</li>
* </ul>
*/
public static Field<BigDecimal> stddevSamp(Field<?> field) {
public static Field<BigDecimal> stddevSamp(Field<? extends Number> field) {
return new Function<BigDecimal>(Term.STDDEV_SAMP, SQLDataType.NUMERIC, field);
}
@ -2076,7 +2693,7 @@ public class Factory implements Configuration {
* <li>Sybase SQL Anywhere</li>
* </ul>
*/
public static Field<BigDecimal> varPop(Field<?> field) {
public static Field<BigDecimal> varPop(Field<? extends Number> field) {
return new Function<BigDecimal>(Term.VAR_POP, SQLDataType.NUMERIC, field);
}
@ -2095,7 +2712,7 @@ public class Factory implements Configuration {
* <li>Sybase SQL Anywhere</li>
* </ul>
*/
public static Field<BigDecimal> varSamp(Field<?> field) {
public static Field<BigDecimal> varSamp(Field<? extends Number> field) {
return new Function<BigDecimal>(Term.VAR_SAMP, SQLDataType.NUMERIC, field);
}

View File

@ -36,6 +36,7 @@
package org.jooq.impl;
import static org.jooq.impl.Factory.function;
import static org.jooq.impl.Factory.round;
import org.jooq.Configuration;
import org.jooq.Field;
@ -43,7 +44,7 @@ import org.jooq.Field;
/**
* @author Lukas Eder
*/
class Floor<T> extends AbstractFunction<T> {
class Floor<T extends Number> extends AbstractFunction<T> {
/**
* Generated UID
@ -64,7 +65,7 @@ class Floor<T> extends AbstractFunction<T> {
// evaluate "floor" if unavailable
case SQLITE:
return argument.sub(0.499999999999999).round();
return round(argument.sub(0.499999999999999));
default:
return function("floor", getDataType(), argument);

View File

@ -37,6 +37,7 @@ package org.jooq.impl;
import static org.jooq.impl.Factory.function;
import static org.jooq.impl.Factory.literal;
import static org.jooq.impl.Factory.ln;
import java.math.BigDecimal;
@ -51,16 +52,16 @@ class Ln extends AbstractFunction<BigDecimal> {
/**
* Generated UID
*/
private static final long serialVersionUID = -7273879239726265322L;
private static final long serialVersionUID = -7273879239726265322L;
private final Field<?> argument;
private final Integer base;
private final Field<? extends Number> argument;
private final Integer base;
Ln(Field<?> argument) {
Ln(Field<? extends Number> argument) {
this(argument, null);
}
Ln(Field<?> argument, Integer base) {
Ln(Field<? extends Number> argument, Integer base) {
super("ln", SQLDataType.NUMERIC, argument);
this.argument = argument;
@ -90,7 +91,7 @@ class Ln extends AbstractFunction<BigDecimal> {
case INGRES:
case SQLSERVER:
case SYBASE:
return argument.ln().div(literal(base).ln());
return ln(argument).div(ln(literal(base)));
default:
return function("log", SQLDataType.NUMERIC, literal(base), argument);

View File

@ -35,6 +35,7 @@
*/
package org.jooq.impl;
import static org.jooq.impl.Factory.asin;
import static org.jooq.impl.Factory.function;
import static org.jooq.impl.Factory.literal;
import static org.jooq.impl.Factory.one;
@ -64,7 +65,7 @@ class Pi extends AbstractFunction<BigDecimal> {
switch (configuration.getDialect()) {
case DB2:
case ORACLE:
return one().asin().mul(two());
return asin(one()).mul(two());
case SQLITE:
return literal(Math.PI, BigDecimal.class);

View File

@ -35,7 +35,9 @@
*/
package org.jooq.impl;
import static org.jooq.impl.Factory.exp;
import static org.jooq.impl.Factory.function;
import static org.jooq.impl.Factory.ln;
import java.math.BigDecimal;
@ -50,12 +52,12 @@ class Power extends AbstractFunction<BigDecimal> {
/**
* Generated UID
*/
private static final long serialVersionUID = -7273879239726265322L;
private static final long serialVersionUID = -7273879239726265322L;
private final Field<?> arg1;
private final Field<? extends Number> arg1;
private final Field<? extends Number> arg2;
Power(Field<?> arg1, Field<? extends Number> arg2) {
Power(Field<? extends Number> arg1, Field<? extends Number> arg2) {
super("ceil", SQLDataType.NUMERIC, arg1, arg2);
this.arg1 = arg1;
@ -67,7 +69,7 @@ class Power extends AbstractFunction<BigDecimal> {
switch (configuration.getDialect()) {
case DERBY:
case SQLITE:
return arg1.ln().mul(arg2).exp();
return exp(ln(arg1).mul(arg2));
default:
return function("power", SQLDataType.NUMERIC, getArguments());

View File

@ -35,6 +35,8 @@
*/
package org.jooq.impl;
import static org.jooq.impl.Factory.ceil;
import static org.jooq.impl.Factory.floor;
import static org.jooq.impl.Factory.function;
import static org.jooq.impl.Factory.val;
@ -46,7 +48,7 @@ import org.jooq.Field;
/**
* @author Lukas Eder
*/
class Round<T> extends AbstractFunction<T> {
class Round<T extends Number> extends AbstractFunction<T> {
/**
* Generated UID
@ -76,18 +78,18 @@ class Round<T> extends AbstractFunction<T> {
case DERBY: {
if (decimals == 0) {
return Factory.decode()
.when(argument.sub((Field<? extends Number>) argument.floor())
.lessThan((T) Double.valueOf(0.5)), argument.floor())
.otherwise(argument.ceil());
.when(argument.sub(floor(argument))
.lessThan((T) Double.valueOf(0.5)), floor(argument))
.otherwise(ceil(argument));
}
else {
Field<BigDecimal> factor = Factory.val(BigDecimal.ONE.movePointRight(decimals));
Field<T> mul = argument.mul(factor);
return Factory.decode()
.when(mul.sub((Field<? extends Number>) mul.floor())
.lessThan((T) Double.valueOf(0.5)), mul.floor().div(factor))
.otherwise(mul.ceil().div(factor));
.when(mul.sub(floor(mul))
.lessThan((T) Double.valueOf(0.5)), floor(mul).div(factor))
.otherwise(ceil(mul).div(factor));
}
}

View File

@ -35,6 +35,7 @@
*/
package org.jooq.impl;
import static org.jooq.impl.Factory.exp;
import static org.jooq.impl.Factory.function;
import static org.jooq.impl.Factory.one;
import static org.jooq.impl.Factory.two;
@ -52,11 +53,11 @@ class Sinh extends AbstractFunction<BigDecimal> {
/**
* Generated UID
*/
private static final long serialVersionUID = -7273879239726265322L;
private static final long serialVersionUID = -7273879239726265322L;
private final Field<?> argument;
private final Field<? extends Number> argument;
Sinh(Field<?> argument) {
Sinh(Field<? extends Number> argument) {
super("sinh", SQLDataType.NUMERIC, argument);
this.argument = argument;
@ -72,7 +73,7 @@ class Sinh extends AbstractFunction<BigDecimal> {
case POSTGRES:
case SQLSERVER:
case SYBASE:
return argument.mul(two()).exp().sub(one()).div(argument.exp().mul(two()));
return exp(argument.mul(two())).sub(one()).div(exp(argument).mul(two()));
default:
return function("sinh", SQLDataType.NUMERIC, argument);

View File

@ -36,6 +36,7 @@
package org.jooq.impl;
import static org.jooq.impl.Factory.function;
import static org.jooq.impl.Factory.power;
import java.math.BigDecimal;
@ -50,11 +51,11 @@ class Sqrt extends AbstractFunction<BigDecimal> {
/**
* Generated UID
*/
private static final long serialVersionUID = -7273879239726265322L;
private static final long serialVersionUID = -7273879239726265322L;
private final Field<?> argument;
private final Field<? extends Number> argument;
Sqrt(Field<?> argument) {
Sqrt(Field<? extends Number> argument) {
super("sqrt", SQLDataType.NUMERIC, argument);
this.argument = argument;
@ -64,7 +65,7 @@ class Sqrt extends AbstractFunction<BigDecimal> {
final Field<BigDecimal> getFunction0(Configuration configuration) {
switch (configuration.getDialect()) {
case SQLITE:
return argument.power(0.5);
return power(argument, 0.5);
default:
return function("sqrt", SQLDataType.NUMERIC, argument);

View File

@ -35,6 +35,7 @@
*/
package org.jooq.impl;
import static org.jooq.impl.Factory.exp;
import static org.jooq.impl.Factory.function;
import static org.jooq.impl.Factory.one;
import static org.jooq.impl.Factory.two;
@ -52,11 +53,11 @@ class Tanh extends AbstractFunction<BigDecimal> {
/**
* Generated UID
*/
private static final long serialVersionUID = -7273879239726265322L;
private static final long serialVersionUID = -7273879239726265322L;
private final Field<?> argument;
private final Field<? extends Number> argument;
Tanh(Field<?> argument) {
Tanh(Field<? extends Number> argument) {
super("tanh", SQLDataType.NUMERIC, argument);
this.argument = argument;
@ -72,7 +73,7 @@ class Tanh extends AbstractFunction<BigDecimal> {
case POSTGRES:
case SQLSERVER:
case SYBASE:
return argument.mul(two()).exp().sub(one()).div(argument.mul(two()).exp().add(one()));
return exp(argument.mul(two())).sub(one()).div(exp(argument.mul(two())).add(one()));
default:
return function("tanh", SQLDataType.NUMERIC, argument);

View File

@ -48,6 +48,7 @@ import static org.jooq.impl.Factory.falseCondition;
import static org.jooq.impl.Factory.field;
import static org.jooq.impl.Factory.max;
import static org.jooq.impl.Factory.min;
import static org.jooq.impl.Factory.round;
import static org.jooq.impl.Factory.sum;
import static org.jooq.impl.Factory.trueCondition;
import static org.jooq.impl.Factory.val;
@ -935,7 +936,7 @@ public class jOOQTest {
public final void testInsertSelect1() throws Exception {
InsertQuery<Table1Record> q = create.insertQuery(TABLE1);
q.addValue(FIELD_ID1, val(10).round());
q.addValue(FIELD_ID1, round(val(10)));
q.addValue(FIELD_NAME1, create.select(FIELD_NAME1).from(TABLE1).where(FIELD_ID1.equal(1)).<String> asField());
assertEquals("insert into \"TABLE1\" (\"ID1\", \"NAME1\") values (round(10), (select \"TABLE1\".\"NAME1\" from \"TABLE1\" where \"TABLE1\".\"ID1\" = 1))", r_refI().render(q));
assertEquals("insert into \"TABLE1\" (\"ID1\", \"NAME1\") values (round(?), (select \"TABLE1\".\"NAME1\" from \"TABLE1\" where \"TABLE1\".\"ID1\" = ?))", r_ref().render(q));