[#1322] Add Factory.dateAdd() and timestampAdd() for convenience
This commit is contained in:
parent
32e823f356
commit
aac93ee93a
@ -52,8 +52,10 @@ import static org.jooq.SQLDialect.SQLSERVER;
|
||||
import static org.jooq.SQLDialect.SYBASE;
|
||||
import static org.jooq.impl.Factory.cast;
|
||||
import static org.jooq.impl.Factory.castNull;
|
||||
import static org.jooq.impl.Factory.dateAdd;
|
||||
import static org.jooq.impl.Factory.dateDiff;
|
||||
import static org.jooq.impl.Factory.inline;
|
||||
import static org.jooq.impl.Factory.timestampAdd;
|
||||
import static org.jooq.impl.Factory.timestampDiff;
|
||||
import static org.jooq.impl.Factory.val;
|
||||
import static org.jooq.tools.unsigned.Unsigned.ubyte;
|
||||
@ -1305,22 +1307,32 @@ extends BaseTest<A, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658, T725
|
||||
// Extra care needs to be taken with Postgres negative DAY TO SECOND
|
||||
// intervals. Postgres allows for having several signs in intervals
|
||||
val(new Date(0)).add(1).as("d1"),
|
||||
dateAdd(new Date(0), 1).as("d1a"),
|
||||
val(new Date(0)).add(-1).as("d2a"),
|
||||
val(new Date(0)).sub(1).as("d2b"),
|
||||
|
||||
val(new Date(0)).add(new YearToMonth(1, 6)).as("d3"),
|
||||
dateAdd(new Date(0), new YearToMonth(1, 6)).as("d3a"),
|
||||
val(new Date(0)).add(new YearToMonth(1, 6).neg()).as("d4a"),
|
||||
val(new Date(0)).sub(new YearToMonth(1, 6)).as("d4b"),
|
||||
|
||||
val(new Date(0)).add(new DayToSecond(2)).as("d5"),
|
||||
dateAdd(new Date(0), new DayToSecond(2)).as("d5a"),
|
||||
val(new Date(0)).add(new DayToSecond(2).neg()).as("d6a"),
|
||||
val(new Date(0)).sub(new DayToSecond(2)).as("d6b"),
|
||||
|
||||
val(new Timestamp(0)).add(1).as("ts1"),
|
||||
timestampAdd(new Timestamp(0), 1).as("ts1a"),
|
||||
val(new Timestamp(0)).add(-1).as("ts2a"),
|
||||
val(new Timestamp(0)).sub(1).as("ts2b"),
|
||||
|
||||
val(new Timestamp(0)).add(new YearToMonth(1, 6)).as("ts3"),
|
||||
timestampAdd(new Timestamp(0), new YearToMonth(1, 6)).as("ts3a"),
|
||||
val(new Timestamp(0)).add(new YearToMonth(1, 6).neg()).as("ts4a"),
|
||||
val(new Timestamp(0)).sub(new YearToMonth(1, 6)).as("ts4b"),
|
||||
|
||||
val(new Timestamp(0)).add(new DayToSecond(2)).as("ts5"),
|
||||
timestampAdd(new Timestamp(0), new DayToSecond(2)).as("ts5a"),
|
||||
val(new Timestamp(0)).add(new DayToSecond(2).neg()).as("ts6a"),
|
||||
val(new Timestamp(0)).sub(new DayToSecond(2)).as("ts6b"),
|
||||
val(new Timestamp(0)).add(new DayToSecond(2, 6)).as("ts7"),
|
||||
@ -1336,7 +1348,9 @@ extends BaseTest<A, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658, T725
|
||||
cal = cal();
|
||||
cal.add(Calendar.DATE, 1);
|
||||
assertEquals(new Date(cal.getTimeInMillis()), record.getValue("d1"));
|
||||
assertEquals(new Date(cal.getTimeInMillis()), record.getValue("d1a"));
|
||||
assertEquals(new Timestamp(cal.getTimeInMillis() - tsShift), record.getValue("ts1"));
|
||||
assertEquals(new Timestamp(cal.getTimeInMillis() - tsShift), record.getValue("ts1a"));
|
||||
|
||||
cal = cal();
|
||||
cal.add(Calendar.DATE, -1);
|
||||
@ -1348,7 +1362,9 @@ extends BaseTest<A, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658, T725
|
||||
cal = cal();
|
||||
cal.add(Calendar.MONTH, 18);
|
||||
assertEquals(new Date(cal.getTimeInMillis()), record.getValue("d3"));
|
||||
assertEquals(new Date(cal.getTimeInMillis()), record.getValue("d3a"));
|
||||
assertEquals(new Timestamp(cal.getTimeInMillis() - tsShift), record.getValue("ts3"));
|
||||
assertEquals(new Timestamp(cal.getTimeInMillis() - tsShift), record.getValue("ts3a"));
|
||||
|
||||
cal = cal();
|
||||
cal.add(Calendar.MONTH, -18);
|
||||
@ -1360,7 +1376,9 @@ extends BaseTest<A, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658, T725
|
||||
cal = cal();
|
||||
cal.add(Calendar.DATE, 2);
|
||||
assertEquals(new Date(cal.getTimeInMillis()), record.getValue("d5"));
|
||||
assertEquals(new Date(cal.getTimeInMillis()), record.getValue("d5a"));
|
||||
assertEquals(new Timestamp(cal.getTimeInMillis() - tsShift), record.getValue("ts5"));
|
||||
assertEquals(new Timestamp(cal.getTimeInMillis() - tsShift), record.getValue("ts5a"));
|
||||
|
||||
cal = cal();
|
||||
cal.add(Calendar.DATE, -2);
|
||||
|
||||
@ -2510,6 +2510,30 @@ public class Factory implements FactoryOperations {
|
||||
return dateDiff(nullSafe(date1), val(date2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an interval to a date
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*
|
||||
* @see Field#add(Number)
|
||||
*/
|
||||
@Support
|
||||
public static Field<Date> dateAdd(Date date, Number interval) {
|
||||
return dateAdd(val(date), val(interval));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an interval to a date
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*
|
||||
* @see Field#add(Field)
|
||||
*/
|
||||
@Support
|
||||
public static Field<Date> dateAdd(Field<Date> date, Field<? extends Number> interval) {
|
||||
return nullSafe(date).add(interval);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the date difference in number of days
|
||||
* <p>
|
||||
@ -2534,6 +2558,30 @@ public class Factory implements FactoryOperations {
|
||||
return new DateDiff(nullSafe(date1), nullSafe(date2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an interval to a timestamp
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*
|
||||
* @see Field#add(Number)
|
||||
*/
|
||||
@Support
|
||||
public static Field<Timestamp> timestampAdd(Timestamp timestamp, Number interval) {
|
||||
return timestampAdd(val(timestamp), val(interval));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an interval to a timestamp
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*
|
||||
* @see Field#add(Field)
|
||||
*/
|
||||
@Support
|
||||
public static Field<Timestamp> timestampAdd(Field<Timestamp> timestamp, Field<? extends Number> interval) {
|
||||
return nullSafe(timestamp).add(interval);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the timestamp difference as a <code>INTERVAL DAY TO SECOND</code>
|
||||
* type
|
||||
@ -2583,7 +2631,7 @@ public class Factory implements FactoryOperations {
|
||||
*/
|
||||
@Support
|
||||
public static Field<DayToSecond> timestampDiff(Field<Timestamp> timestamp1, Field<Timestamp> timestamp2) {
|
||||
return new TimestampDiff(timestamp1, timestamp2);
|
||||
return new TimestampDiff(nullSafe(timestamp1), nullSafe(timestamp2));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -70,6 +70,7 @@ import java.sql.Connection;
|
||||
import java.sql.Date;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.regex.Matcher;
|
||||
@ -363,6 +364,15 @@ public class jOOQTest {
|
||||
assertEquals(
|
||||
Factory.coth((Integer) null),
|
||||
Factory.coth((Field<Integer>) null));
|
||||
assertEquals(
|
||||
Factory.dateAdd((Date) null, (Integer) null),
|
||||
Factory.dateAdd((Field<Date>) null, (Field<Integer>) null));
|
||||
assertEquals(
|
||||
Factory.dateDiff((Date) null, (Date) null),
|
||||
Factory.dateDiff((Field<Date>) null, (Field<Date>) null));
|
||||
assertEquals(
|
||||
Factory.day((java.util.Date) null),
|
||||
Factory.day((Field<java.util.Date>) null));
|
||||
assertEquals(
|
||||
Factory.decode((Integer) null, null, null),
|
||||
Factory.decode((Field<Integer>) null, null, null));
|
||||
@ -378,24 +388,6 @@ public class jOOQTest {
|
||||
assertEquals(
|
||||
Factory.extract((java.util.Date) null, DatePart.DAY),
|
||||
Factory.extract((Field<java.util.Date>) null, DatePart.DAY));
|
||||
assertEquals(
|
||||
Factory.year((java.util.Date) null),
|
||||
Factory.year((Field<java.util.Date>) null));
|
||||
assertEquals(
|
||||
Factory.month((java.util.Date) null),
|
||||
Factory.month((Field<java.util.Date>) null));
|
||||
assertEquals(
|
||||
Factory.day((java.util.Date) null),
|
||||
Factory.day((Field<java.util.Date>) null));
|
||||
assertEquals(
|
||||
Factory.hour((java.util.Date) null),
|
||||
Factory.hour((Field<java.util.Date>) null));
|
||||
assertEquals(
|
||||
Factory.minute((java.util.Date) null),
|
||||
Factory.minute((Field<java.util.Date>) null));
|
||||
assertEquals(
|
||||
Factory.second((java.util.Date) null),
|
||||
Factory.second((Field<java.util.Date>) null));
|
||||
assertEquals(
|
||||
Factory.floor((Integer) null),
|
||||
Factory.floor((Field<Integer>) null));
|
||||
@ -411,6 +403,9 @@ public class jOOQTest {
|
||||
assertEquals(
|
||||
Factory.greatest((Integer) null, (Integer) null),
|
||||
Factory.greatest((Field<Integer>) null, (Field<Integer>) null));
|
||||
assertEquals(
|
||||
Factory.hour((java.util.Date) null),
|
||||
Factory.hour((Field<java.util.Date>) null));
|
||||
assertEquals(
|
||||
Factory.lag((Field<Integer>) null, 1, (Integer) null),
|
||||
Factory.lag((Field<Integer>) null, 1, (Field<Integer>) null));
|
||||
@ -444,6 +439,12 @@ public class jOOQTest {
|
||||
assertEquals(
|
||||
Factory.ltrim((String) null),
|
||||
Factory.ltrim((Field<String>) null));
|
||||
assertEquals(
|
||||
Factory.minute((java.util.Date) null),
|
||||
Factory.minute((Field<java.util.Date>) null));
|
||||
assertEquals(
|
||||
Factory.month((java.util.Date) null),
|
||||
Factory.month((Field<java.util.Date>) null));
|
||||
assertEquals(
|
||||
Factory.nullif((Integer) null, (Integer) null),
|
||||
Factory.nullif((Field<Integer>) null, (Integer) null));
|
||||
@ -513,6 +514,9 @@ public class jOOQTest {
|
||||
assertEquals(
|
||||
Factory.rtrim((String) null),
|
||||
Factory.rtrim((Field<String>) null));
|
||||
assertEquals(
|
||||
Factory.second((java.util.Date) null),
|
||||
Factory.second((Field<java.util.Date>) null));
|
||||
assertEquals(
|
||||
Factory.shl((Integer) null, (Integer) null),
|
||||
Factory.shl((Integer) null, (Field<Integer>) null));
|
||||
@ -549,12 +553,21 @@ public class jOOQTest {
|
||||
assertEquals(
|
||||
Factory.tanh((Integer) null),
|
||||
Factory.tanh((Field<Integer>) null));
|
||||
assertEquals(
|
||||
Factory.timestampAdd((Timestamp) null, (Integer) null),
|
||||
Factory.timestampAdd((Field<Timestamp>) null, (Field<Integer>) null));
|
||||
assertEquals(
|
||||
Factory.timestampDiff((Timestamp) null, (Timestamp) null),
|
||||
Factory.timestampDiff((Field<Timestamp>) null, (Field<Timestamp>) null));
|
||||
assertEquals(
|
||||
Factory.trim((String) null),
|
||||
Factory.trim((Field<String>) null));
|
||||
assertEquals(
|
||||
Factory.upper((String) null),
|
||||
Factory.upper((Field<String>) null));
|
||||
assertEquals(
|
||||
Factory.year((java.util.Date) null),
|
||||
Factory.year((Field<java.util.Date>) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Loading…
Reference in New Issue
Block a user