Skip to content

Commit c4e3805

Browse files
committed
Added WEEKDAY function
1 parent 210804f commit c4e3805

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/Processor/Expression/FunctionEvaluator.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ public static function evaluate(
9696
return self::sqlLastDay($conn, $scope, $expr, $row, $result);
9797
case 'CURDATE':
9898
return self::sqlCurDate($expr);
99+
case 'WEEKDAY':
100+
return self::sqlWeekDay($conn, $scope, $expr, $row, $result);
99101
}
100102

101103
throw new ProcessorException("Function " . $expr->functionName . " not implemented yet");
@@ -235,6 +237,7 @@ public static function getColumnSchema(
235237

236238
case 'DATEDIFF':
237239
case 'DAY':
240+
case 'WEEKDAY':
238241
return new Column\IntColumn(false, 10);
239242
}
240243

@@ -1026,6 +1029,35 @@ private static function sqlCurDate(FunctionExpression $expr): string
10261029
return (new \DateTimeImmutable())->format('Y-m-d');
10271030
}
10281031

1032+
/**
1033+
* @param array<string, mixed> $row
1034+
*/
1035+
private static function sqlWeekDay(
1036+
FakePdoInterface $conn,
1037+
Scope $scope,
1038+
FunctionExpression $expr,
1039+
array $row,
1040+
QueryResult $result
1041+
) : ?int {
1042+
$args = $expr->args;
1043+
1044+
if (\count($args) !== 1) {
1045+
throw new ProcessorException("MySQL WEEKDAY() function must be called with one argument");
1046+
}
1047+
1048+
$subject = Evaluator::evaluate($conn, $scope, $args[0], $row, $result);
1049+
1050+
if (!is_string($subject)) {
1051+
throw new \TypeError('Failed assertion');
1052+
}
1053+
1054+
if (!$subject || \strpos($subject, '0000-00-00') === 0) {
1055+
return null;
1056+
}
1057+
1058+
return (int)(new \DateTimeImmutable($subject))->format('N');
1059+
}
1060+
10291061
/**
10301062
* @param array<string, mixed> $row
10311063
*

tests/EndToEndTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,8 @@ public function testDateArithhmetic()
441441
DATE_ADD(\'2020-02-29 12:31:00\', INTERVAL 1 YEAR) as `g`,
442442
DATE_ADD(\'2020-02-29 12:31:00\', INTERVAL 4 YEAR) as `h`,
443443
DATE_SUB(\'2020-03-30\', INTERVAL 1 MONTH) As `i`,
444-
DATE_SUB(\'2020-03-01\', INTERVAL 1 MONTH) As `j`'
444+
DATE_SUB(\'2020-03-01\', INTERVAL 1 MONTH) As `j`,
445+
WEEKDAY(\'2021-04-29\') AS `k`'
445446
);
446447

447448
$query->execute();
@@ -458,6 +459,7 @@ public function testDateArithhmetic()
458459
'h' => '2024-02-29 12:31:00',
459460
'i' => '2020-02-29',
460461
'j' => '2020-02-01',
462+
'k' => 4,
461463
]],
462464
$query->fetchAll(\PDO::FETCH_ASSOC)
463465
);

0 commit comments

Comments
 (0)