khive-db 0.9.0

SQLite storage backend: entities, edges, notes, events, FTS5, sqlite-vec vectors.
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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
//! SQL-backed `EntityStore` implementation.

use std::collections::HashSet;
use std::sync::Arc;

use async_trait::async_trait;
use rusqlite::OptionalExtension;
use uuid::Uuid;

use khive_storage::attachment::{Attachment, AttachmentSubstrate};
use khive_storage::entity::{Entity, EntityFilter};
use khive_storage::error::{StorageError, WriterTaskRequestState};
use khive_storage::types::{
    BatchWriteSummary, DeleteMode, Page, PageRequest, SeekCursor, SeekPage, SqlStatement, SqlValue,
};
use khive_storage::EntityStore;
use khive_storage::StorageCapability;

use crate::error::SqliteError;
use crate::pool::ConnectionPool;
use crate::sql_bridge::bind_params;
use crate::stores::attachment::{attachment_upsert_statement, delete_record_attachments_statement};
use crate::writer_task::{execute_wrapped_transaction, WriterTaskHandle};

fn map_err(e: rusqlite::Error, op: &'static str) -> StorageError {
    StorageError::driver(StorageCapability::Entities, op, e)
}

fn map_sqlite_err(e: SqliteError, op: &'static str) -> StorageError {
    StorageError::driver(StorageCapability::Entities, op, e)
}

const NAMESPACE_COUNT_CHUNK_SIZE: usize = 500;

const ENTITY_SELECT_COLUMNS: &str =
    "entities.id, entities.namespace, entities.kind, entities.entity_type, entities.name, \
     entities.description, entities.properties, entities.tags, entities.created_at, \
     entities.updated_at, entities.deleted_at, entities.merged_into, entities.merge_event_id, \
     (SELECT attachment.content_ref FROM attachments AS attachment \
      WHERE attachment.record_uuid = entities.id \
        AND attachment.substrate = 'entity' AND attachment.role = 'content') AS content_ref, entities.version";

// ---------------------------------------------------------------------------
// Pure statement builders (ADR-099 B3 r6 structural cut)
//
// These carry NO I/O — they turn an already-computed `Entity` (or a bare id)
// into the exact `SqlStatement` this store executes. `upsert_entity` and
// `delete_entity` below call them and execute the result; ADR-099's atomic
// prepare path (`khive-runtime`) calls them too, to build the same statement
// for its own guarded, synchronous apply. One statement generator, two
// execution mechanisms (async trait dispatch vs. synchronous atomic unit) —
// per ADR-099's accepted "handler-logic-duplication objection" text, the
// bulk-apply path reuses the handler's existing statement generation instead
// of re-deriving it.
// ---------------------------------------------------------------------------

/// Insert at version one, or replace the fields and advance the existing row once.
pub fn entity_upsert_statement(entity: &Entity) -> SqlStatement {
    let mut statement = entity_write_statement(entity, "INSERT", "entity-upsert");
    statement.sql.push_str(
        " ON CONFLICT(id) DO UPDATE SET namespace=excluded.namespace, kind=excluded.kind, \
         entity_type=excluded.entity_type, name=excluded.name, description=excluded.description, \
         properties=excluded.properties, tags=excluded.tags, created_at=excluded.created_at, \
         updated_at=excluded.updated_at, deleted_at=excluded.deleted_at, \
         merged_into=excluded.merged_into, merge_event_id=excluded.merge_event_id, \
         version=entities.version+1",
    );
    statement
}

/// Insert a new entity without replacing an existing live or deleted row.
/// A competing ID causes a constraint error, so a prepared create cannot
/// overwrite a row committed after its absence check.
pub fn entity_insert_statement(entity: &Entity) -> SqlStatement {
    entity_write_statement(entity, "INSERT", "entity-insert")
}

fn entity_write_statement(entity: &Entity, insert: &str, label: &str) -> SqlStatement {
    let properties_str = entity
        .properties
        .as_ref()
        .map(|v| serde_json::to_string(v).unwrap_or_default());
    let tags_str = serde_json::to_string(&entity.tags).unwrap_or_else(|_| "[]".to_string());
    SqlStatement {
        sql: format!(
            "{insert} INTO entities \
              (id, namespace, kind, entity_type, name, description, properties, tags, \
               created_at, updated_at, deleted_at, merged_into, merge_event_id) \
              VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13)"
        ),
        params: vec![
            SqlValue::Text(entity.id.to_string()),
            SqlValue::Text(entity.namespace.clone()),
            SqlValue::Text(entity.kind.clone()),
            match &entity.entity_type {
                Some(t) => SqlValue::Text(t.clone()),
                None => SqlValue::Null,
            },
            SqlValue::Text(entity.name.clone()),
            match &entity.description {
                Some(d) => SqlValue::Text(d.clone()),
                None => SqlValue::Null,
            },
            match properties_str {
                Some(p) => SqlValue::Text(p),
                None => SqlValue::Null,
            },
            SqlValue::Text(tags_str),
            SqlValue::Integer(entity.created_at),
            SqlValue::Integer(entity.updated_at),
            match entity.deleted_at {
                Some(d) => SqlValue::Integer(d),
                None => SqlValue::Null,
            },
            match entity.merged_into {
                Some(u) => SqlValue::Text(u.to_string()),
                None => SqlValue::Null,
            },
            match entity.merge_event_id {
                Some(u) => SqlValue::Text(u.to_string()),
                None => SqlValue::Null,
            },
        ],
        label: Some(label.to_string()),
    }
}

