evento-sql 2.0.0-alpha.28

SQL database implementations for evento event sourcing library.
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
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
//! Core SQL implementation for event sourcing.

use std::{
    ops::{Deref, DerefMut},
    sync::{Arc, Mutex},
    time::{Duration, Instant},
};

#[cfg(feature = "mysql")]
use sea_query::MysqlQueryBuilder;
#[cfg(feature = "postgres")]
use sea_query::PostgresQueryBuilder;
#[cfg(feature = "sqlite")]
use sea_query::SqliteQueryBuilder;
use sea_query::{
    Cond, Expr, ExprTrait, Func, Iden, IntoColumnRef, OnConflict, Query, SelectStatement,
};
use sea_query_sqlx::SqlxBinder;
use sqlx::{Database, Pool};
use ulid::Ulid;

use evento_core::{
    cursor::{self, Args, Cursor, Edge, PageInfo, ReadResult, Value},
    EventFilter, Executor, SubscriberStatus, WriteError,
};

/// Dialect-agnostic access to a statement's affected-row count.
///
/// sqlx exposes `rows_affected` only on each driver's concrete `QueryResult`;
/// this bridges them so the fenced UPDATE in [`Executor::acknowledge`] can
/// report ownership without a follow-up SELECT. On MySQL sqlx connects with
/// `CLIENT_FOUND_ROWS`, so the count is rows *matched* (not changed) — a
/// re-ack of an identical cursor still reports the row as owned.
pub trait RowsAffected {
    /// Number of rows matched/affected by the statement.
    fn affected_rows(&self) -> u64;
}

#[cfg(feature = "sqlite")]
impl RowsAffected for sqlx::sqlite::SqliteQueryResult {
    fn affected_rows(&self) -> u64 {
        self.rows_affected()
    }
}

#[cfg(feature = "mysql")]
impl RowsAffected for sqlx::mysql::MySqlQueryResult {
    fn affected_rows(&self) -> u64 {
        self.rows_affected()
    }
}

#[cfg(feature = "postgres")]
impl RowsAffected for sqlx::postgres::PgQueryResult {
    fn affected_rows(&self) -> u64 {
        self.rows_affected()
    }
}

/// Column identifiers for the `event` table.
///
/// Used with sea-query for type-safe SQL query construction.
///
/// # Columns
///
/// - `Id` - Event identifier (ULID format, VARCHAR(26))
/// - `Name` - Event type name (VARCHAR(50))
/// - `AggregatorType` - Aggregate root type (VARCHAR(50))
/// - `AggregatorId` - Aggregate root instance ID (VARCHAR(64))
/// - `Version` - Event sequence number within the aggregate
/// - `Data` - Serialized event payload (BLOB, bitcode format)
/// - `Metadata` - Serialized event metadata (BLOB, bitcode format)
/// - `RoutingKey` - Optional routing key for partitioning (VARCHAR(50))
/// - `Timestamp` - Event timestamp in seconds (BIGINT)
/// - `TimestampSubsec` - Sub-second precision (BIGINT)
#[derive(Iden, Clone)]
pub enum Event {
    /// The table name: `event`
    Table,
    /// Event ID column (ULID)
    Id,
    /// Event type name
    Name,
    /// Aggregate root type
    AggregatorType,
    /// Aggregate root instance ID
    AggregatorId,
    /// Event version/sequence number
    Version,
    /// Serialized event data
    Data,
    /// Serialized event metadata
    Metadata,
    /// Optional routing key
    RoutingKey,
    /// Timestamp in seconds
    Timestamp,
    /// Sub-second precision
    TimestampSubsec,
}

/// Column identifiers for the `snapshot` table.
///
/// Used with sea-query for type-safe SQL query construction. The snapshot table
/// backs projection snapshots via [`Executor::get_snapshot`], [`Executor::save_snapshot`],
/// and [`Executor::delete_snapshot`].
#[derive(Iden)]
pub enum Snapshot {
    /// The table name: `snapshot`
    Table,
    /// Snapshot ID
    Id,
    /// Snapshot type
    Type,
    /// Event stream cursor position
    Cursor,
    /// Revision identifier
    Revision,
    /// Serialized snapshot data
    Data,
    /// Creation timestamp
    CreatedAt,
    /// Last update timestamp
    UpdatedAt,
}

/// Column identifiers for the `subscriber` table.
///
/// Used with sea-query for type-safe SQL query construction.
///
/// # Columns
///
/// - `Key` - Subscriber identifier (primary key)
/// - `WorkerId` - ULID of the current worker processing events
/// - `Cursor` - Current position in the event stream
/// - `Lag` - Seconds behind the newest matching event (not an event count)
/// - `Enabled` - Whether the subscription is active
/// - `CreatedAt` / `UpdatedAt` - Timestamps
#[derive(Iden)]
pub enum Subscriber {
    /// The table name: `subscriber`
    Table,
    /// Subscriber key (primary key)
    Key,
    /// Current worker ID (ULID)
    WorkerId,
    /// Current cursor position
    Cursor,
    /// Event lag counter
    Lag,
    /// Whether subscription is enabled
    Enabled,
    /// Creation timestamp
    CreatedAt,
    /// Last update timestamp
    UpdatedAt,
}

/// Type alias for MySQL executor.
///
/// Equivalent to `Sql<sqlx::MySql>`.
#[cfg(feature = "mysql")]
pub type MySql = Sql<sqlx::MySql>;

/// Read-write executor pair for MySQL.
///
/// Used in CQRS patterns where you may have separate read and write connections.
#[cfg(feature = "mysql")]
pub type RwMySql = evento_core::Rw<MySql, MySql>;

/// Type alias for PostgreSQL executor.
///
/// Equivalent to `Sql<sqlx::Postgres>`.
#[cfg(feature = "postgres")]
pub type Postgres = Sql<sqlx::Postgres>;

/// Read-write executor pair for PostgreSQL.
///
/// Used in CQRS patterns where you may have separate read and write connections.
#[cfg(feature = "postgres")]
pub type RwPostgres = evento_core::Rw<Postgres, Postgres>;

/// Type alias for SQLite executor.
///
/// Equivalent to `Sql<sqlx::Sqlite>`.
#[cfg(feature = "sqlite")]
pub type Sqlite = Sql<sqlx::Sqlite>;

