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

An UPDATE query being built for SQLite.

This is QueryBuilder in one of the Update* states. Start it with QueryBuilder::update.

§Clause order

  1. set (required before the query can run).
  2. where (required): the rows to update. r#where(true) updates every row.
  3. Optionally returning.

The WHERE condition and the RETURNING columns may only reference the table being updated; other tables do not compile.

§Examples

use drizzle::sqlite::prelude::*;
use drizzle::core::expr::eq;
use drizzle::sqlite::builder::QueryBuilder;

#[SQLiteTable(name = "users")]
struct User {
    #[column(primary)]
    id: i32,
    name: String,
    email: Option<String>,
}

#[derive(SQLiteSchema)]
struct Schema {
    user: User,
}

let builder = QueryBuilder::new::<Schema>();
let Schema { user } = Schema::new();

// Basic UPDATE
let query = builder
    .update(user)
    .set(UpdateUser::default().with_name("Alice Updated"))
    .r#where(eq(user.id, 1));
assert_eq!(
    query.to_sql().sql(),
    r#"UPDATE "users" SET "name" = ? WHERE "users"."id" = ?"#
);

Several columns at once:

let query = builder
    .update(user)
    .set(UpdateUser::default()
        .with_name("Alice Updated")
        .with_email("alice.new@example.com"))
    .r#where(eq(user.id, 1));
assert_eq!(
    query.to_sql().sql(),
    r#"UPDATE "users" SET "name" = ?, "email" = ? WHERE "users"."id" = ?"#
);

With RETURNING:

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

Aliased Type§

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

Fields§

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

The SQL built so far.

Implementations§

Source§

impl<'a, Schema, Table> UpdateBuilder<'a, Schema, UpdateInitial, Table>
where Table: SQLiteTable<'a>,

Source

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

Sets the columns to change, using the table’s generated update model.

Start from UpdateX::default() and call a with_* setter for each column to change. Columns you do not set are left as they are.

§Examples
// Update single column
let query = builder
    .update(user)
    .set(UpdateUser::default().with_name("New Name"));
assert_eq!(query.to_sql().sql(), r#"UPDATE "users" SET "name" = ?"#);

// Update multiple columns
let query = builder
    .update(user)
    .set(UpdateUser::default().with_name("New Name").with_email("new@example.com"));
assert_eq!(query.to_sql().sql(), r#"UPDATE "users" SET "name" = ?, "email" = ?"#);
Source§

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

Source

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

Adds a WHERE clause that picks the rows to update.

An UPDATE runs only with a WHERE clause, so a forgotten condition does not rewrite the whole table; r#where(true) updates every row. The condition must be a boolean expression over the updated table’s columns.

§Examples
// Update specific row by ID
let query = builder
    .update(user)
    .set(UpdateUser::default().with_name("Updated Name"))
    .r#where(eq(user.id, 1));
assert_eq!(
    query.to_sql().sql(),
    r#"UPDATE "users" SET "name" = ? WHERE "users"."id" = ?"#
);

// Update multiple rows with complex condition
let query = builder
    .update(user)
    .set(UpdateUser::default().with_name("Updated"))
    .r#where(and(gt(user.id, 10), eq(user.age, 25)));
Source§

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

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, SQLiteValue<'a>> + IntoSelectTarget, Columns::Sources: SourcesIn<Cons<T, Nil>, ScopeProof>, Columns::Marker: ResolveRow<T>,

Adds a RETURNING clause after WHERE. See returning.