geekorm_core/backends/connect/
mod.rs1use std::fmt::{Debug, Display};
24use std::path::PathBuf;
25use std::sync::atomic::AtomicUsize;
26use url::Url;
27
28pub mod backend;
29pub mod manager;
30
31pub use manager::ConnectionManager;
32
33pub struct Connection<'a> {
37 pool: &'a ConnectionManager,
38 query_count: AtomicUsize,
39 backend: Backend,
40}
41
42#[derive(Default, Clone)]
45pub enum Backend {
46 #[cfg(feature = "libsql")]
48 Libsql {
49 conn: ::libsql::Connection,
51 },
52 #[cfg(feature = "rusqlite")]
54 Rusqlite {
55 conn: std::sync::Arc<::rusqlite::Connection>,
57 },
58 #[default]
60 Unknown,
61}
62
63#[derive(Debug, Default, Clone, PartialEq, Eq)]
65pub enum ConnectionType {
66 #[default]
68 InMemory,
69 Path {
71 file: PathBuf,
73 },
74 Remote {
76 url: Url,
78 },
79}
80
81impl Connection<'_> {
82 pub fn count(&self) -> usize {
88 self.query_count.load(std::sync::atomic::Ordering::Relaxed)
89 }
90}
91
92impl Display for Connection<'_> {
93 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94 match &self.backend {
95 #[cfg(feature = "libsql")]
96 Backend::Libsql { .. } => {
97 write!(f, "Backend::Libsql({})", self.pool.get_database_type())
98 }
99 #[cfg(feature = "rusqlite")]
100 Backend::Rusqlite { .. } => {
101 write!(f, "Backend::Rusqlite({})", self.pool.get_database_type())
102 }
103 Backend::Unknown => write!(f, "Backend::Unknown"),
104 }
105 }
106}
107
108impl Drop for Connection<'_> {
109 fn drop(&mut self) {
110 self.pool.insert_backend(self.backend.clone());
113 }
114}
115
116impl Debug for Connection<'_> {
117 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
118 match self.backend {
119 #[cfg(feature = "libsql")]
120 Backend::Libsql { .. } => write!(f, "Backend::Libsql"),
121 #[cfg(feature = "rusqlite")]
122 Backend::Rusqlite { .. } => write!(f, "Backend::Rusqlite"),
123 Backend::Unknown => write!(f, "Backend::Unknown"),
124 }
125 }
126}
127
128impl Display for ConnectionType {
129 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
130 match self {
131 ConnectionType::InMemory => write!(f, "InMemory"),
132 ConnectionType::Path { .. } => write!(f, "Path"),
133 ConnectionType::Remote { .. } => write!(f, "Remote"),
134 }
135 }
136}