/// Read-write executor pair for SQLite.
///
/// Used in CQRS patterns where you may have separate read and write connections.
#[cfg(feature = "sqlite")]
pub type RwSqlite = evento_core::Rw<Sqlite, Sqlite>;

/// SQL database executor for event sourcing operations.
///
/// A generic wrapper around a SQLx connection pool that implements the
/// [`Executor`](evento_core::Executor) trait for storing and querying events.
///
/// # Type Parameters
///
/// - `DB` - The SQLx database type (e.g., `sqlx::Sqlite`, `sqlx::MySql`, `sqlx::Postgres`)
///
/// # Example
///
/// ```rust,no_run
/// use evento_sql::Sql;
/// use sqlx::sqlite::SqlitePoolOptions;
///
/// # async fn run() -> anyhow::Result<()> {
/// // Create a connection pool
/// let pool = SqlitePoolOptions::new()
///     .connect(":memory:")
///     .await?;
///
/// // Convert to Sql executor
/// let executor: Sql<sqlx::Sqlite> = pool.clone().into();
///
/// // Or use the type alias
/// let executor: evento_sql::Sqlite = pool.into();
/// # let _ = executor;
/// # Ok(())
/// # }
/// ```
///
/// # Executor Implementation
///
/// The `Sql` type implements [`Executor`](evento_core::Executor) with the following operations:
///
/// - **`read`** - Query events with filtering and cursor-based pagination
/// - **`write`** - Persist events with optimistic concurrency control
/// - **`get_subscriber_cursor`** - Get the current cursor position for a subscriber
/// - **`is_subscriber_running`** - Check if a subscriber is active with a specific worker
/// - **`upsert_subscriber`** - Create or update a subscriber record
/// - **`acknowledge`** - Update subscriber cursor after processing events
///
/// # Ordering and the stability watermark
///
/// `write` stamps `timestamp`/`timestamp_subsec` with the **database server
/// clock** (statement time), so cursor order cannot be skewed by writer-host
/// clocks. Because several processes can still commit out of cursor order
/// within a small window, `stable_timestamp` returns DB-server time minus
/// [`stable_margin`](Self::stable_margin) (default 1s) and subscriptions only
/// process events below that watermark. The margin must exceed the worst-case
/// duration of a single event INSERT (plus replica lag when reading from a
/// replica via `Rw`); subscription end-to-end latency grows by roughly the
/// margin.
pub struct Sql<DB: Database> {
    pool: Pool<DB>,
    write_watch: tokio::sync::watch::Sender<u64>,
    stable_margin: Duration,
    /// Last DB-server clock sample: (when it was taken locally, DB `now()` in
    /// microseconds). Shared across clones so one re-sample per TTL serves
    /// every subscription on this executor.
    clock_sample: Arc<Mutex<Option<(Instant, u64)>>>,
}

/// Default stability margin for [`Sql::stable_margin`].
const DEFAULT_STABLE_MARGIN: Duration = Duration::from_secs(1);

/// How long a DB-clock sample may serve [`Executor::stable_timestamp`] before
/// being refreshed with a round trip. Must stay well under any configured
/// [`Sql::stable_margin`], which already absorbs far more skew than a
/// TTL-stale sample introduces.
const CLOCK_SAMPLE_TTL: Duration = Duration::from_secs(1);

impl<DB: Database> Sql<DB> {
    /// Sets the stability margin subtracted from DB-server time to form the
    /// subscription watermark (default 1s).
    ///
    /// Lower values reduce subscription latency but must stay above the
    /// worst-case commit duration of a single event INSERT — otherwise a slow
    /// commit can land below an already-acknowledged cursor and be skipped.
    pub fn stable_margin(mut self, v: Duration) -> Self {
        self.stable_margin = v;
        self
    }

    fn build_sqlx<S: SqlxBinder>(statement: S) -> (String, sea_query_sqlx::SqlxValues) {
        match DB::NAME {
            #[cfg(feature = "sqlite")]
            "SQLite" => statement.build_sqlx(SqliteQueryBuilder),
            #[cfg(feature = "mysql")]
            "MySQL" => statement.build_sqlx(MysqlQueryBuilder),
            #[cfg(feature = "postgres")]
            "PostgreSQL" => statement.build_sqlx(PostgresQueryBuilder),
            name => panic!("'{name}' not supported, consider using SQLite, PostgreSQL or MySQL"),
        }
    }

    /// Per-dialect SQL expressions evaluating to the DB server's current time
    /// as `(whole seconds, milliseconds-within-second)`.
    ///
    /// All date/time functions are statement-stable on every supported
    /// backend (SQLite caches the instant per statement, MySQL's `NOW()` is
    /// statement-start, Postgres' `now()` is transaction-start), so the two
    /// columns — and every row of a multi-event batch — always agree.
    fn server_time_exprs() -> (Expr, Expr) {
        match DB::NAME {
            #[cfg(feature = "sqlite")]
            "SQLite" => (
                Expr::cust("CAST(unixepoch('subsec') AS INTEGER)"),
                Expr::cust("CAST(unixepoch('subsec') * 1000 AS INTEGER) % 1000"),
            ),
            #[cfg(feature = "mysql")]
            "MySQL" => (
                Expr::cust("FLOOR(UNIX_TIMESTAMP(NOW(3)))"),
                Expr::cust("MOD(FLOOR(UNIX_TIMESTAMP(NOW(3)) * 1000), 1000)"),
            ),
            #[cfg(feature = "postgres")]
            "PostgreSQL" => (
                Expr::cust("FLOOR(EXTRACT(EPOCH FROM now()))::BIGINT"),
                Expr::cust("MOD(FLOOR(EXTRACT(EPOCH FROM now()) * 1000)::BIGINT, 1000)"),
            ),
            name => panic!("'{name}' not supported, consider using SQLite, PostgreSQL or MySQL"),
        }
    }

    /// SQL expression evaluating to the DB server's current time in microseconds.
    fn server_time_micros_expr() -> &'static str {
        match DB::NAME {
            #[cfg(feature = "sqlite")]
            "SQLite" => "CAST(unixepoch('subsec') * 1000000 AS INTEGER)",
            #[cfg(feature = "mysql")]
            "MySQL" => "CAST(UNIX_TIMESTAMP(NOW(6)) * 1000000 AS SIGNED)",
            #[cfg(feature = "postgres")]
            "PostgreSQL" => "(EXTRACT(EPOCH FROM now()) * 1000000)::BIGINT",
            name => panic!("'{name}' not supported, consider using SQLite, PostgreSQL or MySQL"),
        }
    }
}

