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