[jOOQ/jOOQ#7900] Improved parameter names

MySQL and SQL Server do not agree on the order of parameters (MySQL
doesn't even agree with itself between DATEDIFF and TIMESTAMPDIFF!) We
need to name parameters to make this clear, and replicate the order of
SQL Server and MySQL's TIMESTAMPDIFF() in the new function.
This commit is contained in:
Lukas Eder 2020-06-05 14:43:52 +02:00
parent 754f1030fc
commit bf1def05b4
2 changed files with 112 additions and 80 deletions

View File

@ -15274,83 +15274,99 @@ public class DSL {
/**
* Get the date difference between <code>date1 - date2</code> in number of
* days.
* Get the date difference between <code>endDate - startDate</code> in
* number of days.
*
* @see Field#sub(Field)
*/
@Support
public static Field<Integer> dateDiff(Date date1, Date date2) {
return dateDiff(Tools.field(date1), Tools.field(date2));
public static Field<Integer> dateDiff(Date endDate, Date startDate) {
return dateDiff(Tools.field(endDate), Tools.field(startDate));
}
/**
* Get the date difference between <code>date1 - date2</code> in number of
* days.
* Get the date difference between <code>endDate - startDate</code> in
* number of days.
*
* @see Field#sub(Field)
*/
@Support
public static Field<Integer> dateDiff(Field<Date> date1, Date date2) {
return dateDiff(nullSafe(date1), Tools.field(date2));
public static Field<Integer> dateDiff(Field<Date> endDate, Date startDate) {
return dateDiff(nullSafe(endDate), Tools.field(startDate));
}
/**
* Get the date difference between <code>date1 - date2</code> in number of
* days.
* Get the date difference between <code>endDate - startDate</code> in
* number of days.
*
* @see Field#sub(Field)
*/
@Support
public static Field<Integer> dateDiff(Date date1, Field<Date> date2) {
return dateDiff(Tools.field(date1), nullSafe(date2));
public static Field<Integer> dateDiff(Date endDate, Field<Date> startDate) {
return dateDiff(Tools.field(endDate), nullSafe(startDate));
}
/**
* Get the date difference between <code>date1 - date2</code> in number of
* days.
* Get the date difference between <code>endDate - startDate</code> in
* number of days.
*
* @see Field#sub(Field)
*/
@Support
public static Field<Integer> dateDiff(Field<Date> date1, Field<Date> date2) {
return new DateDiff<>(null, nullSafe(date1), nullSafe(date2));
public static Field<Integer> dateDiff(Field<Date> endDate, Field<Date> startDate) {
return new DateDiff<>(null, nullSafe(startDate), nullSafe(endDate));
}
/**
* Get the date difference between <code>date1 - date2</code> in terms of
* <code>part</code>.
* Get the date difference between <code>endDate - startDate</code> in terms
* of <code>part</code>.
* <p>
* For example, <code>DATEDIFF(YEAR, '2000-03-01', '2002-01-01') = 2</code>,
* despite there being less than 2 years between the two days. The behaviour
* replicates that of SQL Server.
*/
@Support({ FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
public static Field<Integer> dateDiff(DatePart part, Date date1, Date date2) {
return dateDiff(part, Tools.field(date1), Tools.field(date2));
public static Field<Integer> dateDiff(DatePart part, Date startDate, Date endDate) {
return dateDiff(part, Tools.field(startDate), Tools.field(endDate));
}
/**
* Get the date difference between <code>date1 - date2</code> in terms of
* <code>part</code>.
* Get the date difference between <code>endDate - startDate</code> in terms
* of <code>part</code>.
* <p>
* For example, <code>DATEDIFF(YEAR, '2000-03-01', '2002-01-01') = 2</code>,
* despite there being less than 2 years between the two days. The behaviour
* replicates that of SQL Server.
*/
@Support({ FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
public static Field<Integer> dateDiff(DatePart part, Field<Date> date1, Date date2) {
return dateDiff(part, nullSafe(date1), Tools.field(date2));
public static Field<Integer> dateDiff(DatePart part, Field<Date> startDate, Date endDate) {
return dateDiff(part, nullSafe(startDate), Tools.field(endDate));
}
/**
* Get the date difference between <code>date1 - date2</code> in terms of
* <code>part</code>.
* Get the date difference between <code>endDate - startDate</code> in terms
* of <code>part</code>.
* <p>
* For example, <code>DATEDIFF(YEAR, '2000-03-01', '2002-01-01') = 2</code>,
* despite there being less than 2 years between the two days. The behaviour
* replicates that of SQL Server.
*/
@Support({ FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
public static Field<Integer> dateDiff(DatePart part, Date date1, Field<Date> date2) {
return dateDiff(part, Tools.field(date1), nullSafe(date2));
public static Field<Integer> dateDiff(DatePart part, Date startDate, Field<Date> endDate) {
return dateDiff(part, Tools.field(startDate), nullSafe(endDate));
}
/**
* Get the date difference between <code>date1 - date2</code> in terms of
* <code>part</code>.
* Get the date difference between <code>endDate - startDate</code> in terms
* of <code>part</code>.
* <p>
* For example, <code>DATEDIFF(YEAR, '2000-03-01', '2002-01-01') = 2</code>,
* despite there being less than 2 years between the two days. The behaviour
* replicates that of SQL Server.
*/
@Support({ FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
public static Field<Integer> dateDiff(DatePart part, Field<Date> date1, Field<Date> date2) {
return new DateDiff<>(part, nullSafe(date1), nullSafe(date2));
public static Field<Integer> dateDiff(DatePart part, Field<Date> startDate, Field<Date> endDate) {
return new DateDiff<>(part, nullSafe(startDate), nullSafe(endDate));
}
/**
@ -15664,83 +15680,99 @@ public class DSL {
/**
* Get the date difference between <code>date1 - date2</code> in number of
* days.
* Get the date difference between <code>endDate - startDate</code> in
* number of days.
*
* @see Field#sub(Field)
*/
@Support
public static Field<Integer> localDateDiff(LocalDate date1, LocalDate date2) {
return localDateDiff(Tools.field(date1), Tools.field(date2));
public static Field<Integer> localDateDiff(LocalDate endDate, LocalDate startDate) {
return localDateDiff(Tools.field(endDate), Tools.field(startDate));
}
/**
* Get the date difference between <code>date1 - date2</code> in number of
* days.
* Get the date difference between <code>endDate - startDate</code> in
* number of days.
*
* @see Field#sub(Field)
*/
@Support
public static Field<Integer> localDateDiff(Field<LocalDate> date1, LocalDate date2) {
return localDateDiff(nullSafe(date1), Tools.field(date2));
public static Field<Integer> localDateDiff(Field<LocalDate> endDate, LocalDate startDate) {
return localDateDiff(nullSafe(endDate), Tools.field(startDate));
}
/**
* Get the date difference between <code>date1 - date2</code> in number of
* days.
* Get the date difference between <code>endDate - startDate</code> in
* number of days.
*
* @see Field#sub(Field)
*/
@Support
public static Field<Integer> localDateDiff(LocalDate date1, Field<LocalDate> date2) {
return localDateDiff(Tools.field(date1), nullSafe(date2));
public static Field<Integer> localDateDiff(LocalDate endDate, Field<LocalDate> startDate) {
return localDateDiff(Tools.field(endDate), nullSafe(startDate));
}
/**
* Get the date difference between <code>date1 - date2</code> in number of
* days.
* Get the date difference between <code>endDate - startDate</code> in
* number of days.
*
* @see Field#sub(Field)
*/
@Support
public static Field<Integer> localDateDiff(Field<LocalDate> date1, Field<LocalDate> date2) {
return new DateDiff<>(null, nullSafe(date1), nullSafe(date2));
public static Field<Integer> localDateDiff(Field<LocalDate> endDate, Field<LocalDate> startDate) {
return new DateDiff<>(null, nullSafe(startDate), nullSafe(endDate));
}
/**
* Get the date difference between <code>date1 - date2</code> in terms of
* <code>part</code>.
* Get the date difference between <code>endDate - startDate</code> in terms
* of <code>part</code>.
* <p>
* For example, <code>DATEDIFF(YEAR, '2000-03-01', '2002-01-01') = 2</code>,
* despite there being less than 2 years between the two days. The behaviour
* replicates that of SQL Server.
*/
@Support({ FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
public static Field<Integer> localDateDiff(DatePart part, LocalDate date1, LocalDate date2) {
return localDateDiff(part, Tools.field(date1), Tools.field(date2));
public static Field<Integer> localDateDiff(DatePart part, LocalDate startDate, LocalDate endDate) {
return localDateDiff(part, Tools.field(startDate), Tools.field(endDate));
}
/**
* Get the date difference between <code>date1 - date2</code> in terms of
* <code>part</code>.
* Get the date difference between <code>endDate - startDate</code> in terms
* of <code>part</code>.
* <p>
* For example, <code>DATEDIFF(YEAR, '2000-03-01', '2002-01-01') = 2</code>,
* despite there being less than 2 years between the two days. The behaviour
* replicates that of SQL Server.
*/
@Support({ FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
public static Field<Integer> localDateDiff(DatePart part, Field<LocalDate> date1, LocalDate date2) {
return localDateDiff(part, nullSafe(date1), Tools.field(date2));
public static Field<Integer> localDateDiff(DatePart part, Field<LocalDate> startDate, LocalDate endDate) {
return localDateDiff(part, nullSafe(startDate), Tools.field(endDate));
}
/**
* Get the date difference between <code>date1 - date2</code> in terms of
* <code>part</code>.
* Get the date difference between <code>endDate - startDate</code> in terms
* of <code>part</code>.
* <p>
* For example, <code>DATEDIFF(YEAR, '2000-03-01', '2002-01-01') = 2</code>,
* despite there being less than 2 years between the two days. The behaviour
* replicates that of SQL Server.
*/
@Support({ FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
public static Field<Integer> localDateDiff(DatePart part, LocalDate date1, Field<LocalDate> date2) {
return localDateDiff(part, Tools.field(date1), nullSafe(date2));
public static Field<Integer> localDateDiff(DatePart part, LocalDate startDate, Field<LocalDate> endDate) {
return localDateDiff(part, Tools.field(startDate), nullSafe(endDate));
}
/**
* Get the date difference between <code>date1 - date2</code> in terms of
* <code>part</code>.
* Get the date difference between <code>endDate - startDate</code> in terms
* of <code>part</code>.
* <p>
* For example, <code>DATEDIFF(YEAR, '2000-03-01', '2002-01-01') = 2</code>,
* despite there being less than 2 years between the two days. The behaviour
* replicates that of SQL Server.
*/
@Support({ FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
public static Field<Integer> localDateDiff(DatePart part, Field<LocalDate> date1, Field<LocalDate> date2) {
return new DateDiff<>(part, nullSafe(date1), nullSafe(date2));
public static Field<Integer> localDateDiff(DatePart part, Field<LocalDate> startDate, Field<LocalDate> endDate) {
return new DateDiff<>(part, nullSafe(startDate), nullSafe(endDate));
}
/**

View File

@ -72,15 +72,15 @@ final class DateDiff<T> extends AbstractField<Integer> {
private static final long serialVersionUID = -4813228000332771961L;
private final DatePart part;
private final Field<T> date1;
private final Field<T> date2;
private final Field<T> startDate;
private final Field<T> endDate;
DateDiff(DatePart part, Field<T> date1, Field<T> date2) {
DateDiff(DatePart part, Field<T> startDate, Field<T> endDate) {
super(N_DATEDIFF, SQLDataType.INTEGER);
this.part = part;
this.date1 = date1;
this.date2 = date2;
this.startDate = startDate;
this.endDate = endDate;
}
@Override
@ -108,23 +108,23 @@ final class DateDiff<T> extends AbstractField<Integer> {
return;
case DAY:
ctx.visit(N_DATEDIFF).sql('(').visit(date1).sql(", ").visit(date2).sql(')');
ctx.visit(N_DATEDIFF).sql('(').visit(endDate).sql(", ").visit(startDate).sql(')');
return;
case MILLISECOND:
ctx.visit(new DateDiff<>(MICROSECOND, date1, date2).div(inline(1000)));
ctx.visit(new DateDiff<>(MICROSECOND, startDate, endDate).div(inline(1000)));
return;
case NANOSECOND:
ctx.visit(new DateDiff<>(MICROSECOND, date1, date2).times(inline(1000)));
ctx.visit(new DateDiff<>(MICROSECOND, startDate, endDate).times(inline(1000)));
return;
}
ctx.visit(N_TIMESTAMPDIFF).sql('(').visit(p.toName()).sql(", ").visit(date2).sql(", ").visit(date1).sql(')');
ctx.visit(N_TIMESTAMPDIFF).sql('(').visit(p.toName()).sql(", ").visit(startDate).sql(", ").visit(endDate).sql(')');
return;
case DERBY:
ctx.sql("{fn ").visit(N_TIMESTAMPDIFF).sql('(').visit(keyword("sql_tsi_day")).sql(", ").visit(date2).sql(", ").visit(date1).sql(") }");
ctx.sql("{fn ").visit(N_TIMESTAMPDIFF).sql('(').visit(keyword("sql_tsi_day")).sql(", ").visit(startDate).sql(", ").visit(endDate).sql(") }");
return;
@ -160,14 +160,14 @@ final class DateDiff<T> extends AbstractField<Integer> {
case MICROSECOND:
case NANOSECOND:
if (ctx.family() == HSQLDB) {
ctx.visit(N_DATEDIFF).sql('(').visit(p.toKeyword()).sql(", ").visit(date2.cast(TIMESTAMP)).sql(", ").visit(date1.cast(TIMESTAMP)).sql(')');
ctx.visit(N_DATEDIFF).sql('(').visit(p.toKeyword()).sql(", ").visit(startDate.cast(TIMESTAMP)).sql(", ").visit(endDate.cast(TIMESTAMP)).sql(')');
return;
}
break;
}
ctx.visit(N_DATEDIFF).sql('(').visit(p.toKeyword()).sql(", ").visit(date2).sql(", ").visit(date1).sql(')');
ctx.visit(N_DATEDIFF).sql('(').visit(p.toKeyword()).sql(", ").visit(startDate).sql(", ").visit(endDate).sql(')');
return;
@ -177,7 +177,7 @@ final class DateDiff<T> extends AbstractField<Integer> {
case SQLITE:
ctx.sql('(').visit(N_STRFTIME).sql("('%s', ").visit(date1).sql(") - ").visit(N_STRFTIME).sql("('%s', ").visit(date2).sql(")) / 86400");
ctx.sql('(').visit(N_STRFTIME).sql("('%s', ").visit(endDate).sql(") - ").visit(N_STRFTIME).sql("('%s', ").visit(startDate).sql(")) / 86400");
return;
@ -218,7 +218,7 @@ final class DateDiff<T> extends AbstractField<Integer> {
// [#4481] Parentheses are important in case this expression is
// placed in the context of other arithmetic
ctx.sql('(').visit(date1).sql(" - ").visit(date2).sql(')');
ctx.sql('(').visit(endDate).sql(" - ").visit(startDate).sql(')');
return;
}
@ -248,11 +248,11 @@ final class DateDiff<T> extends AbstractField<Integer> {
}
ctx.visit(castIfNeeded(date1.minus(date2), Integer.class));
ctx.visit(castIfNeeded(endDate.minus(startDate), Integer.class));
}
private final Field<Integer> partDiff(DatePart p) {
return DSL.extract(date1, p).minus(DSL.extract(date2, p));
return DSL.extract(endDate, p).minus(DSL.extract(startDate, p));
}
/**