#[async_trait::async_trait]
impl<DB> Executor for Sql<DB>
where
    DB: Database,
    for<'c> &'c mut DB::Connection: sqlx::Executor<'c, Database = DB>,
    sea_query_sqlx::SqlxValues: sqlx::IntoArguments<DB>,
    String: for<'r> sqlx::Decode<'r, DB> + sqlx::Type<DB>,
    bool: for<'r> sqlx::Decode<'r, DB> + sqlx::Type<DB>,
    Vec<u8>: for<'r> sqlx::Decode<'r, DB> + sqlx::Type<DB>,
    i64: for<'r> sqlx::Decode<'r, DB> + sqlx::Type<DB>,
    usize: sqlx::ColumnIndex<DB::Row>,
    SqlEvent: for<'r> sqlx::FromRow<'r, DB::Row>,
    DB::QueryResult: RowsAffected,
{
    async fn read(
        &self,
        aggregators: Option<Arc<[EventFilter]>>,
        routing_key: Option<evento_core::RoutingKey>,
        args: Args,
        to_micros: Option<u64>,
    ) -> anyhow::Result<ReadResult<evento_core::Event>> {
        let statement = Query::select()
            .columns([
                Event::Id,
                Event::Name,
                Event::AggregatorType,
                Event::AggregatorId,
                Event::Version,
                Event::Data,
                Event::Metadata,
                Event::RoutingKey,
                Event::Timestamp,
                Event::TimestampSubsec,
            ])
            .from(Event::Table)
            .conditions(
                aggregators.is_some(),
                |q| {
                    let Some(aggregators) = aggregators else {
                        return;
                    };

                    let mut cond = Cond::any();

                    for aggregator in aggregators.iter() {
                        let mut aggregator_cond = Cond::all().add(
                            Expr::col(Event::AggregatorType).eq(aggregator.aggregate_type.clone()),
                        );

                        if let Some(id) = &aggregator.aggregate_id {
                            aggregator_cond =
                                aggregator_cond.add(Expr::col(Event::AggregatorId).eq(id.clone()));
                        }

                        if let Some(name) = &aggregator.name {
                            aggregator_cond =
                                aggregator_cond.add(Expr::col(Event::Name).eq(name.clone()));
                        }

                        cond = cond.add(aggregator_cond);
                    }

                    q.and_where(cond.into());
                },
                |_| {},
            )
            .conditions(
                matches!(routing_key, Some(evento_core::RoutingKey::Value(_))),
                |q| {
                    if let Some(evento_core::RoutingKey::Value(Some(ref routing_key))) = routing_key
                    {
                        q.and_where(Expr::col(Event::RoutingKey).eq(routing_key));
                    }

                    if let Some(evento_core::RoutingKey::Value(None)) = routing_key {
                        q.and_where(Expr::col(Event::RoutingKey).is_null());
                    }
                },
                |_q| {},
            )
            .conditions(
                to_micros.is_some(),
                |q| {
                    let Some(bound) = to_micros else {
                        return;
                    };

                    // Exclusive bound on the event stamp (`ts` seconds +
                    // `subsec` milliseconds): stamp < bound ⇔
                    // ts < bound_secs, or ts = bound_secs and
                    // subsec·1000 < bound_rem_micros — the latter rewritten as
                    // an integer comparison (subsec < ceil(rem / 1000)). The
                    // leading `timestamp <` term keeps the predicate sargable.
                    let secs = (bound / 1_000_000) as i64;
                    let subsec_bound = ((bound % 1_000_000).div_ceil(1_000)) as i64;
                    q.and_where(
                        Expr::col(Event::Timestamp)
                            .lt(secs)
                            .or(Expr::col(Event::Timestamp)
                                .eq(secs)
                                .and(Expr::col(Event::TimestampSubsec).lt(subsec_bound))),
                    );
                },
                |_q| {},
            )
            .to_owned();

        Ok(Reader::new(statement)
            .args(args)
            .execute::<_, SqlEvent, _>(&self.pool)
            .await?
            .map(|e| e.0))
    }

    async fn latest_timestamp(
        &self,
        aggregators: Option<Arc<[EventFilter]>>,
        routing_key: Option<evento_core::RoutingKey>,
    ) -> anyhow::Result<u64> {
        let statement = Query::select()
            .expr(Func::max(Expr::col(Event::Timestamp)))
            .from(Event::Table)
            .conditions(
                aggregators.is_some(),
                |q| {
                    let Some(aggregators) = aggregators else {
                        return;
                    };

                    let mut cond = Cond::any();

                    for aggregator in aggregators.iter() {
                        let mut aggregator_cond = Cond::all().add(
                            Expr::col(Event::AggregatorType).eq(aggregator.aggregate_type.clone()),
                        );

                        if let Some(id) = &aggregator.aggregate_id {
                            aggregator_cond =
                                aggregator_cond.add(Expr::col(Event::AggregatorId).eq(id.clone()));
                        }

                        if let Some(name) = &aggregator.name {
                            aggregator_cond =
                                aggregator_cond.add(Expr::col(Event::Name).eq(name.clone()));
                        }

                        cond = cond.add(aggregator_cond);
                    }

                    q.and_where(cond.into());
                },
                |_| {},
            )
            .conditions(
                matches!(routing_key, Some(evento_core::RoutingKey::Value(_))),
                |q| {
                    if let Some(evento_core::RoutingKey::Value(Some(ref routing_key))) = routing_key
                    {
                        q.and_where(Expr::col(Event::RoutingKey).eq(routing_key));
                    }

                    if let Some(evento_core::RoutingKey::Value(None)) = routing_key {
                        q.and_where(Expr::col(Event::RoutingKey).is_null());
                    }
                },
                |_q| {},
            )
            .to_owned();

        let (sql, values) = Self::build_sqlx(statement);

        let (ts,): (Option<i64>,) =
            sqlx::query_as_with::<DB, (Option<i64>,), _>(sqlx::AssertSqlSafe(sql.as_str()), values)
                .fetch_one(&self.pool)
                .await?;

        Ok(ts.map(|v| if v < 0 { 0 } else { v as u64 }).unwrap_or(0))
    }

    async fn get_subscriber_cursor(&self, key: String) -> anyhow::Result<Option<Value>> {
        let statement = Query::select()
            .columns([Subscriber::Cursor])
            .from(Subscriber::Table)
            .and_where(Expr::col(Subscriber::Key).eq(Expr::value(key)))
            .limit(1)
            .to_owned();

        let (sql, values) = Self::build_sqlx(statement);

        let Some((cursor,)) = sqlx::query_as_with::<DB, (Option<String>,), _>(
            sqlx::AssertSqlSafe(sql.as_str()),
            values,
        )
        .fetch_optional(&self.pool)
        .await?
        else {
            return Ok(None);
        };

        Ok(cursor.map(|c| c.into()))
    }

    async fn is_subscriber_running(&self, key: String, worker_id: Ulid) -> anyhow::Result<bool> {
        let statement = Query::select()
            .columns([Subscriber::WorkerId, Subscriber::Enabled])
            .from(Subscriber::Table)
            .and_where(Expr::col(Subscriber::Key).eq(Expr::value(key)))
            .limit(1)
            .to_owned();

        let (sql, values) = Self::build_sqlx(statement);

        // A missing row (e.g. an operator deleted the subscriber to stop it)
        // means "not running", not an error.
        let Some((id, enabled)) =
            sqlx::query_as_with::<DB, (String, bool), _>(sqlx::AssertSqlSafe(sql.as_str()), values)
                .fetch_optional(&self.pool)
                .await?
        else {
            return Ok(false);
        };

        Ok(worker_id.to_string() == id && enabled)
    }

    async fn subscriber_status(
        &self,
        key: String,
        worker_id: Ulid,
    ) -> anyhow::Result<SubscriberStatus> {
        let statement = Query::select()
            .columns([
                Subscriber::WorkerId,
                Subscriber::Enabled,
                Subscriber::Cursor,
            ])
            .from(Subscriber::Table)
            .and_where(Expr::col(Subscriber::Key).eq(Expr::value(key)))
            .limit(1)
            .to_owned();

        let (sql, values) = Self::build_sqlx(statement);

        // A missing row (e.g. an operator deleted the subscriber to stop it)
        // means "not running", not an error.
        let Some((id, enabled, cursor)) = sqlx::query_as_with::<
            DB,
            (String, bool, Option<String>),
            _,
        >(sqlx::AssertSqlSafe(sql.as_str()), values)
        .fetch_optional(&self.pool)
        .await?
        else {
            return Ok(SubscriberStatus::default());
        };

        Ok(SubscriberStatus {
            running: worker_id.to_string() == id && enabled,
            cursor: cursor.map(Into::into),
        })
    }

    async fn latest_version(
        &self,
        aggregate_type: String,
        aggregate_id: String,
    ) -> anyhow::Result<u16> {
        // CAST so the result decodes as i64 on every dialect (Postgres
        // returns MAX(INT4) as INT4; MySQL spells the target type SIGNED).
        let max_version_expr = match DB::NAME {
            #[cfg(feature = "mysql")]
            "MySQL" => "CAST(MAX(version) AS SIGNED)",
            _ => "CAST(MAX(version) AS BIGINT)",
        };
        let statement = Query::select()
            .expr(Expr::cust(max_version_expr))
            .from(Event::Table)
            .and_where(Expr::col(Event::AggregatorType).eq(aggregate_type))
            .and_where(Expr::col(Event::AggregatorId).eq(aggregate_id))
            .to_owned();
        let (sql, values) = Self::build_sqlx(statement);
        let (last,): (Option<i64>,) =
            sqlx::query_as_with::<DB, (Option<i64>,), _>(sqlx::AssertSqlSafe(sql.as_str()), values)
                .fetch_one(&self.pool)
                .await?;

        Ok(last
            .map(|v| u16::try_from(v).unwrap_or(u16::MAX))
            .unwrap_or(0))
    }

    async fn stream_routing_key(
        &self,
        aggregate_type: String,
        aggregate_id: String,
    ) -> anyhow::Result<Option<Option<String>>> {
        // Seek on the unique `(type, id, version)` index: version 1 is the
        // stream's first event and carries the routing key the stream was
        // created with — no full event row (data/metadata blobs) needed.
        let statement = Query::select()
            .columns([Event::RoutingKey])
            .from(Event::Table)
            .and_where(Expr::col(Event::AggregatorType).eq(aggregate_type))
            .and_where(Expr::col(Event::AggregatorId).eq(aggregate_id))
            .and_where(Expr::col(Event::Version).eq(1))
            .limit(1)
            .to_owned();

        let (sql, values) = Self::build_sqlx(statement);

        let row = sqlx::query_as_with::<DB, (Option<String>,), _>(
            sqlx::AssertSqlSafe(sql.as_str()),
            values,
        )
        .fetch_optional(&self.pool)
        .await?;

        Ok(row.map(|(routing_key,)| routing_key))
    }

    async fn upsert_subscriber(&self, key: String, worker_id: Ulid) -> anyhow::Result<()> {
        let statement = Query::insert()
            .into_table(Subscriber::Table)
            .columns([Subscriber::Key, Subscriber::WorkerId, Subscriber::Lag])
            .values_panic([key.into(), worker_id.to_string().into(), 0.into()])
            .on_conflict(
                OnConflict::column(Subscriber::Key)
                    .update_columns([Subscriber::WorkerId])
                    .value(Subscriber::UpdatedAt, Expr::current_timestamp())
                    .to_owned(),
            )
            .to_owned();

        let (sql, values) = Self::build_sqlx(statement);

        sqlx::query_with::<DB, _>(sqlx::AssertSqlSafe(sql.as_str()), values)
            .execute(&self.pool)
            .await?;

        Ok(())
    }

    async fn write(&self, events: Vec<evento_core::Event>) -> Result<(), WriteError> {
        if events.is_empty() {
            return Ok(());
        }

        // Contiguity check: the unique `(type, id, version)` index only catches
        // duplicate versions, not a gap (e.g. writing version 11 when the
        // aggregate is at 5). A concurrent writer that advances the version
        // after this check can only cause the batch to collide on the unique
        // index — never to create a gap — so check + index together enforce
        // contiguity. A brand-new stream (version 1) needs no pre-check: no
        // gap is possible below version 1, and if the stream already exists
        // the unique index turns the insert into `InvalidOriginalVersion`.
        if let Some(first) = events.first() {
            if first.version != 1 {
                let last = self
                    .latest_version(first.aggregate_type.clone(), first.aggregate_id.clone())
                    .await
                    .map_err(WriteError::Unknown)?;

                if u64::from(first.version) != u64::from(last) + 1 {
                    return Err(WriteError::InvalidOriginalVersion);
                }
            }
        }

        self.insert_events(events, true).await?;

        // Wake any in-process subscriptions immediately instead of waiting for
        // their next poll tick.
        self.write_watch.send_modify(|v| *v += 1);

        Ok(())
    }

    async fn replicate(&self, events: Vec<evento_core::Event>) -> Result<(), WriteError> {
        if events.is_empty() {
            return Ok(());
        }

        // Replication layers own ordering and versioning: persist the caller's
        // timestamps verbatim and skip the contiguity pre-check.
        self.insert_events(events, false).await?;
        self.write_watch.send_modify(|v| *v += 1);

        Ok(())
    }

    fn write_watch(&self) -> Option<tokio::sync::watch::Receiver<u64>> {
        Some(self.write_watch.subscribe())
    }

    async fn stable_timestamp(&self) -> anyhow::Result<Option<u64>> {
        // Derive DB-server "now" from a cached sample plus locally elapsed
        // time instead of a round trip per call — the subscription loop calls
        // this once per pass. The sample is taken *after* the query returns,
        // so the derived clock lags the true DB clock slightly (never leads
        // it), which only makes the watermark more conservative.
        let derived = {
            let guard = self.clock_sample.lock().expect("clock sample poisoned");
            guard.and_then(|(at, db_micros)| {
                let elapsed = at.elapsed();
                (elapsed < CLOCK_SAMPLE_TTL)
                    .then(|| db_micros.saturating_add(elapsed.as_micros() as u64))
            })
        };

        let now_micros = match derived {
            Some(v) => v,
            None => {
                let statement = Query::select()
                    .expr(Expr::cust(Self::server_time_micros_expr()))
                    .to_owned();
                let (sql, values) = Self::build_sqlx(statement);
                let (now_micros,): (i64,) =
                    sqlx::query_as_with::<DB, (i64,), _>(sqlx::AssertSqlSafe(sql.as_str()), values)
                        .fetch_one(&self.pool)
                        .await?;

                let now_micros = u64::try_from(now_micros).unwrap_or(0);
                *self.clock_sample.lock().expect("clock sample poisoned") =
                    Some((Instant::now(), now_micros));
                now_micros
            }
        };

        Ok(Some(now_micros.saturating_sub(
            self.stable_margin.as_micros().min(u128::from(u64::MAX)) as u64,
        )))
    }

    async fn acknowledge(
        &self,
        key: String,
        worker_id: Ulid,
        cursor: Value,
        lag: u64,
    ) -> anyhow::Result<bool> {
        let statement = Query::update()
            .table(Subscriber::Table)
            .values([
                (Subscriber::Cursor, cursor.0.into()),
                (Subscriber::Lag, lag.into()),
                (Subscriber::UpdatedAt, Expr::current_timestamp()),
            ])
            .and_where(Expr::col(Subscriber::Key).eq(key.as_str()))
            // Fenced on the worker id: a superseded worker's ack must not
            // rewind the cursor the new owner is advancing. The enabled
            // condition makes a disabled subscriber read as "lost" too,
            // matching `is_subscriber_running`.
            .and_where(Expr::col(Subscriber::WorkerId).eq(worker_id.to_string()))
            .and_where(Expr::col(Subscriber::Enabled).eq(true))
            .to_owned();

        let (sql, values) = Self::build_sqlx(statement);

        // Zero matched rows means the fenced update was a no-op: the key is
        // owned by another worker, disabled, or gone. `affected_rows` counts
        // *matched* rows on every dialect (sqlx's MySQL connection sets
        // CLIENT_FOUND_ROWS), so re-acking an identical cursor still reports
        // ownership.
        let result = sqlx::query_with::<DB, _>(sqlx::AssertSqlSafe(sql.as_str()), values)
            .execute(&self.pool)
            .await?;

        Ok(result.affected_rows() > 0)
    }

    async fn get_snapshot(
        &self,
        aggregate_type: String,
        aggregate_revision: String,
        id: String,
    ) -> anyhow::Result<Option<(Vec<u8>, Value)>> {
        let statement = Query::select()
            .columns([Snapshot::Data, Snapshot::Cursor])
            .from(Snapshot::Table)
            .and_where(Expr::col(Snapshot::Type).eq(Expr::value(aggregate_type)))
            .and_where(Expr::col(Snapshot::Id).eq(Expr::value(id)))
            .and_where(Expr::col(Snapshot::Revision).eq(Expr::value(aggregate_revision)))
            .limit(1)
            .to_owned();

        let (sql, values) = Self::build_sqlx(statement);

        Ok(sqlx::query_as_with::<DB, (Vec<u8>, String), _>(
            sqlx::AssertSqlSafe(sql.as_str()),
            values,
        )
        .fetch_optional(&self.pool)
        .await
        .map(|res| res.map(|(data, cursor)| (data, cursor.into())))?)
    }

    async fn save_snapshot(
        &self,
        aggregate_type: String,
        aggregate_revision: String,
        id: String,
        data: Vec<u8>,
        cursor: Value,
    ) -> anyhow::Result<()> {
        let statement = Query::insert()
            .into_table(Snapshot::Table)
            .columns([
                Snapshot::Type,
                Snapshot::Id,
                Snapshot::Cursor,
                Snapshot::Revision,
                Snapshot::Data,
            ])
            .values_panic([
                aggregate_type.into(),
                id.to_string().into(),
                cursor.to_string().into(),
                aggregate_revision.into(),
                data.into(),
            ])
            .on_conflict(
                OnConflict::columns([Snapshot::Type, Snapshot::Id])
                    .update_columns([Snapshot::Data, Snapshot::Cursor, Snapshot::Revision])
                    .value(Snapshot::UpdatedAt, Expr::current_timestamp())
                    .to_owned(),
            )
            .to_owned();

        let (sql, values) = Self::build_sqlx(statement);

        sqlx::query_with::<DB, _>(sqlx::AssertSqlSafe(sql.as_str()), values)
            .execute(&self.pool)
            .await?;

        Ok(())
    }

    async fn delete_snapshot(&self, aggregate_type: String, id: String) -> anyhow::Result<()> {
        let statement = Query::delete()
            .from_table(Snapshot::Table)
            .and_where(Expr::col(Snapshot::Type).eq(Expr::value(aggregate_type)))
            .and_where(Expr::col(Snapshot::Id).eq(Expr::value(id)))
            .to_owned();

        let (sql, values) = Self::build_sqlx(statement);

        sqlx::query_with::<DB, _>(sqlx::AssertSqlSafe(sql.as_str()), values)
            .execute(&self.pool)
            .await?;

        Ok(())
    }
}

