Skip to content

Commit 210804f

Browse files
committed
Added CURDATE function
1 parent 2e8b092 commit 210804f

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/Processor/Expression/FunctionEvaluator.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ 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+
return self::sqlCurDate($expr);
9799
}
98100

99101
throw new ProcessorException("Function " . $expr->functionName . " not implemented yet");
@@ -203,6 +205,9 @@ public static function getColumnSchema(
203205
case 'NOW':
204206
return new Column\DateTime();
205207

208+
case 'CURDATE':
209+
return new Column\Date();
210+
206211
case 'DATE':
207212
case 'LAST_DAY':
208213
$arg = Evaluator::getColumnSchema($expr->args[0], $scope, $columns);
@@ -1007,6 +1012,20 @@ private static function sqlLastDay(
10071012
return (new \DateTimeImmutable($subject))->format('Y-m-t');
10081013
}
10091014

1015+
/**
1016+
* @param array<string, mixed> $row
1017+
*/
1018+
private static function sqlCurDate(FunctionExpression $expr): string
1019+
{
1020+
$args = $expr->args;
1021+
1022+
if (\count($args) !== 0) {
1023+
throw new ProcessorException("MySQL CURDATE() function takes no arguments.");
1024+
}
1025+
1026+
return (new \DateTimeImmutable())->format('Y-m-d');
1027+
}
1028+
10101029
/**
10111030
* @param array<string, mixed> $row
10121031
*

tests/EndToEndTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,23 @@ public function testDateArithhmetic()
463463
);
464464
}
465465

466+
public function testCurDateFunction()
467+
{
468+
$pdo = self::getPdo('mysql:foo');
469+
470+
$query = $pdo->prepare('SELECT CURDATE() AS date');
471+
472+
$query->execute();
473+
$current_date = date('Y-m-d');
474+
475+
$this->assertSame(
476+
[[
477+
'date' => $current_date,
478+
]],
479+
$query->fetchAll(\PDO::FETCH_ASSOC)
480+
);
481+
}
482+
466483
public function testInOperator()
467484
{
468485
$pdo = self::getPdo('mysql:foo');

0 commit comments

Comments
 (0)