final class Merge
    implements Modification
    uses Snapshot
Public API: explicitly declared with @visibility public.

A MERGE: the rows of a source joined to a target table, each inserted, updated, deleted or skipped by the first WHEN clause that applies.

Mirrors PostgreSQL's MergeStmt (relation, sourceRelation, joinCondition, mergeWhenClauses, returningList (PostgreSQL 17), withClause). The facts follow PG-MERGE-001. Source: https://www.postgresql.org/docs/17/sql-merge.html. Status: Implemented.

Reading the clauses of a MERGEdoctest
$merge = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze('MERGE INTO t USING u ON t.a = u.a WHEN MATCHED THEN UPDATE SET b = NULL WHEN NOT MATCHED THEN INSERT VALUES (u.a) RETURNING MERGE_ACTION(), t.a');
    [count($merge->statement->clauses), $merge->field(0)->name->value] // => [2, 'merge_action']
Refusing MERGE without a WHEN clausedoctest
new \SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Merge(null, new \SqlSemantics\Platform\PostgreSql\Statement\Manipulation\TargetTable(new \SqlSemantics\Platform\PostgreSql\Statement\Name\RelationReference(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')))), new \SqlSemantics\Platform\PostgreSql\Statement\Relation\TableInput(new \SqlSemantics\Platform\PostgreSql\Statement\Name\RelationReference(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('u')))), new \SqlSemantics\Platform\PostgreSql\Statement\Literal\BooleanLiteral(true), []) // throws \SqlSemantics\Platform\PostgreSql\Model\Diagnostic\InvalidConstruction

Properties§

public non-empty-list<MergeWhen> $clauses
public list<Target> $returning
public CommonTables|null $with
public TargetTable $target
public Relation $source
public Scalar $condition

Methods§

public function __construct(
    public CommonTables|null $with,
    public TargetTable $target,
    public Relation $source,
    public Scalar $condition,
    list<MergeWhen> $clauses,
    list<Target> $returning = [],
)

Parameters

$withCommonTables|nullThe WITH clause
$targetTargetTableThe table merged into
$sourceRelationThe source of the rows: a table, a subquery or any other FROM item
$conditionScalarThe join condition written after ON
$clauseslist<MergeWhen>The WHEN clauses in written order; at least one
$returninglist<Target>The RETURNING list in written order (PostgreSQL 17)

Throws

InvalidConstruction When there is no WHEN clause, or a list holds a foreign item
Calls 3
public function returnsRows(): bool

Tells whether the statement has a RETURNING list.

Returns

bool
public function commonTables(): ?CommonTables

Answers the WITH clause written before the statement.

Returns

public function deriveStatement(Derivation $derivation): void

Derives the statement as a root and records the rows of RETURNING as the output.

Parameters

$derivationDerivation
Calls 2
public function deriveQuery(Derivation $derivation, Environment $outer): QueryFact

Derives every part against an outer environment and answers the rows of RETURNING.

Parameters

$derivationDerivation
$outerEnvironment

Returns

Calls 2
public function render(Output $out): void

Writes the statement.

Parameters

$outOutput
Calls 2

Test cases 8§

Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.

Dedicated tests 1
Other tests reaching this symbol 7

Relations§

Instantiated in 1
Type checks 1
Type declarations 3