sqlx-otel 0.2.0

Thin wrapper around SQLx that emits OpenTelemetry spans and metrics following the database client semantic conventions.
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
//! Query-side annotation surface – mirror of the executor-side
//! [`with_annotations`](crate::Pool::with_annotations) /
//! [`with_operation`](crate::Pool::with_operation) methods, but attached to the query
//! builder produced by [`sqlx::query()`], [`sqlx::query_as()`], and
//! [`sqlx::query_scalar()`].
//!
//! # Choosing between executor-side and query-side
//!
//! The two surfaces emit identical telemetry; pick whichever keeps the annotation closer to
//! the thing it describes. As a rule of thumb:
//!
//! - **Query-side** is the default. Per-query attributes describe the *query*, and
//!   colocating them with the query text keeps both readable.
//! - **Executor-side** is for cases where one annotation set is reused across many queries
//!   on the same executor (e.g. a request-scoped logical operation), or where the surface
//!   is `prepare` / `describe` rather than `execute` / `fetch*`.
//!
//! ```no_run
//! # #[cfg(feature = "sqlite")]
//! # async fn _doc() -> Result<(), sqlx::Error> {
//! # use sqlx_otel::PoolBuilder;
//! use sqlx_otel::{QueryAnnotateExt, QueryAnnotations};
//! # let pool = PoolBuilder::from(sqlx::SqlitePool::connect(":memory:").await?).build();
//!
//! sqlx::query("SELECT * FROM users WHERE id = ?")
//!     .bind(42_i64)
//!     .with_annotations(
//!         QueryAnnotations::new()
//!             .operation("SELECT")
//!             .collection("users"),
//!     )
//!     .execute(&pool)
//!     .await?;
//! # Ok(())
//! # }
//! ```
//!
//! The wrapper [`AnnotatedQuery`] exposes the same `execute`/`fetch*` surface as the inner
//! query and threads the annotations into span creation by wrapping the executor with the
//! existing [`Annotated`](Annotated) / [`AnnotatedMut`](AnnotatedMut) executor wrappers.
//!
//! # Map and macro queries
//!
//! [`Query::map`](Query::map) / [`Query::try_map`](Query::try_map) return
//! [`sqlx::query::Map<'q, DB, F, A>`](sqlx::query::Map), which is also covered by the
//! trait. `with_annotations` and `with_operation` may be applied at any of the three
//! positions on a hand-written `Query::map()` chain – before `bind`, between `bind` and
//! `map`, or after `map`:
//!
//! ```no_run
//! # #[cfg(feature = "sqlite")]
//! # async fn _doc() -> Result<(), sqlx::Error> {
//! # use sqlx_otel::PoolBuilder;
//! use sqlx::Row as _;
//! use sqlx_otel::QueryAnnotateExt;
//! # let pool = PoolBuilder::from(sqlx::SqlitePool::connect(":memory:").await?).build();
//!
//! sqlx::query("SELECT id FROM users WHERE name = ?")
//!     .bind("alice")
//!     .map(|row: sqlx::sqlite::SqliteRow| row.get::<i64, _>("id"))
//!     .with_operation("SELECT", "users")
//!     .fetch_one(&pool)
//!     .await?;
//! # Ok(())
//! # }
//! ```
//!
//! The compile-time validated macro forms (`sqlx::query!()`, `sqlx::query_as!()`,
//! `sqlx::query_scalar!()`) expand to either `Query<'q, DB, _>` (for no-result-column
//! shapes) or `Map<'q, DB, _, _>` (for any shape that decodes columns). Both are covered:
//!
//! ```ignore
//! sqlx::query_as!(User, "SELECT id, name FROM users WHERE id = ?", 42_i64)
//!     .with_operation("SELECT", "users")
//!     .fetch_one(&pool)
//!     .await?;
//! ```
//!
//! Macro queries can only carry annotations *after* the macro returns – the macro itself
//! pre-applies `bind` and `try_map`, so positions 1 and 2 are not reachable by the user.
//! The macro example above stays `ignore` because it requires either an offline `.sqlx/`
//! cache or a live `DATABASE_URL`.

use futures::stream::BoxStream;
use sqlx::query::{Map, Query, QueryAs, QueryScalar};

use crate::annotations::{Annotated, AnnotatedMut, QueryAnnotations};
use crate::database::Database;

