classMerge
final class Merge
implements Modification
uses Snapshot
@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.
$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']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\InvalidConstructionProperties§
public non-empty-list<MergeWhen> $clausespublic list<Target> $returningpublic CommonTables|null $withpublic TargetTable $targetpublic Relation $sourcepublic Scalar $conditionMethods§
public function __construct(
public CommonTables|null $with,
public TargetTable $target,
public Relation $source,
public Scalar $condition,
list<MergeWhen> $clauses,
list<Target> $returning = [],
)Parameters
$with | CommonTables|null | The WITH clause |
$target | TargetTable | The table merged into |
$source | Relation | The source of the rows: a table, a subquery or any other FROM item |
$condition | Scalar | The join condition written after ON |
$clauses | list<MergeWhen> | The WHEN clauses in written order; at least one |
$returning | list<Target> | The RETURNING list in written order (PostgreSQL 17) |
Throws
InvalidConstruction When there is no WHEN clause, or a list holds a foreign itemCalls 3
- static-call Check::listOf() line 69
- class-const MergeWhen::class() line 69
- class-const Target::class() line 70
public function returnsRows(): boolTells whether the statement has a RETURNING list.
Returns
boolpublic function commonTables(): ?CommonTablesAnswers the WITH clause written before the statement.
Returns
public function deriveStatement(Derivation $derivation): voidDerives the statement as a root and records the rows of RETURNING as the output.
Parameters
$derivation | Derivation |
Calls 2
- method-call Roots::derive() line 94
- new Roots line 94
public function deriveQuery(Derivation $derivation, Environment $outer): QueryFactDerives every part against an outer environment and answers the rows of RETURNING.
Parameters
$derivation | Derivation | |
$outer | Environment |
Returns
Calls 2
- method-call MergeFacts::derive() line 102
- new MergeFacts line 102
public function render(Output $out): voidWrites the statement.
Parameters
$out | Output |
Calls 2
- method-call Output::node() line 110
- method-call Output::keyword() line 116
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
ManipulationsTest::testPreparableLowersADataModifyingStatementcallsManipulationsTest::testStatementRoutesEveryStatementOfTheFamilycallsMergeRuleTest::testActionLowersEveryInsertFormcallsMergeRuleTest::testClauseLowersTheKindsOfPostgreSql16callsMergeRuleTest::testConditionLowersTheExpressioncallsMergeRuleTest::testMergeLowersReturningOfPostgreSql17callsMergeRuleTest::testValuesLowersTheExpressionscalls
Relations§
Instantiated in 1
Type checks 1
Type declarations 3
- type SqlSemantics\Platform\PostgreSql\Lowering\Manipulation\MergeRule packages/sql-semantics-postgres/src/Lowering/Manipulation/MergeRule.php:76
- type SqlSemantics\Platform\PostgreSql\Rules\Manipulation\MergeFacts packages/sql-semantics-postgres/src/Rules/Manipulation/MergeFacts.php:47
- type SqlSemantics\Platform\PostgreSql\Rules\Manipulation\MergeFacts packages/sql-semantics-postgres/src/Rules/Manipulation/MergeFacts.php:116