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