/// Sealing module for [`QueryAnnotateExt`]. Downstream crates cannot implement the trait
/// because the wrapper types it produces (`Annotated` / `AnnotatedMut`) have private fields
/// that only this crate can construct.
mod sealed {
    /// Marker trait that prevents external impls of [`super::QueryAnnotateExt`].
    pub trait Sealed {}
}

/// Extension trait that attaches OpenTelemetry per-query annotations to the function-form
/// `SQLx` query builders ([`sqlx::query()`], [`sqlx::query_as()`],
/// [`sqlx::query_scalar()`]) and to the [`Map`](sqlx::query::Map) returned by
/// `Query::map` / `Query::try_map`.
///
/// The trait is sealed and cannot be implemented downstream.
///
/// # Example
///
/// ```no_run
/// # #[cfg(feature = "sqlite")]
/// # async fn _doc() -> Result<(), sqlx::Error> {
/// # use sqlx_otel::PoolBuilder;
/// use sqlx_otel::QueryAnnotateExt;
/// # let pool = PoolBuilder::from(sqlx::SqlitePool::connect(":memory:").await?).build();
///
/// sqlx::query("INSERT INTO orders (user_id) VALUES (?)")
///     .bind(42_i64)
///     .with_operation("INSERT", "orders")
///     .execute(&pool)
///     .await?;
/// # Ok(())
/// # }
/// ```
pub trait QueryAnnotateExt: sealed::Sealed + Sized {
    /// Wrap the query with the given per-query annotations.
    ///
    /// Returns an [`AnnotatedQuery`] that exposes the same `execute`/`fetch*` surface as the
    /// inner query but threads the annotations through to OpenTelemetry span creation.
    fn with_annotations(self, annotations: QueryAnnotations) -> AnnotatedQuery<Self>;

    /// Shorthand that sets `db.operation.name` and `db.collection.name`.
    ///
    /// Equivalent to
    /// `self.with_annotations(QueryAnnotations::new().operation(op).collection(coll))`.
    fn with_operation(
        self,
        operation: impl Into<String>,
        collection: impl Into<String>,
    ) -> AnnotatedQuery<Self> {
        self.with_annotations(
            QueryAnnotations::new()
                .operation(operation)
                .collection(collection),
        )
    }
}

impl<DB: sqlx::Database, A> sealed::Sealed for Query<'_, DB, A> {}
impl<DB: sqlx::Database, A> QueryAnnotateExt for Query<'_, DB, A> {
    fn with_annotations(self, annotations: QueryAnnotations) -> AnnotatedQuery<Self> {
        AnnotatedQuery {
            inner: self,
            annotations,
        }
    }
}

impl<DB: sqlx::Database, O, A> sealed::Sealed for QueryAs<'_, DB, O, A> {}
impl<DB: sqlx::Database, O, A> QueryAnnotateExt for QueryAs<'_, DB, O, A> {
    fn with_annotations(self, annotations: QueryAnnotations) -> AnnotatedQuery<Self> {
        AnnotatedQuery {
            inner: self,
            annotations,
        }
    }
}

impl<DB: sqlx::Database, O, A> sealed::Sealed for QueryScalar<'_, DB, O, A> {}
impl<DB: sqlx::Database, O, A> QueryAnnotateExt for QueryScalar<'_, DB, O, A> {
    fn with_annotations(self, annotations: QueryAnnotations) -> AnnotatedQuery<Self> {
        AnnotatedQuery {
            inner: self,
            annotations,
        }
    }
}

impl<DB: sqlx::Database, F, A> sealed::Sealed for Map<'_, DB, F, A> {}
impl<DB: sqlx::Database, F, A> QueryAnnotateExt for Map<'_, DB, F, A> {
    fn with_annotations(self, annotations: QueryAnnotations) -> AnnotatedQuery<Self> {
        AnnotatedQuery {
            inner: self,
            annotations,
        }
    }
}

/// A `SQLx` query builder paired with OpenTelemetry per-query annotations.
///
/// Produced by [`QueryAnnotateExt::with_annotations`] / [`QueryAnnotateExt::with_operation`].
/// Each invocation of the executor-driven methods (`execute`, `fetch_all`, etc.) wraps the
/// executor with the annotations so the resulting span carries them.
///
/// The wrapper exposes [`bind`](AnnotatedQuery::bind) so the caller can chain
/// `.bind(...).with_annotations(...)` or `.with_annotations(...).bind(...)` interchangeably.
#[must_use = "annotated queries do nothing until you call execute / fetch* on them"]
pub struct AnnotatedQuery<Q> {
    inner: Q,
    annotations: QueryAnnotations,
}