/// Conditional-insert companion to [`entity_upsert_statement`]. Every
/// conflict leaves the existing row untouched so a caller can read the
/// winner and explicitly reapply its intended delta.
pub fn entity_insert_if_absent_statement(entity: &Entity) -> SqlStatement {
    let mut statement = entity_upsert_statement(entity);
    statement.sql = "INSERT INTO entities \
              (id, namespace, kind, entity_type, name, description, properties, tags, \
               created_at, updated_at, deleted_at, merged_into, merge_event_id) \
              VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13) \
              ON CONFLICT DO NOTHING"
        .to_string();
    statement.label = Some("entity-insert-if-absent".to_string());
    statement
}

/// Full-entity compare-and-swap update used after caller-side normalization
/// was derived from a read snapshot. Unlike [`entity_upsert_statement`], this
/// never inserts and cannot overwrite a row whose revision or deletion
/// marker moved after the snapshot was read. The replacement revision must
/// also be strictly greater than the persisted snapshot timestamp. The
/// replacement's `version` is the expected persisted snapshot revision; the
/// UPDATE advances it exactly once. Timestamp equality
/// is a refused CAS, never a successful write with an unchanged concurrency
/// token. Mirrors `note_replace_if_unchanged_statement`
/// (`crates/khive-db/src/stores/note.rs`).
pub fn entity_replace_if_unchanged_statement(
    entity: &Entity,
    expected_updated_at: i64,
    expected_deleted_at: Option<i64>,
) -> SqlStatement {
    let properties_str = entity
        .properties
        .as_ref()
        .map(|v| serde_json::to_string(v).unwrap_or_default());
    let tags_str = serde_json::to_string(&entity.tags).unwrap_or_else(|_| "[]".to_string());
    SqlStatement {
        sql: "UPDATE entities SET \
                namespace = ?1, kind = ?2, entity_type = ?3, name = ?4, description = ?5, \
                properties = ?6, tags = ?7, updated_at = ?8, deleted_at = ?9, \
                merged_into = ?10, merge_event_id = ?11, version = version + 1 \
              WHERE id = ?12 AND updated_at = ?13 AND deleted_at IS ?14 \
                AND ?8 > updated_at AND version = ?15"
            .to_string(),
        params: vec![
            SqlValue::Text(entity.namespace.clone()),
            SqlValue::Text(entity.kind.clone()),
            match &entity.entity_type {
                Some(t) => SqlValue::Text(t.clone()),
                None => SqlValue::Null,
            },
            SqlValue::Text(entity.name.clone()),
            match &entity.description {
                Some(d) => SqlValue::Text(d.clone()),
                None => SqlValue::Null,
            },
            match properties_str {
                Some(p) => SqlValue::Text(p),
                None => SqlValue::Null,
            },
            SqlValue::Text(tags_str),
            SqlValue::Integer(entity.updated_at),
            match entity.deleted_at {
                Some(d) => SqlValue::Integer(d),
                None => SqlValue::Null,
            },
            match entity.merged_into {
                Some(u) => SqlValue::Text(u.to_string()),
                None => SqlValue::Null,
            },
            match entity.merge_event_id {
                Some(u) => SqlValue::Text(u.to_string()),
                None => SqlValue::Null,
            },
            SqlValue::Text(entity.id.to_string()),
            SqlValue::Integer(expected_updated_at),
            match expected_deleted_at {
                Some(value) => SqlValue::Integer(value),
                None => SqlValue::Null,
            },
            SqlValue::Integer(entity.version),
        ],
        label: Some("entity-replace-if-unchanged".to_string()),
    }
}

/// The exact soft-delete `UPDATE` this store's `delete_entity(Soft)` issues.
pub fn entity_soft_delete_statement(id: Uuid, deleted_at: i64) -> SqlStatement {
    SqlStatement {
        sql: "UPDATE entities SET deleted_at = ?1, version = version + 1 WHERE id = ?2 AND deleted_at IS NULL".to_string(),
        params: vec![
            SqlValue::Integer(deleted_at),
            SqlValue::Text(id.to_string()),
        ],
        label: Some("entity-delete-soft".to_string()),
    }
}

/// The exact hard-delete `DELETE` this store's `delete_entity(Hard)` issues
/// (no `deleted_at` predicate — purges live and already-tombstoned rows).
pub fn entity_hard_delete_statement(id: Uuid) -> SqlStatement {
    SqlStatement {
        sql: "DELETE FROM entities WHERE id = ?1".to_string(),
        params: vec![SqlValue::Text(id.to_string())],
        label: Some("entity-delete-hard".to_string()),
    }
}

/// An EntityStore backed by SQLite. Namespace is the caller's responsibility.
///
/// UUID is globally unique — get/delete by ID alone. Query/count use the
/// namespace parameter as passed. Read routing is always pool-backed; the
/// constructor's legacy file-backed flag is retained for API compatibility.
pub struct SqlEntityStore {
    pool: Arc<ConnectionPool>,
    writer_task: Option<WriterTaskHandle>,
}

