packages/sql-semantics-postgres/src/Statement/Utility/Session/SetTimeZone.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Statement\Utility\Session;
6
7use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
8use SqlSemantics\Platform\PostgreSql\Model\Contract\NameUse;
9use SqlSemantics\Platform\PostgreSql\Model\Diagnostic\Check;
10use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
11use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
12use SqlSemantics\Platform\PostgreSql\Model\Statement\Snapshot;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Statement;
14use SqlSemantics\Platform\PostgreSql\Statement\Literal\SignedNumber;
15use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
16use SqlSemantics\Platform\PostgreSql\Statement\Literal\TypedLiteral;
17use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\IntervalDesignation;
18use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\IntervalFields;
19use SqlSemantics\Platform\PostgreSql\Statement\Utility\Problem\UtilityProblem;
20use SqlSemantics\Platform\PostgreSql\Statement\Utility\Problem\UtilityProblemKind;
21
22/**
23 * `SET [LOCAL] TIME ZONE value`: a request to change the time zone of the session.
24 *
25 * Rule: PG-SET-003. Mirrors PostgreSQL's `VariableSetStmt` on `timezone`
26 * written with the TIME ZONE keywords. The value is a zone name as a string
27 * or a plain identifier (a keyword must be quoted there), a number of hours east of UTC, an interval constant, or
28 * LOCAL or DEFAULT. Facts: the interval constant has the interval type.
29 * Diagnostic: an interval restricted to fields other than HOUR or HOUR TO
30 * MINUTE is rejected by the server while parsing.
31 * Source: https://www.postgresql.org/docs/17/sql-set.html. Status: Implemented.
32 *
33 * @visibility public
34 * @example Reading a time zone set by an interval
35 * $operation = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze("SET TIME ZONE INTERVAL '-08:00' HOUR TO MINUTE");
36 * [$operation->statement->zone->value->value, $operation->toString()] // => ['-08:00', "SET TIME ZONE INTERVAL '-08:00' HOUR TO MINUTE"]
37 * @example Refusing a constant of another type
38 * new \SqlSemantics\Platform\PostgreSql\Statement\Utility\Session\SetTimeZone(new \SqlSemantics\Platform\PostgreSql\Statement\Literal\TypedLiteral(
39 * new \SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName(new \SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation(new \SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName([new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('date')]))),
40 * new \SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant('1'),
41 * )) // throws \SqlSemantics\Platform\PostgreSql\Model\Diagnostic\InvalidConstruction
42 */
43final class SetTimeZone implements Statement
44{
45 use Snapshot;
46
47 /**
48 * @param StringConstant|Name|SignedNumber|TypedLiteral|ZoneKeyword $zone The zone: a name, an offset in hours, an interval constant, or a keyword
49 * @param bool $local Whether the change lasts for the current transaction only
50 */
51 public function __construct(public readonly StringConstant|Name|SignedNumber|TypedLiteral|ZoneKeyword $zone, public readonly bool $local = false)
52 {
53 Check::input(!$zone instanceof TypedLiteral || $zone->type->designation instanceof IntervalDesignation, 'The constant of SET TIME ZONE is an interval.');
54 }
55
56 /**
57 * Derives the interval constant and reports an interval of other fields than hours and minutes.
58 */
59 public function deriveStatement(Derivation $derivation): void
60 {
61 if (!$this->zone instanceof TypedLiteral) {
62 return;
63 }
64 $derivation->scalar($this->zone, $derivation->environment());
65 $designation = $this->zone->type->designation;
66 if ($designation instanceof IntervalDesignation && !in_array($designation->fields, [null, IntervalFields::Hour, IntervalFields::HourToMinute], true)) {
67 $derivation->report(new UtilityProblem(UtilityProblemKind::ZoneIntervalFields));
68 }
69 }
70
71 /**
72 * Writes the request.
73 */
74 public function render(Output $out): void
75 {
76 $out->keyword('SET');
77 if ($this->local) {
78 $out->keyword('LOCAL');
79 }
80 $out->keyword('TIME', 'ZONE');
81 if ($this->zone instanceof ZoneKeyword) {
82 $out->keyword($this->zone->value);
83
84 return;
85 }
86 if ($this->zone instanceof Name) {
87 $out->name($this->zone, NameUse::Identifier);
88
89 return;
90 }
91 $out->node($this->zone);
92 }
93}
94