dogdata_sqlx/
query_builder.rs

1use sqlx::{Database, Executor};
2
3use crate::execute::{InstrumentedExecute, InstrumentedFetch, InstrumentedPool};
4
5pub struct InstrumentedQueryBuilder<Q> {
6    pub query: Q,
7    pub sql: &'static str,
8}
9
10impl<Q> InstrumentedQueryBuilder<Q> {
11    pub async fn execute<P, DB>(self, pool: &P) -> Result<DB::QueryResult, sqlx::Error>
12    where
13        P: InstrumentedPool<Database = DB> + Send + Sync,
14        DB: Database,
15        Q: InstrumentedExecute<'static, DB>,
16        for<'c> &'c P: Executor<'c, Database = DB>,
17    {
18        self.query.execute_instrumented(pool, self.sql).await
19    }
20
21    pub async fn fetch_one<P, DB>(self, pool: &P) -> Result<Q::Output, sqlx::Error>
22    where
23        P: InstrumentedPool<Database = DB> + Send + Sync,
24        DB: Database,
25        Q: InstrumentedFetch<'static, DB>,
26        for<'c> &'c P: Executor<'c, Database = DB>,
27    {
28        self.query.fetch_one_instrumented(pool, self.sql).await
29    }
30
31    pub async fn fetch_optional<P, DB>(self, pool: &P) -> Result<Option<Q::Output>, sqlx::Error>
32    where
33        P: InstrumentedPool<Database = DB> + Send + Sync,
34        DB: Database,
35        Q: InstrumentedFetch<'static, DB>,
36        for<'c> &'c P: Executor<'c, Database = DB>,
37    {
38        self.query.fetch_optional_instrumented(pool, self.sql).await
39    }
40
41    pub async fn fetch_all<P, DB>(self, pool: &P) -> Result<Vec<Q::Output>, sqlx::Error>
42    where
43        P: InstrumentedPool<Database = DB> + Send + Sync,
44        DB: Database,
45        Q: InstrumentedFetch<'static, DB>,
46        for<'c> &'c P: Executor<'c, Database = DB>,
47    {
48        self.query.fetch_all_instrumented(pool, self.sql).await
49    }
50}