Skip to main content

SetClause

Struct SetClause 

Source
pub struct SetClause { /* private fields */ }
Expand description

Accumulates col = :col SET fragments and their bound values for INSERT INTO … SET … / UPDATE … SET … statements whose shape is only known at runtime (optional fields, conditional writes).

The placeholder name IS the column name, so a fragment can never reference a param that wasn’t bound — the silently-nulling placeholder/key mismatch failure mode is unrepresentable by construction.

ⓘ
let clause = SetClause::new()
    .set("completed_at", now)
    .set_opt("score", score)          // None → column omitted entirely
    .set_when(rewrite, "audio_model", model);
let sql = format!("UPDATE t SET {} WHERE id = :id", clause.sql());
client.execute_with_values(&sql, clause.set("id", id).into_params()).await?;

None handling is deliberately omit, don’t bind: on UPDATE paths the stored value is preserved, on INSERT paths the schema default/null applies.

List writes: append is the server-side append (col = col || :param). Positional element updates have no server-native form: SET list[i] = :v is accepted-but-silently-ignored and PUT list i :v is a syntax error — read-modify-write (select the list, patch in Rust, set the whole list) remains the shape for those.

Implementations§

Source§

impl SetClause

Source

pub fn new() -> Self

Source

pub fn is_empty(&self) -> bool

True when nothing was pushed (callers may want to skip the write).

Source

pub fn set(self, col: &str, value: impl IntoGrpcValue) -> Self

Always include col = :col with the bound value. Build each column once — re-setting emits the fragment twice (last value wins).

Source

pub fn set_opt<T: IntoGrpcValue>(self, col: &str, value: Option<T>) -> Self

set only when value is Some.

Source

pub fn set_when(self, cond: bool, col: &str, value: impl IntoGrpcValue) -> Self

set only when cond holds.

Source

pub fn set_raw( self, fragment: impl Into<String>, name: &str, value: impl IntoGrpcValue, ) -> Self

Escape hatch for fragments that are not a plain col = :col. The fragment must still reference :name; name is registered so it can never dangle.

Source

pub fn set_expr(self, col: &str, expr: &str) -> Self

Set a column to a SERVER-SIDE expression (count = count + 1, checked_at = sysdate()) — the increment/anchor forms that have no client-side value to bind. Both halves are caller-owned SQL: col must be a plain schema identifier (same contract as set, which likewise interpolates it), expr is inserted verbatim — never data.

Source

pub fn bind(self, name: &str, value: impl IntoGrpcValue) -> Self

Register a param WITHOUT emitting a fragment — for values referenced from elsewhere in the statement. Composes with sql/into_params.

Source

pub fn apply_if<T>( self, option: Option<T>, f: impl FnOnce(Self, T) -> Self, ) -> Self

Apply a multi-call step only when option is Some — the conditional-composition form for write shapes that need more than one builder call (e.g. append + bind together). None passes the clause through untouched. Read-side twin: FilterClause::apply_if.

let clause = SetClause::new()
    .apply_if(Some("red"), |c, v| c.append("labels", v))
    .apply_if(None::<String>, |c, v| c.set("note", v));
assert_eq!(clause.sql(), "labels = labels || :labels__append");
Source

pub fn append(self, col: &str, value: impl IntoGrpcValue) -> Self

Server-side list append: emits col = col || :{col}__append with the element bound as a one-element list. One roundtrip, no read-merge-write window, works on a NULL column (null || [x] = [x]) and preserves element kinds.

Chained appends on the same column coalesce into ONE fragment + one merged list param. Appending to a column also plain-set in the same clause is unsupported (build one or the other).

The idempotent append is one statement — CONTAINS accepts the same single-element list param as a membership test:

let clause = SetClause::new().append("labels", "red");
assert_eq!(clause.sql(), "labels = labels || :labels__append");

The update’s affected_records is the “did it change” bool.

Source

pub fn set_or_null<T: IntoGrpcValue>(self, col: &str, value: Option<T>) -> Self

set when value is Some; explicitly null the column when None.

Unlike set_opt (which omits None columns to preserve stored values), this is the write-what-you-mean form for refresh paths where an absent source field must CLEAR stale data.

The None arm cannot bind: the wire oneof has no null kind, so an unset value is simply skipped server-side (no property can be nulled through a bound parameter) — it falls back to a col = null SQL literal, the only spelling the engine actually honors. The only interpolated text is the column name (caller-owned schema identifier, never data).

Source

pub fn sql(&self) -> String

The SET-clause body: a = :a, b = :b. Empty string when nothing was pushed (callers decide whether an empty SET is valid for their shape).

Source

pub fn into_params(self) -> Params

Consume into Params for query_with_values / execute_with_values.

Trait Implementations§

Source§

impl Clone for SetClause

Source§

fn clone(&self) -> Self

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 SetClause

Source§

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

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

impl Default for SetClause

Source§

fn default() -> Self

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

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> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more