Skip to content

Commit 3be4c68

Browse files
authored
Merge pull request #32 from rtm-ctrlz/feat-func-unix_timestamp
feat(UNIX_TIMESTAMP): add basic support for UNIX_TIMESTAMP function
2 parents 4dc64f5 + 6998896 commit 3be4c68

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/Processor/Expression/FunctionEvaluator.php

+28
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public static function evaluate(
7171
return self::sqlBinary($conn, $scope, $expr, $row, $result);
7272
case 'FROM_UNIXTIME':
7373
return self::sqlFromUnixtime($conn, $scope, $expr, $row, $result);
74+
case 'UNIX_TIMESTAMP':
75+
return self::sqlUnixTimestamp($conn, $scope, $expr, $row, $result);
7476
case 'GREATEST':
7577
return self::sqlGreatest($conn, $scope, $expr, $row, $result);
7678
case 'VALUES':
@@ -845,6 +847,32 @@ private static function sqlFromUnixtime(
845847
return \date('Y-m-d H:i:s', (int) $column);
846848
}
847849

850+
/**
851+
* @param array<string, mixed> $row
852+
*/
853+
private static function sqlUnixTimestamp(
854+
FakePdoInterface $conn,
855+
Scope $scope,
856+
FunctionExpression $expr,
857+
array $row,
858+
QueryResult $result
859+
) : ?int {
860+
$args = $expr->args;
861+
862+
switch (\count($args)) {
863+
case 0:
864+
return time();
865+
case 1:
866+
$column = Evaluator::evaluate($conn, $scope, $args[0], $row, $result);
867+
if (!\is_string($column)) {
868+
return null;
869+
}
870+
return \strtotime($column) ?: null;
871+
default:
872+
throw new ProcessorException("MySQL UNIX_TIMESTAPM() SQLFake only implemented for 0 or 1 argument");
873+
}
874+
}
875+
848876
/**
849877
* @param array<string, mixed> $row
850878
*

0 commit comments

Comments
 (0)