@@ -2,6 +2,7 @@ use crate::{
22 search:: { PySortby , StringOrDict , StringOrList } ,
33 Result ,
44} ;
5+ use duckdb:: Connection ;
56use pyo3:: {
67 exceptions:: PyException ,
78 prelude:: * ,
@@ -18,13 +19,40 @@ pub struct DuckdbClient(Mutex<Client>);
1819#[ pymethods]
1920impl DuckdbClient {
2021 #[ new]
21- #[ pyo3( signature = ( * , use_hive_partitioning= false , extension_directory= None , install_extensions =true ) ) ]
22+ #[ pyo3( signature = ( * , extension_directory= None , extensions= Vec :: new ( ) , install_spatial =true , use_hive_partitioning= false ) ) ]
2223 fn new (
23- use_hive_partitioning : bool ,
2424 extension_directory : Option < PathBuf > ,
25- install_extensions : bool ,
25+ extensions : Vec < String > ,
26+ install_spatial : bool ,
27+ use_hive_partitioning : bool ,
2628 ) -> Result < DuckdbClient > {
27- todo ! ( )
29+ let connection = Connection :: open_in_memory ( ) ?;
30+ if let Some ( extension_directory) = extension_directory {
31+ connection. execute (
32+ "SET extension_directory = ?" ,
33+ [ extension_directory. to_string_lossy ( ) ] ,
34+ ) ?;
35+ }
36+ if install_spatial {
37+ connection. execute ( "INSTALL spatial" , [ ] ) ?;
38+ }
39+ for extension in extensions {
40+ connection. execute ( & format ! ( "LOAD '{}'" , extension) , [ ] ) ?;
41+ }
42+ connection. execute ( "LOAD spatial" , [ ] ) ?;
43+ let mut client = Client :: from ( connection) ;
44+ client. use_hive_partitioning = use_hive_partitioning;
45+ Ok ( DuckdbClient ( Mutex :: new ( client) ) )
46+ }
47+
48+ #[ pyo3( signature = ( sql, params = Vec :: new( ) ) ) ]
49+ fn execute < ' py > ( & self , sql : String , params : Vec < String > ) -> Result < usize > {
50+ let client = self
51+ . 0
52+ . lock ( )
53+ . map_err ( |err| PyException :: new_err ( err. to_string ( ) ) ) ?;
54+ let count = client. execute ( & sql, duckdb:: params_from_iter ( params) ) ?;
55+ Ok ( count)
2856 }
2957
3058 #[ pyo3( signature = ( href, * , intersects=None , ids=None , collections=None , limit=None , bbox=None , datetime=None , include=None , exclude=None , sortby=None , filter=None , query=None , * * kwargs) ) ]
0 commit comments