packages/sql-semantics-postgres/src/Rules/Routine/ObjectFacts.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Rules\Routine;
6
7use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
8use SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\RelationFact;
9use SqlSemantics\Platform\PostgreSql\Statement\Name\ObjectKind;
10use SqlSemantics\Platform\PostgreSql\Statement\Name\ObjectReference;
11use SqlSemantics\Platform\PostgreSql\Statement\Routine\Problem\RoutineProblem;
12use SqlSemantics\Platform\PostgreSql\Statement\Routine\Problem\RoutineProblemKind;
13use SqlSemantics\Platform\PostgreSql\Statement\Routine\Signature\OperatorArity;
14use SqlSemantics\Platform\PostgreSql\Statement\Routine\Signature\OperatorSignature;
15
16/**
17 * Derives the objects a generic object command names.
18 *
19 * Rule: PG-OBJECT-FACTS-001. Every object reference derives the type names
20 * and signatures it holds in the statement environment; a relation it names
21 * is resolved by PG-OBJECT-RELATION-001. An operator written `(type, NONE)`
22 * names a postfix operator, which no longer exists, so a command without IF
23 * EXISTS that names one is reported. Routines, operators, types and the other
24 * catalog objects cannot be declared in the context and are not resolved.
25 * Terminates: one pass over the references.
26 * Source: https://www.postgresql.org/docs/17/sql-dropoperator.html,
27 * https://www.postgresql.org/docs/17/release-14.html (postfix operators removed). Status: Implemented.
28 *
29 * @visibility SqlSemantics\Platform\PostgreSql
30 */
31final class ObjectFacts
32{
33 /**
34 * Derives every reference and answers the relation fact of each, null where the kind names no relation.
35 *
36 * @param list<ObjectReference> $objects
37 *
38 * @return list<RelationFact|null>
39 */
40 public function derive(ObjectKind $kind, array $objects, bool $ifExists, Derivation $derivation): array
41 {
42 $environment = $derivation->environment();
43 $facts = [];
44 foreach ($objects as $object) {
45 $object->deriveClause($derivation, $environment);
46 if (!$ifExists && $object instanceof OperatorSignature && $object->arity === OperatorArity::Postfix) {
47 $derivation->report(new RoutineProblem(RoutineProblemKind::PostfixOperator));
48 }
49 $facts[] = (new RelationTargets())->resolve($kind, $object, $ifExists, $derivation);
50 }
51
52 return $facts;
53 }
54}
55