1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use crate::Error;
use futures_core::future::BoxFuture;
use rbs::value::map::ValueMap;
use rbs::Value;
use std::any::Any;
use std::fmt::{Debug, Display, Formatter};
pub trait Driver: Debug + Sync + Send {
fn name(&self) -> &str;
fn connect(&self, url: &str) -> BoxFuture<Result<Box<dyn Connection>, Error>>;
fn connect_opt<'a>(
&'a self,
opt: &'a dyn ConnectOptions,
) -> BoxFuture<Result<Box<dyn Connection>, Error>>;
fn default_option(&self) -> Box<dyn ConnectOptions>;
}
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct ExecResult {
pub rows_affected: u64,
pub last_insert_id: Value,
}
impl Display for ExecResult {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_map()
.key(&"rows_affected")
.value(&self.rows_affected)
.key(&"last_insert_id")
.value(&self.last_insert_id)
.finish()
}
}
pub trait Connection: Send {
fn get_rows(
&mut self,
sql: &str,
params: Vec<Value>,
) -> BoxFuture<Result<Vec<Box<dyn Row>>, Error>>;
fn get_values(
&mut self,
sql: &str,
params: Vec<Value>,
) -> BoxFuture<Result<Vec<Value>, Error>> {
let v = self.get_rows(sql, params);
Box::pin(async move {
let v = v.await?;
let mut rows = Vec::with_capacity(v.len());
for mut x in v {
let md = x.meta_data();
let mut m = ValueMap::with_capacity(md.column_len());
for mut i in 0..md.column_len() {
i = md.column_len() - i - 1;
let n = md.column_name(i);
m.insert(Value::String(n), x.get(i)?);
}
rows.push(Value::Map(m));
}
Ok(rows)
})
}
fn exec(&mut self, sql: &str, params: Vec<Value>) -> BoxFuture<Result<ExecResult, Error>>;
fn close(&mut self) -> BoxFuture<Result<(), Error>>;
fn ping(&mut self) -> BoxFuture<Result<(), Error>>;
}
pub trait Row: 'static + Send + Debug {
fn meta_data(&self) -> Box<dyn MetaData>;
fn get(&mut self, i: usize) -> Result<Value,Error>;
}
pub trait MetaData: Debug {
fn column_len(&self) -> usize;
fn column_name(&self, i: usize) -> String;
fn column_type(&self, i: usize) -> String;
}
pub trait ConnectOptions: Any + Send + Sync + Debug + 'static {
fn connect(&self) -> BoxFuture<Result<Box<dyn Connection>, Error>>;
#[inline]
fn set(&mut self, arg: Box<dyn Any>)
where
Self: Sized,
{
*self = *arg.downcast().expect("must be self type!");
}
fn set_uri(&mut self, uri: &str) -> Result<(), Error>;
fn uppercase_self(&self) -> &(dyn Any + Send + Sync);
}
impl dyn ConnectOptions {
pub fn downcast_ref<E: ConnectOptions>(&self) -> Option<&E> {
self.uppercase_self().downcast_ref()
}
}
pub trait Placeholder {
fn exchange(&self, sql: &str) -> String;
}