packages/sql-semantics-mysql/src/Statement/Call/Temporal/TimestampCall.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Call\Temporal;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Rendering\Output;
9use SqlSemantics\Platform\MySql\Model\Resolution\Environment;
10use SqlSemantics\Platform\MySql\Model\Statement\Fact\ScalarFact;
11use SqlSemantics\Platform\MySql\Model\Statement\Scalar;
12use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
13use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
14use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
15use SqlSemantics\Platform\MySql\Rules\Call\Arguments;
16use SqlSemantics\Platform\MySql\Rules\Call\ResultTyping;
17use SqlSemantics\Platform\MySql\Rules\Call\TypeClass;
18use SqlSemantics\Platform\MySql\Statement\Expression\IntervalUnit;
19
20/**
21 * A call of TIMESTAMPADD(unit, interval, datetime) or TIMESTAMPDIFF(unit, datetime1, datetime2).
22 *
23 * Rule: MYSQL-TIMESTAMP-CALL-001. TIMESTAMPADD adds a number of units to
24 * its second operand and is typed as a date plus an interval; TIMESTAMPDIFF
25 * is the integer number of units from the first to the second operand. The
26 * units are the simple units only (interval_time_stamp). The result is NULL
27 * when an operand is NULL or invalid. Terminates: the operands are strict
28 * parts.
29 * Source: https://dev.mysql.com/doc/refman/8.4/en/date-and-time-functions.html#function_timestampadd,
30 * https://dev.mysql.com/doc/refman/8.4/en/date-and-time-functions.html#function_timestampdiff.
31 * Status: Implemented.
32 *
33 * @visibility public
34 * @example Typing TIMESTAMPDIFF()
35 *     $query = (new \SqlSemantics\Platform\MySql\Semantics())->analyze("SELECT TIMESTAMPDIFF(MONTH, '2024-01-01', '2024-03-01')");
36 *     $query->field(0)->type->descriptor->name() // => 'BIGINT'
37 */
38final class TimestampCall implements Scalar
39{
40    use Snapshot;
41
42    /**
43     * @param TimestampOperation $operation The function
44     * @param IntervalUnit $unit The unit
45     * @param Scalar $first The number of units to add, or the start
46     * @param Scalar $second The date or time to add to, or the end
47     */
48    public function __construct(public readonly TimestampOperation $operation, public readonly IntervalUnit $unit, public readonly Scalar $first, public readonly Scalar $second)
49    {
50    }
51
52    /**
53     * Derives the operands and the result.
54     */
55    public function deriveScalar(Derivation $derivation, Environment $environment): ScalarFact
56    {
57        (new Arguments())->one($this->first, $derivation, $environment);
58        $second = (new Arguments())->one($this->second, $derivation, $environment);
59        $type = $this->operation === TimestampOperation::Add ? (new ResultTyping())->dateArithmetic($second->type, $this->unit) : new Known(TypeClass::Integer->descriptor());
60
61        return new ScalarFact($type, Nullability::Nullable);
62    }
63
64    /**
65     * Writes the call.
66     */
67    public function render(Output $out): void
68    {
69        $out->keyword($this->operation->value)->glue()->symbol('(')->keyword($this->unit->value)->symbol(',')->node($this->first)->symbol(',')->node($this->second)->symbol(')');
70    }
71}
72