pub type InsertBuilder<'a, Schema, State, Table> = QueryBuilder<'a, Schema, State, Table>;Expand description
Builds an INSERT query specifically for SQLite.
InsertBuilder provides a type-safe, fluent API for constructing INSERT statements
with support for conflict resolution, batch inserts, and returning clauses.
§Type Parameters
Schema: The database schema type, ensuring only valid tables can be referencedState: The current builder state, enforcing proper query construction orderTable: The table being inserted into
§Query Building Flow
- Start with
QueryBuilder::insert(table)to specify the target table - Add
values()to specify what data to insert - Optionally add conflict resolution with
on_conflict() - Optionally add a
returning()clause
§Basic Usage
use drizzle_sqlite::builder::QueryBuilder;
use drizzle_macros::{SQLiteTable, SQLiteSchema};
use drizzle_core::ToSQL;
#[SQLiteTable(name = "users")]
struct User {
#[integer(primary)]
id: i32,
#[text]
name: String,
#[text]
email: Option<String>,
}
#[derive(SQLiteSchema)]
struct Schema {
user: User,
}
let builder = QueryBuilder::new::<Schema>();
let Schema { user } = Schema::new();
// Basic INSERT
let query = builder
.insert(user)
.values([InsertUser::new("Alice")]);
assert_eq!(query.to_sql().sql(), r#"INSERT INTO "users" (name) VALUES (?)"#);
// Batch INSERT
let query = builder
.insert(user)
.values([
InsertUser::new("Alice").with_email("alice@example.com"),
InsertUser::new("Bob").with_email("bob@example.com"),
]);§Conflict Resolution
SQLite supports various conflict resolution strategies:
// Ignore conflicts (ON CONFLICT DO NOTHING)
let query = builder
.insert(user)
.values([InsertUser::new("Alice")])
.on_conflict(Conflict::default());Aliased Type§
pub struct InsertBuilder<'a, Schema, State, Table> {
pub sql: SQL<'a, SQLiteValue<'a>>,
/* private fields */
}Fields§
§sql: SQL<'a, SQLiteValue<'a>>Implementations§
Source§impl<'a, Schema, Table> InsertBuilder<'a, Schema, InsertInitial, Table>where
Table: SQLiteTable<'a>,
impl<'a, Schema, Table> InsertBuilder<'a, Schema, InsertInitial, Table>where
Table: SQLiteTable<'a>,
Sourcepub fn values<I, T>(
self,
values: I,
) -> InsertBuilder<'a, Schema, InsertValuesSet, Table>
pub fn values<I, T>( self, values: I, ) -> InsertBuilder<'a, Schema, InsertValuesSet, Table>
Specifies the values to insert into the table.
This method accepts an iterable of insert value objects generated by the
SQLiteTable macro (e.g., InsertUser). You can insert single values or
multiple values for batch operations.
§Examples
// Single insert
let query = builder
.insert(user)
.values([InsertUser::new("Alice")]);
assert_eq!(query.to_sql().sql(), r#"INSERT INTO "users" (name) VALUES (?)"#);
// Batch insert (all values must have the same fields set)
let query = builder
.insert(user)
.values([
InsertUser::new("Alice").with_email("alice@example.com"),
InsertUser::new("Bob").with_email("bob@example.com"),
]);
assert_eq!(
query.to_sql().sql(),
r#"INSERT INTO "users" (name, email) VALUES (?, ?), (?, ?)"#
);Source§impl<'a, S, T> InsertBuilder<'a, S, InsertValuesSet, T>
impl<'a, S, T> InsertBuilder<'a, S, InsertValuesSet, T>
Sourcepub fn on_conflict<TI>(
self,
conflict: Conflict<'a, TI>,
) -> InsertBuilder<'a, S, InsertOnConflictSet, T>
pub fn on_conflict<TI>( self, conflict: Conflict<'a, TI>, ) -> InsertBuilder<'a, S, InsertOnConflictSet, T>
Adds conflict resolution to handle constraint violations.
SQLite supports various conflict resolution strategies when inserting data that would violate unique constraints or primary keys. This method allows you to specify how to handle such conflicts.
§Examples
// Ignore conflicts (do nothing)
let query = builder
.insert(user)
.values([InsertUser::new("Alice")])
.on_conflict(Conflict::default());
assert_eq!(
query.to_sql().sql(),
r#"INSERT INTO "users" (name) VALUES (?) ON CONFLICT DO NOTHING"#
);Sourcepub fn returning(
self,
columns: impl ToSQL<'a, SQLiteValue<'a>>,
) -> InsertBuilder<'a, S, InsertReturningSet, T>
pub fn returning( self, columns: impl ToSQL<'a, SQLiteValue<'a>>, ) -> InsertBuilder<'a, S, InsertReturningSet, T>
Adds a RETURNING clause and transitions to ReturningSet state
Source§impl<'a, S, T> InsertBuilder<'a, S, InsertOnConflictSet, T>
impl<'a, S, T> InsertBuilder<'a, S, InsertOnConflictSet, T>
Sourcepub fn returning(
self,
columns: impl ToSQL<'a, SQLiteValue<'a>>,
) -> InsertBuilder<'a, S, InsertReturningSet, T>
pub fn returning( self, columns: impl ToSQL<'a, SQLiteValue<'a>>, ) -> InsertBuilder<'a, S, InsertReturningSet, T>
Adds a RETURNING clause after ON CONFLICT