Skip to content

Commit

Permalink
Merge pull request #32 from rtm-ctrlz/feat-func-unix_timestamp
Browse files Browse the repository at this point in the history
feat(UNIX_TIMESTAMP): add basic support for UNIX_TIMESTAMP function
  • Loading branch information
muglug authored Apr 12, 2022
2 parents 4dc64f5 + 6998896 commit 3be4c68
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Processor/Expression/FunctionEvaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public static function evaluate(
return self::sqlBinary($conn, $scope, $expr, $row, $result);
case 'FROM_UNIXTIME':
return self::sqlFromUnixtime($conn, $scope, $expr, $row, $result);
case 'UNIX_TIMESTAMP':
return self::sqlUnixTimestamp($conn, $scope, $expr, $row, $result);
case 'GREATEST':
return self::sqlGreatest($conn, $scope, $expr, $row, $result);
case 'VALUES':
Expand Down Expand Up @@ -845,6 +847,32 @@ private static function sqlFromUnixtime(
return \date('Y-m-d H:i:s', (int) $column);
}

/**
* @param array<string, mixed> $row
*/
private static function sqlUnixTimestamp(
FakePdoInterface $conn,
Scope $scope,
FunctionExpression $expr,
array $row,
QueryResult $result
) : ?int {
$args = $expr->args;

switch (\count($args)) {
case 0:
return time();
case 1:
$column = Evaluator::evaluate($conn, $scope, $args[0], $row, $result);
if (!\is_string($column)) {
return null;
}
return \strtotime($column) ?: null;
default:
throw new ProcessorException("MySQL UNIX_TIMESTAPM() SQLFake only implemented for 0 or 1 argument");
}
}

/**
* @param array<string, mixed> $row
*
Expand Down

0 comments on commit 3be4c68

Please sign in to comment.