Skip to main content

cratestack_sqlx/delegate/
model_batch.rs

1//! `ModelDelegate` batch entry points (`batch_get`, `batch_create`,
2//! `batch_update`, `batch_delete`, `batch_upsert`).
3
4use crate::{BatchCreate, BatchDelete, BatchGet, BatchUpdate, BatchUpdateItem, BatchUpsert};
5
6use super::model::ModelDelegate;
7
8impl<'a, M: 'static, PK: 'static> ModelDelegate<'a, M, PK> {
9    /// Fetch many rows by primary key in a single round-trip; missing
10    /// rows surface as per-item `NotFound` in the envelope rather
11    /// than aborting.
12    pub fn batch_get(&self, ids: Vec<PK>) -> BatchGet<'a, M, PK> {
13        BatchGet {
14            runtime: self.runtime,
15            descriptor: self.descriptor,
16            ids,
17        }
18    }
19
20    /// Insert many rows in one outer transaction; each input runs
21    /// under a nested SAVEPOINT, so a per-item failure (validation,
22    /// policy, unique conflict) doesn't take down the rest of the
23    /// batch.
24    pub fn batch_create<I>(&self, inputs: Vec<I>) -> BatchCreate<'a, M, PK, I> {
25        BatchCreate {
26            runtime: self.runtime,
27            descriptor: self.descriptor,
28            inputs,
29        }
30    }
31
32    /// Update many rows in one outer transaction with per-item
33    /// patches and optional `if_match` versions. Per-item failures
34    /// roll back at the savepoint; successful items commit together.
35    pub fn batch_update<I>(&self, items: Vec<BatchUpdateItem<PK, I>>) -> BatchUpdate<'a, M, PK, I> {
36        BatchUpdate {
37            runtime: self.runtime,
38            descriptor: self.descriptor,
39            items,
40        }
41    }
42
43    /// Delete many rows by primary key in a single statement; rows
44    /// that don't exist (or that policy hid) surface as per-item
45    /// `NotFound`.
46    pub fn batch_delete(&self, ids: Vec<PK>) -> BatchDelete<'a, M, PK> {
47        BatchDelete {
48            runtime: self.runtime,
49            descriptor: self.descriptor,
50            ids,
51        }
52    }
53
54    /// Insert-or-update many rows in one outer transaction with
55    /// per-item savepoints. Eligible only for models whose `@id` is
56    /// client-supplied — same compile-time gate as the single-row
57    /// `.upsert(...)`.
58    pub fn batch_upsert<I>(&self, inputs: Vec<I>) -> BatchUpsert<'a, M, PK, I> {
59        BatchUpsert {
60            runtime: self.runtime,
61            descriptor: self.descriptor,
62            inputs,
63        }
64    }
65}