impl<DB> Sql<DB>
where
    DB: Database,
    for<'c> &'c mut DB::Connection: sqlx::Executor<'c, Database = DB>,
    sea_query_sqlx::SqlxValues: sqlx::IntoArguments<DB>,
{
    /// Inserts a batch of events; with `restamp`, `timestamp`/
    /// `timestamp_subsec` are replaced by DB-server statement time so cursor
    /// order matches commit order regardless of writer-host clocks.
    async fn insert_events(
        &self,
        events: Vec<evento_core::Event>,
        restamp: bool,
    ) -> Result<(), WriteError> {
        let mut statement = Query::insert()
            .into_table(Event::Table)
            .columns([
                Event::Id,
                Event::Name,
                Event::Data,
                Event::Metadata,
                Event::AggregatorType,
                Event::AggregatorId,
                Event::Version,
                Event::RoutingKey,
                Event::Timestamp,
                Event::TimestampSubsec,
            ])
            .to_owned();

        for event in events {
            let metadata = bitcode::encode(&event.metadata);
            let (timestamp, timestamp_subsec) = if restamp {
                Self::server_time_exprs()
            } else {
                (event.timestamp.into(), event.timestamp_subsec.into())
            };
            statement.values_panic([
                event.id.to_string().into(),
                event.name.into(),
                event.data.into(),
                metadata.into(),
                event.aggregate_type.into(),
                event.aggregate_id.into(),
                event.version.into(),
                event.routing_key.into(),
                timestamp,
                timestamp_subsec,
            ]);
        }

        let (sql, values) = Self::build_sqlx(statement);

        sqlx::query_with::<DB, _>(sqlx::AssertSqlSafe(sql.as_str()), values)
            .execute(&self.pool)
            .await
            .map_err(|err| {
                // Structured detection: driver `Display` strings are localized
                // (Postgres `lc_messages`, MySQL locale) and matching them
                // silently breaks optimistic concurrency on non-English
                // servers. The only unique constraints on `event` are the
                // primary key (random ULID, collision-free in practice) and
                // the `(type, id, version)` index, so a unique violation means
                // a version conflict.
                let is_unique = err
                    .as_database_error()
                    .is_some_and(|db_err| db_err.is_unique_violation());
                if is_unique {
                    WriteError::InvalidOriginalVersion
                } else {
                    WriteError::Unknown(err.into())
                }
            })?;

        Ok(())
    }
}

