Skip to main content

InsertBuilder

Type Alias InsertBuilder 

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

Builds an INSERT query specifically for PostgreSQL.

Provides a type-safe, fluent API for constructing INSERT statements with support for typed conflict resolution, batch inserts, and returning clauses.

§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 inserted into

Aliased Type§

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

Fields§

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

Implementations§

Source§

impl<'a, Schema, Table> InsertBuilder<'a, Schema, InsertInitial, Table>
where Table: PostgresTable<'a>,

Source

pub fn value<T>( self, value: Table::Insert<T>, ) -> InsertBuilder<'a, Schema, InsertValuesSet, Table>

Specifies a single row to insert. Shorthand for .values([row]).

Source

pub fn values<I, T>( self, values: I, ) -> InsertBuilder<'a, Schema, InsertValuesSet, Table>
where I: IntoIterator<Item = Table::Insert<T>>,

Specifies multiple rows to insert.

Source

pub fn select<Q>( self, query: Q, ) -> InsertBuilder<'a, Schema, InsertValuesSet, Table>
where Q: ToSQL<'a, PostgresValue<'a>>,

Inserts rows produced by a SELECT query without an explicit column list.

The SELECT output must provide every table column in declaration order.

Source§

impl<'a, S, T> InsertBuilder<'a, S, InsertValuesSet, T>

Source

pub fn on_conflict<C: ConflictTarget<T>>( self, target: C, ) -> OnConflictBuilder<'a, S, T>

Begins a typed ON CONFLICT clause targeting specific columns.

The target must implement ConflictTarget<T>, which is auto-generated for primary key columns, unique columns, and unique indexes.

Returns an OnConflictBuilder to specify do_nothing() or do_update().

§Examples
fn main() {
use drizzle::postgres::prelude::*;
use drizzle::postgres::builder::QueryBuilder;

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

#[PostgresIndex(unique)]
struct UserEmailIdx(User::email);

#[derive(PostgresSchema)]
struct Schema {
    user: User,
    user_email_idx: UserEmailIdx,
}

let builder = QueryBuilder::new::<Schema>();
let schema = Schema::new();
let user = schema.user;

// Target a specific column
builder.insert(user).values([InsertUser::new("Alice")])
    .on_conflict(user.id).do_nothing();

// Target with DO UPDATE using EXCLUDED
builder.insert(user).values([InsertUser::new("Alice")])
    .on_conflict(user.email).do_update(UpdateUser::default().with_name("updated"));

// Target a unique index
builder.insert(user).values([InsertUser::new("Alice")])
    .on_conflict(schema.user_email_idx).do_nothing();
}
Source

pub fn on_conflict_on_constraint<C: NamedConstraint<T>>( self, target: C, ) -> OnConflictBuilder<'a, S, T>

Begins a typed ON CONFLICT ON CONSTRAINT clause (PostgreSQL-only).

The target must implement NamedConstraint<T>, which is auto-generated for unique indexes.

Returns an OnConflictBuilder to specify do_nothing() or do_update().

§Examples
fn main() {
use drizzle::postgres::prelude::*;
use drizzle::postgres::builder::QueryBuilder;

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

#[PostgresIndex(unique)]
struct UserEmailIdx(User::email);

#[derive(PostgresSchema)]
struct Schema {
    user: User,
    user_email_idx: UserEmailIdx,
}

let builder = QueryBuilder::new::<Schema>();
let schema = Schema::new();

builder.insert(schema.user).values([InsertUser::new("Alice")])
    .on_conflict_on_constraint(schema.user_email_idx).do_nothing();
}
Source

pub fn on_conflict_do_nothing( self, ) -> InsertBuilder<'a, S, InsertOnConflictSet, T>

Shorthand for ON CONFLICT DO NOTHING without specifying a target.

This matches any constraint violation.

Source

pub fn returning<Columns>( self, columns: Columns, ) -> InsertBuilder<'a, S, InsertReturningSet, T, Scoped<<Columns as IntoSelectTarget>::Marker, Cons<T, Nil>>, <<Columns as IntoSelectTarget>::Marker as ResolveRow<T>>::Row>
where Columns: ToSQL<'a, PostgresValue<'a>> + IntoSelectTarget, Columns::Marker: ResolveRow<T>,

Adds a RETURNING clause and transitions to ReturningSet state

Source§

impl<'a, S, T> InsertBuilder<'a, S, InsertOnConflictSet, T>

Source

pub fn returning<Columns>( self, columns: Columns, ) -> InsertBuilder<'a, S, InsertReturningSet, T, Scoped<<Columns as IntoSelectTarget>::Marker, Cons<T, Nil>>, <<Columns as IntoSelectTarget>::Marker as ResolveRow<T>>::Row>
where Columns: ToSQL<'a, PostgresValue<'a>> + IntoSelectTarget, Columns::Marker: ResolveRow<T>,

Adds a RETURNING clause after ON CONFLICT

Source§

impl<'a, S, T> InsertBuilder<'a, S, InsertDoUpdateSet, T>

Source

pub fn where<E>( self, condition: E, ) -> InsertBuilder<'a, S, InsertOnConflictSet, T>
where E: Expr<'a, PostgresValue<'a>>, E::SQLType: BooleanLike,

Adds a WHERE clause to the DO UPDATE SET clause.

Generates: ON CONFLICT (col) DO UPDATE SET ... WHERE condition

Source

pub fn returning<Columns>( self, columns: Columns, ) -> InsertBuilder<'a, S, InsertReturningSet, T, Scoped<<Columns as IntoSelectTarget>::Marker, Cons<T, Nil>>, <<Columns as IntoSelectTarget>::Marker as ResolveRow<T>>::Row>
where Columns: ToSQL<'a, PostgresValue<'a>> + IntoSelectTarget, Columns::Marker: ResolveRow<T>,

Adds a RETURNING clause after DO UPDATE SET