nz.co.gregs.dbvolution.expressions.DateExpression Maven / Gradle / Ivy
/*
* Copyright 2014 Gregory Graham.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nz.co.gregs.dbvolution.expressions;
import nz.co.gregs.dbvolution.results.DateRepeatResult;
import nz.co.gregs.dbvolution.results.DateResult;
import nz.co.gregs.dbvolution.results.NumberResult;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import nz.co.gregs.dbvolution.databases.definitions.DBDefinition;
import nz.co.gregs.dbvolution.DBReport;
import nz.co.gregs.dbvolution.DBRow;
import nz.co.gregs.dbvolution.databases.supports.SupportsDateRepeatDatatypeFunctions;
import nz.co.gregs.dbvolution.datatypes.*;
import nz.co.gregs.dbvolution.results.AnyResult;
import nz.co.gregs.dbvolution.results.IntegerResult;
import org.joda.time.Period;
/**
* DateExpression implements standard functions that produce a Date or Time
* result.
*
*
* Date and Time are considered synonymous with timestamp as that appears to be
* the standard usage by developers. So every date has a time component and
* every time has a date component. {@link DBDateOnly} implements a time-less
* date for DBvolution but is considered a DBDate with a time of Midnight for
* DateExpression purposes.
*
*
* Most query requirements are provided by {@link QueryableDatatype}s like
* {@link DBString} or {@link DBInteger} but expressions can provide more
* functions or more precise control.
*
*
* Use a DateExpression to produce a date from an existing column, expression or
* value and perform date arithmetic.
*
*
* Generally you get a DateExpression from a column or value using
* {@link DateExpression#value(java.util.Date) } or
* {@link DBRow#column(nz.co.gregs.dbvolution.datatypes.DBDate)}.
*
*
Support DBvolution at
* Patreon
*
* @author Gregory Graham
*/
public class DateExpression extends RangeExpression implements DateResult {
/**
* The integer used to represent the index for Sunday
*/
static final public Number SUNDAY = 1;
/**
* The integer used to represent the index for Monday
*/
static final public Number MONDAY = 2;
/**
* The integer used to represent the index for Tuesday
*/
static final public Number TUESDAY = 3;
/**
* The integer used to represent the index for Wednesday
*/
static final public Number WEDNESDAY = 4;
/**
* The integer used to represent the index for Thursday
*/
static final public Number THURSDAY = 5;
/**
* The integer used to represent the index for Friday
*/
static final public Number FRIDAY = 6;
/**
* The integer used to represent the index for Saturday
*/
static final public Number SATURDAY = 7;
/**
* Default Constructor
*/
protected DateExpression() {
super();
}
/**
* Create a DateExpression based on an existing {@link DateResult}.
*
*
* {@link DateResult} is generally a DateExpression but it may also be a
* {@link DBDate} or {@link DBDateOnly}.
*
* @param dateVariable a date expression or QueryableDatatype
*/
public DateExpression(DateResult dateVariable) {
super(dateVariable);
}
/**
* Create a DateExpression based on an existing {@link DateResult}.
*
*
* {@link DateResult} is generally a DateExpression but it may also be a
* {@link DBDate} or {@link DBDateOnly}.
*
* @param variable a date expression or QueryableDatatype
*/
protected DateExpression(AnyResult> variable) {
super(variable);
}
/**
* Create a DateExpression based on an existing Date.
*
*
* This performs a similar function to {@link DateExpression#value(java.util.Date)
* }.
*
* @param date the date to be used in this expression
*/
public DateExpression(Date date) {
super(new DBDate(date));
}
@Override
public String toSQLString(DBDefinition db) {
return getInnerResult().toSQLString(db);
}
@Override
public DateExpression copy() {
return isNullSafetyTerminator()?nullDate():new DateExpression(this.getInnerResult());
}
@Override
public DateExpression nullExpression() {
return new DateExpression() {
@Override
public String toSQLString(DBDefinition db) {
return db.getNull();
}
};
}
/**
* Creates a date expression that returns only the date part of current date
* on the database.
*
*
* That is to say the expression returns the current day, according to the
* database, with the time set to Midnight.
*
*
Support DBvolution at
* Patreon
*
* @return a date expression of only the date part of the current database
* timestamp.
*/
public static DateExpression currentDateOnly() {
return new DateExpression(
new FunctionWithDateResult() {
@Override
public String toSQLString(DBDefinition db) {
return db.doCurrentDateOnlyTransform();
}
@Override
public DBDate getQueryableDatatypeForExpressionValue() {
return new DBDateOnly();
}
});
}
/**
* Creates a date expression that returns the current date on the database.
*
*
* That is to say the expression returns the current day and time according to
* the database.
*
*
Support DBvolution at
* Patreon
*
* @return a date expression of the current database timestamp.
*/
public static DateExpression currentDate() {
return new DateExpression(
new FunctionWithDateResult() {
@Override
public String toSQLString(DBDefinition db) {
return db.doCurrentDateTimeTransform();
}
});
}
/**
* Creates a date expression that returns the current time on the database.
*
*
* That is to say the expression returns the current time, according to the
* database, with the date set to database's zero date.
*
*
Support DBvolution at
* Patreon
*
* @return a date expression of only the time part of the current database
* timestamp.
*/
public static DateExpression currentTime() {
return new DateExpression(
new FunctionWithDateResult() {
@Override
public String toSQLString(DBDefinition db) {
return db.doCurrentTimeTransform();
}
});
}
/**
* Creates an SQL expression that returns the year part of this date
* expression.
*
* Support DBvolution at
* Patreon
*
* @return the year of this date expression as a number.
*/
public NumberExpression year() {
return new NumberExpression(
new DateExpressionWithNumberResult(this) {
@Override
public String toSQLString(DBDefinition db) {
return db.doYearTransform(this.getInnerResult().toSQLString(db));
}
});
}
/**
* Creates an SQL expression that tests the year part of this date expression.
*
* @param yearRequired the year to used in the expression
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the year is the same as the
* example supplied.
*/
public BooleanExpression yearIs(Number yearRequired) {
return this.year().is(yearRequired);
}
/**
* Creates an SQL expression that tests the year part of this date expression.
*
* @param yearRequired the year to used in the expression
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the year is the same as the
* example supplied.
*/
public BooleanExpression yearIs(Long yearRequired) {
return this.year().is(yearRequired);
}
/**
* Creates an SQL expression that tests the year part of this date expression.
*
* @param yearRequired the year to used in the expression
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the year is the same as the
* example supplied.
*/
public BooleanExpression yearIs(Integer yearRequired) {
return this.year().is(yearRequired);
}
/**
* Creates an SQL expression that tests the year part of this date expression.
*
* @param yearRequired the year to be used in the expression
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the year is the same as the
* example supplied.
*/
public BooleanExpression yearIs(NumberResult yearRequired) {
return this.year().is(yearRequired);
}
/**
* Creates an SQL expression that tests the year part of this date expression.
*
* @param yearRequired the year to be used in the expression
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the year is the same as the
* example supplied.
*/
public BooleanExpression yearIs(IntegerResult yearRequired) {
return this.year().is(yearRequired);
}
/**
* Creates an SQL expression that returns the month part of this date
* expression.
*
* Support DBvolution at
* Patreon
*
* @return the month of this date expression as a number.
*/
public NumberExpression month() {
return new NumberExpression(
new DateExpressionWithNumberResult(this) {
@Override
public String toSQLString(DBDefinition db) {
return db.doMonthTransform(this.getInnerResult().toSQLString(db));
}
});
}
/**
* Creates an SQL expression that tests the month part of this date
* expression.
*
* @param monthRequired the month to be used in the expression
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the month is the same as the
* example supplied.
*/
public BooleanExpression monthIs(Number monthRequired) {
return this.month().is(monthRequired);
}
/**
* Creates an SQL expression that tests the month part of this date
* expression.
*
* @param monthRequired the month to be used in the expression
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the month is the same as the
* example supplied.
*/
public BooleanExpression monthIs(Long monthRequired) {
return this.month().is(monthRequired);
}
/**
* Creates an SQL expression that tests the month part of this date
* expression.
*
* @param monthRequired the month to be used in the expression
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the month is the same as the
* example supplied.
*/
public BooleanExpression monthIs(Integer monthRequired) {
return this.month().is(monthRequired);
}
/**
* Creates an SQL expression that tests the month part of this date
* expression.
*
* @param monthRequired the month to be used in the expression
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the month is the same as the
* example supplied.
*/
public BooleanExpression monthIs(NumberResult monthRequired) {
return this.month().is(monthRequired);
}
/**
* Creates an SQL expression that tests the month part of this date
* expression.
*
* @param monthRequired the month to be used in the expression
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the month is the same as the
* example supplied.
*/
public BooleanExpression monthIs(IntegerResult monthRequired) {
return this.month().is(monthRequired);
}
/**
* Returns the day part of the date.
*
*
* Day in this sense is the number of the day within the month: that is the 25
* part of Monday 25th of August 2014
*
*
Support DBvolution at
* Patreon
*
* @return a NumberExpression that will provide the day of this date.
*/
public NumberExpression day() {
return new NumberExpression(
new DateExpressionWithNumberResult(this) {
@Override
public String toSQLString(DBDefinition db) {
return db.doDayTransform(this.getInnerResult().toSQLString(db));
}
});
}
/**
* Creates an SQL expression that tests the day part of this date expression.
*
* @param dayRequired the day to be used in the expression
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the day is the same as the
* example supplied.
*/
public BooleanExpression dayIs(Number dayRequired) {
return this.day().is(dayRequired);
}
/**
* Creates an SQL expression that tests the day part of this date expression.
*
* @param dayRequired the day to be used in the expression
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the day is the same as the
* example supplied.
*/
public BooleanExpression dayIs(Long dayRequired) {
return this.day().is(dayRequired);
}
/**
* Creates an SQL expression that tests the day part of this date expression.
*
* @param dayRequired the day to be used in the expression
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the day is the same as the
* example supplied.
*/
public BooleanExpression dayIs(Integer dayRequired) {
return this.day().is(dayRequired);
}
/**
* Creates an SQL expression that tests the day part of this date expression.
*
* @param dayRequired the day to be used in the expression
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the day is the same as the
* example supplied.
*/
public BooleanExpression dayIs(NumberResult dayRequired) {
return this.day().is(dayRequired);
}
/**
* Creates an SQL expression that tests the day part of this date expression.
*
* @param dayRequired the day to be used in the expression
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the day is the same as the
* example supplied.
*/
public BooleanExpression dayIs(IntegerResult dayRequired) {
return this.day().is(dayRequired);
}
/**
* Creates an SQL expression that returns the hour part of this date
* expression.
*
* Support DBvolution at
* Patreon
*
* @return the hour of this date expression as a number.
*/
public NumberExpression hour() {
return new NumberExpression(
new DateExpressionWithNumberResult(this) {
@Override
public String toSQLString(DBDefinition db) {
return db.doHourTransform(this.getInnerResult().toSQLString(db));
}
});
}
/**
* Creates an SQL expression that tests the hour part of this date expression.
*
* @param hourRequired the hour to be used in the expression
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the hour is the same as the
* example supplied.
*/
public BooleanExpression hourIs(Number hourRequired) {
return this.hour().is(hourRequired);
}
/**
* Creates an SQL expression that tests the hour part of this date expression.
*
* @param hourRequired the hour to be used in the expression
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the hour is the same as the
* example supplied.
*/
public BooleanExpression hourIs(Long hourRequired) {
return this.hour().is(hourRequired);
}
/**
* Creates an SQL expression that tests the hour part of this date expression.
*
* @param hourRequired the hour to be used in the expression
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the hour is the same as the
* example supplied.
*/
public BooleanExpression hourIs(Integer hourRequired) {
return this.hour().is(hourRequired);
}
/**
* Creates an SQL expression that tests the hour part of this date expression.
*
* @param hourRequired the hour to be compared to.
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the hour is the same as the
* example supplied.
*/
public BooleanExpression hourIs(IntegerResult hourRequired) {
return this.hour().is(hourRequired);
}
/**
* Creates an SQL expression that tests the hour part of this date expression.
*
* @param hourRequired the hour to be compared to.
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the hour is the same as the
* example supplied.
*/
public BooleanExpression hourIs(NumberResult hourRequired) {
return this.hour().is(hourRequired);
}
/**
* Creates an SQL expression that returns the minute part of this date
* expression.
*
* Support DBvolution at
* Patreon
*
* @return the minute of this date expression as a number.
*/
public NumberExpression minute() {
return new NumberExpression(
new DateExpressionWithNumberResult(this) {
@Override
public String toSQLString(DBDefinition db) {
return db.doMinuteTransform(this.getInnerResult().toSQLString(db));
}
});
}
/**
* Creates an SQL expression that tests the minute part of this date
* expression.
*
* @param minuteRequired the minute to be compared to
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the minute is the same as the
* example supplied.
*/
public BooleanExpression minuteIs(Number minuteRequired) {
return this.minute().is(minuteRequired);
}
/**
* Creates an SQL expression that tests the minute part of this date
* expression.
*
* @param minuteRequired the minute to be compared to
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the minute is the same as the
* example supplied.
*/
public BooleanExpression minuteIs(Long minuteRequired) {
return this.minute().is(minuteRequired);
}
/**
* Creates an SQL expression that tests the minute part of this date
* expression.
*
* @param minuteRequired the minute to be compared to
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the minute is the same as the
* example supplied.
*/
public BooleanExpression minuteIs(Integer minuteRequired) {
return this.minute().is(minuteRequired);
}
/**
* Creates an SQL expression that tests the minute part of this date
* expression.
*
* @param minuteRequired the minute to be compared to
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the minute is the same as the
* example supplied.
*/
public BooleanExpression minuteIs(NumberResult minuteRequired) {
return this.minute().is(minuteRequired);
}
/**
* Creates an SQL expression that tests the minute part of this date
* expression.
*
* @param minuteRequired the minute to be compared to
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the minute is the same as the
* example supplied.
*/
public BooleanExpression minuteIs(IntegerResult minuteRequired) {
return this.minute().is(minuteRequired);
}
/**
* Creates an SQL expression that returns the second part of this date
* expression.
*
*
* Contains only whole seconds, use {@link #subsecond()} to retrieve the
* fractional part.
*
*
Support DBvolution at
* Patreon
*
* @return the second of this date expression as a number.
*/
public NumberExpression second() {
return new NumberExpression(
new DateExpressionWithNumberResult(this) {
@Override
public String toSQLString(DBDefinition db) {
return db.doSecondTransform(this.getInnerResult().toSQLString(db));
}
});
}
/**
* Creates an SQL expression that returns the fractions of a second part of
* this date expression.
*
*
* Contains only the fractional part of the seconds, that is always between 0
* and 1, use {@link #second()} to retrieve the integer part.
*
*
Support DBvolution at
* Patreon
*
* @return the second of this date expression as a number.
*/
public NumberExpression subsecond() {
return new NumberExpression(
new DateExpressionWithNumberResult(this) {
@Override
public String toSQLString(DBDefinition db) {
return db.doSubsecondTransform(this.getInnerResult().toSQLString(db));
}
});
}
/**
* Creates an SQL expression that tests the second part of this date
* expression.
*
* @param minuteRequired the minute required
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the second is the same as the
* example supplied.
*/
public BooleanExpression secondIs(Number minuteRequired) {
return this.second().is(minuteRequired);
}
/**
* Creates an SQL expression that tests the second part of this date
* expression.
*
* @param minuteRequired the minute required
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the second is the same as the
* example supplied.
*/
public BooleanExpression secondIs(Long minuteRequired) {
return this.second().is(minuteRequired);
}
/**
* Creates an SQL expression that tests the second part of this date
* expression.
*
* @param minuteRequired the minute required
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the second is the same as the
* example supplied.
*/
public BooleanExpression secondIs(Integer minuteRequired) {
return this.second().is(minuteRequired);
}
/**
* Creates an SQL expression that tests the second part of this date
* expression.
*
* @param minuteRequired the minute that the expression must match
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the second is the same as the
* example supplied.
*/
public BooleanExpression secondIs(NumberResult minuteRequired) {
return this.second().is(minuteRequired);
}
/**
* Creates an SQL expression that tests the second part of this date
* expression.
*
* @param minuteRequired the minute that the expression must match
* Support DBvolution at
* Patreon
* @return a BooleanExpression that is TRUE if the second is the same as the
* example supplied.
*/
public BooleanExpression secondIs(IntegerResult minuteRequired) {
return this.second().is(minuteRequired);
}
/**
* Creates an SQL expression that test whether this date expression is equal
* to the supplied date.
*
*
* Be careful when using this expression as dates have lots of fields and it
* is easy to miss a similar date.
*
* @param date the date the expression must match
*
Support DBvolution at
* Patreon
* @return a BooleanExpression comparing the date and this DateExpression.
*/
@Override
public BooleanExpression is(Date date) {
return is(value(date));
}
/**
* Creates an SQL expression that test whether this date expression is equal
* to the supplied date.
*
* @param dateExpression the date the expression must match
* Support DBvolution at
* Patreon
* @return a BooleanExpression comparing the DateResult and this
* DateExpression.
*/
@Override
public BooleanExpression is(DateResult dateExpression) {
BooleanExpression isExpr = new BooleanExpression(new DateDateExpressionWithBooleanResult(this, dateExpression) {
@Override
protected String getEquationOperator(DBDefinition db) {
return " = ";
}
});
if (isExpr.getIncludesNull()) {
return BooleanExpression.isNull(this);
} else {
return isExpr;
}
}
/**
* Creates an SQL expression that test whether this date expression is NOT
* equal to the supplied date.
*
* @param date the date the expression must not match
* Support DBvolution at
* Patreon
* @return a BooleanExpression comparing the DateResult and this
* DateExpression.
*/
@Override
public BooleanExpression isNot(Date date) {
return this.isNot(value(date));
}
/**
* Creates an SQL expression that test whether this date expression is NOT
* equal to the supplied date.
*
* @param dateExpression the date the expression must not match
* Support DBvolution at
* Patreon
* @return a BooleanExpression comparing the DateResult and this
* DateExpression.
*/
@Override
public BooleanExpression isNot(DateResult dateExpression) {
BooleanExpression isExpr = new BooleanExpression(new DateDateExpressionWithBooleanResult(this, dateExpression) {
@Override
protected String getEquationOperator(DBDefinition db) {
return " <> ";
}
});
if (isExpr.getIncludesNull()) {
return BooleanExpression.isNotNull(this);
} else {
return isExpr;
}
}
/**
* Creates an expression that will return the most common value of the column
* supplied.
*
*
* MODE: The number which appears most often in a set of numbers. For example:
* in {6, 3, 9, 6, 6, 5, 9, 3} the Mode is 6.
*
* Support DBvolution at
* Patreon
*
* @return a number expression.
*/
@Override
public DateExpression modeSimple() {
DateExpression modeExpr = new DateExpression(
new ModeSimpleExpression(this));
return modeExpr;
}
/**
* Returns FALSE if this expression evaluates to NULL, otherwise TRUE.
*
* Support DBvolution at
* Patreon
*
* @return a BooleanExpression
*/
public BooleanExpression isNotNull() {
return BooleanExpression.isNotNull(this);
}
/**
* Returns TRUE if this expression evaluates to NULL, otherwise FALSE.
*
* Support DBvolution at
* Patreon
*
* @return a BooleanExpression
*/
public BooleanExpression isNull() {
return BooleanExpression.isNull(this);
}
/**
* Performs searches based on a range.
*
* if both ends of the range are specified the lower-bound will be included in
* the search and the upper-bound excluded. I.e permittedRange(1,3) will
* return 1 and 2.
*
*
* if the upper-bound is null the range will be open ended and inclusive.
*
* I.e permittedRange(1,null) will return 1,2,3,4,5, etc.
*
*
* if the lower-bound is null the range will be open ended and exclusive.
*
* I.e permittedRange(null, 5) will return 4,3,2,1, etc.
*
* @param lowerBound the lower bound that the expression must exceed
* @param upperBound the upper bound that the expression must not exceed
*
Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
@Override
public BooleanExpression isBetween(DateResult lowerBound, DateResult upperBound) {
return BooleanExpression.allOf(
this.isGreaterThan(lowerBound),
this.isLessThanOrEqual(upperBound)
);
}
/**
* Performs searches based on a range.
*
* if both ends of the range are specified the lower-bound will be included in
* the search and the upper-bound excluded. I.e permittedRange(1,3) will
* return 1 and 2.
*
*
* if the upper-bound is null the range will be open ended and inclusive.
*
* I.e permittedRange(1,null) will return 1,2,3,4,5, etc.
*
*
* if the lower-bound is null the range will be open ended and exclusive.
*
* I.e permittedRange(null, 5) will return 4,3,2,1, etc.
*
* @param lowerBound the lower bound that the expression must exceed
* @param upperBound the upper bound that the expression must not exceed
*
Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
@Override
public BooleanExpression isBetween(Date lowerBound, DateResult upperBound) {
return BooleanExpression.allOf(
this.isGreaterThan(lowerBound),
this.isLessThanOrEqual(upperBound)
);
}
/**
* Performs searches based on a range.
*
* if both ends of the range are specified the lower-bound will be included in
* the search and the upper-bound excluded. I.e permittedRange(1,3) will
* return 1 and 2.
*
*
* if the upper-bound is null the range will be open ended and inclusive.
*
* I.e permittedRange(1,null) will return 1,2,3,4,5, etc.
*
*
* if the lower-bound is null the range will be open ended and exclusive.
*
* I.e permittedRange(null, 5) will return 4,3,2,1, etc.
*
* @param lowerBound the lower bound that the expression must exceed
* @param upperBound the upper bound that the expression must not exceed
*
Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
@Override
public BooleanExpression isBetween(DateResult lowerBound, Date upperBound) {
return BooleanExpression.allOf(
this.isGreaterThan(lowerBound),
this.isLessThanOrEqual(upperBound)
);
}
/**
* Performs searches based on a range.
*
* if both ends of the range are specified the lower-bound will be included in
* the search and the upper-bound excluded. I.e permittedRange(1,3) will
* return 1 and 2.
*
*
* if the upper-bound is null the range will be open ended and inclusive.
*
* I.e permittedRange(1,null) will return 1,2,3,4,5, etc.
*
*
* if the lower-bound is null the range will be open ended and exclusive.
*
* I.e permittedRange(null, 5) will return 4,3,2,1, etc.
*
* @param lowerBound the lower bound that the expression must exceed
* @param upperBound the upper bound that the expression must not exceed
*
Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
@Override
public BooleanExpression isBetween(Date lowerBound, Date upperBound) {
return BooleanExpression.allOf(
this.isGreaterThan(lowerBound),
this.isLessThanOrEqual(upperBound)
);
}
/**
* Performs searches based on a range.
*
* if both ends of the range are specified both the lower- and upper-bound
* will be included in the search. I.e permittedRangeInclusive(1,3) will
* return 1, 2, and 3.
*
*
* if the upper-bound is null the range will be open ended and inclusive.
*
* I.e permittedRangeInclusive(1,null) will return 1,2,3,4,5, etc.
*
*
* if the lower-bound is null the range will be open ended and inclusive.
*
* I.e permittedRangeInclusive(null, 5) will return 5,4,3,2,1, etc.
*
* @param lowerBound the lower bound that the expression must exceed
* @param upperBound the upper bound that the expression must not exceed
*
Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
@Override
public BooleanExpression isBetweenInclusive(DateResult lowerBound, DateResult upperBound) {
return BooleanExpression.allOf(
this.isGreaterThanOrEqual(lowerBound),
this.isLessThanOrEqual(upperBound)
);
}
/**
* Performs searches based on a range.
*
* if both ends of the range are specified both the lower- and upper-bound
* will be included in the search. I.e permittedRangeInclusive(1,3) will
* return 1, 2, and 3.
*
*
* if the upper-bound is null the range will be open ended and inclusive.
*
* I.e permittedRangeInclusive(1,null) will return 1,2,3,4,5, etc.
*
*
* if the lower-bound is null the range will be open ended and inclusive.
*
* I.e permittedRangeInclusive(null, 5) will return 5,4,3,2,1, etc.
*
* @param lowerBound the lower bound that the expression must exceed
* @param upperBound the upper bound that the expression must not exceed
*
Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
@Override
public BooleanExpression isBetweenInclusive(Date lowerBound, DateResult upperBound) {
return isBetweenInclusive(value(lowerBound), upperBound);
}
/**
* Performs searches based on a range.
*
* if both ends of the range are specified both the lower- and upper-bound
* will be included in the search. I.e permittedRangeInclusive(1,3) will
* return 1, 2, and 3.
*
*
* if the upper-bound is null the range will be open ended and inclusive.
*
* I.e permittedRangeInclusive(1,null) will return 1,2,3,4,5, etc.
*
*
* if the lower-bound is null the range will be open ended and inclusive.
*
* I.e permittedRangeInclusive(null, 5) will return 5,4,3,2,1, etc.
*
* @param lowerBound the lower bound that the expression must exceed
* @param upperBound the upper bound that the expression must not exceed
*
Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
@Override
public BooleanExpression isBetweenInclusive(DateResult lowerBound, Date upperBound) {
return isBetweenInclusive(lowerBound, value(upperBound));
}
/**
* Performs searches based on a range.
*
* if both ends of the range are specified both the lower- and upper-bound
* will be included in the search. I.e permittedRangeInclusive(1,3) will
* return 1, 2, and 3.
*
*
* if the upper-bound is null the range will be open ended and inclusive.
*
* I.e permittedRangeInclusive(1,null) will return 1,2,3,4,5, etc.
*
*
* if the lower-bound is null the range will be open ended and inclusive.
*
* I.e permittedRangeInclusive(null, 5) will return 5,4,3,2,1, etc.
*
* @param lowerBound the lower bound that the expression must exceed
* @param upperBound the upper bound that the expression must not exceed
*
Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
@Override
public BooleanExpression isBetweenInclusive(Date lowerBound, Date upperBound) {
return isBetweenInclusive(value(lowerBound), value(upperBound));
}
/**
* Performs searches based on a range.
*
* if both ends of the range are specified both the lower- and upper-bound
* will be excluded in the search. I.e permittedRangeExclusive(1,3) will
* return 2.
*
*
* if the upper-bound is null the range will be open ended upwards and
* exclusive.
*
* I.e permittedRangeExclusive(1,null) will return 2,3,4,5, etc.
*
*
* if the lower-bound is null the range will be open ended downwards and
* exclusive.
*
* I.e permittedRangeExclusive(null, 5) will return 4,3,2,1, etc.
*
* @param lowerBound the lower bound that the expression must exceed
* @param upperBound the upper bound that the expression must not exceed
*
Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
@Override
public BooleanExpression isBetweenExclusive(DateResult lowerBound, DateResult upperBound) {
return BooleanExpression.allOf(
this.isGreaterThan(lowerBound),
this.isLessThan(upperBound)
);
}
/**
* Performs searches based on a range.
*
* if both ends of the range are specified both the lower- and upper-bound
* will be excluded in the search. I.e permittedRangeExclusive(1,3) will
* return 2.
*
*
* if the upper-bound is null the range will be open ended upwards and
* exclusive.
*
* I.e permittedRangeExclusive(1,null) will return 2,3,4,5, etc.
*
*
* if the lower-bound is null the range will be open ended downwards and
* exclusive.
*
* I.e permittedRangeExclusive(null, 5) will return 4,3,2,1, etc.
*
* @param lowerBound the lower bound that the expression must exceed
* @param upperBound the upper bound that the expression must not exceed
*
Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
@Override
public BooleanExpression isBetweenExclusive(Date lowerBound, DateResult upperBound) {
return isBetweenExclusive(value(lowerBound), upperBound);
}
/**
* Performs searches based on a range.
*
* if both ends of the range are specified both the lower- and upper-bound
* will be excluded in the search. I.e permittedRangeExclusive(1,3) will
* return 2.
*
*
* if the upper-bound is null the range will be open ended upwards and
* exclusive.
*
* I.e permittedRangeExclusive(1,null) will return 2,3,4,5, etc.
*
*
* if the lower-bound is null the range will be open ended downwards and
* exclusive.
*
* I.e permittedRangeExclusive(null, 5) will return 4,3,2,1, etc.
*
* @param lowerBound the lower bound that the expression must exceed
* @param upperBound the upper bound that the expression must not exceed
*
Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
@Override
public BooleanExpression isBetweenExclusive(DateResult lowerBound, Date upperBound) {
return isBetween(lowerBound, value(upperBound));
}
/**
* Performs searches based on a range.
*
* if both ends of the range are specified both the lower- and upper-bound
* will be excluded in the search. I.e permittedRangeExclusive(1,3) will
* return 2.
*
*
* if the upper-bound is null the range will be open ended upwards and
* exclusive.
*
* I.e permittedRangeExclusive(1,null) will return 2,3,4,5, etc.
*
*
* if the lower-bound is null the range will be open ended downwards and
* exclusive.
*
* I.e permittedRangeExclusive(null, 5) will return 4,3,2,1, etc.
*
* @param lowerBound the lower bound that the expression must exceed
* @param upperBound the upper bound that the expression must not exceed
*
Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
@Override
public BooleanExpression isBetweenExclusive(Date lowerBound, Date upperBound) {
return isBetween(value(lowerBound), value(upperBound));
}
/**
* Creates an SQL expression that test whether this date expression is less
* than to the supplied date.
*
* @param date the date this expression must not exceed
* Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
@Override
public BooleanExpression isLessThan(Date date) {
return isLessThan(value(date));
}
/**
* Creates an SQL expression that test whether this date expression is less
* than to the supplied date.
*
* @param dateExpression the date this expression must not exceed
* Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
@Override
public BooleanExpression isLessThan(DateResult dateExpression) {
return new BooleanExpression(new DateExpression.DateDateExpressionWithBooleanResult(this, dateExpression) {
@Override
protected String getEquationOperator(DBDefinition db) {
return " < ";
}
@Override
public boolean getIncludesNull() {
return false;
}
});
}
/**
* Create a DateRepeat value representing the difference between this date
* expression and the one provided
*
* @param date the other date which defines this DateRepeat
* Support DBvolution at
* Patreon
* @return a DateRepeat expression
*/
public DateRepeatExpression getDateRepeatFrom(Date date) {
return getDateRepeatFrom(value(date));
}
/**
* Create a DateRepeat value representing the difference between this date
* expression and the one provided
*
* @param dateExpression the other date which defines this DateRepeat
* Support DBvolution at
* Patreon
* @return DateRepeat expression
*/
public DateRepeatExpression getDateRepeatFrom(DateResult dateExpression) {
return new DateRepeatExpression(new DateDateExpressionWithDateRepeatResult(this, dateExpression) {
@Override
public String toSQLString(DBDefinition db) {
if (db instanceof SupportsDateRepeatDatatypeFunctions) {
return db.doDateMinusToDateRepeatTransformation(getFirst().toSQLString(db), getSecond().toSQLString(db));
} else {
final DateExpression left = getFirst();
final DateExpression right = new DateExpression(getSecond());
return BooleanExpression.anyOf(left.isNull(), right.isNull())
.ifThenElse(
nullString(),
StringExpression.value(INTERVAL_PREFIX)
.append(left.year().minus(right.year()).bracket()).append(YEAR_SUFFIX)
.append(left.month().minus(right.month()).bracket()).append(MONTH_SUFFIX)
.append(left.day().minus(right.day()).bracket()).append(DAY_SUFFIX)
.append(left.hour().minus(right.hour()).bracket()).append(HOUR_SUFFIX)
.append(left.minute().minus(right.minute()).bracket()).append(MINUTE_SUFFIX)
.append(left.second().minus(right.second()).bracket())
.append(".")
.append(left.subsecond().minus(right.subsecond()).absoluteValue().stringResult().substringAfter("."))
.append(SECOND_SUFFIX)
).toSQLString(db);
}
}
@Override
public boolean getIncludesNull() {
return false;
}
});
}
/**
* Subtract the period/duration provided from this date expression to get an
* offset date.
*
* @param interval the amount of time this date needs to be offset by.
* Support DBvolution at
* Patreon
* @return a Date expression
*/
public DateExpression minus(Period interval) {
return minus(DateRepeatExpression.value(interval));
}
/**
* Subtract the period/duration provided from this date expression to get an
* offset date.
*
* @param intervalExpression the amount of time this date needs to be offset
* by.
* Support DBvolution at
* Patreon
* @return a Date expression
*/
public DateExpression minus(DateRepeatResult intervalExpression) {
return new DateExpression(new DateDateRepeatArithmeticDateResult(this, intervalExpression) {
@Override
protected String doExpressionTransformation(DBDefinition db) {
if (db instanceof SupportsDateRepeatDatatypeFunctions) {
return db.doDateMinusDateRepeatTransform(getFirst().toSQLString(db), getSecond().toSQLString(db));
} else {
final DateExpression left = getFirst();
final DateRepeatExpression right = new DateRepeatExpression(getSecond());
return BooleanExpression.anyOf(left.isNull(), right.isNull())
.ifThenElse(
nullDate(),
left.addYears(right.getYears().times(-1))
.addMonths(right.getMonths().times(-1))
.addDays(right.getDays().times(-1))
.addHours(right.getHours().times(-1))
.addMinutes(right.getMinutes().times(-1))
.addSeconds(right.getSeconds().times(-1))
// .addMilliseconds(right.getMilliseconds().times(-1))
).toSQLString(db);
}
}
@Override
public boolean getIncludesNull() {
return false;
}
});
}
/**
* Add the period/duration provided from this date expression to get an offset
* date.
*
* @param interval the amount of time this date needs to be offset by.
* Support DBvolution at
* Patreon
* @return a Date expression
*/
public DateExpression plus(Period interval) {
return plus(DateRepeatExpression.value(interval));
}
/**
* Add the period/duration provided from this date expression to get an offset
* date.
*
* @param intervalExpression the amount of time this date needs to be offset
* by.
* Support DBvolution at
* Patreon
* @return a Date expression
*/
public DateExpression plus(DateRepeatResult intervalExpression) {
return new DateExpression(new DateDateRepeatArithmeticDateResult(this, intervalExpression) {
@Override
protected String doExpressionTransformation(DBDefinition db) {
if (db instanceof SupportsDateRepeatDatatypeFunctions) {
return db.doDatePlusDateRepeatTransform(getFirst().toSQLString(db), getSecond().toSQLString(db));
} else {
final DateExpression left = getFirst();
final DateRepeatExpression right = new DateRepeatExpression(getSecond());
return BooleanExpression.anyOf(left.isNull(), right.isNull())
.ifThenElse(
nullDate(),
left.addYears(right.getYears())
.addMonths(right.getMonths())
.addDays(right.getDays())
.addHours(right.getHours())
.addMinutes(right.getMinutes())
.addSeconds(right.getSeconds())
).toSQLString(db);
}
}
@Override
public boolean getIncludesNull() {
return false;
}
});
}
/**
* Creates an SQL expression that test whether this date expression is less
* than or equal to the supplied date.
*
* @param date the date this expression must not exceed
* Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
@Override
public BooleanExpression isLessThanOrEqual(Date date) {
return isLessThanOrEqual(value(date));
}
/**
* Creates an SQL expression that test whether this date expression is less
* than or equal to the supplied DateResult.
*
* @param dateExpression the date this expression must not exceed
* Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
@Override
public BooleanExpression isLessThanOrEqual(DateResult dateExpression) {
return new BooleanExpression(new DateDateExpressionWithBooleanResult(this, dateExpression) {
@Override
protected String getEquationOperator(DBDefinition db) {
return " <= ";
}
@Override
public boolean getIncludesNull() {
return false;
}
});
}
/**
* Creates an SQL expression that test whether this date expression is greater
* than the supplied date.
*
* @param date the date this expression must be compared to
* Support DBvolution at
* Patreon
* @return an expression that will evaluate to a greater than operation
*/
@Override
public BooleanExpression isGreaterThan(Date date) {
return isGreaterThan(value(date));
}
/**
* Creates an SQL expression that test whether this date expression is greater
* than the supplied DateResult.
*
* @param dateExpression the date this expression must be compared to
* Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
@Override
public BooleanExpression isGreaterThan(DateResult dateExpression) {
return new BooleanExpression(new DateDateExpressionWithBooleanResult(this, dateExpression) {
@Override
protected String getEquationOperator(DBDefinition db) {
return " > ";
}
@Override
public boolean getIncludesNull() {
return false;
}
});
}
/**
* Creates an SQL expression that test whether this date expression is greater
* than or equal to the supplied Date.
*
* @param date the date this expression must be compared to
* Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
@Override
public BooleanExpression isGreaterThanOrEqual(Date date) {
return isGreaterThanOrEqual(value(date));
}
/**
* Creates an SQL expression that test whether this date expression is greater
* than or equal to the supplied DateResult.
*
* @param dateExpression the date this expression must be compared to
* Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
@Override
public BooleanExpression isGreaterThanOrEqual(DateResult dateExpression) {
return new BooleanExpression(new DateDateExpressionWithBooleanResult(this, dateExpression) {
@Override
protected String getEquationOperator(DBDefinition db) {
return " >= ";
}
@Override
public boolean getIncludesNull() {
return false;
}
});
}
/**
* Like LESSTHAN_OR_EQUAL but only includes the EQUAL values if the fallback
* matches.
*
*
* Often used to implement efficient paging by using LESSTHAN across 2
* columns. For example:
* {@code table.column(table.name).isLessThan(5, table.column(table.pkid).isLessThan(1100));}
*
*
* If you are using this for pagination, remember to sort by the columns as
* well
*
* @param value the right side of the internal comparison
* @param fallBackWhenEquals the comparison used when the two values are
* equal.
*
Support DBvolution at
* Patreon
* @return a BooleanExpression
*/
@Override
public BooleanExpression isLessThan(Date value, BooleanExpression fallBackWhenEquals) {
return this.isLessThan(value(value), fallBackWhenEquals);
}
/**
* Like GREATERTHAN_OR_EQUAL but only includes the EQUAL values if the
* fallback matches.
*
*
* Often used to implement efficient paging by using LESSTHAN across 2
* columns. For example:
* {@code table.column(table.name).isLessThan(5, table.column(table.pkid).isLessThan(1100));}
*
*
* If you are using this for pagination, remember to sort by the columns as
* well
*
* @param value the right side of the internal comparison
* @param fallBackWhenEquals the comparison used when the two values are
* equal.
*
Support DBvolution at
* Patreon
* @return a BooleanExpression
*/
@Override
public BooleanExpression isGreaterThan(Date value, BooleanExpression fallBackWhenEquals) {
return this.isGreaterThan(value(value), fallBackWhenEquals);
}
/**
* Like LESSTHAN_OR_EQUAL but only includes the EQUAL values if the fallback
* matches.
*
*
* Often used to implement efficient paging by using LESSTHAN across 2
* columns. For example:
* {@code table.column(table.name).isLessThan(5, table.column(table.pkid).isLessThan(1100));}
*
*
* If you are using this for pagination, remember to sort by the columns as
* well
*
* @param value the right side of the internal comparison
* @param fallBackWhenEquals the comparison used when the two values are
* equal.
*
Support DBvolution at
* Patreon
* @return a BooleanExpression
*/
@Override
public BooleanExpression isLessThan(DateResult value, BooleanExpression fallBackWhenEquals) {
return this.isLessThan(value).or(this.is(value).and(fallBackWhenEquals));
}
/**
* Like GREATERTHAN_OR_EQUAL but only includes the EQUAL values if the
* fallback matches.
*
*
* Often used to implement efficient paging by using LESSTHAN across 2
* columns. For example:
* {@code table.column(table.name).isLessThan(5, table.column(table.pkid).isLessThan(1100));}
*
*
* If you are using this for pagination, remember to sort by the columns as
* well
*
* @param value the right side of the internal comparison
* @param fallBackWhenEquals the comparison used when the two values are
* equal.
*
Support DBvolution at
* Patreon
* @return a BooleanExpression
*/
@Override
public BooleanExpression isGreaterThan(DateResult value, BooleanExpression fallBackWhenEquals) {
return this.isGreaterThan(value).or(this.is(value).and(fallBackWhenEquals));
}
/**
* Creates an SQL expression that test whether this date expression is
* included in the list of Dates.
*
*
* Be careful when using this expression as dates have lots of fields and it
* is easy to miss a similar date.
*
* @param possibleValues allowed values
*
Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
@Override
public BooleanExpression isIn(Date... possibleValues) {
List possVals = new ArrayList();
for (Date num : possibleValues) {
possVals.add(value(num));
}
return isIn(possVals.toArray(new DateExpression[]{}));
}
/**
* Creates an SQL expression that test whether this date expression is
* included in the list of Dates.
*
*
* Be careful when using this expression as dates have lots of fields and it
* is easy to miss a similar date.
*
* @param possibleValues allowed values
*
Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
public BooleanExpression isIn(Collection extends DateResult> possibleValues) {
//List possVals = new ArrayList();
//for (Date num : possibleValues) {
// possVals.add(value(num));
//}
return isIn(possibleValues.toArray(new DateResult[]{}));
}
/**
* Creates an SQL expression that test whether this date expression is
* included in the list of DateResults.
*
*
* Be careful when using this expression as dates have lots of fields and it
* is easy to miss a similar date.
*
* @param possibleValues allowed values
*
Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
@Override
public BooleanExpression isIn(DateResult... possibleValues) {
BooleanExpression isInExpr = new BooleanExpression(new DateDateResultFunctionWithBooleanResult(this, possibleValues) {
@Override
public String toSQLString(DBDefinition db) {
List sqlValues = new ArrayList();
for (DateResult value : getValues()) {
sqlValues.add(value.toSQLString(db));
}
return db.doInTransform(getColumn().toSQLString(db), sqlValues);
}
});
if (isInExpr.getIncludesNull()) {
return BooleanExpression.anyOf(BooleanExpression.isNull(this), isInExpr);
} else {
return isInExpr;
}
}
/**
* Creates and expression that replaces a NULL result with the supplied date.
*
*
* This is a way of handling dates that should have a value but don't.
*
* @param alternative use this value if the expression evaluates to NULL
*
Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
public DateExpression ifDBNull(Date alternative) {
return ifDBNull(value(alternative));
// return new DateExpression(
// new DateExpression.DateDateFunctionWithDateResult(this, new DateExpression(alternative)) {
// @Override
// protected String getFunctionName(DBDefinition db) {
// return db.getIfNullFunctionName();
// }
//
// @Override
// public boolean getIncludesNull() {
// return false;
// }
// });
}
/**
* Creates and expression that replaces a NULL result with the supplied
* DateResult.
*
*
* This is a way of handling dates that should have a value but don't.
*
* @param alternative use this value if the expression evaluates to NULL
*
Support DBvolution at
* Patreon
* @return a boolean expression representing the required comparison
*/
public DateExpression ifDBNull(DateResult alternative) {
return new DateExpression(
new DateExpression.DateDateFunctionWithDateResult(this, alternative) {
@Override
public String toSQLString(DBDefinition db) {
return db.doDateIfNullTransform(this.getFirst().toSQLString(db), getSecond().toSQLString(db));
}
@Override
public boolean getIncludesNull() {
return false;
}
});
}
/**
* Aggregates the dates found in a query to find the maximum date in the
* selection.
*
*
* For use in expression columns and {@link DBReport}.
*
*
Support DBvolution at
* Patreon
*
* @return a number expression.
*/
public DateExpression max() {
return new DateExpression(new DateFunctionWithDateResult(this) {
@Override
protected String getFunctionName(DBDefinition db) {
return db.getMaxFunctionName();
}
@Override
public boolean isAggregator() {
return true;
}
@Override
public boolean getIncludesNull() {
return false;
}
});
}
/**
* Aggregates the dates found in a query to find the minimum date in the
* selection.
*
*
* For use in expression columns and {@link DBReport}.
*
*
Support DBvolution at
* Patreon
*
* @return a number expression.
*/
public DateExpression min() {
return new DateExpression(new DateFunctionWithDateResult(this) {
@Override
protected String getFunctionName(DBDefinition db) {
return db.getMinFunctionName();
}
@Override
public boolean isAggregator() {
return true;
}
@Override
public boolean getIncludesNull() {
return false;
}
});
}
@Override
public DBDate getQueryableDatatypeForExpressionValue() {
return new DBDate();
}
/**
* Date Arithmetic: add the supplied number of seconds to the date expression.
*
*
* Negative seconds are supported.
*
* @param secondsToAdd seconds to offset by
*
Support DBvolution at
* Patreon
* @return a DateExpression
*/
public DateExpression addSeconds(int secondsToAdd) {
return this.addSeconds(value(secondsToAdd));
}
/**
* Date Arithmetic: add the supplied number of seconds to the date expression.
*
*
* Negative seconds are supported.
*
* @param secondsToAdd seconds to offset by
*
Support DBvolution at
* Patreon
* @return a DateExpression
*/
public DateExpression addSeconds(NumberExpression secondsToAdd) {
return new DateExpression(
new DateNumberExpressionWithDateResult(this, secondsToAdd) {
@Override
public boolean getIncludesNull() {
return false;
}
@Override
public String toSQLString(DBDefinition db) {
return db.doAddSecondsTransform(first.toSQLString(db), second.toSQLString(db));
}
});
}
/**
* Date Arithmetic: add the supplied number of seconds to the date expression.
*
*
* Negative seconds are supported.
*
* @param secondsToAdd seconds to offset by
*
Support DBvolution at
* Patreon
* @return a DateExpression
*/
public DateExpression addSeconds(IntegerExpression secondsToAdd) {
return new DateExpression(
new DateIntegerExpressionWithDateResult(this, secondsToAdd) {
@Override
public boolean getIncludesNull() {
return false;
}
@Override
public String toSQLString(DBDefinition db) {
return db.doAddSecondsTransform(first.toSQLString(db), second.toSQLString(db));
}
});
}
/**
* Date Arithmetic: add the supplied number of minutes to the date expression.
*
*
* Negative values are supported.
*
* @param minutesToAdd minutes to offset by
*
Support DBvolution at
* Patreon
* @return a DateExpression
*/
public DateExpression addMinutes(int minutesToAdd) {
return this.addMinutes(value(minutesToAdd));
}
/**
* Date Arithmetic: add the supplied number of minutes to the date expression.
*
*
* Negative values are supported.
*
* @param minutesToAdd minutes to offset by
*
Support DBvolution at
* Patreon
* @return a DateExpression
*/
public DateExpression addMinutes(IntegerExpression minutesToAdd) {
return new DateExpression(
new DateIntegerExpressionWithDateResult(this, minutesToAdd) {
@Override
public boolean getIncludesNull() {
return false;
}
@Override
public String toSQLString(DBDefinition db) {
return db.doAddMinutesTransform(first.toSQLString(db), second.toSQLString(db));
}
});
}
/**
* Date Arithmetic: add the supplied number of days to the date expression.
*
*
* Negative values are supported.
*
* @param daysToAdd days to offset by
*
Support DBvolution at
* Patreon
* @return a DateExpression
*/
public DateExpression addDays(Integer daysToAdd) {
return this.addDays(value(daysToAdd));
}
/**
* Date Arithmetic: add the supplied number of days to the date expression.
*
*
* Negative values are supported.
*
* @param daysToAdd days to offset by
*
Support DBvolution at
* Patreon
* @return a DateExpression
*/
public DateExpression addDays(Long daysToAdd) {
return this.addDays(value(daysToAdd));
}
/**
* Date Arithmetic: add the supplied number of days to the date expression.
*
*
* Negative values are supported.
*
* @param daysToAdd days to offset by
*
Support DBvolution at
* Patreon
* @return a DateExpression
*/
public DateExpression addDays(Number daysToAdd) {
return this.addDays(value(daysToAdd));
}
/**
* Date Arithmetic: add the supplied number of days to the date expression.
*
*
* Negative values are supported.
*
* @param daysToAdd days to offset by
*
Support DBvolution at
* Patreon
* @return a DateExpression
*/
public DateExpression addDays(IntegerExpression daysToAdd) {
return new DateExpression(
new DateIntegerExpressionWithDateResult(this, daysToAdd) {
@Override
public boolean getIncludesNull() {
return false;
}
@Override
public String toSQLString(DBDefinition db) {
return db.doAddDaysTransform(first.toSQLString(db), second.toSQLString(db));
}
});
}
/**
* Date Arithmetic: add the supplied number of days to the date expression.
*
*
* Negative values are supported.
*
* @param daysToAdd days to offset by
*
Support DBvolution at
* Patreon
* @return a DateExpression
*/
public DateExpression addDays(NumberExpression daysToAdd) {
return new DateExpression(
new DateNumberExpressionWithDateResult(this, daysToAdd) {
@Override
public boolean getIncludesNull() {
return false;
}
@Override
public String toSQLString(DBDefinition db) {
return db.doAddDaysTransform(first.toSQLString(db), second.toSQLString(db));
}
});
}
/**
* Date Arithmetic: add the supplied number of hours to the date expression.
*
*
* Negative values are supported.
*
* @param hoursToAdd hours to offset by
*
Support DBvolution at
* Patreon
* @return a DateExpression
*/
public DateExpression addHours(int hoursToAdd) {
return this.addHours(value(hoursToAdd));
}
/**
* Date Arithmetic: add the supplied number of hours to the date expression.
*
*
* Negative values are supported.
*
* @param hoursToAdd hours to offset by
*
Support DBvolution at
* Patreon
* @return a DateExpression
*/
public DateExpression addHours(IntegerExpression hoursToAdd) {
return new DateExpression(
new DateIntegerExpressionWithDateResult(this, hoursToAdd) {
@Override
public boolean getIncludesNull() {
return false;
}
@Override
public String toSQLString(DBDefinition db) {
return db.doAddHoursTransform(first.toSQLString(db), second.toSQLString(db));
}
});
}
/**
* Date Arithmetic: add the supplied number of weeks to the date expression.
*
*
* Negative values are supported.
*
* @param weeksToAdd weeks to offset by
*
Support DBvolution at
* Patreon
* @return a DateExpression
*/
public DateExpression addWeeks(int weeksToAdd) {
return this.addWeeks(value(weeksToAdd));
}
/**
* Date Arithmetic: add the supplied number of weeks to the date expression.
*
*
* Negative values are supported.
*
* @param weeksToAdd weeks to offset by
*
Support DBvolution at
* Patreon
* @return a DateExpression
*/
public DateExpression addWeeks(IntegerExpression weeksToAdd) {
return new DateExpression(
new DateIntegerExpressionWithDateResult(this, weeksToAdd) {
@Override
public boolean getIncludesNull() {
return false;
}
@Override
public String toSQLString(DBDefinition db) {
return db.doAddWeeksTransform(first.toSQLString(db), second.toSQLString(db));
}
});
}
/**
* Date Arithmetic: add the supplied number of months to the date expression.
*
*
* Negative values are supported.
*
* @param monthsToAdd months to offset by
*
Support DBvolution at
* Patreon
* @return a DateExpression
*/
public DateExpression addMonths(Number monthsToAdd) {
return this.addMonths(value(monthsToAdd));
}
/**
* Date Arithmetic: add the supplied number of months to the date expression.
*
*
* Negative values are supported.
*
* @param monthsToAdd months to offset by
*
Support DBvolution at
* Patreon
* @return a DateExpression
*/
public DateExpression addMonths(Integer monthsToAdd) {
return this.addMonths(value(monthsToAdd));
}
/**
* Date Arithmetic: add the supplied number of months to the date expression.
*
*
* Negative values are supported.
*
* @param monthsToAdd months to offset by
*
Support DBvolution at
* Patreon
* @return a DateExpression
*/
public DateExpression addMonths(Long monthsToAdd) {
return this.addMonths(value(monthsToAdd));
}
/**
* Date Arithmetic: add the supplied number of months to the date expression.
*
*
* Negative values are supported.
*
* @param monthsToAdd months to offset by
*
Support DBvolution at
* Patreon
* @return a DateExpression
*/
public DateExpression addMonths(IntegerExpression monthsToAdd) {
return new DateExpression(
new DateIntegerExpressionWithDateResult(this, monthsToAdd) {
@Override
public boolean getIncludesNull() {
return false;
}
@Override
public String toSQLString(DBDefinition db) {
return db.doAddMonthsTransform(first.toSQLString(db), second.toSQLString(db));
}
});
}
/**
* Date Arithmetic: add the supplied number of months to the date expression.
*
*
* Negative values are supported.
*
* @param monthsToAdd months to offset by
*
Support DBvolution at
* Patreon
* @return a DateExpression
*/
public DateExpression addMonths(NumberExpression monthsToAdd) {
return new DateExpression(
new DateNumberExpressionWithDateResult(this, monthsToAdd) {
@Override
public boolean getIncludesNull() {
return false;
}
@Override
public String toSQLString(DBDefinition db) {
return db.doAddMonthsTransform(first.toSQLString(db), second.toSQLString(db));
}
});
}
/**
* Date Arithmetic: add the supplied number of years to the date expression.
*
*
* Negative values are supported.
*
* @param yearsToAdd years to offset by
*
Support DBvolution at
* Patreon
* @return a DateExpression
*/
public DateExpression addYears(int yearsToAdd) {
return this.addYears(value(yearsToAdd));
}
/**
* Date Arithmetic: add the supplied number of years to the date expression.
*
*
* Negative values are supported.
*
* @param yearsToAdd years to offset by
*
Support DBvolution at
* Patreon
* @return a DateExpression
*/
public DateExpression addYears(IntegerExpression yearsToAdd) {
return new DateExpression(
new DateIntegerExpressionWithDateResult(this, yearsToAdd) {
@Override
public boolean getIncludesNull() {
return false;
}
@Override
public String toSQLString(DBDefinition db) {
return db.doAddYearsTransform(first.toSQLString(db), second.toSQLString(db));
}
});
}
/**
* Date Arithmetic: get the days between the date expression and the supplied
* date.
*
*
* Negative values are supported.
*
* @param dateToCompareTo date to compare
*
Support DBvolution at
* Patreon
* @return a NumberExpression
*/
public NumberExpression daysFrom(Date dateToCompareTo) {
return daysFrom(DateExpression.value(dateToCompareTo));
}
/**
* Date Arithmetic: get the days between the date expression and the supplied
* date.
*
*
* Negative values are supported.
*
* @param dateToCompareTo date to compare
*
Support DBvolution at
* Patreon
* @return a NumberExpression
*/
public NumberExpression daysFrom(DateResult dateToCompareTo) {
return new NumberExpression(
new DateDateFunctionWithNumberResult(this, dateToCompareTo) {
@Override
public boolean getIncludesNull() {
return false;
}
@Override
public String toSQLString(DBDefinition db) {
return db.doDayDifferenceTransform(first.toSQLString(db), second.toSQLString(db));
}
});
}
/**
* Date Arithmetic: get the weeks between the date expression and the supplied
* date.
*
*
* Negative values are supported.
*
* @param dateToCompareTo date to compare
*
Support DBvolution at
* Patreon
* @return a NumberExpression
*/
public NumberExpression weeksFrom(Date dateToCompareTo) {
return weeksFrom(DateExpression.value(dateToCompareTo));
}
/**
* Date Arithmetic: get the weeks between the date expression and the supplied
* date.
*
*
* Negative values are supported.
*
* @param dateToCompareTo date to compare
*
Support DBvolution at
* Patreon
* @return a NumberExpression
*/
public NumberExpression weeksFrom(DateExpression dateToCompareTo) {
return new NumberExpression(
new DateDateFunctionWithNumberResult(this, dateToCompareTo) {
@Override
public boolean getIncludesNull() {
return false;
}
@Override
public String toSQLString(DBDefinition db) {
return db.doWeekDifferenceTransform(first.toSQLString(db), second.toSQLString(db));
}
});
}
/**
* Date Arithmetic: get the months between the date expression and the
* supplied date.
*
*
* Negative values are supported.
*
* @param dateToCompareTo date to compare
*
Support DBvolution at
* Patreon
* @return a NumberExpression
*/
public NumberExpression monthsFrom(Date dateToCompareTo) {
return monthsFrom(DateExpression.value(dateToCompareTo));
}
/**
* Date Arithmetic: get the months between the date expression and the
* supplied date.
*
*
* Negative values are supported.
*
* @param dateToCompareTo date to compare
*
Support DBvolution at
* Patreon
* @return a NumberExpression
*/
public NumberExpression monthsFrom(DateResult dateToCompareTo) {
return new NumberExpression(
new DateDateFunctionWithNumberResult(this, dateToCompareTo) {
@Override
public boolean getIncludesNull() {
return false;
}
@Override
public String toSQLString(DBDefinition db) {
return db.doMonthDifferenceTransform(first.toSQLString(db), second.toSQLString(db));
}
});
}
/**
* Date Arithmetic: get the years between the date expression and the supplied
* date.
*
*
* Negative values are supported.
*
* @param dateToCompareTo date to compare
*
Support DBvolution at
* Patreon
* @return a NumberExpression
*/
public NumberExpression yearsFrom(Date dateToCompareTo) {
return yearsFrom(DateExpression.value(dateToCompareTo));
}
/**
* Date Arithmetic: get the years between the date expression and the supplied
* date.
*
*
* Negative values are supported.
*
* @param dateToCompareTo date to compare
*
Support DBvolution at
* Patreon
* @return a NumberExpression
*/
public NumberExpression yearsFrom(DateResult dateToCompareTo) {
return new NumberExpression(
new DateDateFunctionWithNumberResult(this, dateToCompareTo) {
@Override
public boolean getIncludesNull() {
return false;
}
@Override
public String toSQLString(DBDefinition db) {
return db.doYearDifferenceTransform(first.toSQLString(db), second.toSQLString(db));
}
});
}
/**
* Date Arithmetic: get the Hours between the date expression and the supplied
* date.
*
*
* Negative values are supported.
*
* @param dateToCompareTo date to compare
*
Support DBvolution at
* Patreon
* @return a NumberExpression
*/
public NumberExpression hoursFrom(Date dateToCompareTo) {
return hoursFrom(DateExpression.value(dateToCompareTo));
}
/**
* Date Arithmetic: get the Hours between the date expression and the supplied
* date.
*
*
* Negative values are supported.
*
* @param dateToCompareTo date to compare
*
Support DBvolution at
* Patreon
* @return a NumberExpression
*/
public NumberExpression hoursFrom(DateResult dateToCompareTo) {
return new NumberExpression(
new DateDateFunctionWithNumberResult(this, dateToCompareTo) {
@Override
public boolean getIncludesNull() {
return false;
}
@Override
public String toSQLString(DBDefinition db) {
return db.doHourDifferenceTransform(first.toSQLString(db), second.toSQLString(db));
}
});
}
/**
* Date Arithmetic: get the minutes between the date expression and the
* supplied date.
*
*
* Negative values are supported.
*
* @param dateToCompareTo date to compare
*
Support DBvolution at
* Patreon
* @return a NumberExpression
*/
public NumberExpression minutesFrom(Date dateToCompareTo) {
return minutesFrom(DateExpression.value(dateToCompareTo));
}
/**
* Date Arithmetic: get the days between the date expression and the supplied
* date.
*
*
* Negative values are supported.
*
* @param dateToCompareTo date to compare
*
Support DBvolution at
* Patreon
* @return a NumberExpression
*/
public NumberExpression minutesFrom(DateResult dateToCompareTo) {
return new NumberExpression(
new DateDateFunctionWithNumberResult(this, dateToCompareTo) {
@Override
public boolean getIncludesNull() {
return false;
}
@Override
public String toSQLString(DBDefinition db) {
return db.doMinuteDifferenceTransform(first.toSQLString(db), second.toSQLString(db));
}
});
}
/**
* Date Arithmetic: get the seconds between the date expression and the
* supplied date.
*
*
* Negative values are supported.
*
* @param dateToCompareTo date to compare
*
Support DBvolution at
* Patreon
* @return a NumberExpression
*/
public NumberExpression secondsFrom(Date dateToCompareTo) {
return secondsFrom(DateExpression.value(dateToCompareTo));
}
/**
* Date Arithmetic: get the days between the date expression and the supplied
* date.
*
*
* Negative values are supported.
*
* @param dateToCompareTo date to compare
*
Support DBvolution at
* Patreon
* @return a NumberExpression
*/
public NumberExpression secondsFrom(DateResult dateToCompareTo) {
return new NumberExpression(
new DateDateFunctionWithNumberResult(this, dateToCompareTo) {
@Override
public boolean getIncludesNull() {
return false;
}
@Override
public String toSQLString(DBDefinition db) {
return db.doSecondDifferenceTransform(first.toSQLString(db), second.toSQLString(db));
}
});
}
/**
* Derive the first day of the month for this date expression
*
* Support DBvolution at
* Patreon
*
* @return a Date expression
*/
public DateExpression firstOfMonth() {
return this.addDays(this.day().minus(1).bracket().times(-1).integerResult());
}
/**
* Derive the last day of the month for this date expression
*
* Support DBvolution at
* Patreon
*
* @return a Date expression
*/
public DateExpression endOfMonth() {
return new DateExpression(
new DateExpression(this) {
@Override
public String toSQLString(DBDefinition db) {
try {
return db.doEndOfMonthTransform(this.getInnerResult().toSQLString(db));
} catch (UnsupportedOperationException exp) {
DateExpression only = (DateExpression)getInnerResult();
return only
.addDays(only.day().minus(1).bracket().times(-1).integerResult())
.addMonths(1).addDays(-1).toSQLString(db);
}
}
}
);
}
/**
* Return the index of the day of the week that this date expression refers
* to.
*
* Refer to {@link #SUNDAY}, {@link #MONDAY}, etc
*
* Support DBvolution at
* Patreon
*
* @return an index of the day of the week.
*/
public NumberExpression dayOfWeek() {
return new NumberExpression(
new DateExpressionWithNumberResult(this) {
@Override
public String toSQLString(DBDefinition db) {
return db.doDayOfWeekTransform(this.getInnerResult().toSQLString(db));
}
});
}
/**
* Considering the first and second dates as end point for a time period and
* similarly for the third and fourth, tests whether the 2 time periods
* overlap.
*
* @param firstStartTime the beginning of the first interval
* @param firstEndTime the end of the first interval
* @param secondStartTime the beginning of the second interval
* @param secondEndtime the end of the second interval
* Support DBvolution at
* Patreon
* @return a boolean expression
*/
public static BooleanExpression overlaps(Date firstStartTime, Date firstEndTime, Date secondStartTime, Date secondEndtime) {
return DateExpression.overlaps(
DateExpression.value(firstStartTime), DateExpression.value(firstEndTime),
DateExpression.value(secondStartTime), DateExpression.value(secondEndtime)
);
}
/**
* Considering the first and second dates as end point for a time period and
* similarly for the third and fourth, tests whether the 2 time periods
* overlap.
*
* @param firstStartTime the beginning of the first interval
* @param firstEndTime the end of the first interval
* @param secondStartTime the beginning of the second interval
* @param secondEndtime the end of the second interval
* Support DBvolution at
* Patreon
* @return a boolean expression
*/
public static BooleanExpression overlaps(DateResult firstStartTime, DateResult firstEndTime, DateResult secondStartTime, DateResult secondEndtime) {
return DateExpression.overlaps(
new DateExpression(firstStartTime), new DateExpression(firstEndTime),
secondStartTime, secondEndtime
);
}
/**
* Considering the first and second dates as end point for a time period and
* similarly for the third and fourth, tests whether the 2 time periods
* overlap.
*
* @param firstStartTime the beginning of the first interval
* @param firstEndTime the end of the first interval
* @param secondStartTime the beginning of the second interval
* @param secondEndtime the end of the second interval
* Support DBvolution at
* Patreon
* @return a Boolean expression
*/
public static BooleanExpression overlaps(DateExpression firstStartTime, DateExpression firstEndTime, DateResult secondStartTime, DateResult secondEndtime) {
return BooleanExpression.anyOf(
firstStartTime.isBetween(
leastOf(secondStartTime, secondEndtime),
greatestOf(secondStartTime, secondEndtime)),
firstEndTime.isBetween(
leastOf(secondStartTime, secondEndtime),
greatestOf(secondStartTime, secondEndtime)
)
);
}
/**
* Returns the least/smallest value from the list.
*
*
* Similar to {@link #min() } but this operates on the list provided, rather
* than aggregating a column.
*
* @param possibleValues needs to be the least of these
*
Support DBvolution at
* Patreon
* @return the least/smallest value from the list.
*/
public static DateExpression leastOf(Date... possibleValues) {
ArrayList possVals = new ArrayList();
for (Date num : possibleValues) {
possVals.add(value(num));
}
return leastOf(possVals.toArray(new DateExpression[]{}));
}
/**
* Returns the least/smallest value from the list.
*
*
* Similar to {@link #min() } but this operates on the list provided, rather
* than aggregating a column.
*
* @param possibleValues needs to be the least of these
*
Support DBvolution at
* Patreon
* @return the least/smallest value from the list.
*/
public static DateExpression leastOf(Collection extends DateResult> possibleValues) {
ArrayList possVals = new ArrayList();
for (DateResult num : possibleValues) {
possVals.add(new DateExpression(num));
}
return leastOf(possVals.toArray(new DateExpression[]{}));
}
/**
* Returns the least/smallest value from the list.
*
*
* Similar to {@link #min() } but this operates on the list provided, rather
* than aggregating a column.
*
* @param possibleValues needs to be the least of these
*
Support DBvolution at
* Patreon
* @return the least/smallest value from the list.
*/
public static DateExpression leastOf(DateResult... possibleValues) {
DateExpression leastExpr
= new DateExpression(new DateArrayFunctionWithDateResult(possibleValues) {
@Override
public String toSQLString(DBDefinition db) {
List strs = new ArrayList();
for (DateResult num : this.values) {
strs.add(num.toSQLString(db));
}
return db.doLeastOfTransformation(strs);
}
@Override
protected String getFunctionName(DBDefinition db) {
return db.getLeastOfFunctionName();
}
@Override
public boolean getIncludesNull() {
return true;
}
});
return leastExpr;
}
/**
* Returns the largest value from the list.
*
*
* Similar to {@link #min() } but this operates on the list provided, rather
* than aggregating a column.
*
* @param possibleValues needs to be the largest of these
*
Support DBvolution at
* Patreon
* @return the largest value from the list.
*/
public static DateExpression greatestOf(Date... possibleValues) {
ArrayList possVals = new ArrayList();
for (Date num : possibleValues) {
possVals.add(value(num));
}
return greatestOf(possVals.toArray(new DateExpression[]{}));
}
/**
* Returns the largest value from the list.
*
*
* Similar to {@link #min() } but this operates on the list provided, rather
* than aggregating a column.
*
* @param possibleValues needs to be the largest of these
*
Support DBvolution at
* Patreon
* @return the largest value from the list.
*/
public static DateExpression greatestOf(Collection extends DateResult> possibleValues) {
ArrayList possVals = new ArrayList();
for (DateResult num : possibleValues) {
possVals.add(new DateExpression(num));
}
return greatestOf(possVals.toArray(new DateExpression[]{}));
}
/**
* Returns the largest value from the list.
*
*
* Similar to {@link #min() } but this operates on the list provided, rather
* than aggregating a column.
*
* @param possibleValues needs to be the largest of these
*
Support DBvolution at
* Patreon
* @return the largest value from the list.
*/
public static DateExpression greatestOf(DateResult... possibleValues) {
DateExpression greatestOf
= new DateExpression(new DateArrayFunctionWithDateResult(possibleValues) {
@Override
public String toSQLString(DBDefinition db) {
List strs = new ArrayList();
for (DateResult num : this.values) {
strs.add(num.toSQLString(db));
}
return db.doGreatestOfTransformation(strs);
}
@Override
protected String getFunctionName(DBDefinition db) {
return db.getGreatestOfFunctionName();
}
});
return greatestOf;
}
@Override
public DBDate asExpressionColumn() {
return new DBDate(this);
}
/**
* Returns the date as an ISO 8601 formatted string NOT including time zone.
*
*
* ISO 8601 form at is YYYY-MM-DDTHH:mm:ss.sss
*
*
* May not be zero padded but the format is still unambiguous.
*
* @return a ISO formatted version of this date
*/
@Override
public StringExpression stringResult() {
return this.stringResultISOFormat();
}
/**
* Returns the date as an ISO 8601 formatted string NOT including time zone.
*
*
* ISO 8601 form at is YYYY-MM-DDTHH:mm:ss.sss
*
*
* May not be zero padded but the format is still unambiguous.
*
* @return a ISO formatted version of this date
*/
public StringExpression stringResultISOFormat() {
StringExpression isoFormatDateTime = new StringExpression("")
.append(this.year())
.append("-")
.append(this.month())
.append("-")
.append(this.day())
.append("T")
.append(this.hour())
.append(":")
.append(this.minute())
.append(":")
.append(this.second())
.append(".")
.append(this.subsecond());
return isoFormatDateTime;
}
/**
* Returns the date as a USA formatted string NOT including time zone.
*
*
*USA format is MM-DD-YYYY HH:mm:ss.sss
*
*
* May not be zero padded but the format is still unambiguous.
*
* @return a USA formatted version of this date
*/
public StringExpression stringResultUSAFormat() {
StringExpression usaFormatDateTime = new StringExpression("")
.append(this.month())
.append("-")
.append(this.day())
.append("-")
.append(this.year())
.append(" ")
.append(this.hour())
.append(":")
.append(this.minute())
.append(":")
.append(this.second())
.append(".")
.append(this.subsecond());
return usaFormatDateTime;
}
/**
* Returns the date as formatted string NOT including time zone.
*
*
* Common format is DD-MM-YYYY HH:mm:ss.sss
*
*
* May not be zero padded but the format is still unambiguous.
*
* @return a formatted version of this date using the format commonly used around the world
*/
public StringExpression stringResultCommonFormat() {
StringExpression commonFormatDateTime = new StringExpression("")
.append(this.day())
.append("-")
.append(this.month())
.append("-")
.append(this.year())
.append(" ")
.append(this.hour())
.append(":")
.append(this.minute())
.append(":")
.append(this.second())
.append(".")
.append(this.subsecond());
return commonFormatDateTime;
}
@Override
public DateExpression expression(Date value) {
return new DateExpression(value);
}
@Override
public DateExpression expression(DateResult value) {
return new DateExpression(value);
}
@Override
public DateResult expression(DBDate value) {
return new DateExpression(value);
}
private static abstract class FunctionWithDateResult extends DateExpression {
FunctionWithDateResult() {
}
protected String getFunctionName(DBDefinition db) {
return "";
}
protected String beforeValue(DBDefinition db) {
return " " + getFunctionName(db) + "";
}
protected String afterValue(DBDefinition db) {
return " ";
}
@Override
public String toSQLString(DBDefinition db) {
return this.beforeValue(db) + this.afterValue(db);
}
@Override
public DateExpression.FunctionWithDateResult copy() {
DateExpression.FunctionWithDateResult newInstance;
try {
newInstance = getClass().newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
throw new RuntimeException(ex);
}
return newInstance;
}
@Override
public Set getTablesInvolved() {
return new HashSet();
}
@Override
public boolean isAggregator() {
return false;
}
@Override
public boolean getIncludesNull() {
return false;
}
@Override
public boolean isPurelyFunctional() {
return true;
}
}
private static abstract class DateExpressionWithNumberResult extends NumberExpression {
DateExpressionWithNumberResult() {
super();
}
DateExpressionWithNumberResult(DateExpression only) {
super(only);
}
@Override
public DBNumber getQueryableDatatypeForExpressionValue() {
return new DBNumber();
}
@Override
public abstract String toSQLString(DBDefinition db);
}
private static abstract class DateDateExpressionWithBooleanResult extends BooleanExpression {
private DateExpression first;
private DateResult second;
private boolean requiresNullProtection = false;
DateDateExpressionWithBooleanResult(DateExpression first, DateResult second) {
this.first = first;
this.second = second;
if (second == null || second.getIncludesNull()) {
this.requiresNullProtection = true;
}
}
@Override
public DBBoolean getQueryableDatatypeForExpressionValue() {
return new DBBoolean();
}
@Override
public String toSQLString(DBDefinition db) {
return first.toSQLString(db) + this.getEquationOperator(db) + second.toSQLString(db);
}
@Override
public DateDateExpressionWithBooleanResult copy() {
DateDateExpressionWithBooleanResult newInstance;
try {
newInstance = getClass().newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
throw new RuntimeException(ex);
}
newInstance.first = first.copy();
newInstance.second = second.copy();
return newInstance;
}
@Override
public Set getTablesInvolved() {
HashSet hashSet = new HashSet();
if (first != null) {
hashSet.addAll(first.getTablesInvolved());
}
if (second != null) {
hashSet.addAll(second.getTablesInvolved());
}
return hashSet;
}
protected abstract String getEquationOperator(DBDefinition db);
@Override
public boolean isAggregator() {
return first.isAggregator() || second.isAggregator();
}
@Override
public boolean getIncludesNull() {
return requiresNullProtection;
}
}
private static abstract class DateDateExpressionWithDateRepeatResult extends DateRepeatExpression {
private DateExpression first;
private DateResult second;
private boolean requiresNullProtection = false;
DateDateExpressionWithDateRepeatResult(DateExpression first, DateResult second) {
this.first = first;
this.second = second;
if (second == null || second.getIncludesNull()) {
this.requiresNullProtection = true;
}
}
@Override
public DateDateExpressionWithDateRepeatResult copy() {
DateDateExpressionWithDateRepeatResult newInstance;
try {
newInstance = getClass().newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
throw new RuntimeException(ex);
}
newInstance.first = getFirst().copy();
newInstance.second = getSecond().copy();
return newInstance;
}
@Override
public Set getTablesInvolved() {
HashSet hashSet = new HashSet();
if (getFirst() != null) {
hashSet.addAll(getFirst().getTablesInvolved());
}
if (getSecond() != null) {
hashSet.addAll(getSecond().getTablesInvolved());
}
return hashSet;
}
@Override
public boolean isAggregator() {
return getFirst().isAggregator() || getSecond().isAggregator();
}
@Override
public boolean getIncludesNull() {
return requiresNullProtection;
}
/**
* Support DBvolution at
* Patreon
*
* @return the first
*/
public DateExpression getFirst() {
return first;
}
/**
* Support DBvolution at
* Patreon
*
* @return the second
*/
public DateResult getSecond() {
return second;
}
}
private static abstract class DateDateRepeatArithmeticDateResult extends DateExpression {
private DateExpression first;
private DateRepeatResult second;
private boolean requiresNullProtection = false;
DateDateRepeatArithmeticDateResult(DateExpression first, DateRepeatResult second) {
this.first = first;
this.second = second;
if (second == null || second.getIncludesNull()) {
this.requiresNullProtection = true;
}
}
@Override
public String toSQLString(DBDefinition db) {
return this.doExpressionTransformation(db);
}
@Override
public DateDateRepeatArithmeticDateResult copy() {
DateDateRepeatArithmeticDateResult newInstance;
try {
newInstance = getClass().newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
throw new RuntimeException(ex);
}
newInstance.first = getFirst().copy();
newInstance.second = getSecond().copy();
return newInstance;
}
@Override
public Set getTablesInvolved() {
HashSet hashSet = new HashSet();
if (getFirst() != null) {
hashSet.addAll(getFirst().getTablesInvolved());
}
if (getSecond() != null) {
hashSet.addAll(getSecond().getTablesInvolved());
}
return hashSet;
}
protected abstract String doExpressionTransformation(DBDefinition db);
@Override
public boolean isAggregator() {
return getFirst().isAggregator() || getSecond().isAggregator();
}
@Override
public boolean getIncludesNull() {
return requiresNullProtection;
}
/**
* Support DBvolution at
* Patreon
*
* @return the first
*/
public DateExpression getFirst() {
return first;
}
/**
* Support DBvolution at
* Patreon
*
* @return the second
*/
public DateRepeatResult getSecond() {
return second;
}
}
private static abstract class DateArrayFunctionWithDateResult extends DateExpression {
protected DateExpression column;
protected final List values = new ArrayList();
boolean nullProtectionRequired = false;
DateArrayFunctionWithDateResult() {
}
DateArrayFunctionWithDateResult(DateResult[] rightHandSide) {
for (DateResult dateResult : rightHandSide) {
if (dateResult == null) {
this.nullProtectionRequired = true;
} else {
if (dateResult.getIncludesNull()) {
this.nullProtectionRequired = true;
}
values.add(dateResult);
}
}
}
protected String getFunctionName(DBDefinition db) {
return "";
}
protected String beforeValue(DBDefinition db) {
return "( ";
}
protected String afterValue(DBDefinition db) {
return ") ";
}
@Override
public String toSQLString(DBDefinition db) {
StringBuilder builder = new StringBuilder();
builder
.append(this.getFunctionName(db))
.append(this.beforeValue(db));
String separator = "";
for (DateResult val : values) {
if (val != null) {
builder.append(separator).append(val.toSQLString(db));
}
separator = ", ";
}
builder.append(this.afterValue(db));
return builder.toString();
}
@Override
public DateArrayFunctionWithDateResult copy() {
DateArrayFunctionWithDateResult newInstance;
try {
newInstance = getClass().newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
throw new RuntimeException(ex);
}
newInstance.column = this.column.copy();
Collections.copy(this.values, newInstance.values);
return newInstance;
}
@Override
public Set getTablesInvolved() {
HashSet hashSet = new HashSet();
if (column != null) {
hashSet.addAll(column.getTablesInvolved());
}
for (DateResult second : values) {
if (second != null) {
hashSet.addAll(second.getTablesInvolved());
}
}
return hashSet;
}
@Override
public boolean isAggregator() {
boolean result = false;
if (column != null) {
result = column.isAggregator();
}
for (DateResult numer : values) {
result = result || numer.isAggregator();
}
return result;
}
@Override
public boolean getIncludesNull() {
return nullProtectionRequired;
}
@Override
public boolean isPurelyFunctional() {
if (column == null && values.isEmpty()) {
return true;
} else {
boolean result = column == null ? true : column.isPurelyFunctional();
for (DateResult value : values) {
result &= value.isPurelyFunctional();
}
return result;
}
}
}
private static abstract class DateDateResultFunctionWithBooleanResult extends BooleanExpression {
private DateExpression column;
private List values = new ArrayList();
boolean nullProtectionRequired = false;
DateDateResultFunctionWithBooleanResult() {
}
DateDateResultFunctionWithBooleanResult(DateExpression leftHandSide, DateResult[] rightHandSide) {
this.column = leftHandSide;
for (DateResult dateResult : rightHandSide) {
if (dateResult == null) {
this.nullProtectionRequired = true;
} else {
if (dateResult.getIncludesNull()) {
this.nullProtectionRequired = true;
} else {
values.add(dateResult);
}
}
}
}
@Override
public DBBoolean getQueryableDatatypeForExpressionValue() {
return new DBBoolean();
}
protected String getFunctionName(DBDefinition db) {
return "";
}
protected String beforeValue(DBDefinition db) {
return "( ";
}
protected String afterValue(DBDefinition db) {
return ") ";
}
@Override
public String toSQLString(DBDefinition db) {
StringBuilder builder = new StringBuilder();
builder
.append(getColumn().toSQLString(db))
.append(this.getFunctionName(db))
.append(this.beforeValue(db));
String separator = "";
for (DateResult val : getValues()) {
if (val != null) {
builder.append(separator).append(val.toSQLString(db));
}
separator = ", ";
}
builder.append(this.afterValue(db));
return builder.toString();
}
@Override
public DateDateResultFunctionWithBooleanResult copy() {
DateDateResultFunctionWithBooleanResult newInstance;
try {
newInstance = getClass().newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
throw new RuntimeException(ex);
}
newInstance.column = this.getColumn().copy();
Collections.copy(this.getValues(), newInstance.getValues());
return newInstance;
}
@Override
public Set getTablesInvolved() {
HashSet hashSet = new HashSet();
if (getColumn() != null) {
hashSet.addAll(getColumn().getTablesInvolved());
}
for (DateResult val : getValues()) {
if (val != null) {
hashSet.addAll(val.getTablesInvolved());
}
}
return hashSet;
}
@Override
public boolean isAggregator() {
boolean result = false || getColumn().isAggregator();
for (DateResult dater : getValues()) {
result = result || dater.isAggregator();
}
return result;
}
@Override
public boolean getIncludesNull() {
return nullProtectionRequired;
}
/**
* Support DBvolution at
* Patreon
*
* @return the column
*/
protected DateExpression getColumn() {
return column;
}
/**
* Support DBvolution at
* Patreon
*
* @return the values
*/
protected List getValues() {
return values;
}
}
private static abstract class DateDateFunctionWithDateResult extends DateExpression {
private DateExpression first;
private DateResult second;
DateDateFunctionWithDateResult(DateExpression first) {
this.first = first;
this.second = null;
}
DateDateFunctionWithDateResult(DateExpression first, DateResult second) {
this.first = first;
this.second = second;
}
@Override
public abstract String toSQLString(DBDefinition db);
@Override
public DateDateFunctionWithDateResult copy() {
DateDateFunctionWithDateResult newInstance;
try {
newInstance = getClass().newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
throw new RuntimeException(ex);
}
newInstance.first = getFirst().copy();
newInstance.second = getSecond().copy();
return newInstance;
}
@Override
public Set getTablesInvolved() {
HashSet hashSet = new HashSet();
if (getFirst() != null) {
hashSet.addAll(getFirst().getTablesInvolved());
}
if (getSecond() != null) {
hashSet.addAll(getSecond().getTablesInvolved());
}
return hashSet;
}
@Override
public boolean isAggregator() {
return getFirst().isAggregator() || getSecond().isAggregator();
}
/**
* Support DBvolution at
* Patreon
*
* @return the first
*/
protected DateExpression getFirst() {
return first;
}
/**
* Support DBvolution at
* Patreon
*
* @return the second
*/
protected DateResult getSecond() {
return second;
}
@Override
public boolean isPurelyFunctional() {
if (first == null && second == null) {
return true;
} else if (first == null) {
return second.isPurelyFunctional();
} else if (second == null) {
return first.isPurelyFunctional();
} else {
return first.isPurelyFunctional() && second.isPurelyFunctional();
}
}
}
private static abstract class DateFunctionWithDateResult extends DateExpression {
DateFunctionWithDateResult() {
super();
}
DateFunctionWithDateResult(DateExpression only) {
super(only);
}
protected String getFunctionName(DBDefinition db) {
return "";
}
protected String beforeValue(DBDefinition db) {
return "" + getFunctionName(db) + "( ";
}
protected String afterValue(DBDefinition db) {
return ") ";
}
@Override
public String toSQLString(DBDefinition db) {
return this.beforeValue(db) + (getInnerResult() == null ? "" : getInnerResult().toSQLString(db)) + this.afterValue(db);
}
}
private static abstract class DateIntegerExpressionWithDateResult extends DateExpression {
protected DateExpression first;
protected IntegerExpression second;
DateIntegerExpressionWithDateResult() {
this.first = null;
this.second = null;
}
DateIntegerExpressionWithDateResult(DateExpression dateExp, IntegerExpression numbExp) {
this.first = dateExp;
this.second = numbExp;
}
@Override
abstract public String toSQLString(DBDefinition db);
@Override
public DateIntegerExpressionWithDateResult copy() {
DateIntegerExpressionWithDateResult newInstance;
try {
newInstance = getClass().newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
throw new RuntimeException(ex);
}
newInstance.first = first.copy();
newInstance.second = second.copy();
return newInstance;
}
@Override
public boolean isAggregator() {
return first.isAggregator() || second.isAggregator();
}
@Override
public Set getTablesInvolved() {
final Set tablesInvolved = first.getTablesInvolved();
tablesInvolved.addAll(second.getTablesInvolved());
return tablesInvolved;
}
@Override
public boolean isPurelyFunctional() {
if (first == null && second == null) {
return true;
} else if (first == null) {
return second.isPurelyFunctional();
} else if (second == null) {
return first.isPurelyFunctional();
} else {
return first.isPurelyFunctional() && second.isPurelyFunctional();
}
}
}
private static abstract class DateNumberExpressionWithDateResult extends DateExpression {
protected DateExpression first;
protected NumberExpression second;
DateNumberExpressionWithDateResult() {
this.first = null;
this.second = null;
}
DateNumberExpressionWithDateResult(DateExpression dateExp, NumberExpression numbExp) {
this.first = dateExp;
this.second = numbExp;
}
@Override
abstract public String toSQLString(DBDefinition db);
@Override
public DateNumberExpressionWithDateResult copy() {
DateNumberExpressionWithDateResult newInstance;
try {
newInstance = getClass().newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
throw new RuntimeException(ex);
}
newInstance.first = first.copy();
newInstance.second = second.copy();
return newInstance;
}
@Override
public boolean isAggregator() {
return first.isAggregator() || second.isAggregator();
}
@Override
public Set getTablesInvolved() {
final Set tablesInvolved = first.getTablesInvolved();
tablesInvolved.addAll(second.getTablesInvolved());
return tablesInvolved;
}
@Override
public boolean isPurelyFunctional() {
if (first == null && second == null) {
return true;
} else if (first == null) {
return second.isPurelyFunctional();
} else if (second == null) {
return first.isPurelyFunctional();
} else {
return first.isPurelyFunctional() && second.isPurelyFunctional();
}
}
}
private static abstract class DateDateFunctionWithNumberResult extends NumberExpression {
protected DateExpression first;
protected DateResult second;
DateDateFunctionWithNumberResult() {
this.first = null;
this.second = null;
}
DateDateFunctionWithNumberResult(DateExpression dateExp, DateResult otherDateExp) {
this.first = dateExp;
this.second = otherDateExp;
}
@Override
abstract public String toSQLString(DBDefinition db);
@Override
public DateDateFunctionWithNumberResult copy() {
DateDateFunctionWithNumberResult newInstance;
try {
newInstance = getClass().newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
throw new RuntimeException(ex);
}
newInstance.first = first.copy();
newInstance.second = second.copy();
return newInstance;
}
@Override
public boolean isAggregator() {
return first.isAggregator() || second.isAggregator();
}
@Override
public Set getTablesInvolved() {
final Set tablesInvolved = first.getTablesInvolved();
tablesInvolved.addAll(second.getTablesInvolved());
return tablesInvolved;
}
@Override
public boolean isPurelyFunctional() {
if (first == null && second == null) {
return true;
} else if (first == null) {
return second.isPurelyFunctional();
} else if (second == null) {
return first.isPurelyFunctional();
} else {
return first.isPurelyFunctional() && second.isPurelyFunctional();
}
}
}
}