impl<D: Database> Clone for Sql<D> {
    fn clone(&self) -> Self {
        // `watch::Sender` clones share the same channel, so all clones of this
        // executor notify the same set of subscription receivers on write.
        Self {
            pool: self.pool.clone(),
            write_watch: self.write_watch.clone(),
            stable_margin: self.stable_margin,
            clock_sample: self.clock_sample.clone(),
        }
    }
}

impl<D: Database> From<Pool<D>> for Sql<D> {
    /// Builds an executor with a fresh write-wake channel and the default
    /// [`stable_margin`](Sql::stable_margin).
    ///
    /// Note: two executors built from the same pool via separate `.into()`
    /// calls do **not** share the wake channel — a subscription started on one
    /// is not woken by writes through the other and falls back to its poll
    /// interval. Build once and [`Clone`] (clones share the channel) when
    /// low-latency wakeups matter.
    fn from(value: Pool<D>) -> Self {
        Self {
            pool: value,
            write_watch: tokio::sync::watch::channel(0).0,
            stable_margin: DEFAULT_STABLE_MARGIN,
            clock_sample: Arc::new(Mutex::new(None)),
        }
    }
}

/// Query builder for reading events with cursor-based pagination.
///
/// `Reader` wraps a sea-query [`SelectStatement`] and adds support for:
/// - Forward pagination (first N after cursor)
/// - Backward pagination (last N before cursor)
/// - Ascending/descending order
///
/// # Example
///
/// ```rust,no_run
/// use evento_sql::{Event, Reader, SqlEvent};
/// use sea_query::Query;
///
/// # async fn run(pool: sqlx::SqlitePool) -> anyhow::Result<()> {
/// let statement = Query::select()
///     .columns([Event::Id, Event::Name, Event::Data])
///     .from(Event::Table)
///     .to_owned();
///
/// let result = Reader::new(statement.clone())
///     .forward(10, None)  // First 10 events
///     .execute::<_, SqlEvent, _>(&pool)
///     .await?;
///
/// for edge in &result.edges {
///     println!("Event: {:?}, Cursor: {:?}", edge.node, edge.cursor);
/// }
///
/// // Continue with next page
/// if result.page_info.has_next_page {
///     let next_result = Reader::new(statement)
///         .forward(10, result.page_info.end_cursor)
///         .execute::<_, SqlEvent, _>(&pool)
///         .await?;
/// #   let _ = next_result;
/// }
/// # Ok(())
/// # }
/// ```
///
/// # Deref
///
/// `Reader` implements `Deref` and `DerefMut` to the underlying `SelectStatement`,
/// allowing direct access to sea-query builder methods.
pub struct Reader {
    statement: SelectStatement,
    args: Args,
    order: cursor::Order,
}

