Skip to content

Commit

Permalink
Reworked convertWeekModeFromMySqlToJava Function
Browse files Browse the repository at this point in the history
Signed-off-by: GabeFernandez310 <Gabriel.Fernandez@improving.com>
  • Loading branch information
GabeFernandez310 committed Mar 8, 2023
1 parent 84dec18 commit 271bf2d
Showing 1 changed file with 12 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -907,15 +907,15 @@ private DefaultFunctionResolver year() {
private DefaultFunctionResolver yearweek() {
return define(BuiltinFunctionName.YEARWEEK.getName(),
implWithProperties(nullMissingHandlingWithProperties((functionProperties, arg)
-> DateTimeFunction.yearweekToday(
-> yearweekToday(
DEFAULT_WEEK_OF_YEAR_MODE,
functionProperties.getQueryStartClock())), INTEGER, TIME),
impl(nullMissingHandling(DateTimeFunction::exprYearweekWithoutMode), INTEGER, DATE),
impl(nullMissingHandling(DateTimeFunction::exprYearweekWithoutMode), INTEGER, DATETIME),
impl(nullMissingHandling(DateTimeFunction::exprYearweekWithoutMode), INTEGER, TIMESTAMP),
impl(nullMissingHandling(DateTimeFunction::exprYearweekWithoutMode), INTEGER, STRING),
implWithProperties(nullMissingHandlingWithProperties((functionProperties, time, modeArg)
-> DateTimeFunction.yearweekToday(
-> yearweekToday(
modeArg,
functionProperties.getQueryStartClock())), INTEGER, TIME, INTEGER),
impl(nullMissingHandling(DateTimeFunction::exprYearweek), INTEGER, DATE, INTEGER),
Expand Down Expand Up @@ -1770,13 +1770,9 @@ public int convertWeekModeFromMySqlToJava(LocalDate date, int mode) {
// See description of modes here ...
// https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_week

if ((0 <= mode && mode <= 4) && (CalendarLookup.getWeekNumber(mode, date) == 0)) {
mode = 2;
} else if ((mode == 5) && (CalendarLookup.getWeekNumber(mode, date) == 0)) {
mode = 7;
}

return mode;
return CalendarLookup.getWeekNumber(mode, date) != 0 ? mode :
mode <= 4 ? 2 :
7;
}

/**
Expand All @@ -1786,12 +1782,12 @@ public int convertWeekModeFromMySqlToJava(LocalDate date, int mode) {
* @param mode is an integer containing the mode used to parse the LocalDate.
* @return is a long containing the formatted output for the yearweek function.
*/
private int extractYearweek(LocalDate date, int mode) {
mode = convertWeekModeFromMySqlToJava(date, mode);
int formatted = CalendarLookup.getYearNumber(mode, date) * 100
+ CalendarLookup.getWeekNumber(mode, date);
private ExprIntegerValue extractYearweek(LocalDate date, int mode) {
int modeJava = convertWeekModeFromMySqlToJava(date, mode);
int formatted = CalendarLookup.getYearNumber(modeJava, date) * 100
+ CalendarLookup.getWeekNumber(modeJava, date);

return formatted;
return new ExprIntegerValue(formatted);
}

/**
Expand All @@ -1801,7 +1797,7 @@ private int extractYearweek(LocalDate date, int mode) {
* @param mode ExprValue of Integer type.
*/
private ExprValue exprYearweek(ExprValue date, ExprValue mode) {
return new ExprIntegerValue(extractYearweek(date.dateValue(), mode.integerValue()));
return extractYearweek(date.dateValue(), mode.integerValue());
}

/**
Expand All @@ -1816,8 +1812,7 @@ private ExprValue exprYearweekWithoutMode(ExprValue date) {
}

private ExprValue yearweekToday(ExprValue mode, Clock clock) {
return new ExprIntegerValue(
extractYearweek(LocalDateTime.now(clock).toLocalDate(), mode.integerValue()));
return extractYearweek(LocalDateTime.now(clock).toLocalDate(), mode.integerValue());
}

private ExprValue monthOfYearToday(Clock clock) {
Expand Down

0 comments on commit 271bf2d

Please sign in to comment.