packages/sql-semantics-postgres/src/Rules/Invocation/XmlChecks.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Rules\Invocation;
6
7use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
8use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Invalid;
9use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
10use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\TypeFact;
11use SqlSemantics\Platform\PostgreSql\Statement\Expression\ColumnReference;
12use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Problem\XmlProblem;
13use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Problem\XmlProblemKind;
14use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Xml\XmlAttribute;
15
16/**
17 * Reports the SQL/XML constructors the server rejects while analyzing them.
18 *
19 * Rule: PG-XML-CHECKS-001. An attribute of XMLELEMENT, or a value of
20 * XMLFOREST, without AS takes the name of the column it references, so it
21 * must be a column reference; the attribute names of one element, written or
22 * taken, are distinct (compared exactly); XMLSERIALIZE produces a character
23 * string type: `text`, `character varying` or `character`.
24 * Source: https://www.postgresql.org/docs/17/functions-xml.html#FUNCTIONS-PRODUCING-XML,
25 * `transformXmlExpr` and `transformXmlSerialize` in `src/backend/parser/parse_expr.c` of PostgreSQL 17.
26 * Termination: one pass over the values. Status: Implemented.
27 *
28 * @visibility SqlSemantics\Platform\PostgreSql
29 */
30final class XmlChecks
31{
32    /**
33     * The character string types XMLSERIALIZE accepts.
34     */
35    private const SERIALIZED = ['text', 'varchar', 'bpchar'];
36
37    /**
38     * Reports unnamed attributes that are not column references and attribute names used twice.
39     *
40     * @param list<XmlAttribute> $attributes
41     */
42    public function attributes(Derivation $derivation, array $attributes): void
43    {
44        $names = [];
45        foreach ($attributes as $attribute) {
46            $name = $attribute->name ?? ($attribute->value instanceof ColumnReference ? $attribute->value->outputName() : null);
47            if ($name === null) {
48                $derivation->report(new XmlProblem(XmlProblemKind::UnnamedAttribute));
49                continue;
50            }
51            if (in_array($name->value, $names, true)) {
52                $derivation->report(new XmlProblem(XmlProblemKind::RepeatedAttribute, $name->value));
53            }
54            $names[] = $name->value;
55        }
56    }
57
58    /**
59     * Reports unnamed XMLFOREST values that are not column references.
60     *
61     * @param list<XmlAttribute> $elements
62     */
63    public function elements(Derivation $derivation, array $elements): void
64    {
65        foreach ($elements as $element) {
66            if ($element->name === null && !$element->value instanceof ColumnReference) {
67                $derivation->report(new XmlProblem(XmlProblemKind::UnnamedElement));
68            }
69        }
70    }
71
72    /**
73     * Answers the type of an XMLSERIALIZE result, reporting a type that is not a character string type.
74     */
75    public function serialized(Derivation $derivation, TypeFact $type): TypeFact
76    {
77        if (!$type instanceof Known) {
78            return $type;
79        }
80        if (in_array((new Coercions())->descriptor($type->descriptor), self::SERIALIZED, true)) {
81            return $type;
82        }
83        $problem = new XmlProblem(XmlProblemKind::SerializeTarget, $type->descriptor->name());
84        $derivation->report($problem);
85
86        return new Invalid($problem);
87    }
88}
89