sqlite-diff-rs 0.2.0

Build SQLite changeset and patchset binary formats programmatically, without SQLite
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
//! `diesel` feature: makes patchset operations executable Diesel queries.
//!
//! Identifiers are quoted per backend via [`AstPass::push_identifier`],
//! values travel through [`AstPass::push_bind_param`] with the diesel
//! `SqlType` the target column expects. No `CAST` wrappers are emitted.
//! `Value::Null` renders as the literal keyword `NULL`.
//!
//! # Example: native BOOLEAN bind through an [`Adapter`]
//!
//! The load-bearing case. The source patchset stores the value as
//! `Value::Integer(1)` (SQLite has no boolean type). The target Postgres
//! column is `BOOLEAN`. The adapter maps `("users", column 1, Integer)` to
//! a `BoolBinder` that calls
//! [`push_bind_param::<Bool, bool>`](AstPass::push_bind_param), so the wire
//! carries a native `bool` and no `CAST` appears in the emitted SQL.
//!
//! ```
//! use diesel::debug_query;
//! use diesel::pg::Pg;
//! use diesel::query_builder::AstPass;
//! use diesel::result::QueryResult;
//! use diesel::sql_types::Bool;
//! use sqlite_diff_rs::{
//!     Adapter, Binder, DefaultBinder, DiffOps, Insert, PatchSet, SimpleTable, Value,
//! };
//!
//! struct BoolBinder(bool);
//! impl Binder<Pg> for BoolBinder {
//!     fn walk<'b>(&'b self, out: &mut AstPass<'_, 'b, Pg>) -> QueryResult<()> {
//!         out.push_bind_param::<Bool, bool>(&self.0)
//!     }
//! }
//!
//! struct UsersAdapter;
//! impl<S, B> Adapter<Pg, S, B> for UsersAdapter
//! where
//!     S: AsRef<str> + Sync,
//!     B: AsRef<[u8]> + Sync,
//! {
//!     fn column_name(&self, _table: &str, index: usize) -> &str {
//!         ["id", "active"][index]
//!     }
//!     fn bind<'a>(
//!         &self,
//!         table: &str,
//!         column_index: usize,
//!         value: &'a Value<S, B>,
//!     ) -> diesel::result::QueryResult<Box<dyn Binder<Pg> + Send + 'a>> {
//!         match (table, column_index, value) {
//!             ("users", 1, Value::Integer(i)) => Ok(Box::new(BoolBinder(*i != 0))),
//!             _ => Ok(Box::new(DefaultBinder::from(value))),
//!         }
//!     }
//! }
//!
//! let table = SimpleTable::new("users", &["id", "active"], &[0]);
//! let patchset = PatchSet::<SimpleTable, String, Vec<u8>>::new().insert(
//!     Insert::from(table.clone())
//!         .set(0, 1_i64)
//!         .unwrap()
//!         .set(1, 1_i64)
//!         .unwrap(),
//! );
//!
//! let op = patchset
//!     .iter()
//!     .next()
//!     .unwrap()
//!     .with_adapter::<Pg, _>(&UsersAdapter);
//! let sql = debug_query::<Pg, _>(&op).to_string();
//! assert!(sql.starts_with(r#"INSERT INTO "users" ("id", "active") VALUES ($1, $2)"#));
//! assert!(!sql.contains("CAST")); // native bind, no cast wrapper
//! assert!(sql.contains("true")); // bind rendered via bool's Debug
//! ```

use alloc::boxed::Box;
use alloc::string::ToString;
use alloc::vec::Vec;

use diesel::backend::Backend;
use diesel::query_builder::{AstPass, QueryFragment, QueryId};
use diesel::query_dsl::RunQueryDsl;
use diesel::result::{Error as DieselError, QueryResult};
use diesel::serialize::ToSql;
use diesel::sql_types::{BigInt, Binary, Double, HasSqlType, Text};

use super::sql_output::ColumnNames;
use super::view::{PatchsetOp, PatchsetUpdateEntry};
use crate::SchemaWithPK;
use crate::encoding::Value;

