UpdateBuilder

Type Alias UpdateBuilder 

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

Builds an UPDATE query specifically for SQLite.

UpdateBuilder provides a type-safe, fluent API for constructing UPDATE statements with support for conditional updates, returning clauses, and precise column targeting.

§Type Parameters

  • Schema: The database schema type, ensuring only valid tables can be referenced
  • State: The current builder state, enforcing proper query construction order
  • Table: The table being updated

§Query Building Flow

  1. Start with QueryBuilder::update(table) to specify the target table
  2. Add set() to specify which columns to update and their new values
  3. Optionally add where() to limit which rows are updated
  4. Optionally add returning() to get updated values back

§Basic Usage

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" = ?"#
);

§Advanced Updates

§Multiple Column Updates

let query = builder
    .update(user)
    .set(UpdateUser::default()
        .with_name("Alice Updated")
        .with_email("alice.new@example.com"))
    .r#where(eq(user.id, 1));

§UPDATE 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));

Aliased Type§

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

Fields§

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

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>

Specifies which columns to update and their new values.

This method accepts update expressions that specify which columns should be modified. You can update single or multiple columns using condition functions from drizzle_core::expressions::conditions.

§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"));
Source§

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

Source

pub fn where( self, condition: impl ToSQL<'a, SQLiteValue<'a>>, ) -> UpdateBuilder<'a, S, UpdateWhereSet, T>

Adds a WHERE clause to specify which rows to update.

Without a WHERE clause, all rows in the table would be updated. This method allows you to specify conditions to limit which rows are affected by the update.

§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

pub fn returning( self, columns: impl ToSQL<'a, SQLiteValue<'a>>, ) -> UpdateBuilder<'a, S, UpdateReturningSet, T>

Adds a RETURNING clause and transitions to the ReturningSet state

Source§

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

Source

pub fn returning( self, columns: impl ToSQL<'a, SQLiteValue<'a>>, ) -> UpdateBuilder<'a, S, UpdateReturningSet, T>

Adds a RETURNING clause after WHERE