pub struct MergeQuery {
pub with: With,
pub target: TableRef,
pub source: TableRef,
pub on: Vec<Expr>,
pub whens: Vec<MergeWhen>,
pub returning: Returning,
}Expand description
A PostgreSQL MERGE (PostgreSQL 15+).
From https://www.postgresql.org/docs/17/sql-merge.html:
[ WITH with_query [, ...] ]
MERGE INTO [ ONLY ] target_table_name [ * ] [ [ AS ] target_alias ]
USING data_source ON join_condition
when_clause [...]
[ RETURNING … ]
when_clause:
WHEN MATCHED [ AND condition ] THEN { merge_update | merge_delete | DO NOTHING }
| WHEN NOT MATCHED BY SOURCE [ AND condition ] THEN
{ merge_update | merge_delete | DO NOTHING }
| WHEN NOT MATCHED [ BY TARGET ] [ AND condition ] THEN
{ merge_insert | DO NOTHING }Three parts of the production are newer than 15 and are marked where the API
produces them: WHEN NOT MATCHED BY SOURCE, the explicit BY TARGET
spelling, and RETURNING are all PostgreSQL 17+.
The target lives in HasTargetTable — like an UPDATE’s table — and the
USING source in HasTableRef, like a DELETE’s USING item, which is
what lets one TableChain serve both slots.
The source also carries HasJoins: gram.y’s MergeStmt reads
USING table_ref ON a_expr, and a table_ref may be a joined_table, so a
joined source is grammatical. The target is a relation_expr_opt_alias,
which admits no joins — the same split an UPDATE has.
Which actions a WHEN clause may take depends on which WHEN it is, and that
is enforced by the chain types in crate::merge rather than re-checked
here: a MergeWhen holds whatever it was built with.
Fields§
§with: WithWITH …. MERGE takes a plain WITH; PostgreSQL rejects
WITH RECURSIVE on it at analysis time, which is why
crate::merge does not re-export recursive.
target: TableRefThe target: MERGE INTO [ ONLY ] table [ * ] [ AS alias ].
source: TableRefThe data source: a table or a parenthesised query, with an alias.
on: Vec<Expr>The ON join condition. Several entries are AND-joined, as in a join’s
ON.
whens: Vec<MergeWhen>The WHEN clauses, applied in order — the grammar requires at least one.
returning: ReturningRETURNING … (PostgreSQL 17+).
Implementations§
Source§impl MergeQuery
impl MergeQuery
Sourcepub fn new() -> MergeQuery
pub fn new() -> MergeQuery
A MERGE with nothing set yet.
Sourcepub fn apply(&mut self, mods: impl Mod<MergeQuery>)
pub fn apply(&mut self, mods: impl Mod<MergeQuery>)
Apply more mods to an existing query.
Trait Implementations§
Source§impl Clone for MergeQuery
impl Clone for MergeQuery
Source§fn clone(&self) -> MergeQuery
fn clone(&self) -> MergeQuery
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for MergeQuery
impl Debug for MergeQuery
Source§impl Default for MergeQuery
impl Default for MergeQuery
Source§fn default() -> MergeQuery
fn default() -> MergeQuery
Source§impl Expression for MergeQuery
impl Expression for MergeQuery
Source§impl HasJoins for MergeQuery
impl HasJoins for MergeQuery
Source§impl HasReturning for MergeQuery
impl HasReturning for MergeQuery
Source§fn returning_mut(&mut self) -> &mut Returning
fn returning_mut(&mut self) -> &mut Returning
RETURNING clause to modify.Source§impl HasTableRef for MergeQuery
impl HasTableRef for MergeQuery
Source§fn table_ref_mut(&mut self) -> &mut TableRef
fn table_ref_mut(&mut self) -> &mut TableRef
Source§impl HasTargetTable for MergeQuery
impl HasTargetTable for MergeQuery
Source§fn target_table_mut(&mut self) -> &mut TableRef
fn target_table_mut(&mut self) -> &mut TableRef
Source§impl HasWith for MergeQuery
impl HasWith for MergeQuery
Source§impl IntoExprList for MergeQuery
impl IntoExprList for MergeQuery
Source§fn into_expr_list(self) -> Vec<Expr>
fn into_expr_list(self) -> Vec<Expr>
Source§impl Mod<MergeQuery> for MergeInsertChain
impl Mod<MergeQuery> for MergeInsertChain
Source§fn apply(self, q: &mut MergeQuery)
fn apply(self, q: &mut MergeQuery)
q.Source§impl Mod<MergeQuery> for MergeWhenMod
impl Mod<MergeQuery> for MergeWhenMod
Source§fn apply(self, q: &mut MergeQuery)
fn apply(self, q: &mut MergeQuery)
q.