/// Reasons a `PatchsetOp` cannot render into valid SQL. Wrapped in
/// [`DieselError::QueryBuilderError`].
#[derive(Debug, Clone, thiserror::Error)]
enum RenderError {
    /// `ColumnNames::column_name(index)` returned `None`.
    #[error("missing column name for index {column_index}")]
    MissingColumnName { column_index: usize },
    /// UPDATE whose non-PK entries are all `None`; `SET` would be empty.
    #[error("patchset UPDATE has an empty SET clause")]
    EmptyUpdateSet,
    /// UPDATE/DELETE against a table with no primary-key columns.
    #[error("patchset UPDATE/DELETE targets a table with no primary key")]
    EmptyRowPredicate,
    /// [`Adapter::bind`] returned `Err`. Holds the display of the source
    /// error the adapter produced; the specific `DieselError` variant is
    /// collapsed to `QueryBuilderError` here.
    #[error("adapter rejected column {column_index} of table {table_name:?}: {message}")]
    AdapterBindFailure {
        table_name: alloc::string::String,
        column_index: usize,
        message: alloc::string::String,
    },
}

#[inline]
fn render_err(err: RenderError) -> DieselError {
    DieselError::QueryBuilderError(Box::new(err))
}

#[inline]
fn column_name<T: ColumnNames>(table: &T, index: usize) -> QueryResult<&str> {
    table.column_name(index).ok_or_else(|| {
        render_err(RenderError::MissingColumnName {
            column_index: index,
        })
    })
}

fn push_value<'b, S, B, DB>(
    out: &mut AstPass<'_, 'b, DB>,
    value: &'b Value<S, B>,
) -> QueryResult<()>
where
    S: AsRef<str>,
    B: AsRef<[u8]>,
    DB: Backend + HasSqlType<BigInt> + HasSqlType<Double> + HasSqlType<Text> + HasSqlType<Binary>,
    i64: ToSql<BigInt, DB>,
    f64: ToSql<Double, DB>,
    str: ToSql<Text, DB>,
    [u8]: ToSql<Binary, DB>,
{
    match value {
        Value::Null => {
            out.push_sql("NULL");
            Ok(())
        }
        Value::Integer(i) => out.push_bind_param::<BigInt, i64>(i),
        Value::Real(f) => out.push_bind_param::<Double, f64>(f),
        Value::Text(s) => out.push_bind_param::<Text, str>(s.as_ref()),
        Value::Blob(b) => out.push_bind_param::<Binary, [u8]>(b.as_ref()),
    }
}

fn walk_insert<'b, T, S, B, DB>(
    table: &T,
    values: &'b [Value<S, B>],
    out: &mut AstPass<'_, 'b, DB>,
) -> QueryResult<()>
where
    T: ColumnNames,
    S: AsRef<str>,
    B: AsRef<[u8]>,
    DB: Backend + HasSqlType<BigInt> + HasSqlType<Double> + HasSqlType<Text> + HasSqlType<Binary>,
    i64: ToSql<BigInt, DB>,
    f64: ToSql<Double, DB>,
    str: ToSql<Text, DB>,
    [u8]: ToSql<Binary, DB>,
{
    out.push_sql("INSERT INTO ");
    out.push_identifier(table.name())?;
    out.push_sql(" (");
    for index in 0..table.number_of_columns() {
        if index > 0 {
            out.push_sql(", ");
        }
        out.push_identifier(column_name(table, index)?)?;
    }
    out.push_sql(") VALUES (");
    for (index, value) in values.iter().enumerate() {
        if index > 0 {
            out.push_sql(", ");
        }
        push_value(out, value)?;
    }
    out.push_sql(")");
    Ok(())
}

