datafusion_table_providers/sql/db_connection_pool/
mod.rs

1use async_trait::async_trait;
2use dbconnection::DbConnection;
3use std::sync::Arc;
4
5pub mod dbconnection;
6#[cfg(feature = "duckdb")]
7pub mod duckdbpool;
8#[cfg(feature = "mysql")]
9pub mod mysqlpool;
10#[cfg(feature = "odbc")]
11pub mod odbcpool;
12#[cfg(feature = "postgres")]
13pub mod postgrespool;
14#[cfg(feature = "sqlite")]
15pub mod sqlitepool;
16
17pub type Error = Box<dyn std::error::Error + Send + Sync>;
18type Result<T, E = Error> = std::result::Result<T, E>;
19
20/// Controls whether join pushdown is allowed, and under what conditions
21#[derive(Clone, Debug, PartialEq, Eq)]
22pub enum JoinPushDown {
23    /// This connection pool should not allow join push down. (i.e. we don't know under what conditions it is safe to send a join query to the database)
24    Disallow,
25    /// Allows join push down for other tables that share the same context.
26    ///
27    /// The context can be part of the connection string that uniquely identifies the server.
28    AllowedFor(String),
29}
30
31#[async_trait]
32pub trait DbConnectionPool<T, P: 'static> {
33    async fn connect(&self) -> Result<Box<dyn DbConnection<T, P>>>;
34
35    fn join_push_down(&self) -> JoinPushDown;
36}
37
38#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
39pub enum Mode {
40    #[default]
41    Memory,
42    File,
43}
44
45impl From<&str> for Mode {
46    fn from(m: &str) -> Self {
47        match m {
48            "file" => Mode::File,
49            "memory" => Mode::Memory,
50            _ => Mode::default(),
51        }
52    }
53}
54
55/// A key that uniquely identifies a database instance.
56#[derive(Debug, Clone, PartialEq, Eq, Hash)]
57pub enum DbInstanceKey {
58    /// The database is a file on disk, with the given path.
59    File(Arc<str>),
60    /// The database is in memory.
61    Memory,
62}
63
64impl DbInstanceKey {
65    pub fn memory() -> Self {
66        DbInstanceKey::Memory
67    }
68
69    pub fn file(path: Arc<str>) -> Self {
70        DbInstanceKey::File(path)
71    }
72}