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
#[cfg(feature = "postgres")]
pub mod bind_pg;
#[cfg(feature = "mysql")]
pub mod bind_mysql;
#[cfg(feature = "sqlite")]
pub mod bind_sqlite;
#[cfg(feature = "mssql")]
pub mod bind_mssql;
use std::time::Duration;
use rbson::Bson;
use chrono::NaiveDateTime;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
pub use db_adapter::{
DBConnectOption, DBExecResult, DBPool, DBPoolConn, DBQuery, DBTx,
};
use crate::convert::StmtConvert;
use crate::db::db_adapter::DataDecoder;
pub mod db_adapter;
#[derive(Debug)]
pub struct DBPoolOptions {
pub max_connections: u32,
pub min_connections: u32,
pub connect_timeout: Duration,
pub max_lifetime: Option<Duration>,
pub idle_timeout: Option<Duration>,
pub test_before_acquire: bool,
pub decoder: Box<dyn DataDecoder>,
}
impl Default for DBPoolOptions {
fn default() -> Self {
Self {
max_connections: 10,
min_connections: 0,
connect_timeout: Duration::from_secs(60),
max_lifetime: Some(Duration::from_secs(1800)),
idle_timeout: None,
test_before_acquire: true,
decoder: Box::new(DefaultDecoder {}),
}
}
}
impl DBPoolOptions {
pub fn new() -> Self {
DBPoolOptions::default()
}
}
#[derive(Clone, Debug)]
pub struct DefaultDecoder {}
impl DataDecoder for DefaultDecoder {
fn decode(&self, _key: &str, _data: &mut Bson) -> crate::Result<()> {
return Ok(());
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy, Eq, PartialEq)]
pub enum DriverType {
None = 0,
Mysql = 1,
Postgres = 2,
Sqlite = 3,
Mssql = 4,
}
impl DriverType {
pub fn is_number_type(&self) -> bool {
match self {
DriverType::Postgres|DriverType::Mssql => {
return true;
}
_ => {
return false;
}
}
}
}