fn walk_update<'b, T, S, B, DB>(
    table: &T,
    pk: &'b [Value<S, B>],
    entries: &'b [PatchsetUpdateEntry<S, B>],
    out: &mut AstPass<'_, 'b, DB>,
) -> QueryResult<()>
where
    T: ColumnNames,
    S: AsRef<str>,
    B: AsRef<[u8]>,
    DB: Backend + HasSqlType<BigInt> + HasSqlType<Double> + HasSqlType<Text> + HasSqlType<Binary>,
    i64: ToSql<BigInt, DB>,
    f64: ToSql<Double, DB>,
    str: ToSql<Text, DB>,
    [u8]: ToSql<Binary, DB>,
{
    out.push_sql("UPDATE ");
    out.push_identifier(table.name())?;
    out.push_sql(" SET ");

    let mut first_set = true;
    for (col_idx, ((), new)) in entries.iter().enumerate() {
        let Some(new_value) = new.as_ref() else {
            continue;
        };
        if table.primary_key_index(col_idx).is_some() {
            continue;
        }
        if !first_set {
            out.push_sql(", ");
        }
        first_set = false;
        out.push_identifier(column_name(table, col_idx)?)?;
        out.push_sql(" = ");
        push_value(out, new_value)?;
    }
    if first_set {
        return Err(render_err(RenderError::EmptyUpdateSet));
    }

    out.push_sql(" WHERE ");
    walk_pk_predicate(table, pk, out)
}

fn walk_delete<'b, T, S, B, DB>(
    table: &T,
    pk: &'b [Value<S, B>],
    out: &mut AstPass<'_, 'b, DB>,
) -> QueryResult<()>
where
    T: ColumnNames,
    S: AsRef<str>,
    B: AsRef<[u8]>,
    DB: Backend + HasSqlType<BigInt> + HasSqlType<Double> + HasSqlType<Text> + HasSqlType<Binary>,
    i64: ToSql<BigInt, DB>,
    f64: ToSql<Double, DB>,
    str: ToSql<Text, DB>,
    [u8]: ToSql<Binary, DB>,
{
    out.push_sql("DELETE FROM ");
    out.push_identifier(table.name())?;
    out.push_sql(" WHERE ");
    walk_pk_predicate(table, pk, out)
}

fn walk_pk_predicate<'b, T, S, B, DB>(
    table: &T,
    pk: &'b [Value<S, B>],
    out: &mut AstPass<'_, 'b, DB>,
) -> QueryResult<()>
where
    T: ColumnNames,
    S: AsRef<str>,
    B: AsRef<[u8]>,
    DB: Backend + HasSqlType<BigInt> + HasSqlType<Double> + HasSqlType<Text> + HasSqlType<Binary>,
    i64: ToSql<BigInt, DB>,
    f64: ToSql<Double, DB>,
    str: ToSql<Text, DB>,
    [u8]: ToSql<Binary, DB>,
{
    let pk_indices = table.pk_indices();
    if pk_indices.is_empty() {
        return Err(render_err(RenderError::EmptyRowPredicate));
    }
    for (pk_ordinal, &col_idx) in pk_indices.iter().enumerate() {
        if pk_ordinal > 0 {
            out.push_sql(" AND ");
        }
        out.push_identifier(column_name(table, col_idx)?)?;
        out.push_sql(" = ");
        push_value(out, &pk[pk_ordinal])?;
    }
    Ok(())
}

impl<T, S, B, DB> QueryFragment<DB> for PatchsetOp<'_, T, S, B>
where
    T: ColumnNames,
    S: AsRef<str>,
    B: AsRef<[u8]>,
    DB: Backend + HasSqlType<BigInt> + HasSqlType<Double> + HasSqlType<Text> + HasSqlType<Binary>,
    i64: ToSql<BigInt, DB>,
    f64: ToSql<Double, DB>,
    str: ToSql<Text, DB>,
    [u8]: ToSql<Binary, DB>,
{
    fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
        // Identifiers are chosen at runtime, so the prepared-statement cache
        // must not retain this query.
        out.unsafe_to_cache_prepared();

        match *self {
            PatchsetOp::Insert { table, values, .. } => walk_insert(table, values, &mut out),
            PatchsetOp::Update {
                table, pk, entries, ..
            } => walk_update(table, pk, entries, &mut out),
            PatchsetOp::Delete { table, pk, .. } => walk_delete(table, pk, &mut out),
        }
    }
}

