rbatis_core/db/
mod.rs

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