|
| 1 | +use crate::sql::db_connection_pool::oraclepool::OracleConnectionPool; |
| 2 | +use crate::sql::{db_connection_pool, sql_provider_datafusion::SqlTable}; |
| 3 | +use async_trait::async_trait; |
| 4 | + |
| 5 | +use datafusion::error::DataFusionError; |
| 6 | +use datafusion::{ |
| 7 | + catalog::{Session, TableProviderFactory}, |
| 8 | + datasource::TableProvider, |
| 9 | + logical_expr::CreateExternalTable, |
| 10 | + sql::TableReference, |
| 11 | +}; |
| 12 | +use secrecy::SecretString; |
| 13 | +use snafu::prelude::*; |
| 14 | +use std::collections::HashMap; |
| 15 | +use std::sync::Arc; |
| 16 | + |
| 17 | +#[cfg(feature = "oracle-federation")] |
| 18 | +pub mod federation; |
| 19 | +pub mod sql_table; |
| 20 | +pub mod write; |
| 21 | + |
| 22 | +use self::sql_table::OracleTable; |
| 23 | +use crate::sql::db_connection_pool::dbconnection::oracleconn::OraclePooledConnection; |
| 24 | + |
| 25 | +#[derive(Debug, Snafu)] |
| 26 | +pub enum Error { |
| 27 | + #[snafu(display("DbConnectionError: {source}"))] |
| 28 | + DbConnectionError { |
| 29 | + source: db_connection_pool::dbconnection::GenericError, |
| 30 | + }, |
| 31 | + |
| 32 | + #[snafu(display("Unable to create Oracle connection pool: {source}"))] |
| 33 | + UnableToCreateConnectionPool { |
| 34 | + source: db_connection_pool::oraclepool::Error, |
| 35 | + }, |
| 36 | + |
| 37 | + #[snafu(display("Unable to create table provider: {source}"))] |
| 38 | + UnableToCreateTableProvider { |
| 39 | + source: Box<dyn std::error::Error + Send + Sync>, |
| 40 | + }, |
| 41 | +} |
| 42 | + |
| 43 | +pub type Result<T, E = Error> = std::result::Result<T, E>; |
| 44 | + |
| 45 | +pub struct OracleTableFactory { |
| 46 | + pool: Arc<OracleConnectionPool>, |
| 47 | +} |
| 48 | + |
| 49 | +impl OracleTableFactory { |
| 50 | + #[must_use] |
| 51 | + pub fn new(pool: Arc<OracleConnectionPool>) -> Self { |
| 52 | + Self { pool } |
| 53 | + } |
| 54 | + |
| 55 | + pub async fn table_provider( |
| 56 | + &self, |
| 57 | + table_reference: TableReference, |
| 58 | + ) -> Result<Arc<dyn TableProvider + 'static>, Box<dyn std::error::Error + Send + Sync>> { |
| 59 | + let pool = Arc::clone(&self.pool); |
| 60 | + let dyn_pool = pool as Arc< |
| 61 | + dyn db_connection_pool::DbConnectionPool< |
| 62 | + OraclePooledConnection, |
| 63 | + oracle::sql_type::OracleType, |
| 64 | + > + Send |
| 65 | + + Sync |
| 66 | + + 'static, |
| 67 | + >; |
| 68 | + |
| 69 | + let table = SqlTable::new("oracle", &dyn_pool, table_reference) |
| 70 | + .await |
| 71 | + .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?; |
| 72 | + |
| 73 | + let oracle_table = Arc::new(OracleTable::new(Arc::clone(&self.pool), table)); |
| 74 | + |
| 75 | + #[cfg(feature = "oracle-federation")] |
| 76 | + let oracle_table = Arc::new( |
| 77 | + oracle_table |
| 78 | + .create_federated_table_provider() |
| 79 | + .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?, |
| 80 | + ); |
| 81 | + |
| 82 | + Ok(oracle_table) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +#[derive(Debug)] |
| 87 | +pub struct OracleTableProviderFactory {} |
| 88 | + |
| 89 | +impl OracleTableProviderFactory { |
| 90 | + #[must_use] |
| 91 | + pub fn new() -> Self { |
| 92 | + Self {} |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl Default for OracleTableProviderFactory { |
| 97 | + fn default() -> Self { |
| 98 | + Self::new() |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +#[async_trait] |
| 103 | +impl TableProviderFactory for OracleTableProviderFactory { |
| 104 | + async fn create( |
| 105 | + &self, |
| 106 | + _state: &dyn Session, |
| 107 | + cmd: &CreateExternalTable, |
| 108 | + ) -> datafusion::common::Result<Arc<dyn TableProvider>> { |
| 109 | + let name = cmd.name.to_string(); |
| 110 | + let options = &cmd.options; |
| 111 | + |
| 112 | + // Construct params from options |
| 113 | + let mut params: HashMap<String, SecretString> = HashMap::new(); |
| 114 | + for (k, v) in options { |
| 115 | + params.insert(k.clone(), SecretString::from(v.clone())); |
| 116 | + } |
| 117 | + |
| 118 | + let pool = OracleConnectionPool::new(params) |
| 119 | + .await |
| 120 | + .map_err(|e| DataFusionError::External(Box::new(e)))?; |
| 121 | + |
| 122 | + let factory = OracleTableFactory::new(Arc::new(pool)); |
| 123 | + |
| 124 | + let table = factory |
| 125 | + .table_provider(TableReference::from(name)) |
| 126 | + .await |
| 127 | + .map_err(|e| DataFusionError::External(e))?; |
| 128 | + |
| 129 | + Ok(table) |
| 130 | + } |
| 131 | +} |
0 commit comments