[#3009] Add DSL.dateAdd(Field<Date>, Field<? extends Number>, DatePart) and timestampAdd(Field<Timestamp>, Field<? extends Number>, DatePart) for better cross-database datetime arithmetic
This commit is contained in:
parent
963fb4331e
commit
6f7a93091c
@ -88,6 +88,7 @@ import java.util.UUID;
|
||||
import org.jooq.Converter;
|
||||
import org.jooq.DSLContext;
|
||||
import org.jooq.DataType;
|
||||
import org.jooq.DatePart;
|
||||
import org.jooq.Field;
|
||||
import org.jooq.InsertSetMoreStep;
|
||||
import org.jooq.Record;
|
||||
@ -102,6 +103,7 @@ import org.jooq.SQLDialect;
|
||||
import org.jooq.TableRecord;
|
||||
import org.jooq.UpdatableRecord;
|
||||
import org.jooq.conf.Settings;
|
||||
import org.jooq.impl.DSL;
|
||||
import org.jooq.impl.SQLDataType;
|
||||
import org.jooq.test.BaseTest;
|
||||
import org.jooq.test.jOOQAbstractTest;
|
||||
@ -1574,6 +1576,50 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
|
||||
assertEquals(new DayToSecond(1, 6), record.getValue("ts2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFunctionsOnDates_DATE_ADD() throws Exception {
|
||||
Calendar cal;
|
||||
|
||||
// Adding
|
||||
cal = cal();
|
||||
|
||||
Record6<Timestamp, Timestamp, Timestamp, Timestamp, Timestamp, Timestamp> r1 = create().select(
|
||||
DSL.timestampAdd(new Timestamp(cal.getTimeInMillis()), 2, DatePart.YEAR) .as("yy"),
|
||||
DSL.timestampAdd(new Timestamp(cal.getTimeInMillis()), 2, DatePart.MONTH) .as("mm"),
|
||||
DSL.timestampAdd(new Timestamp(cal.getTimeInMillis()), 2, DatePart.DAY) .as("dd"),
|
||||
DSL.timestampAdd(new Timestamp(cal.getTimeInMillis()), 2, DatePart.HOUR) .as("hh"),
|
||||
DSL.timestampAdd(new Timestamp(cal.getTimeInMillis()), 2, DatePart.MINUTE).as("mi"),
|
||||
DSL.timestampAdd(new Timestamp(cal.getTimeInMillis()), 2, DatePart.SECOND).as("ss")
|
||||
).fetchOne();
|
||||
|
||||
cal = cal(); cal.add(Calendar.YEAR , 2); assertEquals(new Timestamp(cal.getTimeInMillis()), r1.value1());
|
||||
cal = cal(); cal.add(Calendar.MONTH , 2); assertEquals(new Timestamp(cal.getTimeInMillis()), r1.value2());
|
||||
cal = cal(); cal.add(Calendar.DAY_OF_YEAR, 2); assertEquals(new Timestamp(cal.getTimeInMillis()), r1.value3());
|
||||
cal = cal(); cal.add(Calendar.HOUR , 2); assertEquals(new Timestamp(cal.getTimeInMillis()), r1.value4());
|
||||
cal = cal(); cal.add(Calendar.MINUTE , 2); assertEquals(new Timestamp(cal.getTimeInMillis()), r1.value5());
|
||||
cal = cal(); cal.add(Calendar.SECOND , 2); assertEquals(new Timestamp(cal.getTimeInMillis()), r1.value6());
|
||||
|
||||
// Subtracting
|
||||
cal = cal();
|
||||
|
||||
Record6<Timestamp, Timestamp, Timestamp, Timestamp, Timestamp, Timestamp> r2 = create().select(
|
||||
DSL.timestampAdd(new Timestamp(cal.getTimeInMillis()), -2, DatePart.YEAR) .as("yy"),
|
||||
DSL.timestampAdd(new Timestamp(cal.getTimeInMillis()), -2, DatePart.MONTH) .as("mm"),
|
||||
DSL.timestampAdd(new Timestamp(cal.getTimeInMillis()), -2, DatePart.DAY) .as("dd"),
|
||||
DSL.timestampAdd(new Timestamp(cal.getTimeInMillis()), -2, DatePart.HOUR) .as("hh"),
|
||||
DSL.timestampAdd(new Timestamp(cal.getTimeInMillis()), -2, DatePart.MINUTE).as("mi"),
|
||||
DSL.timestampAdd(new Timestamp(cal.getTimeInMillis()), -2, DatePart.SECOND).as("ss")
|
||||
).fetchOne();
|
||||
|
||||
cal = cal(); cal.add(Calendar.YEAR , -2); assertEquals(new Timestamp(cal.getTimeInMillis()), r2.value1());
|
||||
cal = cal(); cal.add(Calendar.MONTH , -2); assertEquals(new Timestamp(cal.getTimeInMillis()), r2.value2());
|
||||
cal = cal(); cal.add(Calendar.DAY_OF_YEAR, -2); assertEquals(new Timestamp(cal.getTimeInMillis()), r2.value3());
|
||||
cal = cal(); cal.add(Calendar.HOUR , -2); assertEquals(new Timestamp(cal.getTimeInMillis()), r2.value4());
|
||||
cal = cal(); cal.add(Calendar.MINUTE , -2); assertEquals(new Timestamp(cal.getTimeInMillis()), r2.value5());
|
||||
cal = cal(); cal.add(Calendar.SECOND , -2); assertEquals(new Timestamp(cal.getTimeInMillis()), r2.value6());
|
||||
|
||||
}
|
||||
|
||||
private Calendar cal() {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTimeInMillis(-3600000);
|
||||
|
||||
@ -1776,6 +1776,11 @@ public abstract class jOOQAbstractTest<
|
||||
new DataTypeTests(this).testDateTimeArithmetic();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFunctionsOnDates_DATE_ADD() throws Exception {
|
||||
new DataTypeTests(this).testFunctionsOnDates_DATE_ADD();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCurrentDate() throws Exception {
|
||||
new DataTypeTests(this).testCurrentDate();
|
||||
|
||||
@ -7565,6 +7565,46 @@ public class DSL {
|
||||
return nullSafe(date).add(interval);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an interval to a date, given a date part.
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*/
|
||||
@Support
|
||||
public static Field<Date> dateAdd(Date date, Number interval, DatePart datePart) {
|
||||
return new DateAdd<Date>(Utils.field(date), Utils.field(interval), datePart);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an interval to a date, given a date part.
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*/
|
||||
@Support
|
||||
public static Field<Date> dateAdd(Date date, Field<? extends Number> interval, DatePart datePart) {
|
||||
return new DateAdd<Date>(Utils.field(date), nullSafe(interval), datePart);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an interval to a date, given a date part.
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*/
|
||||
@Support
|
||||
public static Field<Date> dateAdd(Field<Date> date, Number interval, DatePart datePart) {
|
||||
return new DateAdd<Date>(nullSafe(date), Utils.field(interval), datePart);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an interval to a date, given a date part.
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*/
|
||||
@Support
|
||||
public static Field<Date> dateAdd(Field<Date> date, Field<? extends Number> interval, DatePart datePart) {
|
||||
return new DateAdd<Date>(nullSafe(date), nullSafe(interval), datePart);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the date difference in number of days.
|
||||
* <p>
|
||||
@ -7645,6 +7685,46 @@ public class DSL {
|
||||
return nullSafe(timestamp).add(interval);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an interval to a timestamp, given a date part.
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*/
|
||||
@Support
|
||||
public static Field<Timestamp> timestampAdd(Timestamp date, Number interval, DatePart datePart) {
|
||||
return new DateAdd<Timestamp>(Utils.field(date), Utils.field(interval), datePart);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an interval to a timestamp, given a date part.
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*/
|
||||
@Support
|
||||
public static Field<Timestamp> timestampAdd(Timestamp date, Field<? extends Number> interval, DatePart datePart) {
|
||||
return new DateAdd<Timestamp>(Utils.field(date), nullSafe(interval), datePart);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an interval to a timestamp, given a date part.
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*/
|
||||
@Support
|
||||
public static Field<Timestamp> timestampAdd(Field<Timestamp> date, Number interval, DatePart datePart) {
|
||||
return new DateAdd<Timestamp>(nullSafe(date), Utils.field(interval), datePart);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an interval to a timestamp, given a date part.
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*/
|
||||
@Support
|
||||
public static Field<Timestamp> timestampAdd(Field<Timestamp> date, Field<? extends Number> interval, DatePart datePart) {
|
||||
return new DateAdd<Timestamp>(nullSafe(date), nullSafe(interval), datePart);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the timestamp difference as a <code>INTERVAL DAY TO SECOND</code>
|
||||
* type.
|
||||
|
||||
@ -414,7 +414,7 @@ class Expression<T> extends AbstractFunction<T> {
|
||||
x
|
||||
|
||||
xxxx xxxxxxx x
|
||||
xxxxx xxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxx xxxxxxxxxx xxx xxxxxxxxx xx xxxxxxx xxxxxxxxxxxxx xxxxxxxxxxx
|
||||
xxxxx xxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxx xxxxxxxxxxx xx xxx xxx xxxxxxxxxxxxxx
|
||||
x
|
||||
|
||||
xxxx xxxxxxx
|
||||
|
||||
Loading…
Reference in New Issue
Block a user