packages/sql-semantics-postgres/src/Statement/Invocation/Json/Constructor/JsonObjectConstructor.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Constructor;
6
7use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
8use SqlSemantics\Platform\PostgreSql\Model\Diagnostic\Check;
9use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
10use SqlSemantics\Platform\PostgreSql\Model\Resolution\Environment;
11use SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\ScalarFact;
12use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Scalar;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Snapshot;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
17use SqlSemantics\Platform\PostgreSql\Rules\Expression\Precedence;
18use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonNullHandling;
19use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonPair;
20use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonReturning;
21use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonUniqueKeys;
22use SqlSemantics\Platform\PostgreSql\Statement\OutputNaming;
23use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
24
25/**
26 * `JSON_OBJECT (key : value, ... [NULL | ABSENT ON NULL] [WITH | WITHOUT UNIQUE] [RETURNING type])`.
27 *
28 * Mirrors PostgreSQL's `JsonObjectConstructor`. Without pairs, no NULL or
29 * uniqueness clause can be written. Rule: PG-JSON-OBJECT-001. Facts: the
30 * RETURNING type, otherwise `json`; never NULL. The result column is named
31 * `json_object`.
32 * Source: https://www.postgresql.org/docs/17/functions-json.html#FUNCTIONS-JSON-CREATION-TABLE. Status: Implemented.
33 *
34 * @visibility public
35 * @example Reading an empty object constructor
36 * $object = new \SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Constructor\JsonObjectConstructor([]);
37 * [$object->pairs, $object->outputName()->value] // => [[], 'json_object']
38 * @example Rejecting a NULL clause without pairs
39 * new \SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Constructor\JsonObjectConstructor([], \SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonNullHandling::Absent) // throws \SqlSemantics\Platform\PostgreSql\Model\Diagnostic\InvalidConstruction
40 */
41final class JsonObjectConstructor implements Scalar, OutputNaming
42{
43 use Snapshot;
44
45 /**
46 * @var list<JsonPair> The key and value pairs
47 */
48 public readonly array $pairs;
49
50 /**
51 * @param list<JsonPair> $pairs The key and value pairs
52 * @param JsonNullHandling|null $nulls The NULL clause written
53 * @param JsonUniqueKeys|null $unique The uniqueness clause written
54 * @param JsonReturning|null $returning The RETURNING clause
55 */
56 public function __construct(array $pairs, public readonly ?JsonNullHandling $nulls = null, public readonly ?JsonUniqueKeys $unique = null, public readonly ?JsonReturning $returning = null)
57 {
58 $this->pairs = Check::listOf($pairs, JsonPair::class, 'JSON_OBJECT takes key and value pairs.');
59 Check::input($this->pairs !== [] || ($nulls === null && $unique === null), 'JSON_OBJECT without pairs takes no NULL or uniqueness clause.');
60 $last = $this->pairs === [] ? null : $this->pairs[count($this->pairs) - 1]->value;
61 Check::input($unique === null || $nulls !== null || $last === null || $last->format !== null || !(new Precedence())->takesUniqueness($last->value), 'A last value ending in IS JSON without a uniqueness clause would take the uniqueness clause; group it.');
62 }
63
64 /**
65 * Names an unaliased result column.
66 */
67 public function outputName(): Name
68 {
69 return new Name('json_object');
70 }
71
72 /**
73 * Derives the pairs and the returning type.
74 */
75 public function deriveScalar(Derivation $derivation, Environment $environment): ScalarFact
76 {
77 foreach ($this->pairs as $pair) {
78 $pair->deriveClause($derivation, $environment);
79 }
80 $this->returning?->deriveClause($derivation, $environment);
81
82 return new ScalarFact($this->returning?->type->typeFact($derivation->context) ?? new Known(Builtin::Json), Nullability::NotNull);
83 }
84
85 /**
86 * Writes JSON_OBJECT with its pairs and clauses.
87 */
88 public function render(Output $out): void
89 {
90 $out->keyword('JSON_OBJECT')->glue()->symbol('(')->list($this->pairs);
91 if ($this->nulls !== null) {
92 $out->keyword($this->nulls->value, 'ON', 'NULL');
93 }
94 $out->node($this->unique)->node($this->returning)->symbol(')');
95 }
96}
97