From d3d65b4aac55d0c91394ff6a1183a658bbd797d7 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Thu, 5 Apr 2012 06:01:59 +0000 Subject: [PATCH] [#1269] Add YEAR(), MONTH(), DAY(), HOUR(), MINUTE(), SECOND() function support as shortcuts for EXTRACT() --- .../jooq/test/_/testcases/FunctionTests.java | 48 ++- jOOQ/src/main/java/org/jooq/impl/Factory.java | 396 ++++++++++++------ .../src/test/java/org/jooq/test/jOOQTest.java | 18 + 3 files changed, 315 insertions(+), 147 deletions(-) diff --git a/jOOQ-test/src/org/jooq/test/_/testcases/FunctionTests.java b/jOOQ-test/src/org/jooq/test/_/testcases/FunctionTests.java index cb9befa2d4..f6c8fa617c 100644 --- a/jOOQ-test/src/org/jooq/test/_/testcases/FunctionTests.java +++ b/jOOQ-test/src/org/jooq/test/_/testcases/FunctionTests.java @@ -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 year = extract(now, DatePart.YEAR).as("y"); - Field month = extract(now, DatePart.MONTH).as("m"); - Field day = extract(now, DatePart.DAY).as("dd"); - Field hour = extract(now, DatePart.HOUR).as("h"); - Field minute = extract(now, DatePart.MINUTE).as("mn"); - Field second = extract(now, DatePart.SECOND).as("sec"); + Field year1 = extract(now, DatePart.YEAR).as("y1"); + Field month1 = extract(now, DatePart.MONTH).as("m1"); + Field day1 = extract(now, DatePart.DAY).as("dd1"); + Field hour1 = extract(now, DatePart.HOUR).as("h1"); + Field minute1 = extract(now, DatePart.MINUTE).as("mn1"); + Field second1 = extract(now, DatePart.SECOND).as("sec1"); - q1.addSelect(ts, date, time, year, month, day, hour, minute, second); + Field year2 = year(now).as("y2"); + Field month2 = month(now).as("m2"); + Field day2 = day(now).as("dd2"); + Field hour2 = hour(now).as("h2"); + Field minute2 = minute(now).as("mn2"); + Field 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 + * This translates into any dialect + */ + @Support + public static Field currentDate() { + return new CurrentDate(); + } + + /** + * Get the current_time() function + *

+ * This translates into any dialect + */ + @Support + public static Field

+ * This translates into any dialect + */ + @Support + public static Field currentTimestamp() { + return new CurrentTimestamp(); + } + + /** + * Get the date difference in number of days + *

+ * This translates into any dialect + * + * @see Field#sub(Field) + */ + @Support + public static Field dateDiff(Date date1, Date date2) { + return dateDiff(val(date1), val(date2)); + } + + /** + * Get the date difference in number of days + *

+ * This translates into any dialect + * + * @see Field#sub(Field) + */ + @Support + public static Field dateDiff(Field date1, Date date2) { + return dateDiff(nullSafe(date1), val(date2)); + } + + /** + * Get the date difference in number of days + *

+ * This translates into any dialect + * + * @see Field#sub(Field) + */ + @Support + public static Field dateDiff(Date date1, Field date2) { + return dateDiff(val(date1), nullSafe(date2)); + } + + /** + * Get the date difference in number of days + *

+ * This translates into any dialect + * + * @see Field#sub(Field) + */ + @Support + public static Field dateDiff(Field date1, Field date2) { + return new DateDiff(nullSafe(date1), nullSafe(date2)); + } + + /** + * Get the timestamp difference as a INTERVAL DAY TO SECOND + * type + *

+ * This translates into any dialect + * + * @see Field#sub(Field) + */ + @Support + public static Field timestampDiff(Timestamp timestamp1, Timestamp timestamp2) { + return timestampDiff(val(timestamp1), val(timestamp2)); + } + + /** + * Get the timestamp difference as a INTERVAL DAY TO SECOND + * type + *

+ * This translates into any dialect + * + * @see Field#sub(Field) + */ + @Support + public static Field timestampDiff(Field timestamp1, Timestamp timestamp2) { + return timestampDiff(nullSafe(timestamp1), val(timestamp2)); + } + + /** + * Get the timestamp difference as a INTERVAL DAY TO SECOND + * type + *

+ * This translates into any dialect + * + * @see Field#sub(Field) + */ + @Support + public static Field timestampDiff(Timestamp timestamp1, Field timestamp2) { + return timestampDiff(val(timestamp1), nullSafe(timestamp2)); + } + + /** + * Get the timestamp difference as a INTERVAL DAY TO SECOND + * type + *

+ * This translates into any dialect + * + * @see Field#sub(Field) + */ + @Support + public static Field timestampDiff(Field timestamp1, Field timestamp2) { + return new TimestampDiff(timestamp1, timestamp2); + } + /** * Get the extract(field, datePart) function *

@@ -2352,6 +2482,138 @@ public class Factory implements FactoryOperations { return new Extract(nullSafe(field), datePart); } + /** + * Get the year part of a date + *

+ * This is the same as calling {@link #extract(java.util.Date, DatePart)} + * with {@link DatePart#YEAR} + */ + @Support + public static Field year(java.util.Date value) { + return extract(value, DatePart.YEAR); + } + + /** + * Get the year part of a date + *

+ * This is the same as calling {@link #extract(java.util.Field, DatePart)} + * with {@link DatePart#YEAR} + */ + @Support + public static Field year(Field field) { + return extract(field, DatePart.YEAR); + } + + /** + * Get the month part of a date + *

+ * This is the same as calling {@link #extract(java.util.Date, DatePart)} + * with {@link DatePart#MONTH} + */ + @Support + public static Field month(java.util.Date value) { + return extract(value, DatePart.MONTH); + } + + /** + * Get the month part of a date + *

+ * This is the same as calling {@link #extract(java.util.Field, DatePart)} + * with {@link DatePart#MONTH} + */ + @Support + public static Field month(Field field) { + return extract(field, DatePart.MONTH); + } + + /** + * Get the day part of a date + *

+ * This is the same as calling {@link #extract(java.util.Date, DatePart)} + * with {@link DatePart#DAY} + */ + @Support + public static Field day(java.util.Date value) { + return extract(value, DatePart.DAY); + } + + /** + * Get the day part of a date + *

+ * This is the same as calling {@link #extract(java.util.Field, DatePart)} + * with {@link DatePart#DAY} + */ + @Support + public static Field day(Field field) { + return extract(field, DatePart.DAY); + } + + /** + * Get the hour part of a date + *

+ * This is the same as calling {@link #extract(java.util.Date, DatePart)} + * with {@link DatePart#HOUR} + */ + @Support + public static Field hour(java.util.Date value) { + return extract(value, DatePart.HOUR); + } + + /** + * Get the hour part of a date + *

+ * This is the same as calling {@link #extract(java.util.Field, DatePart)} + * with {@link DatePart#HOUR} + */ + @Support + public static Field hour(Field field) { + return extract(field, DatePart.HOUR); + } + + /** + * Get the minute part of a date + *

+ * This is the same as calling {@link #extract(java.util.Date, DatePart)} + * with {@link DatePart#MINUTE} + */ + @Support + public static Field minute(java.util.Date value) { + return extract(value, DatePart.MINUTE); + } + + /** + * Get the minute part of a date + *

+ * This is the same as calling {@link #extract(java.util.Field, DatePart)} + * with {@link DatePart#MINUTE} + */ + @Support + public static Field minute(Field field) { + return extract(field, DatePart.MINUTE); + } + + /** + * Get the second part of a date + *

+ * This is the same as calling {@link #extract(java.util.Date, DatePart)} + * with {@link DatePart#SECOND} + */ + @Support + public static Field second(java.util.Date value) { + return extract(value, DatePart.SECOND); + } + + /** + * Get the second part of a date + *

+ * This is the same as calling {@link #extract(java.util.Field, DatePart)} + * with {@link DatePart#SECOND} + */ + @Support + public static Field second(Field 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 - *

- * This translates into any dialect - */ - @Support - public static Field currentDate() { - return new CurrentDate(); - } - - /** - * Get the current_time() function - *

- * This translates into any dialect - */ - @Support - public static Field

- * This translates into any dialect - */ - @Support - public static Field currentTimestamp() { - return new CurrentTimestamp(); - } - - /** - * Get the date difference in number of days - *

- * This translates into any dialect - * - * @see Field#sub(Field) - */ - @Support - public static Field dateDiff(Date date1, Date date2) { - return dateDiff(val(date1), val(date2)); - } - - /** - * Get the date difference in number of days - *

- * This translates into any dialect - * - * @see Field#sub(Field) - */ - @Support - public static Field dateDiff(Field date1, Date date2) { - return dateDiff(nullSafe(date1), val(date2)); - } - - /** - * Get the date difference in number of days - *

- * This translates into any dialect - * - * @see Field#sub(Field) - */ - @Support - public static Field dateDiff(Date date1, Field date2) { - return dateDiff(val(date1), nullSafe(date2)); - } - - /** - * Get the date difference in number of days - *

- * This translates into any dialect - * - * @see Field#sub(Field) - */ - @Support - public static Field dateDiff(Field date1, Field date2) { - return new DateDiff(nullSafe(date1), nullSafe(date2)); - } - - /** - * Get the timestamp difference as a INTERVAL DAY TO SECOND - * type - *

- * This translates into any dialect - * - * @see Field#sub(Field) - */ - @Support - public static Field timestampDiff(Timestamp timestamp1, Timestamp timestamp2) { - return timestampDiff(val(timestamp1), val(timestamp2)); - } - - /** - * Get the timestamp difference as a INTERVAL DAY TO SECOND - * type - *

- * This translates into any dialect - * - * @see Field#sub(Field) - */ - @Support - public static Field timestampDiff(Field timestamp1, Timestamp timestamp2) { - return timestampDiff(nullSafe(timestamp1), val(timestamp2)); - } - - /** - * Get the timestamp difference as a INTERVAL DAY TO SECOND - * type - *

- * This translates into any dialect - * - * @see Field#sub(Field) - */ - @Support - public static Field timestampDiff(Timestamp timestamp1, Field timestamp2) { - return timestampDiff(val(timestamp1), nullSafe(timestamp2)); - } - - /** - * Get the timestamp difference as a INTERVAL DAY TO SECOND - * type - *

- * This translates into any dialect - * - * @see Field#sub(Field) - */ - @Support - public static Field timestampDiff(Field timestamp1, Field timestamp2) { - return new TimestampDiff(timestamp1, timestamp2); - } - // ------------------------------------------------------------------------- // XXX other functions // ------------------------------------------------------------------------- diff --git a/jOOQ/src/test/java/org/jooq/test/jOOQTest.java b/jOOQ/src/test/java/org/jooq/test/jOOQTest.java index c2d7c998e7..e91617ed98 100644 --- a/jOOQ/src/test/java/org/jooq/test/jOOQTest.java +++ b/jOOQ/src/test/java/org/jooq/test/jOOQTest.java @@ -376,6 +376,24 @@ public class jOOQTest { assertEquals( Factory.extract((java.util.Date) null, DatePart.DAY), Factory.extract((Field) null, DatePart.DAY)); + assertEquals( + Factory.year((java.util.Date) null), + Factory.year((Field) null)); + assertEquals( + Factory.month((java.util.Date) null), + Factory.month((Field) null)); + assertEquals( + Factory.day((java.util.Date) null), + Factory.day((Field) null)); + assertEquals( + Factory.hour((java.util.Date) null), + Factory.hour((Field) null)); + assertEquals( + Factory.minute((java.util.Date) null), + Factory.minute((Field) null)); + assertEquals( + Factory.second((java.util.Date) null), + Factory.second((Field) null)); assertEquals( Factory.floor((Integer) null), Factory.floor((Field) null));