impl<Q> std::fmt::Debug for AnnotatedQuery<Q> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AnnotatedQuery")
            .field("annotations", &self.annotations)
            .finish_non_exhaustive()
    }
}

// ---------------------------------------------------------------------------
// Internal trait: convert an executor argument into the existing Annotated /
// AnnotatedMut wrappers. Implemented for the same three borrow shapes that the
// `impl_executor!` macro covers.
// ---------------------------------------------------------------------------

/// Internal conversion trait. Implemented for the three executor borrow shapes that
/// `impl_executor!` already supports (`&Pool`, `&mut PoolConnection`, `&mut Transaction`),
/// each producing the matching [`Annotated`] / [`AnnotatedMut`] wrapper.
///
/// The HRTB `for<'a> &'a mut DB::Connection: sqlx::Executor<'a, Database = DB>` lives on
/// each individual impl rather than on the trait, to avoid trait-resolution recursion when
/// the user's executor type is itself constructed via the same HRTB.
///
/// Users do not call this trait directly – they pass `&pool`, `&mut conn`, or `&mut tx` to
/// [`AnnotatedQuery::execute`] / `fetch*` and the trait dispatches internally. The trait is
/// effectively sealed because only this crate can construct the [`Annotated`] /
/// [`AnnotatedMut`] wrapper types (their fields are crate-private).
#[doc(hidden)]
pub trait IntoAnnotatedExecutor<'e, DB: Database> {
    /// The annotated executor wrapper type produced by [`into_annotated`](Self::into_annotated).
    type Wrapper: sqlx::Executor<'e, Database = DB>;

    /// Consume `self` together with the per-query annotations to produce the wrapper.
    fn into_annotated(self, annotations: QueryAnnotations) -> Self::Wrapper;
}

impl<'e, DB> IntoAnnotatedExecutor<'e, DB> for &'e crate::Pool<DB>
where
    DB: Database,
    for<'a> &'a mut DB::Connection: sqlx::Executor<'a, Database = DB>,
{
    type Wrapper = Annotated<'e, crate::Pool<DB>>;

    fn into_annotated(self, annotations: QueryAnnotations) -> Self::Wrapper {
        Annotated {
            inner: self,
            annotations,
            state: self.state.clone(),
        }
    }
}

impl<'e, DB> IntoAnnotatedExecutor<'e, DB> for &'e mut crate::PoolConnection<DB>
where
    DB: Database,
    for<'a> &'a mut DB::Connection: sqlx::Executor<'a, Database = DB>,
{
    type Wrapper = AnnotatedMut<'e, crate::PoolConnection<DB>>;

    fn into_annotated(self, annotations: QueryAnnotations) -> Self::Wrapper {
        AnnotatedMut {
            state: self.state.clone(),
            annotations,
            inner: self,
        }
    }
}

impl<'e, 'tx, DB> IntoAnnotatedExecutor<'e, DB> for &'e mut crate::Transaction<'tx, DB>
where
    DB: Database,
    for<'a> &'a mut DB::Connection: sqlx::Executor<'a, Database = DB>,
{
    type Wrapper = AnnotatedMut<'e, crate::Transaction<'tx, DB>>;

    fn into_annotated(self, annotations: QueryAnnotations) -> Self::Wrapper {
        AnnotatedMut {
            state: self.state.clone(),
            annotations,
            inner: self,
        }
    }
}

// ---------------------------------------------------------------------------
// Forwarder macros: emit method bodies inside a caller-supplied `impl` block.
//
// The macros expand to method items only (no `impl` header), so each call site
// declares its own generics and where-clauses. This avoids the local-ambiguity
// that arises when the macro tries to consume an `impl<...>` header via `tt`
// repetition, while still removing the per-builder duplication.
//
// Symbols referenced in the bodies (`Self`, `DB`, `A`, `O`, `QueryAnnotations`,
// `IntoAnnotatedExecutor`, `BoxStream`) resolve in the expansion context.
// ---------------------------------------------------------------------------

