easy_sql/database_structs/
connection.rs1use std::{
2 fmt::Debug,
3 ops::{Deref, DerefMut},
4};
5
6use easy_macros::always_context;
7
8use sqlx::{Database, Executor};
9
10use crate::{
11 Driver, EasyExecutor,
12 traits::{DriverConnection, InternalDriver, SetupSql},
13};
14#[derive(Debug)]
18pub struct Connection<D: Driver> {
19 internal: sqlx::pool::PoolConnection<InternalDriver<D>>,
20}
21
22#[always_context]
23impl<D: Driver> Connection<D> {
24 pub fn new(conn: sqlx::pool::PoolConnection<InternalDriver<D>>) -> Self {
25 Connection { internal: conn }
26 }
27}
28
29#[always_context]
30impl<D: Driver> EasyExecutor<D> for &mut Connection<D>
31where
32 for<'b> &'b mut DriverConnection<D>: Executor<'b, Database = D::InternalDriver>,
33{
34 type InternalExecutor<'b>
35 = &'b mut DriverConnection<D>
36 where
37 Self: 'b;
38
39 type IntoInternalExecutor<'b>
40 = &'b mut DriverConnection<D>
41 where
42 Self: 'b;
43
44 async fn query_setup<O: SetupSql<D> + Send + Sync>(
45 &mut self,
46 sql: O,
47 ) -> anyhow::Result<O::Output>
48 where
49 DriverConnection<D>: Send + Sync,
50 {
51 sql.query(self).await
52 }
53
54 fn executor<'a>(&'a mut self) -> Self::InternalExecutor<'a> {
55 &mut *self.internal
56 }
57
58 fn into_executor<'a>(self) -> Self::IntoInternalExecutor<'a>
59 where
60 Self: 'a,
61 {
62 &mut *self.internal
63 }
64}
65
66impl<D: Driver> Deref for Connection<D> {
67 type Target = <InternalDriver<D> as Database>::Connection;
68
69 fn deref(&self) -> &Self::Target {
70 &self.internal
71 }
72}
73
74impl<D: Driver> DerefMut for Connection<D> {
75 fn deref_mut(&mut self) -> &mut Self::Target {
76 &mut self.internal
77 }
78}