packages/sql-semantics-postgres/src/Statement/Manipulation/Merge/MergeMatch.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Merge;
6
7/**
8 * Which rows a WHEN clause of MERGE applies to.
9 *
10 * Mirrors PostgreSQL's `MergeMatchKind`: `MERGE_WHEN_MATCHED` (a target row
11 * joined with a source row), `MERGE_WHEN_NOT_MATCHED_BY_SOURCE` (a target
12 * row without source row, PostgreSQL 17) and `MERGE_WHEN_NOT_MATCHED_BY_TARGET`
13 * (a source row without target row, written `NOT MATCHED` or, in
14 * PostgreSQL 17, `NOT MATCHED BY TARGET`).
15 * Source: https://www.postgresql.org/docs/17/sql-merge.html.
16 *
17 * @visibility public
18 * @example Reading the kind of a WHEN clause
19 * $merge = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze('MERGE INTO t USING u ON t.a = u.a WHEN NOT MATCHED BY TARGET THEN DO NOTHING');
20 * [$merge->statement->clauses[0]->match, $merge->toString()] // => [\SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Merge\MergeMatch::NotMatched, 'MERGE INTO t USING u ON t.a = u.a WHEN NOT MATCHED THEN DO NOTHING']
21 */
22enum MergeMatch
23{
24 case Matched;
25 case NotMatchedBySource;
26 case NotMatched;
27
28 /**
29 * Tells whether the clause applies to rows that have a target row: MATCHED and NOT MATCHED BY SOURCE.
30 */
31 public function targeted(): bool
32 {
33 return $this !== self::NotMatched;
34 }
35}
36