forked from datafusion-contrib/datafusion-table-providers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathodbc.rs
More file actions
66 lines (56 loc) · 1.92 KB
/
odbc.rs
File metadata and controls
66 lines (56 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use std::{collections::HashMap, sync::Arc};
use datafusion_table_providers::{
odbc::ODBCTableFactory, sql::db_connection_pool::odbcpool::ODBCPool,
util::secrets::to_secret_map,
};
use pyo3::{prelude::*, types::PyDict};
use crate::{
utils::{to_pyerr, wait_for_future},
RawTableProvider,
};
#[pyclass(module = "datafusion_table_providers._internal.odbc")]
struct RawODBCTableFactory {
_pool: Arc<ODBCPool>,
// TODO: 'static lifetime might be wrong, we want the lifetime to be 'py but it is
// still unclear how to define it.
factory: ODBCTableFactory<'static>,
}
#[pymethods]
impl RawODBCTableFactory {
#[new]
#[pyo3(signature = (params))]
pub fn new(params: &Bound<'_, PyDict>) -> PyResult<Self> {
// Convert Python dict into Rust hashmap, and convert it to secret map
let mut hashmap = HashMap::new();
for (key, value) in params.iter() {
let key: String = key.extract()?;
let value: String = value.extract()?;
hashmap.insert(key, value);
}
let hashmap = to_secret_map(hashmap);
let pool = Arc::new(ODBCPool::new(hashmap).map_err(to_pyerr)?);
Ok(Self {
factory: ODBCTableFactory::new(pool.clone()),
_pool: pool,
})
}
pub fn tables(&self) -> PyResult<Vec<String>> {
// This method is not supported yet because of unimplemented traints in odbcconn.
unimplemented!();
}
pub fn get_table(&self, py: Python, table_reference: &str) -> PyResult<RawTableProvider> {
let table = wait_for_future(
py,
self.factory.table_provider(table_reference.into(), None),
)
.map_err(to_pyerr)?;
Ok(RawTableProvider {
table,
supports_pushdown_filters: true,
})
}
}
pub(crate) fn init_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<RawODBCTableFactory>()?;
Ok(())
}