Skip to main content

UpdateBuilder

Type Alias UpdateBuilder 

Source
pub type UpdateBuilder<'a, Schema, State, Table, Marker = (), Row = ()> = QueryBuilder<'a, Schema, State, Table, Marker, Row>;
Expand description

A PostgreSQL UPDATE being built: a QueryBuilder in one of the Update* states.

Start with .set(update_model), then optionally add FROM, WHERE and RETURNING, in that order. Without WHERE, every row is updated.

§Examples

use drizzle::core::expr::eq;

let query = db
    .update(user)
    .set(UpdateUser::default().with_name("Bob"))
    .r#where(eq(user.id, 1))
    .returning(user.id);
assert_eq!(
    query.to_sql().sql(),
    r#"UPDATE "users" SET "name" = $1 WHERE "users"."id" = $2 RETURNING "users"."id""#
);

Aliased Type§

pub struct UpdateBuilder<'a, Schema, State, Table, Marker = (), Row = ()> {
    pub sql: SQL<'a, PostgresValue<'a>>,
    /* private fields */
}

Fields§

§sql: SQL<'a, PostgresValue<'a>>

The SQL built so far.

Implementations§

Source§

impl<'a, Schema, Table> UpdateBuilder<'a, Schema, UpdateInitial, Table>
where Table: SQLTable<'a, PostgresSchemaType, PostgresValue<'a>>,

Source

pub fn set( self, values: Table::Update, ) -> UpdateBuilder<'a, Schema, UpdateSetClauseSet, Table>

Sets the new column values from the table’s Update* model.

Only the fields set on the model (with .with_*) are written.

Source§

impl<'a, S, T> UpdateBuilder<'a, S, UpdateSetClauseSet, T>

Source

pub fn from<F>( self, source: F, ) -> UpdateBuilder<'a, S, UpdateFromSet, T, Cons<F, Nil>>
where F: ToSQL<'a, PostgresValue<'a>> + ScopeEntry,

Adds FROM source, making another table’s columns available to WHERE and RETURNING.

§Examples
use drizzle::core::expr::eq;

let query = db
    .update(user)
    .set(UpdateUser::default().with_name("Author"))
    .from(post)
    .r#where(eq(post.author_id, user.id));
assert_eq!(
    query.to_sql().sql(),
    r#"UPDATE "users" SET "name" = $1 FROM "posts" WHERE "posts"."author_id" = "users"."id""#
);
Source

pub fn where<E, ScopeProof>( self, condition: E, ) -> UpdateBuilder<'a, S, UpdateWhereSet, T>
where E: ExprSources + Expr<'a, PostgresValue<'a>>, E::Sources: SourcesIn<Cons<T, Nil>, ScopeProof>, E::SQLType: BooleanLike,

Adds a WHERE condition; only matching rows are updated.

The condition may only use columns of the updated table; to use other tables, add from first.

Source

pub fn returning<Columns, ScopeProof>( self, columns: Columns, ) -> UpdateBuilder<'a, S, UpdateReturningSet, T, Scoped<<Columns as IntoSelectTarget>::Marker, Cons<T, Nil>>, <<Columns as IntoSelectTarget>::Marker as ResolveRow<T>>::Row>
where Columns: ExprSources + ToSQL<'a, PostgresValue<'a>> + IntoSelectTarget, Columns::Sources: SourcesIn<Cons<T, Nil>, ScopeProof>, Columns::Marker: ResolveRow<T>,

Adds RETURNING columns, so the statement returns the updated rows.

Source§

impl<'a, S, T, M> UpdateBuilder<'a, S, UpdateFromSet, T, M>

Source

pub fn where<E, ScopeProof>( self, condition: E, ) -> UpdateBuilder<'a, S, UpdateWhereSet, T, M>
where E: ExprSources + Expr<'a, PostgresValue<'a>>, E::Sources: SourcesIn<Cons<T, M>, ScopeProof>, E::SQLType: BooleanLike,

Adds a WHERE condition, which may use the updated table and the FROM source.

Source

pub fn returning<Columns, ScopeProof>( self, columns: Columns, ) -> UpdateBuilder<'a, S, UpdateReturningSet, T, Scoped<<Columns as IntoSelectTarget>::Marker, Cons<T, Nil>>, <<Columns as IntoSelectTarget>::Marker as ResolveRow<T>>::Row>
where Columns: ExprSources + ToSQL<'a, PostgresValue<'a>> + IntoSelectTarget, Columns::Sources: SourcesIn<Cons<T, M>, ScopeProof>, Columns::Marker: ResolveRow<T>,

Adds RETURNING columns after FROM.

Source§

impl<'a, S, T, M> UpdateBuilder<'a, S, UpdateWhereSet, T, M>

Source

pub fn returning<Columns, ScopeProof>( self, columns: Columns, ) -> UpdateBuilder<'a, S, UpdateReturningSet, T, Scoped<<Columns as IntoSelectTarget>::Marker, Cons<T, Nil>>, <<Columns as IntoSelectTarget>::Marker as ResolveRow<T>>::Row>
where Columns: ExprSources + ToSQL<'a, PostgresValue<'a>> + IntoSelectTarget, Columns::Sources: SourcesIn<Cons<T, M>, ScopeProof>, Columns::Marker: ResolveRow<T>,

Adds RETURNING columns after WHERE.