impl Reader {
    /// Creates a new reader from a sea-query select statement.
    pub fn new(statement: SelectStatement) -> Self {
        Self {
            statement,
            args: Args::default(),
            order: cursor::Order::Asc,
        }
    }

    /// Sets the sort order for results.
    pub fn order(&mut self, order: cursor::Order) -> &mut Self {
        self.order = order;

        self
    }

    /// Sets descending sort order.
    pub fn desc(&mut self) -> &mut Self {
        self.order(cursor::Order::Desc)
    }

    /// Sets pagination arguments directly.
    pub fn args(&mut self, args: Args) -> &mut Self {
        self.args = args;

        self
    }

    /// Configures backward pagination (last N before cursor).
    ///
    /// # Arguments
    ///
    /// - `last` - Number of items to return
    /// - `before` - Optional cursor to paginate before
    pub fn backward(&mut self, last: u16, before: Option<Value>) -> &mut Self {
        self.args(Args {
            last: Some(last),
            before,
            ..Default::default()
        })
    }

    /// Configures forward pagination (first N after cursor).
    ///
    /// # Arguments
    ///
    /// - `first` - Number of items to return
    /// - `after` - Optional cursor to paginate after
    pub fn forward(&mut self, first: u16, after: Option<Value>) -> &mut Self {
        self.args(Args {
            first: Some(first),
            after,
            ..Default::default()
        })
    }