/// Emit the methods that every `AnnotatedQuery<Inner>` impl block has in common:
/// `with_annotations`, `with_operation`, and the five `fetch*` forwarders.
///
/// Parameters:
/// * `row` – the row type of the inner builder (`DB::Row` for `Query`, `O` for
///   `QueryAs` and `QueryScalar`).
/// * `extra_bounds` – additional lifetime bounds threaded onto each fetch-forwarder's
///   where-clause (`DB: 'e, O: 'e,` for `QueryAs` / `QueryScalar`; empty for `Query`).
macro_rules! impl_annotated_query_fetch_forwarders {
    (row = $row:ty, extra_bounds = ($($extra_bounds:tt)*)) => {
        /// Replace the wrapper's annotations. Last-call-wins – any previously set annotations
        /// on this wrapper are discarded.
        pub fn with_annotations(mut self, annotations: QueryAnnotations) -> Self {
            self.annotations = annotations;
            self
        }

        /// Shorthand replacement that sets `db.operation.name` and `db.collection.name`.
        /// Discards any previously set annotations on this wrapper.
        pub fn with_operation(
            self,
            operation: impl Into<String>,
            collection: impl Into<String>,
        ) -> Self {
            self.with_annotations(
                QueryAnnotations::new()
                    .operation(operation)
                    .collection(collection),
            )
        }

        /// Stream the resulting rows.
        pub fn fetch<'e, E>(self, executor: E) -> BoxStream<'e, Result<$row, sqlx::Error>>
        where
            'q: 'e,
            A: 'e,
            $($extra_bounds)*
            E: 'e + IntoAnnotatedExecutor<'e, DB>,
        {
            let wrapper = executor.into_annotated(self.annotations);
            self.inner.fetch(wrapper)
        }

        /// Stream a mix of `QueryResult`s and rows for multi-statement queries.
        ///
        /// `fetch_many` is `#[deprecated]` in `SQLx` 0.8 but kept for parity with the
        /// executor-side surface.
        #[allow(deprecated, clippy::type_complexity)]
        pub fn fetch_many<'e, E>(
            self,
            executor: E,
        ) -> BoxStream<'e, Result<sqlx::Either<DB::QueryResult, $row>, sqlx::Error>>
        where
            'q: 'e,
            A: 'e,
            $($extra_bounds)*
            E: 'e + IntoAnnotatedExecutor<'e, DB>,
        {
            let wrapper = executor.into_annotated(self.annotations);
            self.inner.fetch_many(wrapper)
        }

        /// Collect every row into a `Vec`.
        ///
        /// # Errors
        ///
        /// Returns any [`sqlx::Error`] surfaced by the underlying driver, including row
        /// decoding errors.
        pub async fn fetch_all<'e, E>(self, executor: E) -> Result<Vec<$row>, sqlx::Error>
        where
            'q: 'e,
            A: 'e,
            $($extra_bounds)*
            E: 'e + IntoAnnotatedExecutor<'e, DB>,
        {
            let wrapper = executor.into_annotated(self.annotations);
            self.inner.fetch_all(wrapper).await
        }

        /// Return exactly one row, erroring if none or more than one.
        ///
        /// # Errors
        ///
        /// Returns [`sqlx::Error::RowNotFound`] when the result set is empty, or any other
        /// [`sqlx::Error`] surfaced by the underlying driver.
        pub async fn fetch_one<'e, E>(self, executor: E) -> Result<$row, sqlx::Error>
        where
            'q: 'e,
            A: 'e,
            $($extra_bounds)*
            E: 'e + IntoAnnotatedExecutor<'e, DB>,
        {
            let wrapper = executor.into_annotated(self.annotations);
            self.inner.fetch_one(wrapper).await
        }

        /// Return at most one row.
        ///
        /// # Errors
        ///
        /// Returns any [`sqlx::Error`] surfaced by the underlying driver.
        pub async fn fetch_optional<'e, E>(
            self,
            executor: E,
        ) -> Result<Option<$row>, sqlx::Error>
        where
            'q: 'e,
            A: 'e,
            $($extra_bounds)*
            E: 'e + IntoAnnotatedExecutor<'e, DB>,
        {
            let wrapper = executor.into_annotated(self.annotations);
            self.inner.fetch_optional(wrapper).await
        }
    };
}