impl<T, S, B> QueryId for PatchsetOp<'_, T, S, B> {
    type QueryId = ();
    const HAS_STATIC_QUERY_ID: bool = false;
}

// `RunQueryDsl<Conn>` has a blanket impl only for `T: Table`, and `PatchsetOp`
// is not a Table. We add an unconditional impl the same way `SqlQuery` and
// `BoxedSqlQuery` do; the trait's own methods carry their bounds.
impl<T, S, B, Conn> RunQueryDsl<Conn> for PatchsetOp<'_, T, S, B> {}

// ============================================================================
// Adapter / Binder: user-driven per-column native binding, no CAST wrappers.
// ============================================================================

/// Writes one column value onto the query AST.
///
/// Owns any parsed representation the target column needs (`bool`,
/// `uuid::Uuid`, ...) so the value survives [`AstPass::push_bind_param`]'s
/// bind-lifetime requirement.
pub trait Binder<DB: Backend> {
    /// Push the column's value onto the AST. Typically calls
    /// [`AstPass::push_bind_param`] with the diesel `SqlType` matching the
    /// target column, or emits a literal via [`AstPass::push_sql`].
    ///
    /// # Errors
    ///
    /// Propagates any [`DieselError`](diesel::result::Error) from
    /// [`AstPass`].
    fn walk<'b>(&'b self, out: &mut AstPass<'_, 'b, DB>) -> QueryResult<()>;
}

/// The naive binder used when nothing custom is required for a column.
///
/// Binds `Integer -> BigInt`, `Real -> Double`, `Text -> Text`,
/// `Blob -> Binary`; pushes literal `NULL` for `Value::Null`.
pub struct DefaultBinder<'a, S, B> {
    value: &'a Value<S, B>,
}

impl<'a, S, B> DefaultBinder<'a, S, B> {
    /// Construct a default binder that reads through to `value`.
    #[must_use]
    pub fn new(value: &'a Value<S, B>) -> Self {
        Self { value }
    }
}

impl<'a, S, B> From<&'a Value<S, B>> for DefaultBinder<'a, S, B> {
    fn from(value: &'a Value<S, B>) -> Self {
        Self { value }
    }
}

impl<S, B, DB> Binder<DB> for DefaultBinder<'_, S, B>
where
    S: AsRef<str>,
    B: AsRef<[u8]>,
    DB: Backend + HasSqlType<BigInt> + HasSqlType<Double> + HasSqlType<Text> + HasSqlType<Binary>,
    i64: ToSql<BigInt, DB>,
    f64: ToSql<Double, DB>,
    str: ToSql<Text, DB>,
    [u8]: ToSql<Binary, DB>,
{
    fn walk<'b>(&'b self, out: &mut AstPass<'_, 'b, DB>) -> QueryResult<()> {
        push_value(out, self.value)
    }
}

/// Downstream-implemented source of truth for column names and native
/// bindings.
///
/// One adapter per schema (set of tables); it dispatches internally on
/// `table_name`. The wire format carries table names and column positions
/// but not names or types, so the adapter fills that in.
///
/// Object-safe.
pub trait Adapter<DB, S, B> {
    /// Column identifier emitted via `push_identifier` for
    /// `(table_name, column_index)`.
    fn column_name(&self, table_name: &str, column_index: usize) -> &str;

    /// Binder for `(table_name, column_index, value)`. Match on
    /// `(table_name, column_index)`, parse `value` into any owned
    /// representation the target column needs, and box it. Fall through to
    /// [`DefaultBinder`] for columns that need no custom handling.
    ///
    /// # Errors
    ///
    /// Return an error when the source value cannot be represented as the
    /// target column's SQL type — for example, a `Value::Integer` for a
    /// target `UUID`. The error is captured at resolve time and re-surfaced
    /// as [`DieselError::QueryBuilderError`] when the resulting
    /// [`BoundPatchsetOp`] is executed.
    fn bind<'a>(
        &self,
        table_name: &str,
        column_index: usize,
        value: &'a Value<S, B>,
    ) -> QueryResult<Box<dyn Binder<DB> + Send + 'a>>;
}