impl SqlEntityStore {
    /// Create a new store.
    ///
    /// When `KHIVE_WRITE_QUEUE=1` (`PoolConfig::write_queue_enabled`), every
    /// write path on this store — the batch `upsert_entities` (its own
    /// explicit flag check) AND every single-row write routed through the
    /// shared `with_writer` helper (`upsert_entity`, `delete_entity`) —
    /// routes through the pool-wide `WriterTask`
    /// (`ConnectionPool::writer_task_handle`) instead of the legacy
    /// pool-mutex path. The handle is a clone of the ONE writer task owned
    /// by `pool` — constructing multiple stores (or multiple namespaces)
    /// over the same pool never spawns more than one writer task; see
    /// `ConnectionPool::writer_task_handle`'s doc comment for why that
    /// matters. `None` (falling back to the legacy path for every write)
    /// if the resolved flag is off, or if the writer task failed to spawn
    /// (for example, an in-memory pool, which has no standalone-connection
    /// support) — enabled by default for file-backed pools; explicit
    /// off/degraded fallback remains possible.
    pub fn new(pool: Arc<ConnectionPool>, _is_file_backed: bool) -> Self {
        // Enabled by default for file-backed pools; explicit off/degraded
        // fallback remains possible: a missing writer task — whether
        // explicitly disabled, spawn degraded (e.g. in-memory pool), or no
        // Tokio runtime was available at this first access (ADR-067
        // Component A runtime-handle guard) — is cached without failing
        // construction. Every write re-resolves it; strict mode refuses a
        // remaining miss and compatibility mode may use the legacy path.
        let writer_task = pool.writer_task_handle().ok().flatten();

        Self { pool, writer_task }
    }

    fn current_writer_task(
        &self,
        operation: &'static str,
    ) -> Result<Option<WriterTaskHandle>, StorageError> {
        self.pool
            .writer_task_for_write(self.writer_task.as_ref(), operation)
    }