/// Emit the `bind` method on a specialised impl block.
///
/// `SQLx` restricts `bind` to the default `<DB as Database>::Arguments<'q>` parameter, so
/// each builder family has its own dedicated impl block keyed on the default arguments.
macro_rules! impl_annotated_query_bind {
    () => {
        /// Append a parameter binding, forwarding to the inner query. Mirrors the inner
        /// builder's own `bind` method.
        pub fn bind<T>(mut self, value: T) -> Self
        where
            T: 'q + sqlx::Encode<'q, DB> + sqlx::Type<DB>,
        {
            self.inner = self.inner.bind(value);
            self
        }
    };
}

// --- AnnotatedQuery<Query<'q, DB, A>> --------------------------------------

impl<'q, DB, A> AnnotatedQuery<Query<'q, DB, A>>
where
    DB: Database,
    A: 'q + Send + sqlx::IntoArguments<'q, DB>,
{
    impl_annotated_query_fetch_forwarders!(row = DB::Row, extra_bounds = ());

    /// Execute the query and return the number of rows affected. Wraps the executor with
    /// the carried annotations so the resulting span is annotated.
    ///
    /// # Errors
    ///
    /// Returns any [`sqlx::Error`] surfaced by the underlying driver.
    pub async fn execute<'e, E>(self, executor: E) -> Result<DB::QueryResult, sqlx::Error>
    where
        'q: 'e,
        A: 'e,
        E: 'e + IntoAnnotatedExecutor<'e, DB>,
    {
        let wrapper = executor.into_annotated(self.annotations);
        self.inner.execute(wrapper).await
    }

    /// Execute multiple statements separated by `;` and return their results as a stream.
    ///
    /// `execute_many` is `#[deprecated]` in `SQLx` 0.8 but kept here for parity with the
    /// existing executor-side surface. Only `Query` exposes this method – `QueryAs`,
    /// `QueryScalar`, and `Map` have no `execute_many` upstream.
    #[allow(deprecated)]
    pub async fn execute_many<'e, E>(
        self,
        executor: E,
    ) -> BoxStream<'e, Result<DB::QueryResult, sqlx::Error>>
    where
        'q: 'e,
        A: 'e,
        E: 'e + IntoAnnotatedExecutor<'e, DB>,
    {
        let wrapper = executor.into_annotated(self.annotations);
        self.inner.execute_many(wrapper).await
    }

    /// Map each row to another type. Mirrors [`sqlx::query::Query::map`] and carries the
    /// existing annotations forward unchanged onto the resulting `AnnotatedQuery<Map<...>>`,
    /// so `with_annotations` can be applied either before or after `.map()`.
    #[allow(clippy::type_complexity)]
    pub fn map<F, O>(
        self,
        f: F,
    ) -> AnnotatedQuery<Map<'q, DB, impl FnMut(DB::Row) -> Result<O, sqlx::Error> + Send, A>>
    where
        F: FnMut(DB::Row) -> O + Send,
        O: Unpin,
    {
        AnnotatedQuery {
            inner: self.inner.map(f),
            annotations: self.annotations,
        }
    }

    /// Map each row to a `Result`. Mirrors [`sqlx::query::Query::try_map`] and carries the
    /// existing annotations forward unchanged.
    pub fn try_map<F, O>(self, f: F) -> AnnotatedQuery<Map<'q, DB, F, A>>
    where
        F: FnMut(DB::Row) -> Result<O, sqlx::Error> + Send,
        O: Unpin,
    {
        AnnotatedQuery {
            inner: self.inner.try_map(f),
            annotations: self.annotations,
        }
    }
}

impl<'q, DB> AnnotatedQuery<Query<'q, DB, <DB as sqlx::Database>::Arguments<'q>>>
where
    DB: sqlx::Database,
{
    impl_annotated_query_bind!();
}

// --- AnnotatedQuery<QueryAs<'q, DB, O, A>> ---------------------------------

impl<'q, DB, O, A> AnnotatedQuery<QueryAs<'q, DB, O, A>>
where
    DB: Database,
    A: 'q + Send + sqlx::IntoArguments<'q, DB>,
    O: Send + Unpin + for<'r> sqlx::FromRow<'r, DB::Row>,
{
    impl_annotated_query_fetch_forwarders!(row = O, extra_bounds = (DB: 'e, O: 'e,));
}

impl<'q, DB, O> AnnotatedQuery<QueryAs<'q, DB, O, <DB as sqlx::Database>::Arguments<'q>>>
where
    DB: sqlx::Database,
{
    impl_annotated_query_bind!();
}

// --- AnnotatedQuery<QueryScalar<'q, DB, O, A>> -----------------------------

