Skip to main content

MergeQuery

Struct MergeQuery 

Source
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: With

WITH …. 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: TableRef

The target: MERGE INTO [ ONLY ] table [ * ] [ AS alias ].

§source: TableRef

The 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: Returning

RETURNING … (PostgreSQL 17+).

Implementations§

Source§

impl MergeQuery

Source

pub fn new() -> MergeQuery

A MERGE with nothing set yet.

Source

pub fn apply(&mut self, mods: impl Mod<MergeQuery>)

Apply more mods to an existing query.

Trait Implementations§

Source§

impl Clone for MergeQuery

Source§

fn clone(&self) -> MergeQuery

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MergeQuery

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for MergeQuery

Source§

fn default() -> MergeQuery

Returns the “default value” for a type. Read more
Source§

impl Expression for MergeQuery

Source§

fn write_sql(&self, w: &mut SqlWriter<'_>)

Append this fragment to w. Read more
Source§

impl HasJoins for MergeQuery

Source§

fn joins_mut(&mut self) -> &mut Vec<Join>

The join list to modify.
Source§

impl HasReturning for MergeQuery

Source§

fn returning_mut(&mut self) -> &mut Returning

The RETURNING clause to modify.
Source§

impl HasTableRef for MergeQuery

Source§

fn table_ref_mut(&mut self) -> &mut TableRef

The table reference to modify.
Source§

impl HasTargetTable for MergeQuery

Source§

fn target_table_mut(&mut self) -> &mut TableRef

The target table to modify.
Source§

impl HasWith for MergeQuery

Source§

fn with_mut(&mut self) -> &mut With

The WITH clause to modify.
Source§

impl IntoExpr for MergeQuery

Source§

fn into_expr(self) -> Expr

Perform the conversion.
Source§

impl IntoExprList for MergeQuery

Source§

fn into_expr_list(self) -> Vec<Expr>

Perform the conversion.
Source§

impl Mod<MergeQuery> for MergeInsertChain

Source§

fn apply(self, q: &mut MergeQuery)

Apply this modification to q.
Source§

impl Mod<MergeQuery> for MergeWhenMod

Source§

fn apply(self, q: &mut MergeQuery)

Apply this modification to q.
Source§

impl Query for MergeQuery

Source§

fn query_type(&self) -> QueryType

Which statement this renders.
Source§

fn dialect(&self) -> &dyn Dialect

The dialect this query renders itself in. Read more
Source§

fn build(&self) -> Result<(String, Vec<Value>), Error>

Render to SQL and arguments, numbering placeholders from 1. Read more
Source§

fn build_from(&self, start: usize) -> Result<(String, Vec<Value>), Error>

build with a different first placeholder position, for splicing into a statement that already has arguments — bob’s BuildN.
Source§

impl<H, L, M> QueryExtensions<H, L, M> for MergeQuery

Source§

fn hooks(&self) -> &[Hook]

Hooks to run before this query executes.
Source§

fn loaders(&self) -> &[Loader]

Loaders to run after this query executes, for eagerly loaded relations.
Source§

fn mapper_mods(&self) -> &[MapperMod]

Adjustments to the row mapper, for relations loaded in the same query.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.