    /// Executes the query and returns paginated results.
    ///
    /// # Type Parameters
    ///
    /// - `DB` - The SQLx database type
    /// - `O` - The output row type (must implement `FromRow`, `Cursor`, and `Bind`)
    /// - `E` - The executor type
    ///
    /// # Returns
    ///
    /// A [`ReadResult`](evento_core::cursor::ReadResult) containing edges with nodes and cursors,
    /// plus pagination info.
    pub async fn execute<'e, 'c: 'e, DB, O, E>(
        &mut self,
        executor: E,
    ) -> anyhow::Result<ReadResult<O>>
    where
        DB: Database,
        E: 'e + sqlx::Executor<'c, Database = DB>,
        O: for<'r> sqlx::FromRow<'r, DB::Row>,
        O: Cursor,
        O: Send + Unpin,
        O: Bind<Cursor = O>,
        <<O as Bind>::I as IntoIterator>::IntoIter: DoubleEndedIterator,
        <<O as Bind>::V as IntoIterator>::IntoIter: DoubleEndedIterator,
        sea_query_sqlx::SqlxValues: sqlx::IntoArguments<DB>,
    {
        let limit = self.build_reader::<O, O>()?;

        let (sql, values) = match DB::NAME {
            #[cfg(feature = "sqlite")]
            "SQLite" => self.statement.build_sqlx(SqliteQueryBuilder),
            #[cfg(feature = "mysql")]
            "MySQL" => self.build_sqlx(MysqlQueryBuilder),
            #[cfg(feature = "postgres")]
            "PostgreSQL" => self.build_sqlx(PostgresQueryBuilder),
            name => panic!("'{name}' not supported, consider using SQLite, PostgreSQL or MySQL"),
        };

        let mut rows = sqlx::query_as_with::<DB, O, _>(sqlx::AssertSqlSafe(sql.as_str()), values)
            .fetch_all(executor)
            .await?;

        let has_more = rows.len() > limit as usize;
        if has_more {
            rows.pop();
        }

        let mut edges = vec![];
        for node in rows.into_iter() {
            edges.push(Edge {
                cursor: node.serialize_cursor()?,
                node,
            });
        }

        if self.args.is_backward() {
            edges = edges.into_iter().rev().collect();
        }

        // Both boundary cursors are always populated so a caller can reverse
        // direction from either end of a page. Only the paging direction's
        // "more" flag can be computed from the probe row; the opposite flag
        // stays `false` (unknown), per the GraphQL cursor-connection spec.
        let start_cursor = edges.first().map(|e| e.cursor.clone());
        let end_cursor = edges.last().map(|e| e.cursor.clone());
        let page_info = if self.args.is_backward() {
            PageInfo {
                has_previous_page: has_more,
                has_next_page: false,
                start_cursor,
                end_cursor,
            }
        } else {
            PageInfo {
                has_previous_page: false,
                has_next_page: has_more,
                start_cursor,
                end_cursor,
            }
        };

        Ok(ReadResult { edges, page_info })
    }

    fn build_reader<O: Cursor, B: Bind<Cursor = O>>(&mut self) -> Result<u16, cursor::CursorError>
    where
        B::T: Clone,
        <<B as Bind>::I as IntoIterator>::IntoIter: DoubleEndedIterator,
        <<B as Bind>::V as IntoIterator>::IntoIter: DoubleEndedIterator,
    {
        let (limit, cursor) = self.args.get_info();

        if let Some(cursor) = cursor.as_ref() {
            self.build_reader_where::<O, B>(cursor)?;
        }

        self.build_reader_order::<B>();
        self.limit(limit as u64 + 1);

        Ok(limit)
    }

    fn build_reader_where<O, B>(&mut self, cursor: &Value) -> Result<(), cursor::CursorError>
    where
        O: Cursor,
        B: Bind<Cursor = O>,
        B::T: Clone,
        <<B as Bind>::I as IntoIterator>::IntoIter: DoubleEndedIterator,
        <<B as Bind>::V as IntoIterator>::IntoIter: DoubleEndedIterator,
    {
        let is_order_desc = self.is_order_desc();
        let cursor = O::deserialize_cursor(cursor)?;
        let columns = B::columns().into_iter().rev();
        let values = B::values(cursor).into_iter().rev();

        let mut expr = None::<Expr>;
        for (col, value) in columns.zip(values) {
            let current_expr = if is_order_desc {
                Expr::col(col.clone()).lt(value.clone())
            } else {
                Expr::col(col.clone()).gt(value.clone())
            };

            let Some(ref prev_expr) = expr else {
                expr = Some(current_expr.clone());
                continue;
            };

            expr = Some(current_expr.or(Expr::col(col).eq(value).and(prev_expr.clone())));
        }

        // `expr` is only `None` when a `Bind` impl declares zero columns; such
        // a cursor cannot constrain anything, so leave the query unfiltered
        // rather than panicking on a public-trait misuse.
        if let Some(expr) = expr {
            self.and_where(expr);
        }

        Ok(())
    }

    fn build_reader_order<O: Bind>(&mut self) {
        let order = if self.is_order_desc() {
            sea_query::Order::Desc
        } else {
            sea_query::Order::Asc
        };

        let columns = O::columns();
        for col in columns {
            self.order_by(col, order.clone());
        }
    }

    fn is_order_desc(&self) -> bool {
        matches!(
            (&self.order, self.args.is_backward()),
            (cursor::Order::Asc, true) | (cursor::Order::Desc, false)
        )
    }
}