/// Executable Diesel query built from one `PatchsetOp` + [`Adapter`].
///
/// Constructed via [`PatchsetOp::with_adapter`]. Implements
/// [`QueryFragment`], [`QueryId`], and [`RunQueryDsl`].
pub struct BoundPatchsetOp<'a, T, S, B, DB, A>
where
    DB: Backend,
{
    op: PatchsetOp<'a, T, S, B>,
    resolved: Result<Vec<Box<dyn Binder<DB> + Send + 'a>>, RenderError>,
    adapter: &'a A,
}

impl<'a, T, S, B, DB, A> BoundPatchsetOp<'a, T, S, B, DB, A>
where
    T: SchemaWithPK,
    S: AsRef<str>,
    B: AsRef<[u8]>,
    DB: Backend,
    A: Adapter<DB, S, B> + Send + Sync,
{
    /// Resolve every column of `op` through `adapter` up front.
    ///
    /// Order of the collected binders exactly matches the order in which
    /// [`Self::walk_ast`] emits placeholders, so the two iterate in lockstep
    /// at execute time.
    fn resolve(op: PatchsetOp<'a, T, S, B>, adapter: &'a A) -> Self {
        let table = op.table();
        let table_name = table.name();

        let resolved = (|| -> Result<Vec<Box<dyn Binder<DB> + Send + 'a>>, RenderError> {
            let mut binders: Vec<Box<dyn Binder<DB> + Send + 'a>> = Vec::new();
            let bind = |col_idx: usize,
                        value: &'a Value<S, B>|
             -> Result<Box<dyn Binder<DB> + Send + 'a>, RenderError> {
                adapter.bind(table_name, col_idx, value).map_err(|err| {
                    RenderError::AdapterBindFailure {
                        table_name: table_name.into(),
                        column_index: col_idx,
                        message: err.to_string(),
                    }
                })
            };

            match op {
                PatchsetOp::Insert { values, .. } => {
                    for (col_idx, value) in values.iter().enumerate() {
                        binders.push(bind(col_idx, value)?);
                    }
                }
                PatchsetOp::Update { pk, entries, .. } => {
                    // SET clause: non-PK columns with a new value, in column order.
                    for (col_idx, ((), new)) in entries.iter().enumerate() {
                        if let Some(new_val) = new
                            && table.primary_key_index(col_idx).is_none()
                        {
                            binders.push(bind(col_idx, new_val)?);
                        }
                    }
                    // WHERE clause: PK columns, in PK-ordinal order.
                    for (pk_ordinal, col_idx) in pk_indices(table).into_iter().enumerate() {
                        binders.push(bind(col_idx, &pk[pk_ordinal])?);
                    }
                }
                PatchsetOp::Delete { pk, .. } => {
                    for (pk_ordinal, col_idx) in pk_indices(table).into_iter().enumerate() {
                        binders.push(bind(col_idx, &pk[pk_ordinal])?);
                    }
                }
            }

            Ok(binders)
        })();

        Self {
            op,
            resolved,
            adapter,
        }
    }
}

/// Derive PK-ordinal-ordered column indices from a [`SchemaWithPK`].
///
/// Mirrors the default `ColumnNames::pk_indices` behavior, but works with
/// any schema (including `TableSchema<String>` parsed from binary, which
/// does not implement `ColumnNames`).
fn pk_indices<T: SchemaWithPK>(table: &T) -> Vec<usize> {
    let mut pk_cols: Vec<(usize, usize)> = Vec::new();
    for col_idx in 0..table.number_of_columns() {
        if let Some(pk_ordinal) = table.primary_key_index(col_idx) {
            pk_cols.push((pk_ordinal, col_idx));
        }
    }
    pk_cols.sort_by_key(|(ordinal, _)| *ordinal);
    pk_cols.into_iter().map(|(_, col_idx)| col_idx).collect()
}