impl<'q, DB, O, A> AnnotatedQuery<QueryScalar<'q, DB, O, A>>
where
    DB: Database,
    A: 'q + Send + sqlx::IntoArguments<'q, DB>,
    O: Send + Unpin,
    (O,): Send + Unpin + for<'r> sqlx::FromRow<'r, DB::Row>,
{
    impl_annotated_query_fetch_forwarders!(row = O, extra_bounds = (DB: 'e, O: 'e,));
}

impl<'q, DB, O> AnnotatedQuery<QueryScalar<'q, DB, O, <DB as sqlx::Database>::Arguments<'q>>>
where
    DB: sqlx::Database,
{
    impl_annotated_query_bind!();
}

// --- AnnotatedQuery<Map<'q, DB, F, A>> -------------------------------------

impl<'q, DB, F, A, O> AnnotatedQuery<Map<'q, DB, F, A>>
where
    DB: Database,
    F: FnMut(DB::Row) -> Result<O, sqlx::Error> + Send,
    O: Send + Unpin,
    A: 'q + Send + sqlx::IntoArguments<'q, DB>,
{
    impl_annotated_query_fetch_forwarders!(row = O, extra_bounds = (DB: 'e, F: 'e, O: 'e,));

    /// Compose a further mapping on top of this annotated map. Mirrors
    /// [`sqlx::query::Map::map`] (which itself composes via `f(row).and_then(&mut g)`) and
    /// preserves the existing annotations on the wrapper.
    #[allow(clippy::type_complexity)]
    pub fn map<G, P>(
        self,
        g: G,
    ) -> AnnotatedQuery<Map<'q, DB, impl FnMut(DB::Row) -> Result<P, sqlx::Error> + Send, A>>
    where
        G: FnMut(O) -> P + Send,
        P: Unpin,
    {
        AnnotatedQuery {
            inner: self.inner.map(g),
            annotations: self.annotations,
        }
    }

    /// Fallible variant of [`map`](Self::map). Mirrors [`sqlx::query::Map::try_map`] and
    /// preserves the existing annotations on the wrapper.
    #[allow(clippy::type_complexity)]
    pub fn try_map<G, P>(
        self,
        g: G,
    ) -> AnnotatedQuery<Map<'q, DB, impl FnMut(DB::Row) -> Result<P, sqlx::Error> + Send, A>>
    where
        G: FnMut(O) -> Result<P, sqlx::Error> + Send,
        P: Unpin,
    {
        AnnotatedQuery {
            inner: self.inner.try_map(g),
            annotations: self.annotations,
        }
    }
}

#[cfg(all(test, feature = "sqlite"))]
mod tests {
    use sqlx::Execute as _;
    use sqlx::Sqlite;

    use super::*;

    // --- query() / query_as() / query_scalar() --------------------

    #[test]
    fn with_annotations_replaces_previous() {
        let q = sqlx::query::<Sqlite>("SELECT 1")
            .with_annotations(QueryAnnotations::new().operation("FIRST"))
            .with_annotations(QueryAnnotations::new().operation("SECOND"));
        assert_eq!(q.annotations.operation.as_deref(), Some("SECOND"));
    }

    #[test]
    fn with_operation_sets_both_fields() {
        let q = sqlx::query::<Sqlite>("SELECT 1").with_operation("SELECT", "users");
        assert_eq!(q.annotations.operation.as_deref(), Some("SELECT"));
        assert_eq!(q.annotations.collection.as_deref(), Some("users"));
        assert!(q.annotations.query_summary.is_none());
        assert!(q.annotations.stored_procedure.is_none());
    }

    #[test]
    fn with_operation_replaces_previous_annotations() {
        // Documents the last-call-wins behaviour: with_operation discards earlier fields.
        let q = sqlx::query::<Sqlite>("SELECT 1")
            .with_annotations(QueryAnnotations::new().query_summary("legacy summary"))
            .with_operation("SELECT", "users");
        assert!(
            q.annotations.query_summary.is_none(),
            "with_operation must replace, not merge"
        );
    }

    #[test]
    fn debug_impl_includes_annotations() {
        let q = sqlx::query::<Sqlite>("SELECT 1")
            .with_annotations(QueryAnnotations::new().operation("DEBUG_OP"));
        let debug = format!("{q:?}");
        assert!(debug.contains("AnnotatedQuery"));
        assert!(debug.contains("DEBUG_OP"));
    }

    #[test]
    fn bind_then_with_annotations_compose() {
        let q = sqlx::query::<Sqlite>("SELECT ?1, ?2")
            .bind(1_i32)
            .with_annotations(QueryAnnotations::new().operation("SELECT"));
        assert_eq!(q.inner.sql(), "SELECT ?1, ?2");
        assert_eq!(q.annotations.operation.as_deref(), Some("SELECT"));
    }

    #[test]
    fn with_annotations_first_then_bind_compose() {
        let q = sqlx::query::<Sqlite>("SELECT ?1, ?2")
            .with_annotations(QueryAnnotations::new().operation("SELECT"))
            .bind(1_i32);
        assert_eq!(q.inner.sql(), "SELECT ?1, ?2");
        assert_eq!(q.annotations.operation.as_deref(), Some("SELECT"));
    }

    #[test]
    fn query_as_supports_with_annotations() {
        let q = sqlx::query_as::<Sqlite, (i32,)>("SELECT 1").with_operation("SELECT", "users");
        assert_eq!(q.annotations.operation.as_deref(), Some("SELECT"));
        assert_eq!(q.annotations.collection.as_deref(), Some("users"));
    }

    #[test]
    fn query_as_wrapper_with_annotations_replaces() {
        // Exercise the inherent `with_annotations` on AnnotatedQuery<QueryAs<_>>.
        let q = sqlx::query_as::<Sqlite, (i32,)>("SELECT 1")
            .with_annotations(QueryAnnotations::new().operation("FIRST"))
            .with_annotations(QueryAnnotations::new().operation("SECOND"));
        assert_eq!(q.annotations.operation.as_deref(), Some("SECOND"));
    }

    #[test]
    fn query_as_wrapper_with_operation_chains_on_wrapper() {
        // First call uses the trait method (on QueryAs); second uses the inherent method
        // on AnnotatedQuery, which exercises the wrapper-level shorthand.
        let q = sqlx::query_as::<Sqlite, (i32,)>("SELECT 1")
            .with_annotations(QueryAnnotations::new().query_summary("legacy"))
            .with_operation("SELECT", "users");
        assert_eq!(q.annotations.operation.as_deref(), Some("SELECT"));
        assert_eq!(q.annotations.collection.as_deref(), Some("users"));
        assert!(q.annotations.query_summary.is_none());
    }

    #[test]
    fn query_as_wrapper_bind_after_annotations() {
        let q = sqlx::query_as::<Sqlite, (i32,)>("SELECT ?1")
            .with_annotations(QueryAnnotations::new().operation("SELECT"))
            .bind(7_i32);
        assert_eq!(q.inner.sql(), "SELECT ?1");
        assert_eq!(q.annotations.operation.as_deref(), Some("SELECT"));
    }

    #[test]
    fn query_scalar_supports_with_annotations() {
        let q = sqlx::query_scalar::<Sqlite, i32>("SELECT 1").with_operation("SELECT", "users");
        assert_eq!(q.annotations.operation.as_deref(), Some("SELECT"));
        assert_eq!(q.annotations.collection.as_deref(), Some("users"));
    }

    #[test]
    fn query_scalar_wrapper_with_annotations_replaces() {
        let q = sqlx::query_scalar::<Sqlite, i32>("SELECT 1")
            .with_annotations(QueryAnnotations::new().operation("FIRST"))
            .with_annotations(QueryAnnotations::new().operation("SECOND"));
        assert_eq!(q.annotations.operation.as_deref(), Some("SECOND"));
    }

    #[test]
    fn query_scalar_wrapper_with_operation_chains_on_wrapper() {
        let q = sqlx::query_scalar::<Sqlite, i32>("SELECT 1")
            .with_annotations(QueryAnnotations::new().query_summary("legacy"))
            .with_operation("SELECT", "users");
        assert_eq!(q.annotations.operation.as_deref(), Some("SELECT"));
        assert_eq!(q.annotations.collection.as_deref(), Some("users"));
        assert!(q.annotations.query_summary.is_none());
    }

    #[test]
    fn query_scalar_wrapper_bind_after_annotations() {
        let q = sqlx::query_scalar::<Sqlite, i32>("SELECT ?1")
            .with_annotations(QueryAnnotations::new().operation("SELECT"))
            .bind(7_i32);
        assert_eq!(q.inner.sql(), "SELECT ?1");
        assert_eq!(q.annotations.operation.as_deref(), Some("SELECT"));
    }

    // --- Map / Query::map / Query::try_map composition --------------------

    #[test]
    fn query_with_annotations_map_preserves_annotations() {
        // Position 1: annotate before `.map()`. Annotations must survive the wrap.
        let q = sqlx::query::<Sqlite>("SELECT 1")
            .with_annotations(QueryAnnotations::new().operation("SELECT"))
            .map(|_row: sqlx::sqlite::SqliteRow| 42_i64);
        assert_eq!(q.annotations.operation.as_deref(), Some("SELECT"));
    }

    #[test]
    fn query_bind_with_annotations_map_preserves_annotations() {
        // Position 2: annotate between `.bind()` and `.map()`.
        let q = sqlx::query::<Sqlite>("SELECT ?1")
            .bind(1_i32)
            .with_annotations(QueryAnnotations::new().operation("SELECT"))
            .map(|_row: sqlx::sqlite::SqliteRow| 42_i64);
        assert_eq!(q.annotations.operation.as_deref(), Some("SELECT"));
    }

    #[test]
    fn query_map_with_annotations_replaces_previous() {
        // Position 3 + last-call-wins on the new `Map` wrapper.
        let q = sqlx::query::<Sqlite>("SELECT 1")
            .map(|_row: sqlx::sqlite::SqliteRow| 42_i64)
            .with_annotations(QueryAnnotations::new().operation("FIRST"))
            .with_annotations(QueryAnnotations::new().operation("SECOND"));
        assert_eq!(q.annotations.operation.as_deref(), Some("SECOND"));
    }

    #[test]
    fn query_try_map_with_annotations_compose() {
        // `try_map` stores `F` directly; sanity-check the non-opaque closure branch.
        let q = sqlx::query::<Sqlite>("SELECT 1")
            .try_map(|_row: sqlx::sqlite::SqliteRow| Ok::<_, sqlx::Error>(42_i64))
            .with_operation("SELECT", "users");
        assert_eq!(q.annotations.operation.as_deref(), Some("SELECT"));
        assert_eq!(q.annotations.collection.as_deref(), Some("users"));
    }

    #[test]
    fn annotated_query_try_map_preserves_annotations() {
        // Exercise `AnnotatedQuery<Query>::try_map` (the wrapper's own method, not sqlx's).
        // Position-1-then-fallible-mapper.
        let q = sqlx::query::<Sqlite>("SELECT 1")
            .with_annotations(QueryAnnotations::new().operation("SELECT"))
            .try_map(|_row: sqlx::sqlite::SqliteRow| Ok::<_, sqlx::Error>(42_i64));
        assert_eq!(q.annotations.operation.as_deref(), Some("SELECT"));
    }

    #[test]
    fn map_compose_after_annotations() {
        // Multi-map composition: annotations survive across two `.map()` calls.
        let q = sqlx::query::<Sqlite>("SELECT 1")
            .with_annotations(QueryAnnotations::new().operation("SELECT"))
            .map(|_row: sqlx::sqlite::SqliteRow| 1_i64)
            .map(|n| n + 1);
        assert_eq!(q.annotations.operation.as_deref(), Some("SELECT"));
    }

    #[test]
    fn map_then_with_operation_replaces_via_wrapper() {
        // `with_operation` shorthand on the new `AnnotatedQuery<Map<...>>` wrapper.
        let q = sqlx::query::<Sqlite>("SELECT 1")
            .map(|_row: sqlx::sqlite::SqliteRow| 1_i64)
            .with_annotations(QueryAnnotations::new().query_summary("legacy"))
            .with_operation("SELECT", "users");
        assert_eq!(q.annotations.operation.as_deref(), Some("SELECT"));
        assert_eq!(q.annotations.collection.as_deref(), Some("users"));
        assert!(q.annotations.query_summary.is_none());
    }

    #[test]
    fn debug_impl_for_annotated_map_includes_annotations() {
        // The manual `Debug` impl on the new `Map` wrapper still prints annotations.
        let q = sqlx::query::<Sqlite>("SELECT 1")
            .map(|_row: sqlx::sqlite::SqliteRow| 1_i64)
            .with_annotations(QueryAnnotations::new().operation("DEBUG_MAP"));
        let debug = format!("{q:?}");
        assert!(debug.contains("AnnotatedQuery"));
        assert!(debug.contains("DEBUG_MAP"));
    }
}