geekorm_core/backends/connect/
backend.rs

1//! # GeekConnection trait implementation for Connection
2#![allow(unused_imports, unused_variables)]
3use crate::GeekConnection;
4
5use super::{Backend, Connection};
6
7impl GeekConnection for Connection<'_> {
8    type Connection = Self;
9
10    async fn create_table<T>(connection: &Self::Connection) -> Result<(), crate::Error>
11    where
12        T: crate::TableBuilder
13            + crate::QueryBuilderTrait
14            + Sized
15            + serde::Serialize
16            + serde::de::DeserializeOwned,
17    {
18        connection
19            .query_count
20            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
21        match &connection.backend {
22            #[cfg(feature = "libsql")]
23            Backend::Libsql { conn, .. } => {
24                <libsql::Connection as GeekConnection>::create_table::<T>(conn).await
25            }
26            #[cfg(feature = "rusqlite")]
27            Backend::Rusqlite { conn } => {
28                <rusqlite::Connection as GeekConnection>::create_table::<T>(conn).await
29            }
30            _ => unimplemented!(),
31        }
32    }
33
34    async fn batch(connection: &Self::Connection, query: crate::Query) -> Result<(), crate::Error> {
35        connection
36            .query_count
37            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
38        match &connection.backend {
39            #[cfg(feature = "libsql")]
40            Backend::Libsql { conn, .. } => {
41                <libsql::Connection as GeekConnection>::batch(conn, query).await
42            }
43            #[cfg(feature = "rusqlite")]
44            Backend::Rusqlite { conn } => {
45                <rusqlite::Connection as GeekConnection>::batch(conn, query).await
46            }
47            _ => unimplemented!(),
48        }
49    }
50
51    async fn query<T>(
52        connection: &Self::Connection,
53        query: crate::Query,
54    ) -> Result<Vec<T>, crate::Error>
55    where
56        T: serde::de::DeserializeOwned,
57    {
58        connection
59            .query_count
60            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
61        match &connection.backend {
62            #[cfg(feature = "libsql")]
63            Backend::Libsql { conn, .. } => {
64                <libsql::Connection as GeekConnection>::query(conn, query).await
65            }
66            #[cfg(feature = "rusqlite")]
67            Backend::Rusqlite { conn } => {
68                <rusqlite::Connection as GeekConnection>::query(conn, query).await
69            }
70            _ => unimplemented!(),
71        }
72    }
73
74    async fn execute(
75        connection: &Self::Connection,
76        query: crate::Query,
77    ) -> Result<(), crate::Error> {
78        connection
79            .query_count
80            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
81        match &connection.backend {
82            #[cfg(feature = "libsql")]
83            Backend::Libsql { conn, .. } => {
84                <libsql::Connection as GeekConnection>::execute(conn, query).await
85            }
86            #[cfg(feature = "rusqlite")]
87            Backend::Rusqlite { conn } => {
88                <rusqlite::Connection as GeekConnection>::execute(conn, query).await
89            }
90            _ => unimplemented!(),
91        }
92    }
93
94    async fn row_count(
95        connection: &Self::Connection,
96        query: crate::Query,
97    ) -> Result<i64, crate::Error> {
98        connection
99            .query_count
100            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
101        match &connection.backend {
102            #[cfg(feature = "libsql")]
103            Backend::Libsql { conn, .. } => {
104                <libsql::Connection as GeekConnection>::row_count(conn, query).await
105            }
106            #[cfg(feature = "rusqlite")]
107            Backend::Rusqlite { conn } => {
108                <rusqlite::Connection as GeekConnection>::row_count(conn, query).await
109            }
110            _ => unimplemented!(),
111        }
112    }
113
114    async fn query_raw(
115        connection: &Self::Connection,
116        query: crate::Query,
117    ) -> Result<Vec<std::collections::HashMap<String, crate::Value>>, crate::Error> {
118        connection
119            .query_count
120            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
121        match &connection.backend {
122            #[cfg(feature = "libsql")]
123            Backend::Libsql { conn, .. } => {
124                <libsql::Connection as GeekConnection>::query_raw(conn, query).await
125            }
126            #[cfg(feature = "rusqlite")]
127            Backend::Rusqlite { conn } => {
128                <rusqlite::Connection as GeekConnection>::query_raw(conn, query).await
129            }
130            _ => unimplemented!(),
131        }
132    }
133
134    async fn query_first<T>(
135        connection: &Self::Connection,
136        query: crate::Query,
137    ) -> Result<T, crate::Error>
138    where
139        T: serde::de::DeserializeOwned,
140    {
141        connection
142            .query_count
143            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
144        match &connection.backend {
145            #[cfg(feature = "libsql")]
146            Backend::Libsql { conn, .. } => {
147                <libsql::Connection as GeekConnection>::query_first(conn, query).await
148            }
149            #[cfg(feature = "rusqlite")]
150            Backend::Rusqlite { conn } => {
151                <rusqlite::Connection as GeekConnection>::query_first(conn, query).await
152            }
153            _ => unimplemented!(),
154        }
155    }
156}