impl<T, S, B, DB, A> QueryFragment<DB> for BoundPatchsetOp<'_, T, S, B, DB, A>
where
    T: SchemaWithPK,
    S: AsRef<str>,
    B: AsRef<[u8]>,
    DB: Backend,
    A: Adapter<DB, S, B> + Send + Sync,
{
    fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
        out.unsafe_to_cache_prepared();
        let binder_vec = self
            .resolved
            .as_ref()
            .map_err(|err| render_err(err.clone()))?;
        let mut binders = binder_vec.iter();
        let bind_next = |binders: &mut core::slice::Iter<'b, Box<dyn Binder<DB> + Send + '_>>,
                         out: &mut AstPass<'_, 'b, DB>|
         -> QueryResult<()> {
            let binder = binders.next().ok_or_else(|| {
                DieselError::QueryBuilderError(alloc::boxed::Box::new(BinderResolutionError))
            })?;
            binder.walk(out)
        };

        let adapter = self.adapter;

        match self.op {
            PatchsetOp::Insert { table, .. } => {
                let table_name = table.name();
                out.push_sql("INSERT INTO ");
                out.push_identifier(table_name)?;
                out.push_sql(" (");
                let column_count = table.number_of_columns();
                for index in 0..column_count {
                    if index > 0 {
                        out.push_sql(", ");
                    }
                    out.push_identifier(adapter.column_name(table_name, index))?;
                }
                out.push_sql(") VALUES (");
                for index in 0..column_count {
                    if index > 0 {
                        out.push_sql(", ");
                    }
                    bind_next(&mut binders, &mut out)?;
                }
                out.push_sql(")");
                Ok(())
            }
            PatchsetOp::Update {
                table, pk, entries, ..
            } => {
                let table_name = table.name();
                out.push_sql("UPDATE ");
                out.push_identifier(table_name)?;
                out.push_sql(" SET ");

                let mut first_set = true;
                for (col_idx, ((), new)) in entries.iter().enumerate() {
                    if new.is_none() || table.primary_key_index(col_idx).is_some() {
                        continue;
                    }
                    if !first_set {
                        out.push_sql(", ");
                    }
                    first_set = false;
                    out.push_identifier(adapter.column_name(table_name, col_idx))?;
                    out.push_sql(" = ");
                    bind_next(&mut binders, &mut out)?;
                }
                if first_set {
                    return Err(render_err(RenderError::EmptyUpdateSet));
                }

                out.push_sql(" WHERE ");
                let pk_col_indices = pk_indices(table);
                if pk_col_indices.is_empty() {
                    return Err(render_err(RenderError::EmptyRowPredicate));
                }
                for (pk_ordinal, col_idx) in pk_col_indices.iter().copied().enumerate() {
                    if pk_ordinal > 0 {
                        out.push_sql(" AND ");
                    }
                    out.push_identifier(adapter.column_name(table_name, col_idx))?;
                    out.push_sql(" = ");
                    bind_next(&mut binders, &mut out)?;
                }
                // Silence unused for the pk slice; the actual PK values live
                // inside the pre-resolved binders.
                let _ = pk;
                Ok(())
            }
            PatchsetOp::Delete { table, pk, .. } => {
                let table_name = table.name();
                out.push_sql("DELETE FROM ");
                out.push_identifier(table_name)?;
                out.push_sql(" WHERE ");
                let pk_col_indices = pk_indices(table);
                if pk_col_indices.is_empty() {
                    return Err(render_err(RenderError::EmptyRowPredicate));
                }
                for (pk_ordinal, col_idx) in pk_col_indices.iter().copied().enumerate() {
                    if pk_ordinal > 0 {
                        out.push_sql(" AND ");
                    }
                    out.push_identifier(adapter.column_name(table_name, col_idx))?;
                    out.push_sql(" = ");
                    bind_next(&mut binders, &mut out)?;
                }
                let _ = pk;
                Ok(())
            }
        }
    }
}

impl<T, S, B, DB, A> QueryId for BoundPatchsetOp<'_, T, S, B, DB, A>
where
    DB: Backend,
{
    type QueryId = ();
    const HAS_STATIC_QUERY_ID: bool = false;
}

