Skip to content

Commit 54dcd11

Browse files
authored
Merge pull request #20 from dmitr1y/feature/curdate-weekday
Add functions CURDATE and WEEKDAY
2 parents 2e8b092 + b17c846 commit 54dcd11

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

src/Processor/Expression/FunctionEvaluator.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ public static function evaluate(
9494
return self::sqlDay($conn, $scope, $expr, $row, $result);
9595
case 'LAST_DAY':
9696
return self::sqlLastDay($conn, $scope, $expr, $row, $result);
97+
case 'CURDATE':
98+
case 'CURRENT_DATE':
99+
return self::sqlCurDate($expr);
100+
case 'WEEKDAY':
101+
return self::sqlWeekDay($conn, $scope, $expr, $row, $result);
97102
}
98103

99104
throw new ProcessorException("Function " . $expr->functionName . " not implemented yet");
@@ -203,6 +208,10 @@ public static function getColumnSchema(
203208
case 'NOW':
204209
return new Column\DateTime();
205210

211+
case 'CURDATE':
212+
case 'CURRENT_DATE':
213+
return new Column\Date();
214+
206215
case 'DATE':
207216
case 'LAST_DAY':
208217
$arg = Evaluator::getColumnSchema($expr->args[0], $scope, $columns);
@@ -230,6 +239,7 @@ public static function getColumnSchema(
230239

231240
case 'DATEDIFF':
232241
case 'DAY':
242+
case 'WEEKDAY':
233243
return new Column\IntColumn(false, 10);
234244
}
235245

@@ -1007,6 +1017,49 @@ private static function sqlLastDay(
10071017
return (new \DateTimeImmutable($subject))->format('Y-m-t');
10081018
}
10091019

1020+
/**
1021+
* @param array<string, mixed> $row
1022+
*/
1023+
private static function sqlCurDate(FunctionExpression $expr): string
1024+
{
1025+
$args = $expr->args;
1026+
1027+
if (\count($args) !== 0) {
1028+
throw new ProcessorException("MySQL CURDATE() function takes no arguments.");
1029+
}
1030+
1031+
return (new \DateTimeImmutable())->format('Y-m-d');
1032+
}
1033+
1034+
/**
1035+
* @param array<string, mixed> $row
1036+
*/
1037+
private static function sqlWeekDay(
1038+
FakePdoInterface $conn,
1039+
Scope $scope,
1040+
FunctionExpression $expr,
1041+
array $row,
1042+
QueryResult $result
1043+
) : ?int {
1044+
$args = $expr->args;
1045+
1046+
if (\count($args) !== 1) {
1047+
throw new ProcessorException("MySQL WEEKDAY() function must be called with one argument");
1048+
}
1049+
1050+
$subject = Evaluator::evaluate($conn, $scope, $args[0], $row, $result);
1051+
1052+
if (!is_string($subject)) {
1053+
throw new \TypeError('Failed assertion');
1054+
}
1055+
1056+
if (!$subject || \strpos($subject, '0000-00-00') === 0) {
1057+
return null;
1058+
}
1059+
1060+
return (int)(new \DateTimeImmutable($subject))->format('N');
1061+
}
1062+
10101063
/**
10111064
* @param array<string, mixed> $row
10121065
*

tests/EndToEndTest.php

Lines changed: 21 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,25 @@ public function testDateArithhmetic()
458459
'h' => '2024-02-29 12:31:00',
459460
'i' => '2020-02-29',
460461
'j' => '2020-02-01',
462+
'k' => 4,
463+
]],
464+
$query->fetchAll(\PDO::FETCH_ASSOC)
465+
);
466+
}
467+
468+
public function testCurDateFunction()
469+
{
470+
$pdo = self::getPdo('mysql:foo');
471+
472+
$query = $pdo->prepare('SELECT CURDATE() AS date, CURRENT_DATE() AS date1');
473+
474+
$query->execute();
475+
$current_date = date('Y-m-d');
476+
477+
$this->assertSame(
478+
[[
479+
'date' => $current_date,
480+
'date1' => $current_date,
461481
]],
462482
$query->fetchAll(\PDO::FETCH_ASSOC)
463483
);

0 commit comments

Comments
 (0)