[#1269] Add YEAR(), MONTH(), DAY(), HOUR(), MINUTE(), SECOND() function support as shortcuts for EXTRACT()
This commit is contained in:
parent
4d50e15b16
commit
d3d65b4aac
@ -70,6 +70,7 @@ 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.day;
|
||||
import static org.jooq.impl.Factory.decode;
|
||||
import static org.jooq.impl.Factory.deg;
|
||||
import static org.jooq.impl.Factory.exp;
|
||||
@ -77,6 +78,7 @@ import static org.jooq.impl.Factory.extract;
|
||||
import static org.jooq.impl.Factory.field;
|
||||
import static org.jooq.impl.Factory.floor;
|
||||
import static org.jooq.impl.Factory.greatest;
|
||||
import static org.jooq.impl.Factory.hour;
|
||||
import static org.jooq.impl.Factory.least;
|
||||
import static org.jooq.impl.Factory.length;
|
||||
import static org.jooq.impl.Factory.ln;
|
||||
@ -84,6 +86,8 @@ 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.ltrim;
|
||||
import static org.jooq.impl.Factory.minute;
|
||||
import static org.jooq.impl.Factory.month;
|
||||
import static org.jooq.impl.Factory.nullif;
|
||||
import static org.jooq.impl.Factory.nvl;
|
||||
import static org.jooq.impl.Factory.nvl2;
|
||||
@ -97,6 +101,7 @@ import static org.jooq.impl.Factory.replace;
|
||||
import static org.jooq.impl.Factory.round;
|
||||
import static org.jooq.impl.Factory.rpad;
|
||||
import static org.jooq.impl.Factory.rtrim;
|
||||
import static org.jooq.impl.Factory.second;
|
||||
import static org.jooq.impl.Factory.shl;
|
||||
import static org.jooq.impl.Factory.shr;
|
||||
import static org.jooq.impl.Factory.sign;
|
||||
@ -109,6 +114,7 @@ import static org.jooq.impl.Factory.tanh;
|
||||
import static org.jooq.impl.Factory.trim;
|
||||
import static org.jooq.impl.Factory.upper;
|
||||
import static org.jooq.impl.Factory.val;
|
||||
import static org.jooq.impl.Factory.year;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Date;
|
||||
@ -762,14 +768,23 @@ extends BaseTest<A, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658, T725
|
||||
|
||||
// ... and the extract function
|
||||
// ----------------------------
|
||||
Field<Integer> year = extract(now, DatePart.YEAR).as("y");
|
||||
Field<Integer> month = extract(now, DatePart.MONTH).as("m");
|
||||
Field<Integer> day = extract(now, DatePart.DAY).as("dd");
|
||||
Field<Integer> hour = extract(now, DatePart.HOUR).as("h");
|
||||
Field<Integer> minute = extract(now, DatePart.MINUTE).as("mn");
|
||||
Field<Integer> second = extract(now, DatePart.SECOND).as("sec");
|
||||
Field<Integer> year1 = extract(now, DatePart.YEAR).as("y1");
|
||||
Field<Integer> month1 = extract(now, DatePart.MONTH).as("m1");
|
||||
Field<Integer> day1 = extract(now, DatePart.DAY).as("dd1");
|
||||
Field<Integer> hour1 = extract(now, DatePart.HOUR).as("h1");
|
||||
Field<Integer> minute1 = extract(now, DatePart.MINUTE).as("mn1");
|
||||
Field<Integer> second1 = extract(now, DatePart.SECOND).as("sec1");
|
||||
|
||||
q1.addSelect(ts, date, time, year, month, day, hour, minute, second);
|
||||
Field<Integer> year2 = year(now).as("y2");
|
||||
Field<Integer> month2 = month(now).as("m2");
|
||||
Field<Integer> day2 = day(now).as("dd2");
|
||||
Field<Integer> hour2 = hour(now).as("h2");
|
||||
Field<Integer> minute2 = minute(now).as("mn2");
|
||||
Field<Integer> second2 = second(now).as("sec2");
|
||||
|
||||
q1.addSelect(ts, date, time,
|
||||
year1, month1, day1, hour1, minute1, second1,
|
||||
year2, month2, day2, hour2, minute2, second2);
|
||||
q1.execute();
|
||||
|
||||
Record record = q1.getResult().get(0);
|
||||
@ -784,12 +799,19 @@ extends BaseTest<A, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658, T725
|
||||
assertEquals(timestamp.split(" ")[1], record.getValue(time).toString());
|
||||
}
|
||||
|
||||
assertEquals(Integer.valueOf(timestamp.split(" ")[0].split("-")[0]), record.getValue(year));
|
||||
assertEquals(Integer.valueOf(timestamp.split(" ")[0].split("-")[1]), record.getValue(month));
|
||||
assertEquals(Integer.valueOf(timestamp.split(" ")[0].split("-")[2]), record.getValue(day));
|
||||
assertEquals(Integer.valueOf(timestamp.split(" ")[1].split(":")[0]), record.getValue(hour));
|
||||
assertEquals(Integer.valueOf(timestamp.split(" ")[1].split(":")[1]), record.getValue(minute));
|
||||
assertEquals(Integer.valueOf(timestamp.split(" ")[1].split(":")[2].split("\\.")[0]), record.getValue(second));
|
||||
assertEquals(Integer.valueOf(timestamp.split(" ")[0].split("-")[0]), record.getValue(year1));
|
||||
assertEquals(Integer.valueOf(timestamp.split(" ")[0].split("-")[1]), record.getValue(month1));
|
||||
assertEquals(Integer.valueOf(timestamp.split(" ")[0].split("-")[2]), record.getValue(day1));
|
||||
assertEquals(Integer.valueOf(timestamp.split(" ")[1].split(":")[0]), record.getValue(hour1));
|
||||
assertEquals(Integer.valueOf(timestamp.split(" ")[1].split(":")[1]), record.getValue(minute1));
|
||||
assertEquals(Integer.valueOf(timestamp.split(" ")[1].split(":")[2].split("\\.")[0]), record.getValue(second1));
|
||||
|
||||
assertEquals(record.getValue(year1), record.getValue(year2));
|
||||
assertEquals(record.getValue(month1), record.getValue(month2));
|
||||
assertEquals(record.getValue(day1), record.getValue(day2));
|
||||
assertEquals(record.getValue(hour1), record.getValue(hour2));
|
||||
assertEquals(record.getValue(minute1), record.getValue(minute2));
|
||||
assertEquals(record.getValue(second1), record.getValue(second2));
|
||||
|
||||
// Timestamp arithmetic
|
||||
// --------------------
|
||||
|
||||
@ -2332,6 +2332,136 @@ public class Factory implements FactoryOperations {
|
||||
// XXX Date and time functions
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Get the current_date() function
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*/
|
||||
@Support
|
||||
public static Field<Date> currentDate() {
|
||||
return new CurrentDate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current_time() function
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*/
|
||||
@Support
|
||||
public static Field<Time> currentTime() {
|
||||
return new CurrentTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current_timestamp() function
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*/
|
||||
@Support
|
||||
public static Field<Timestamp> currentTimestamp() {
|
||||
return new CurrentTimestamp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the date difference in number of days
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*
|
||||
* @see Field#sub(Field)
|
||||
*/
|
||||
@Support
|
||||
public static Field<Integer> dateDiff(Date date1, Date date2) {
|
||||
return dateDiff(val(date1), val(date2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the date difference in number of days
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*
|
||||
* @see Field#sub(Field)
|
||||
*/
|
||||
@Support
|
||||
public static Field<Integer> dateDiff(Field<Date> date1, Date date2) {
|
||||
return dateDiff(nullSafe(date1), val(date2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the date difference in number of days
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*
|
||||
* @see Field#sub(Field)
|
||||
*/
|
||||
@Support
|
||||
public static Field<Integer> dateDiff(Date date1, Field<Date> date2) {
|
||||
return dateDiff(val(date1), nullSafe(date2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the date difference in number of days
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*
|
||||
* @see Field#sub(Field)
|
||||
*/
|
||||
@Support
|
||||
public static Field<Integer> dateDiff(Field<Date> date1, Field<Date> date2) {
|
||||
return new DateDiff(nullSafe(date1), nullSafe(date2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the timestamp difference as a <code>INTERVAL DAY TO SECOND</code>
|
||||
* type
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*
|
||||
* @see Field#sub(Field)
|
||||
*/
|
||||
@Support
|
||||
public static Field<DayToSecond> timestampDiff(Timestamp timestamp1, Timestamp timestamp2) {
|
||||
return timestampDiff(val(timestamp1), val(timestamp2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the timestamp difference as a <code>INTERVAL DAY TO SECOND</code>
|
||||
* type
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*
|
||||
* @see Field#sub(Field)
|
||||
*/
|
||||
@Support
|
||||
public static Field<DayToSecond> timestampDiff(Field<Timestamp> timestamp1, Timestamp timestamp2) {
|
||||
return timestampDiff(nullSafe(timestamp1), val(timestamp2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the timestamp difference as a <code>INTERVAL DAY TO SECOND</code>
|
||||
* type
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*
|
||||
* @see Field#sub(Field)
|
||||
*/
|
||||
@Support
|
||||
public static Field<DayToSecond> timestampDiff(Timestamp timestamp1, Field<Timestamp> timestamp2) {
|
||||
return timestampDiff(val(timestamp1), nullSafe(timestamp2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the timestamp difference as a <code>INTERVAL DAY TO SECOND</code>
|
||||
* type
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*
|
||||
* @see Field#sub(Field)
|
||||
*/
|
||||
@Support
|
||||
public static Field<DayToSecond> timestampDiff(Field<Timestamp> timestamp1, Field<Timestamp> timestamp2) {
|
||||
return new TimestampDiff(timestamp1, timestamp2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the extract(field, datePart) function
|
||||
* <p>
|
||||
@ -2352,6 +2482,138 @@ public class Factory implements FactoryOperations {
|
||||
return new Extract(nullSafe(field), datePart);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the year part of a date
|
||||
* <p>
|
||||
* This is the same as calling {@link #extract(java.util.Date, DatePart)}
|
||||
* with {@link DatePart#YEAR}
|
||||
*/
|
||||
@Support
|
||||
public static Field<Integer> year(java.util.Date value) {
|
||||
return extract(value, DatePart.YEAR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the year part of a date
|
||||
* <p>
|
||||
* This is the same as calling {@link #extract(java.util.Field, DatePart)}
|
||||
* with {@link DatePart#YEAR}
|
||||
*/
|
||||
@Support
|
||||
public static Field<Integer> year(Field<? extends java.util.Date> field) {
|
||||
return extract(field, DatePart.YEAR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the month part of a date
|
||||
* <p>
|
||||
* This is the same as calling {@link #extract(java.util.Date, DatePart)}
|
||||
* with {@link DatePart#MONTH}
|
||||
*/
|
||||
@Support
|
||||
public static Field<Integer> month(java.util.Date value) {
|
||||
return extract(value, DatePart.MONTH);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the month part of a date
|
||||
* <p>
|
||||
* This is the same as calling {@link #extract(java.util.Field, DatePart)}
|
||||
* with {@link DatePart#MONTH}
|
||||
*/
|
||||
@Support
|
||||
public static Field<Integer> month(Field<? extends java.util.Date> field) {
|
||||
return extract(field, DatePart.MONTH);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the day part of a date
|
||||
* <p>
|
||||
* This is the same as calling {@link #extract(java.util.Date, DatePart)}
|
||||
* with {@link DatePart#DAY}
|
||||
*/
|
||||
@Support
|
||||
public static Field<Integer> day(java.util.Date value) {
|
||||
return extract(value, DatePart.DAY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the day part of a date
|
||||
* <p>
|
||||
* This is the same as calling {@link #extract(java.util.Field, DatePart)}
|
||||
* with {@link DatePart#DAY}
|
||||
*/
|
||||
@Support
|
||||
public static Field<Integer> day(Field<? extends java.util.Date> field) {
|
||||
return extract(field, DatePart.DAY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the hour part of a date
|
||||
* <p>
|
||||
* This is the same as calling {@link #extract(java.util.Date, DatePart)}
|
||||
* with {@link DatePart#HOUR}
|
||||
*/
|
||||
@Support
|
||||
public static Field<Integer> hour(java.util.Date value) {
|
||||
return extract(value, DatePart.HOUR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the hour part of a date
|
||||
* <p>
|
||||
* This is the same as calling {@link #extract(java.util.Field, DatePart)}
|
||||
* with {@link DatePart#HOUR}
|
||||
*/
|
||||
@Support
|
||||
public static Field<Integer> hour(Field<? extends java.util.Date> field) {
|
||||
return extract(field, DatePart.HOUR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the minute part of a date
|
||||
* <p>
|
||||
* This is the same as calling {@link #extract(java.util.Date, DatePart)}
|
||||
* with {@link DatePart#MINUTE}
|
||||
*/
|
||||
@Support
|
||||
public static Field<Integer> minute(java.util.Date value) {
|
||||
return extract(value, DatePart.MINUTE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the minute part of a date
|
||||
* <p>
|
||||
* This is the same as calling {@link #extract(java.util.Field, DatePart)}
|
||||
* with {@link DatePart#MINUTE}
|
||||
*/
|
||||
@Support
|
||||
public static Field<Integer> minute(Field<? extends java.util.Date> field) {
|
||||
return extract(field, DatePart.MINUTE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the second part of a date
|
||||
* <p>
|
||||
* This is the same as calling {@link #extract(java.util.Date, DatePart)}
|
||||
* with {@link DatePart#SECOND}
|
||||
*/
|
||||
@Support
|
||||
public static Field<Integer> second(java.util.Date value) {
|
||||
return extract(value, DatePart.SECOND);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the second part of a date
|
||||
* <p>
|
||||
* This is the same as calling {@link #extract(java.util.Field, DatePart)}
|
||||
* with {@link DatePart#SECOND}
|
||||
*/
|
||||
@Support
|
||||
public static Field<Integer> second(Field<? extends java.util.Date> field) {
|
||||
return extract(field, DatePart.SECOND);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// XXX Construction of special grouping functions
|
||||
// ------------------------------------------------------------------------
|
||||
@ -4503,140 +4765,6 @@ public class Factory implements FactoryOperations {
|
||||
return new Euler();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX date time functions
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Get the current_date() function
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*/
|
||||
@Support
|
||||
public static Field<Date> currentDate() {
|
||||
return new CurrentDate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current_time() function
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*/
|
||||
@Support
|
||||
public static Field<Time> currentTime() {
|
||||
return new CurrentTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current_timestamp() function
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*/
|
||||
@Support
|
||||
public static Field<Timestamp> currentTimestamp() {
|
||||
return new CurrentTimestamp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the date difference in number of days
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*
|
||||
* @see Field#sub(Field)
|
||||
*/
|
||||
@Support
|
||||
public static Field<Integer> dateDiff(Date date1, Date date2) {
|
||||
return dateDiff(val(date1), val(date2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the date difference in number of days
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*
|
||||
* @see Field#sub(Field)
|
||||
*/
|
||||
@Support
|
||||
public static Field<Integer> dateDiff(Field<Date> date1, Date date2) {
|
||||
return dateDiff(nullSafe(date1), val(date2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the date difference in number of days
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*
|
||||
* @see Field#sub(Field)
|
||||
*/
|
||||
@Support
|
||||
public static Field<Integer> dateDiff(Date date1, Field<Date> date2) {
|
||||
return dateDiff(val(date1), nullSafe(date2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the date difference in number of days
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*
|
||||
* @see Field#sub(Field)
|
||||
*/
|
||||
@Support
|
||||
public static Field<Integer> dateDiff(Field<Date> date1, Field<Date> date2) {
|
||||
return new DateDiff(nullSafe(date1), nullSafe(date2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the timestamp difference as a <code>INTERVAL DAY TO SECOND</code>
|
||||
* type
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*
|
||||
* @see Field#sub(Field)
|
||||
*/
|
||||
@Support
|
||||
public static Field<DayToSecond> timestampDiff(Timestamp timestamp1, Timestamp timestamp2) {
|
||||
return timestampDiff(val(timestamp1), val(timestamp2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the timestamp difference as a <code>INTERVAL DAY TO SECOND</code>
|
||||
* type
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*
|
||||
* @see Field#sub(Field)
|
||||
*/
|
||||
@Support
|
||||
public static Field<DayToSecond> timestampDiff(Field<Timestamp> timestamp1, Timestamp timestamp2) {
|
||||
return timestampDiff(nullSafe(timestamp1), val(timestamp2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the timestamp difference as a <code>INTERVAL DAY TO SECOND</code>
|
||||
* type
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*
|
||||
* @see Field#sub(Field)
|
||||
*/
|
||||
@Support
|
||||
public static Field<DayToSecond> timestampDiff(Timestamp timestamp1, Field<Timestamp> timestamp2) {
|
||||
return timestampDiff(val(timestamp1), nullSafe(timestamp2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the timestamp difference as a <code>INTERVAL DAY TO SECOND</code>
|
||||
* type
|
||||
* <p>
|
||||
* This translates into any dialect
|
||||
*
|
||||
* @see Field#sub(Field)
|
||||
*/
|
||||
@Support
|
||||
public static Field<DayToSecond> timestampDiff(Field<Timestamp> timestamp1, Field<Timestamp> timestamp2) {
|
||||
return new TimestampDiff(timestamp1, timestamp2);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX other functions
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@ -376,6 +376,24 @@ 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));
|
||||
|
||||
Loading…
Reference in New Issue
Block a user