mybatis_core/db/
mod.rs

1#[cfg(feature = "mssql")]
2pub mod bind_mssql;
3#[cfg(feature = "mysql")]
4pub mod bind_mysql;
5#[cfg(feature = "postgres")]
6pub mod bind_pg;
7#[cfg(feature = "sqlite")]
8pub mod bind_sqlite;
9
10use rbson::Bson;
11use std::time::Duration;
12
13use chrono::NaiveDateTime;
14use serde::de::DeserializeOwned;
15use serde::{Deserialize, Serialize};
16
17use crate::convert::StmtConvert;
18use crate::db::db_adapter::DataDecoder;
19pub use db_adapter::{DBConnectOption, DBExecResult, DBPool, DBPoolConn, DBQuery, DBTx};
20
21pub mod db_adapter;
22
23#[derive(Debug)]
24pub struct DBPoolOptions {
25    pub max_connections: u32,
26    pub min_connections: u32,
27    pub connect_timeout: Duration,
28    pub max_lifetime: Option<Duration>,
29    pub idle_timeout: Option<Duration>,
30    pub test_before_acquire: bool,
31    pub decoder: Box<dyn DataDecoder>,
32}
33
34impl Default for DBPoolOptions {
35    fn default() -> Self {
36        Self {
37            // pool a maximum of 10 connections to the same database
38            max_connections: 10,
39            // don't open connections until necessary
40            min_connections: 0,
41            // try to connect for 10 seconds before erroring
42            connect_timeout: Duration::from_secs(60),
43            // reap connections that have been alive > 30 minutes
44            // prevents unbounded live-leaking of memory due to naive prepared statement caching
45            // see src/cache.rs for context
46            max_lifetime: Some(Duration::from_secs(1800)),
47            // don't reap connections based on idle time
48            idle_timeout: None,
49            // If true, test the health of a connection on acquire
50            test_before_acquire: true,
51            decoder: Box::new(DefaultDecoder {}),
52        }
53    }
54}
55
56impl DBPoolOptions {
57    pub fn new() -> Self {
58        DBPoolOptions::default()
59    }
60}
61
62#[derive(Clone, Debug)]
63pub struct DefaultDecoder {}
64
65impl DataDecoder for DefaultDecoder {
66    fn decode(&self, _key: &str, _data: &mut Bson) -> crate::Result<()> {
67        return Ok(());
68    }
69}
70
71#[derive(Serialize, Deserialize, Debug, Clone, Copy, Eq, PartialEq)]
72pub enum DriverType {
73    None = 0,
74    Mysql = 1,
75    Postgres = 2,
76    Sqlite = 3,
77    Mssql = 4,
78}
79
80impl DriverType {
81    pub fn is_number_type(&self) -> bool {
82        match self {
83            DriverType::Postgres | DriverType::Mssql => {
84                return true;
85            }
86            _ => {
87                return false;
88            }
89        }
90    }
91}