impl<T, S, B, DB, A, Conn> RunQueryDsl<Conn> for BoundPatchsetOp<'_, T, S, B, DB, A> where
    DB: Backend
{
}

/// Internal error: binder count out of sync between resolve and walk_ast.
/// Only reachable if the two disagree about column bookkeeping, which is a
/// bug in this crate.
#[derive(Debug, thiserror::Error)]
#[error("internal: binder count mismatch between BoundPatchsetOp resolve and walk_ast")]
struct BinderResolutionError;

// ============================================================================
// Entry point on PatchsetOp
// ============================================================================

impl<'a, T, S, B> PatchsetOp<'a, T, S, B>
where
    T: SchemaWithPK,
    S: AsRef<str>,
    B: AsRef<[u8]>,
{
    /// Build an executable [`BoundPatchsetOp`] from this op and `adapter`.
    #[must_use]
    pub fn with_adapter<DB, A>(self, adapter: &'a A) -> BoundPatchsetOp<'a, T, S, B, DB, A>
    where
        A: Adapter<DB, S, B> + Send + Sync,
        DB: Backend,
    {
        BoundPatchsetOp::resolve(self, adapter)
    }
}

// ============================================================================
// ApplyOps: batch execute a patchset iterator without hand-rolled ceremony.
// ============================================================================

/// Extension trait: run every op in an iterator against a Diesel connection.
///
/// Works uniformly on the naive path (`patchset.iter()`) and the adapter
/// path (`patchset.iter().map(|op| op.with_adapter::<DB, _>(&adapter))`).
///
/// ```
/// use sqlite_diff_rs::ApplyOps;
/// // With `ApplyOps` in scope, on any iterator of Diesel-executable ops:
/// //
/// //   patchset.iter().apply_transactional(&mut conn)?;              // naive path
/// //   patchset.iter()                                                // adapter path
/// //       .map(|op| op.with_adapter::<Pg, _>(&adapter))
/// //       .apply_transactional(&mut conn)?;
/// ```
pub trait ApplyOps: Iterator + Sized {
    /// Execute every op in sequence, returning the summed affected-row
    /// count. No transaction; a mid-op failure leaves prior ops committed.
    ///
    /// # Errors
    ///
    /// Returns the first [`DieselError`](diesel::result::Error) any op
    /// produces; remaining ops are not run.
    fn apply<Conn>(self, conn: &mut Conn) -> QueryResult<usize>
    where
        Conn: diesel::Connection,
        Self::Item: QueryFragment<Conn::Backend> + QueryId + RunQueryDsl<Conn>;

    /// Same as [`Self::apply`] wrapped in a diesel
    /// [`Connection::transaction`](diesel::Connection::transaction), so a
    /// mid-op failure rolls the whole batch back.
    ///
    /// # Errors
    ///
    /// Returns the first [`DieselError`](diesel::result::Error) any op
    /// produces, after rollback.
    fn apply_transactional<Conn>(self, conn: &mut Conn) -> QueryResult<usize>
    where
        Conn: diesel::Connection,
        Self::Item: QueryFragment<Conn::Backend> + QueryId + RunQueryDsl<Conn>;
}

impl<I> ApplyOps for I
where
    I: Iterator + Sized,
{
    fn apply<Conn>(self, conn: &mut Conn) -> QueryResult<usize>
    where
        Conn: diesel::Connection,
        Self::Item: QueryFragment<Conn::Backend> + QueryId + RunQueryDsl<Conn>,
    {
        let mut total = 0_usize;
        for op in self {
            total = total.saturating_add(op.execute(conn)?);
        }
        Ok(total)
    }

    fn apply_transactional<Conn>(self, conn: &mut Conn) -> QueryResult<usize>
    where
        Conn: diesel::Connection,
        Self::Item: QueryFragment<Conn::Backend> + QueryId + RunQueryDsl<Conn>,
    {
        conn.transaction(|conn| self.apply(conn))
    }
}