    /// Route a single-row write through the pool-wide `WriterTask` when
    /// the write queue is enabled and a handle is available. Strict mode
    /// refuses a missing handle; compatibility mode falls back to the legacy
    /// pool-mutex path.
    ///
    /// ADR-067 Component A (Fork C slice 2): this is the ONE routing point
    /// for every `with_writer` caller in this store — `upsert_entity`,
    /// `delete_entity` (soft/hard) all reach the WriterTask through this
    /// helper rather than each duplicating the flag check. `f` must be
    /// DML-only (a single statement, no bare `BEGIN IMMEDIATE`): on the
    /// flag-on path it runs inside the WriterTask's own transaction, and a
    /// nested `BEGIN IMMEDIATE` would violate SQLite's nested-transaction
    /// rule. `upsert_entities` (the batch method) performs the same write-time
    /// lookup first; a non-strict `None` then falls through this helper, which
    /// records the actual compatibility fallback. Strict mode returns before
    /// either direct-writer seam is reached.
    async fn with_writer<F, R>(&self, op: &'static str, f: F) -> Result<R, StorageError>
    where
        F: FnOnce(&rusqlite::Connection) -> Result<R, rusqlite::Error> + Send + 'static,
        R: Send + 'static,
    {
        if let Some(writer_task) = self.current_writer_task(op)? {
            return writer_task
                .send_bounded(move |conn| f(conn).map_err(|e| map_err(e, op)))
                .await;
        }

        self.pool
            .record_direct_route(crate::timeout_sink::Site::DirectRouteEntity);
        let pool = Arc::clone(&self.pool);
        tokio::task::spawn_blocking(move || {
            let guard = pool.try_writer().map_err(|e| map_sqlite_err(e, op))?;
            f(guard.conn()).map_err(|e| map_err(e, op))
        })
        .await
        .map_err(|e| StorageError::driver(StorageCapability::Entities, op, e))?
    }

    /// Route multi-statement entity mutations through one write transaction.
    /// The writer task already supplies that transaction; the direct fallback
    /// opens and closes its own fail-closed `BEGIN IMMEDIATE` unit.
    async fn with_writer_tx<F, R>(&self, op: &'static str, f: F) -> Result<R, StorageError>
    where
        F: FnOnce(&rusqlite::Connection) -> Result<R, rusqlite::Error> + Send + 'static,
        R: Send + 'static,
    {
        if let Some(writer_task) = self.current_writer_task(op)? {
            return writer_task
                .send_bounded(move |conn| f(conn).map_err(|error| map_err(error, op)))
                .await;
        }

        self.pool
            .record_direct_route(crate::timeout_sink::Site::DirectRouteEntity);
        let pool = Arc::clone(&self.pool);
        tokio::task::spawn_blocking(move || {
            let guard = pool
                .try_writer()
                .map_err(|error| map_sqlite_err(error, op))?;
            let conn = guard.conn();
            if !conn.is_autocommit() {
                pool.retire_pooled_writer(conn);
                return Err(StorageError::WriterTaskTerminated {
                    request_state: WriterTaskRequestState::SideEffectsUnknown,
                });
            }
            if let Err(begin_error) = conn.execute_batch("BEGIN IMMEDIATE") {
                if !conn.is_autocommit() {
                    pool.retire_pooled_writer(conn);
                    return Err(StorageError::WriterTaskTerminated {
                        request_state: WriterTaskRequestState::SideEffectsUnknown,
                    });
                }
                return Err(map_err(begin_error, op));
            }

            let (result, terminal_state) = execute_wrapped_transaction(conn, op, move |conn| {
                f(conn).map_err(|error| map_err(error, op))
            });
            if terminal_state.is_some() {
                pool.retire_pooled_writer(conn);
            }
            result
        })
        .await
        .map_err(|error| StorageError::driver(StorageCapability::Entities, op, error))?
    }

    async fn with_reader<F, R>(&self, op: &'static str, f: F) -> Result<R, StorageError>
    where
        F: FnOnce(&rusqlite::Connection) -> Result<R, rusqlite::Error> + Send + 'static,
        R: Send + 'static,
    {
        super::run_pooled_store_read(
            Arc::clone(&self.pool),
            StorageCapability::Entities,
            op,
            move |conn| f(conn).map_err(|error| map_err(error, op)),
        )
        .await
    }
}

// =============================================================================
// Helpers
// =============================================================================

fn read_entity(row: &rusqlite::Row<'_>) -> Result<Entity, rusqlite::Error> {
    let id_str: String = row.get(0)?;
    let namespace: String = row.get(1)?;
    let kind: String = row.get(2)?;
    let entity_type: Option<String> = row.get(3)?;
    let name: String = row.get(4)?;
    let description: Option<String> = row.get(5)?;
    let properties_str: Option<String> = row.get(6)?;
    let tags_str: String = row.get(7)?;
    let created_at: i64 = row.get(8)?;
    let updated_at: i64 = row.get(9)?;
    let deleted_at: Option<i64> = row.get(10)?;
    let merged_into_str: Option<String> = row.get(11)?;
    let merge_event_id_str: Option<String> = row.get(12)?;
    let content_ref: Option<String> = row.get(13)?;
    let version: i64 = row.get(14)?;

    let id = parse_uuid(&id_str)?;

    let properties = properties_str
        .map(|s| {
            serde_json::from_str(&s).map_err(|e| {
                rusqlite::Error::FromSqlConversionFailure(
                    6,
                    rusqlite::types::Type::Text,
                    Box::new(e),
                )
            })
        })
        .transpose()?;

    let tags: Vec<String> = serde_json::from_str(&tags_str).map_err(|e| {
        rusqlite::Error::FromSqlConversionFailure(7, rusqlite::types::Type::Text, Box::new(e))
    })?;

    let merged_into = merged_into_str
        .as_deref()
        .map(Uuid::parse_str)
        .transpose()
        .map_err(|e| {
            rusqlite::Error::FromSqlConversionFailure(10, rusqlite::types::Type::Text, Box::new(e))
        })?;

    let merge_event_id = merge_event_id_str
        .as_deref()
        .map(Uuid::parse_str)
        .transpose()
        .map_err(|e| {
            rusqlite::Error::FromSqlConversionFailure(11, rusqlite::types::Type::Text, Box::new(e))
        })?;

    Ok(Entity {
        id,
        namespace,
        kind,
        entity_type,
        name,
        description,
        properties,
        tags,
        created_at,
        updated_at,
        version,
        deleted_at,
        merged_into,
        merge_event_id,
        content_ref,
    })
}

/// DML-only batch upsert loop shared by both the legacy (flag-off) and
/// WriterTask-routed (flag-on) `upsert_entities` paths (ADR-067 slice 1).
///
/// Issues no `BEGIN` / `COMMIT` / `ROLLBACK` itself — the caller owns the
/// enclosing transaction. Per-row failures are captured into
/// `BatchWriteSummary::failed`/`first_error` rather than aborting the loop,
/// matching the existing partial-success contract: this function's own
/// `Result` is `Ok` unless a caller bug is present, since no branch here
/// returns `Err`.
fn batch_upsert_entities(
    conn: &rusqlite::Connection,
    entities: &[Entity],
    attempted: u64,
) -> Result<BatchWriteSummary, rusqlite::Error> {
    let mut summary = BatchWriteSummary {
        attempted,
        ..BatchWriteSummary::default()
    };

    for (index, entity) in entities.iter().enumerate() {
        let id_str = entity.id.to_string();
        let statement = entity_upsert_statement(entity);
        let result = (|| {
            let mut prepared = conn.prepare(&statement.sql)?;
            bind_params(&mut prepared, &statement.params)?;
            prepared.raw_execute()
        })();
        match result {
            Ok(_) => summary.affected = summary.affected.saturating_add(1),
            Err(e) => {
                let (class, retryability) = super::classify_batch_sqlite_error(&e);
                summary.record_failure(index, Some(id_str), class, retryability, e.to_string());
            }
        }
    }

    Ok(summary)
}

fn parse_uuid(s: &str) -> Result<Uuid, rusqlite::Error> {
    Uuid::parse_str(s).map_err(|e| {
        rusqlite::Error::FromSqlConversionFailure(0, rusqlite::types::Type::Text, Box::new(e))
    })
}

/// Escape SQLite `LIKE` wildcard characters (`%`, `_`) and the escape
/// character itself (`\`) so a caller-supplied name is matched literally
/// under `LIKE ... ESCAPE '\'` rather than as a pattern (#818: an
/// entity named e.g. `a_b` must not also match `aXb`, and a name containing
/// `%` must not silently widen into a broad substring scan).
fn escape_like(input: &str) -> String {
    let mut out = String::with_capacity(input.len());
    for c in input.chars() {
        if matches!(c, '\\' | '%' | '_') {
            out.push('\\');
        }
        out.push(c);
    }
    out
}

fn build_entity_where(
    namespace: &str,
    filter: &EntityFilter,
) -> (String, Vec<Box<dyn rusqlite::types::ToSql>>) {
    // When filter.namespaces is non-empty use `namespace IN (...)` so that
    // multi-namespace read visibility works.  Otherwise fall back to the
    // single-namespace equality check for backward compatibility.
    let (ns_condition, ns_params): (String, Vec<Box<dyn rusqlite::types::ToSql>>) =
        if !filter.namespaces.is_empty() {
            let placeholders: Vec<String> = (1..=filter.namespaces.len())
                .map(|i| format!("?{i}"))
                .collect();
            let params: Vec<Box<dyn rusqlite::types::ToSql>> = filter
                .namespaces
                .iter()
                .map(|ns| -> Box<dyn rusqlite::types::ToSql> { Box::new(ns.clone()) })
                .collect();
            (
                format!("namespace IN ({})", placeholders.join(", ")),
                params,
            )
        } else {
            (
                "namespace = ?1".to_string(),
                vec![Box::new(namespace.to_string())],
            )
        };

    let mut conditions: Vec<String> = vec![ns_condition, "deleted_at IS NULL".to_string()];
    let mut params: Vec<Box<dyn rusqlite::types::ToSql>> = ns_params;

    if !filter.ids.is_empty() {
        let placeholders: Vec<String> = filter
            .ids
            .iter()
            .map(|id| {
                params.push(Box::new(id.to_string()));
                format!("?{}", params.len())
            })
            .collect();
        conditions.push(format!("id IN ({})", placeholders.join(", ")));
    }

    if !filter.kinds.is_empty() {
        let placeholders: Vec<String> = filter
            .kinds
            .iter()
            .map(|k| {
                params.push(Box::new(k.clone()));
                format!("?{}", params.len())
            })
            .collect();
        conditions.push(format!("kind IN ({})", placeholders.join(", ")));
    }

    let type_scope = conditions.join(" AND ");
    let type_predicate = |scope: &str, placeholders: &str| {
        if filter.legacy_entity_type_fallback {
            // Legacy properties can contain invalid JSON. Exclude those rows
            // from type fallback without rewriting them. Keep json_valid as
            // an explicit term matching the partial legacy-type index;
            // json_type alone does not imply its validity predicate to SQLite.
            format!(
                "id IN (SELECT id FROM entities WHERE {scope} \
                 AND entity_type IN ({placeholders}) \
                 UNION ALL SELECT id FROM entities WHERE {scope} \
                 AND entity_type IS NULL AND json_valid(properties) \
                 AND json_type(properties, '$.type') = 'text' \
                 AND json_extract(properties, '$.type') IN ({placeholders}))"
            )
        } else {
            format!("entity_type IN ({placeholders})")
        }
    };
    if !filter.entity_types.is_empty() {
        let placeholders: Vec<String> = filter
            .entity_types
            .iter()
            .map(|t| {
                params.push(Box::new(t.clone()));
                format!("?{}", params.len())
            })
            .collect();
        conditions.push(type_predicate(&type_scope, &placeholders.join(", ")));
    }

    if !filter.entity_types_by_kind.is_empty() {
        let mut groups = Vec::new();
        for (kind, types) in &filter.entity_types_by_kind {
            if types.is_empty() {
                continue;
            }
            params.push(Box::new(kind.clone()));
            let kind_param = params.len();
            let placeholders = types
                .iter()
                .map(|value| {
                    params.push(Box::new(value.clone()));
                    format!("?{}", params.len())
                })
                .collect::<Vec<_>>()
                .join(", ");
            let scope = format!("{type_scope} AND kind = ?{kind_param}");
            let predicate = type_predicate(&scope, &placeholders);
            groups.push(format!("(kind = ?{kind_param} AND {predicate})"));
        }
        conditions.push(if groups.is_empty() {
            "0".to_string()
        } else {
            format!("({})", groups.join(" OR "))
        });
    }

    if let Some(ref prefix) = filter.name_prefix {
        params.push(Box::new(format!("{}%", escape_like(prefix))));
        conditions.push(format!("name LIKE ?{} ESCAPE '\\'", params.len()));
    }

    if let Some(ref exact) = filter.name_exact {
        params.push(Box::new(exact.clone()));
        // `entities.name` has no `COLLATE NOCASE` (see sql/schema.sql), so
        // `=` is already SQLite's default case-sensitive BINARY comparison.
        // `COLLATE BINARY` is spelled out here so this predicate stays
        // correct even if the column's default collation ever changes.
        conditions.push(format!("name = ?{} COLLATE BINARY", params.len()));
    }

    if !filter.names_ci.is_empty() {
        // ADR-104 Stage C, R1: one batched `LOWER(name) IN (...)` predicate,
        // served by `idx_entities_namespace_name_ci (namespace, LOWER(name))`.
        let placeholders: Vec<String> = filter
            .names_ci
            .iter()
            .map(|n| {
                params.push(Box::new(n.to_ascii_lowercase()));
                format!("?{}", params.len())
            })
            .collect();
        conditions.push(format!("LOWER(name) IN ({})", placeholders.join(", ")));
    }

    if !filter.tags_any.is_empty() {
        let placeholders: Vec<String> = filter
            .tags_any
            .iter()
            .map(|t| {
                // Normalise to lowercase so the comparison is case-insensitive
                // domain filter must be case-insensitive.
                params.push(Box::new(t.to_lowercase()));
                format!("?{}", params.len())
            })
            .collect();
        conditions.push(format!(
            "EXISTS (SELECT 1 FROM json_each(tags) WHERE LOWER(json_each.value) IN ({}))",
            placeholders.join(", ")
        ));
    }

    let clause = format!(" WHERE {}", conditions.join(" AND "));
    (clause, params)
}

fn build_candidate_entity_query(
    columns: &str,
    where_sql: &str,
    candidate_param_indices: &[usize],
    order_by: &str,
    limit_idx: usize,
    offset_idx: usize,
) -> String {
    let candidate_rows = candidate_param_indices
        .iter()
        .map(|idx| format!("(?{idx})"))
        .collect::<Vec<_>>()
        .join(", ");

    format!(
        "WITH candidates(folded_name) AS (VALUES {candidate_rows}), \
         matched_entities(entity_id) AS (\
             SELECT (\
                 SELECT id FROM entities{where_sql} \
                 AND LOWER(name) = candidates.folded_name LIMIT 1\
             ) FROM candidates\
         ) \
         SELECT {columns} FROM entities \
         JOIN matched_entities ON entities.id = matched_entities.entity_id \
         ORDER BY {order_by} LIMIT ?{limit_idx} OFFSET ?{offset_idx}"
    )
}

fn is_complete_id_lookup(filter: &EntityFilter, page: &PageRequest) -> bool {
    !filter.ids.is_empty()
        && filter.kinds.is_empty()
        && filter.entity_types.is_empty()
        && filter.entity_types_by_kind.is_empty()
        && filter.name_prefix.is_none()
        && filter.name_exact.is_none()
        && filter.tags_any.is_empty()
        && filter.names_ci.is_empty()
        && page.offset == 0
        && usize::try_from(page.limit).ok() == Some(filter.ids.len())
}

// =============================================================================
// EntityStore implementation
// =============================================================================

#[async_trait]
impl EntityStore for SqlEntityStore {
    async fn upsert_entity(&self, entity: Entity) -> Result<(), StorageError> {
        let statement = entity_upsert_statement(&entity);
        self.with_writer("upsert_entity", move |conn| {
            let mut stmt = conn.prepare(&statement.sql)?;
            bind_params(&mut stmt, &statement.params)?;
            stmt.raw_execute()?;
            Ok(())
        })
        .await
    }

    async fn insert_entity_if_absent(&self, entity: Entity) -> Result<bool, StorageError> {
        let statement = entity_insert_if_absent_statement(&entity);
        self.with_writer("insert_entity_if_absent", move |conn| {
            let mut stmt = conn.prepare(&statement.sql)?;
            bind_params(&mut stmt, &statement.params)?;
            Ok(stmt.raw_execute()? > 0)
        })
        .await
    }

    async fn upsert_entity_with_attachments(
        &self,
        entity: Entity,
        attachments: Vec<Attachment>,
    ) -> Result<(), StorageError> {
        let entity_id = entity.id;
        let entity_statement = entity_upsert_statement(&entity);
        let mut attachment_statements = Vec::with_capacity(attachments.len());
        for attachment in attachments {
            attachment.validate()?;
            if attachment.record_uuid != entity_id
                || attachment.substrate != AttachmentSubstrate::Entity
            {
                return Err(StorageError::InvalidInput {
                    capability: StorageCapability::Attachments,
                    operation: "upsert_entity_with_attachments".into(),
                    message: format!(
                        "attachment {} must target entity {entity_id}",
                        attachment.role
                    ),
                });
            }
            attachment_statements.push(attachment_upsert_statement(&attachment)?);
        }

        self.with_writer_tx("upsert_entity_with_attachments", move |conn| {
            let mut entity_stmt = conn.prepare(&entity_statement.sql)?;
            bind_params(&mut entity_stmt, &entity_statement.params)?;
            entity_stmt.raw_execute()?;
            drop(entity_stmt);

            for statement in attachment_statements {
                let mut stmt = conn.prepare(&statement.sql)?;
                bind_params(&mut stmt, &statement.params)?;
                stmt.raw_execute()?;
            }
            Ok(())
        })
        .await
    }

    async fn upsert_entities(
        &self,
        entities: Vec<Entity>,
    ) -> Result<BatchWriteSummary, StorageError> {
        let attempted = entities.len() as u64;

        // ADR-067 slice 1: when the write queue is enabled, route through
        // the WriterTask channel. The closure is DML-only — no BEGIN
        // IMMEDIATE/COMMIT/ROLLBACK here, since the WriterTask's run loop
        // owns the transaction and `WriteRequest::execute_and_reply` owns
        // the commit/rollback decision (a bare BEGIN IMMEDIATE inside this
        // closure would violate SQLite's nested-transaction rule).
        if let Some(writer_task) = self.current_writer_task("upsert_entities")? {
            return writer_task
                .send_bounded(move |conn| {
                    batch_upsert_entities(conn, &entities, attempted)
                        .map_err(|e| map_err(e, "upsert_entities"))
                })
                .await;
        }

        // Explicitly disabled or degraded fallback path: byte-for-byte unchanged from pre-ADR-067
        // behavior — the closure owns its own BEGIN IMMEDIATE/COMMIT/ROLLBACK
        // via the pool-mutex writer.
        let origin = self.pool.origin();
        self.with_writer("upsert_entities", move |conn| {
            conn.execute_batch("BEGIN IMMEDIATE")?;
            let _tx_handle = khive_storage::tx_registry::register_scoped(
                Some("entity_upsert_batch".to_string()),
                origin,
            );

            let summary = batch_upsert_entities(conn, &entities, attempted)?;

            if let Err(e) = conn.execute_batch("COMMIT") {
                let _ = conn.execute_batch("ROLLBACK");
                return Err(e);
            }
            Ok(summary)
        })
        .await
    }

    async fn replace_entity_if_unchanged(
        &self,
        entity: Entity,
        expected_updated_at: i64,
        expected_deleted_at: Option<i64>,
    ) -> Result<bool, StorageError> {
        let statement = entity_replace_if_unchanged_statement(
            &entity,
            expected_updated_at,
            expected_deleted_at,
        );
        self.with_writer("replace_entity_if_unchanged", move |conn| {
            let mut stmt = conn.prepare(&statement.sql)?;
            bind_params(&mut stmt, &statement.params)?;
            Ok(stmt.raw_execute()? > 0)
        })
        .await
    }

    async fn get_entity(&self, id: Uuid) -> Result<Option<Entity>, StorageError> {
        let id_str = id.to_string();

        self.with_reader("get_entity", move |conn| {
            let sql = format!(
                "SELECT {ENTITY_SELECT_COLUMNS} FROM entities \
                 WHERE entities.id = ?1 AND entities.deleted_at IS NULL"
            );
            let mut stmt = conn.prepare(&sql)?;
            let mut rows = stmt.query(rusqlite::params![id_str])?;
            match rows.next()? {
                Some(row) => Ok(Some(read_entity(row)?)),
                None => Ok(None),
            }
        })
        .await
    }

    async fn entity_sequence(&self, id: Uuid) -> Result<Option<i64>, StorageError> {
        let id = id.to_string();
        self.with_reader("entity_sequence", move |conn| {
            conn.query_row(
                "SELECT seq FROM entities_seq WHERE entity_id = ?1",
                rusqlite::params![id],
                |row| row.get(0),
            )
            .optional()
        })
        .await
    }

    async fn delete_entity(&self, id: Uuid, mode: DeleteMode) -> Result<bool, StorageError> {
        match mode {
            DeleteMode::Soft => {
                let now = chrono::Utc::now().timestamp_micros();
                let statement = entity_soft_delete_statement(id, now);
                self.with_writer("delete_entity_soft", move |conn| {
                    let mut stmt = conn.prepare(&statement.sql)?;
                    bind_params(&mut stmt, &statement.params)?;
                    Ok(stmt.raw_execute()? > 0)
                })
                .await
            }
            DeleteMode::Hard => {
                let entity_statement = entity_hard_delete_statement(id);
                let attachment_statement =
                    delete_record_attachments_statement(id, AttachmentSubstrate::Entity);
                self.with_writer_tx("delete_entity_hard", move |conn| {
                    let mut entity_stmt = conn.prepare(&entity_statement.sql)?;
                    bind_params(&mut entity_stmt, &entity_statement.params)?;
                    let deleted = entity_stmt.raw_execute()? > 0;
                    drop(entity_stmt);
                    if deleted {
                        let mut attachment_stmt = conn.prepare(&attachment_statement.sql)?;
                        bind_params(&mut attachment_stmt, &attachment_statement.params)?;
                        attachment_stmt.raw_execute()?;
                    }
                    Ok(deleted)
                })
                .await
            }
        }
    }

    async fn query_entities(
        &self,
        namespace: &str,
        filter: EntityFilter,
        page: PageRequest,
    ) -> Result<Page<Entity>, StorageError> {
        let namespace = namespace.to_string();
        let skip_total = is_complete_id_lookup(&filter, &page);
        let limit_i64 = i64::from(page.limit);
        let offset_i64 = i64::try_from(page.offset).map_err(|_| StorageError::InvalidInput {
            capability: StorageCapability::Entities,
            operation: "query_entities".into(),
            message: format!(
                "PageRequest: offset must be <= i64::MAX, got {}",
                page.offset
            ),
        })?;

        self.with_reader("query_entities", move |conn| {
            let total = if filter.names_ci.is_empty() && !skip_total {
                let (count_sql, count_params) = build_entity_where(&namespace, &filter);
                let sql = format!("SELECT COUNT(*) FROM entities{count_sql}");
                let mut stmt = conn.prepare(&sql)?;
                let param_refs: Vec<&dyn rusqlite::types::ToSql> =
                    count_params.iter().map(|p| p.as_ref()).collect();
                Some(stmt.query_row(param_refs.as_slice(), |row| row.get::<_, i64>(0))? as u64)
            } else {
                None
            };

            let mut lookup_filter = filter.clone();
            lookup_filter.names_ci.clear();
            let effective_filter = if filter.names_ci.is_empty() {
                &filter
            } else {
                &lookup_filter
            };
            let (where_sql, mut data_params) = build_entity_where(&namespace, effective_filter);

            let candidate_param_indices = if filter.names_ci.is_empty() {
                Vec::new()
            } else {
                let mut candidates: Vec<String> = filter
                    .names_ci
                    .iter()
                    .map(|name| name.to_ascii_lowercase())
                    .collect();
                candidates.sort_unstable();
                candidates.dedup();
                candidates
                    .into_iter()
                    .map(|candidate| {
                        data_params.push(Box::new(candidate));
                        data_params.len()
                    })
                    .collect()
            };

            // #818: when a name_prefix filter is active, an exact
            // ASCII-case-insensitive match must never be pushed out of the page by
            // pattern candidates that merely share the prefix. Rank exact
            // matches first (deterministic tiebreak via created_at) so page
            // truncation can never hide the record a caller resolved by name.
            let order_by = if let Some(ref prefix) = filter.name_prefix {
                data_params.push(Box::new(prefix.to_ascii_lowercase()));
                format!(
                    "CASE WHEN LOWER(name) = ?{} THEN 0 ELSE 1 END, created_at DESC, id DESC",
                    data_params.len()
                )
            } else {
                // #1671: append `id` as the final tiebreak in the primary
                // key's direction so equal-`created_at` rows keep a fixed
                // order across page boundaries. The deterministic total order
                // removes tie-order instability only — offset paging can still
                // duplicate or skip rows under concurrent inserts/deletes or
                // sort-key updates (that would need snapshot isolation or
                // keyset pagination).
                "created_at DESC, id DESC".to_string()
            };

            data_params.push(Box::new(limit_i64));
            data_params.push(Box::new(offset_i64));

            let limit_idx = data_params.len() - 1;
            let offset_idx = data_params.len();

            let columns = ENTITY_SELECT_COLUMNS;
            let data_sql = if filter.names_ci.is_empty() {
                format!(
                    "SELECT {columns} FROM entities{where_sql} \
                     ORDER BY {order_by} LIMIT ?{limit_idx} OFFSET ?{offset_idx}"
                )
            } else {
                build_candidate_entity_query(
                    columns,
                    &where_sql,
                    &candidate_param_indices,
                    &order_by,
                    limit_idx,
                    offset_idx,
                )
            };

            let mut stmt = conn.prepare(&data_sql)?;
            let param_refs: Vec<&dyn rusqlite::types::ToSql> =
                data_params.iter().map(|p| p.as_ref()).collect();
            let rows = stmt.query_map(param_refs.as_slice(), read_entity)?;

            let mut items = Vec::new();
            for row in rows {
                items.push(row?);
            }

            Ok(Page { items, total })
        })
        .await
    }

    async fn query_entities_after(
        &self,
        namespace: &str,
        filter: EntityFilter,
        after: Option<SeekCursor>,
        limit: u32,
    ) -> Result<SeekPage<Entity>, StorageError> {
        if limit == 0 {
            return Ok(SeekPage::default());
        }
        if !filter.names_ci.is_empty() {
            return Err(StorageError::InvalidInput {
                capability: StorageCapability::Entities,
                operation: "query_entities_after".into(),
                message: "names_ci candidate folding is not compatible with seek pagination".into(),
            });
        }

        let namespace = namespace.to_string();
        let limit_usize = limit as usize;
        let probe_limit_i64 = i64::from(limit) + 1;
        self.with_reader("query_entities_after", move |conn| {
            let (mut where_sql, mut params) = build_entity_where(&namespace, &filter);
            if let Some(cursor) = after {
                params.push(Box::new(cursor.sequence));
                where_sql.push_str(&format!(" AND entities_seq.seq > ?{}", params.len()));
            }
            params.push(Box::new(probe_limit_i64));
            let limit_idx = params.len();

            let columns = ENTITY_SELECT_COLUMNS;
            // CROSS JOIN is load-bearing for the unfiltered walk: SQLite must
            // drive the query from the sequence INTEGER PRIMARY KEY range
            // instead of scanning the namespace index and sorting all matches
            // into a temp B-tree. When a kind filter is active, forcing that
            // same seq-first plan prevents SQLite from using
            // idx_entities_kind(namespace, kind) as the driving index, so a
            // plain JOIN is used instead and left to the query planner — the
            // cursor's page order is still entities_seq.seq, unchanged.
            let join_kind = if filter.kinds.is_empty() {
                "CROSS JOIN"
            } else {
                "JOIN"
            };
            let sql = format!(
                "SELECT {columns}, entities_seq.seq FROM entities_seq \
                 {join_kind} entities ON entities.id = entities_seq.entity_id{where_sql} \
                 ORDER BY entities_seq.seq ASC LIMIT ?{limit_idx}"
            );
            let mut stmt = conn.prepare(&sql)?;
            let param_refs: Vec<&dyn rusqlite::types::ToSql> =
                params.iter().map(|param| param.as_ref()).collect();
            let rows = stmt.query_map(param_refs.as_slice(), |row| {
                Ok((read_entity(row)?, row.get::<_, i64>(15)?))
            })?;
            let mut entries = rows.collect::<Result<Vec<_>, _>>()?;
            let has_more = entries.len() > limit_usize;
            if has_more {
                entries.truncate(limit_usize);
            }
            let next_after = if has_more {
                entries.last().map(|(entity, sequence)| SeekCursor {
                    sequence: *sequence,
                    id: entity.id,
                })
            } else {
                None
            };
            let items = entries.into_iter().map(|(entity, _)| entity).collect();
            Ok(SeekPage { items, next_after })
        })
        .await
    }

    async fn get_entity_including_deleted(&self, id: Uuid) -> Result<Option<Entity>, StorageError> {
        let id_str = id.to_string();

        self.with_reader("get_entity_including_deleted", move |conn| {
            let sql =
                format!("SELECT {ENTITY_SELECT_COLUMNS} FROM entities WHERE entities.id = ?1");
            let mut stmt = conn.prepare(&sql)?;
            let mut rows = stmt.query(rusqlite::params![id_str])?;
            match rows.next()? {
                Some(row) => Ok(Some(read_entity(row)?)),
                None => Ok(None),
            }
        })
        .await
    }

    async fn count_entities(
        &self,
        namespace: &str,
        filter: EntityFilter,
    ) -> Result<u64, StorageError> {
        let namespace = namespace.to_string();

        self.with_reader("count_entities", move |conn| {
            if filter.namespaces.is_empty() {
                let (where_sql, params) = build_entity_where(&namespace, &filter);
                let sql = format!("SELECT COUNT(*) FROM entities{}", where_sql);
                let mut stmt = conn.prepare(&sql)?;
                let param_refs: Vec<&dyn rusqlite::types::ToSql> =
                    params.iter().map(|p| p.as_ref()).collect();
                let count: i64 = stmt.query_row(param_refs.as_slice(), |row| row.get(0))?;
                return Ok(count as u64);
            }

            let deduped_namespaces: Vec<String> = filter
                .namespaces
                .iter()
                .cloned()
                .collect::<HashSet<_>>()
                .into_iter()
                .collect();

            let mut total = 0;
            for chunk in deduped_namespaces.chunks(NAMESPACE_COUNT_CHUNK_SIZE) {
                let chunk_filter = EntityFilter {
                    namespaces: chunk.to_vec(),
                    ..filter.clone()
                };
                let (where_sql, params) = build_entity_where(&namespace, &chunk_filter);
                let sql = format!("SELECT COUNT(*) FROM entities{}", where_sql);
                let mut stmt = conn.prepare(&sql)?;
                let param_refs: Vec<&dyn rusqlite::types::ToSql> =
                    params.iter().map(|p| p.as_ref()).collect();
                let count: i64 = stmt.query_row(param_refs.as_slice(), |row| row.get(0))?;
                total += count as u64;
            }
            Ok(total)
        })
        .await
    }
}

// =============================================================================
// DDL
// =============================================================================

const ENTITIES_DDL: &str = include_str!("../../sql/entities-ddl.sql");

pub(crate) fn ensure_entities_schema(conn: &rusqlite::Connection) -> Result<(), rusqlite::Error> {
    conn.execute_batch(ENTITIES_DDL)
}

#[cfg(test)]
#[path = "entity_tests.rs"]
mod tests;