1use crate::Error;
2use futures_core::future::BoxFuture;
3use rbs::Value;
4use rbs::value::map::ValueMap;
5use std::any::Any;
6use std::fmt::{Debug, Display, Formatter};
7use std::ops::{Deref, DerefMut};
8
9pub trait Driver: Debug + Sync + Send {
12 fn name(&self) -> &str;
13 fn connect(&self, url: &str) -> BoxFuture<'_, Result<Box<dyn Connection>, Error>>;
16
17 fn connect_opt<'a>(
18 &'a self,
19 opt: &'a dyn ConnectOptions,
20 ) -> BoxFuture<'a, Result<Box<dyn Connection>, Error>>;
21
22 fn default_option(&self) -> Box<dyn ConnectOptions>;
24}
25
26impl Driver for Box<dyn Driver> {
27 fn name(&self) -> &str {
28 self.deref().name()
29 }
30
31 fn connect(&self, url: &str) -> BoxFuture<'_, Result<Box<dyn Connection>, Error>> {
32 self.deref().connect(url)
33 }
34
35 fn connect_opt<'a>(
36 &'a self,
37 opt: &'a dyn ConnectOptions,
38 ) -> BoxFuture<'a, Result<Box<dyn Connection>, Error>> {
39 self.deref().connect_opt(opt)
40 }
41
42 fn default_option(&self) -> Box<dyn ConnectOptions> {
43 self.deref().default_option()
44 }
45}
46
47#[derive(Debug, Default, serde::Serialize, serde::Deserialize, Eq, PartialEq)]
48pub struct ExecResult {
49 pub rows_affected: u64,
50 pub last_insert_id: Value,
52}
53
54impl Display for ExecResult {
55 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
56 struct DisplayBox<'a> {
57 inner: &'a Value,
58 }
59 impl<'a> Debug for DisplayBox<'a> {
60 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
61 std::fmt::Display::fmt(&self.inner, f)
62 }
63 }
64 f.debug_map()
65 .key(&"rows_affected")
66 .value(&self.rows_affected)
67 .key(&"last_insert_id")
68 .value(&DisplayBox {
69 inner: &self.last_insert_id,
70 })
71 .finish()
72 }
73}
74
75impl From<(u64, Value)> for ExecResult {
76 fn from(value: (u64, Value)) -> Self {
77 Self {
78 rows_affected: value.0,
79 last_insert_id: value.1,
80 }
81 }
82}
83
84pub trait Connection: Send + Sync {
86 fn exec_rows(
88 &mut self,
89 sql: &str,
90 params: Vec<Value>,
91 ) -> BoxFuture<'_, Result<Vec<Box<dyn Row>>, Error>>;
92
93 fn exec_decode(&mut self, sql: &str, params: Vec<Value>) -> BoxFuture<'_, Result<Value, Error>> {
96 let v = self.exec_rows(sql, params);
97 Box::pin(async move {
98 let v = v.await?;
99 let mut rows = Vec::with_capacity(v.len());
100 for mut x in v {
101 let md = x.meta_data();
102 let mut m = ValueMap::with_capacity(md.column_len());
103 for mut i in 0..md.column_len() {
104 i = md.column_len() - i - 1;
105 let n = md.column_name(i);
106 m.insert(Value::String(n), x.get(i)?);
107 }
108 rows.push(Value::Map(m));
109 }
110 Ok(Value::Array(rows))
111 })
112 }
113
114 fn exec(&mut self, sql: &str, params: Vec<Value>) -> BoxFuture<'_, Result<ExecResult, Error>>;
116
117 fn ping(&mut self) -> BoxFuture<'_, Result<(), Error>>;
119
120 fn close(&mut self) -> BoxFuture<'_, Result<(), Error>>;
127
128 fn begin(&mut self) -> BoxFuture<'_, Result<(), Error>> {
130 Box::pin(async {
131 _ = self.exec("begin", vec![]).await?;
132 Ok(())
133 })
134 }
135
136 fn commit(&mut self) -> BoxFuture<'_, Result<(), Error>> {
138 Box::pin(async {
139 _ = self.exec("commit", vec![]).await?;
140 Ok(())
141 })
142 }
143
144 fn rollback(&mut self) -> BoxFuture<'_, Result<(), Error>> {
146 Box::pin(async {
147 _ = self.exec("rollback", vec![]).await?;
148 Ok(())
149 })
150 }
151}
152
153impl Connection for Box<dyn Connection> {
154 fn exec_rows(
155 &mut self,
156 sql: &str,
157 params: Vec<Value>,
158 ) -> BoxFuture<'_, Result<Vec<Box<dyn Row>>, Error>> {
159 self.deref_mut().exec_rows(sql, params)
160 }
161
162 fn exec_decode(&mut self, sql: &str, params: Vec<Value>) -> BoxFuture<'_, Result<Value, Error>> {
163 self.deref_mut().exec_decode(sql, params)
164 }
165
166 fn exec(&mut self, sql: &str, params: Vec<Value>) -> BoxFuture<'_, Result<ExecResult, Error>> {
167 self.deref_mut().exec(sql, params)
168 }
169
170 fn ping(&mut self) -> BoxFuture<'_, Result<(), Error>> {
171 self.deref_mut().ping()
172 }
173
174 fn close(&mut self) -> BoxFuture<'_, Result<(), Error>> {
175 self.deref_mut().close()
176 }
177
178 fn begin(&mut self) -> BoxFuture<'_, Result<(), Error>> {
179 self.deref_mut().begin()
180 }
181 fn rollback(&mut self) -> BoxFuture<'_, Result<(), Error>> {
182 self.deref_mut().rollback()
183 }
184 fn commit(&mut self) -> BoxFuture<'_, Result<(), Error>> {
185 self.deref_mut().commit()
186 }
187}
188
189pub trait Row: 'static + Send + Debug {
191 fn meta_data(&self) -> Box<dyn MetaData>;
193
194 fn get(&mut self, i: usize) -> Result<Value, Error>;
196}
197
198pub trait MetaData: Debug {
200 fn column_len(&self) -> usize;
201 fn column_name(&self, i: usize) -> String;
202 fn column_type(&self, i: usize) -> String;
203}
204
205pub trait ConnectOptions: Any + Send + Sync + Debug + 'static {
207 fn connect(&self) -> BoxFuture<'_, Result<Box<dyn Connection>, Error>>;
209
210 #[inline]
238 fn set(&mut self, arg: Box<dyn Any>)
239 where
240 Self: Sized,
241 {
242 *self = *arg.downcast().expect("must be self type!");
243 }
244
245 fn set_uri(&mut self, uri: &str) -> Result<(), Error>;
247}
248
249impl dyn ConnectOptions {
251 pub fn downcast_ref<E: ConnectOptions>(&self) -> Option<&E> {
252 <dyn Any>::downcast_ref::<E>(self)
253 }
254
255 pub fn downcast_ref_mut<E: ConnectOptions>(&mut self) -> Option<&mut E> {
256 <dyn Any>::downcast_mut::<E>(self)
257 }
258}
259
260pub trait Placeholder {
281 fn exchange(&self, sql: &str) -> String;
282}