impl Deref for Reader {
    type Target = SelectStatement;

    fn deref(&self) -> &Self::Target {
        &self.statement
    }
}

impl DerefMut for Reader {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.statement
    }
}

/// Trait for binding cursor values in paginated queries.
///
/// This trait defines how to serialize cursor data for keyset pagination.
/// It specifies which columns are used for ordering and how to extract
/// their values from a cursor.
///
/// # Implementation
///
/// The trait is implemented for [`evento_core::Event`] to enable pagination
/// over the event table using timestamp, version, and ID columns.
///
/// # Associated Types
///
/// - `T` - Column reference type
/// - `I` - Iterator over column references
/// - `V` - Iterator over value expressions
/// - `Cursor` - The cursor type that provides pagination data
pub trait Bind {
    /// Column reference type (e.g., `Event` enum variant).
    type T: IntoColumnRef + Clone;
    /// Iterator type for columns.
    type I: IntoIterator<Item = Self::T>;
    /// Iterator type for values.
    type V: IntoIterator<Item = Expr>;
    /// The cursor type used for pagination.
    type Cursor: Cursor;

    /// Returns the columns used for cursor-based ordering.
    fn columns() -> Self::I;
    /// Extracts values from a cursor for WHERE clause construction.
    fn values(cursor: <<Self as Bind>::Cursor as Cursor>::T) -> Self::V;
}

impl evento_core::cursor::Cursor for SqlEvent {
    type T = evento_core::EventCursor;

    fn serialize(&self) -> Self::T {
        evento_core::EventCursor {
            i: self.0.id.to_string(),
            v: self.0.version,
            t: self.0.timestamp,
            s: self.0.timestamp_subsec,
        }
    }
}

impl Bind for SqlEvent {
    type T = Event;
    type I = [Self::T; 4];
    type V = [Expr; 4];
    type Cursor = Self;

    fn columns() -> Self::I {
        [
            Event::Timestamp,
            Event::TimestampSubsec,
            Event::Version,
            Event::Id,
        ]
    }

    fn values(cursor: <<Self as Bind>::Cursor as Cursor>::T) -> Self::V {
        [
            cursor.t.into(),
            cursor.s.into(),
            cursor.v.into(),
            cursor.i.into(),
        ]
    }
}

#[cfg(feature = "sqlite")]
impl From<Sqlite> for evento_core::Evento {
    fn from(value: Sqlite) -> Self {
        evento_core::Evento::new(value)
    }
}

#[cfg(feature = "sqlite")]
impl From<&Sqlite> for evento_core::Evento {
    fn from(value: &Sqlite) -> Self {
        evento_core::Evento::new(value.clone())
    }
}

#[cfg(feature = "mysql")]
impl From<MySql> for evento_core::Evento {
    fn from(value: MySql) -> Self {
        evento_core::Evento::new(value)
    }
}

#[cfg(feature = "mysql")]
impl From<&MySql> for evento_core::Evento {
    fn from(value: &MySql) -> Self {
        evento_core::Evento::new(value.clone())
    }
}

#[cfg(feature = "postgres")]
impl From<Postgres> for evento_core::Evento {
    fn from(value: Postgres) -> Self {
        evento_core::Evento::new(value)
    }
}

#[cfg(feature = "postgres")]
impl From<&Postgres> for evento_core::Evento {
    fn from(value: &Postgres) -> Self {
        evento_core::Evento::new(value.clone())
    }
}

/// Newtype over [`evento_core::Event`] carrying the `sqlx::FromRow` mapping
/// from the events table.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct SqlEvent(pub evento_core::Event);

impl<R: sqlx::Row> sqlx::FromRow<'_, R> for SqlEvent
where
    i32: sqlx::Type<R::Database> + for<'r> sqlx::Decode<'r, R::Database>,
    Vec<u8>: sqlx::Type<R::Database> + for<'r> sqlx::Decode<'r, R::Database>,
    String: sqlx::Type<R::Database> + for<'r> sqlx::Decode<'r, R::Database>,
    i64: sqlx::Type<R::Database> + for<'r> sqlx::Decode<'r, R::Database>,
    for<'r> &'r str: sqlx::Type<R::Database> + sqlx::Decode<'r, R::Database>,
    for<'r> &'r str: sqlx::ColumnIndex<R>,
{
    fn from_row(row: &R) -> Result<Self, sqlx::Error> {
        let timestamp: i64 = sqlx::Row::try_get(row, "timestamp")?;
        let timestamp_subsec: i64 = sqlx::Row::try_get(row, "timestamp_subsec")?;
        let version: i32 = sqlx::Row::try_get(row, "version")?;
        let metadata: Vec<u8> = sqlx::Row::try_get(row, "metadata")?;
        let metadata: evento_core::metadata::Metadata =
            bitcode::decode(&metadata).map_err(|e| sqlx::Error::Decode(e.into()))?;

        // Checked narrowing: an out-of-range column value (negative timestamp,
        // version above u16::MAX) is a decode error, not silent wraparound.
        let version = u16::try_from(version)
            .map_err(|_| sqlx::Error::Decode(format!("version {version} out of range").into()))?;
        let timestamp = u64::try_from(timestamp).map_err(|_| {
            sqlx::Error::Decode(format!("timestamp {timestamp} out of range").into())
        })?;
        let timestamp_subsec = u32::try_from(timestamp_subsec).map_err(|_| {
            sqlx::Error::Decode(format!("timestamp_subsec {timestamp_subsec} out of range").into())
        })?;

        Ok(SqlEvent(evento_core::Event {
            id: Ulid::from_string(sqlx::Row::try_get(row, "id")?)
                .map_err(|err| sqlx::Error::InvalidArgument(err.to_string()))?,
            aggregate_id: sqlx::Row::try_get(row, "aggregator_id")?,
            aggregate_type: sqlx::Row::try_get(row, "aggregator_type")?,
            version,
            name: sqlx::Row::try_get(row, "name")?,
            routing_key: sqlx::Row::try_get(row, "routing_key")?,
            data: sqlx::Row::try_get(row, "data")?,
            timestamp,
            timestamp_subsec,
            metadata,
        }))
    }
}