@@ -15,47 +15,44 @@ use super::{
1515 transaction_options:: { IsolationLevel , ReadVariant } ,
1616} ;
1717
18- pub struct RustConnection {
18+ #[ pyclass]
19+ pub struct Connection {
1920 pub db_client : Arc < tokio:: sync:: RwLock < Object > > ,
2021}
2122
22- impl RustConnection {
23- /// Execute querystring with parameters.
24- ///
25- /// Method doesn't acquire lock on database connection.
26- /// It prepares and caches querystring in the inner Object object.
27- ///
28- /// Then execute the query.
29- ///
30- /// # Errors:
31- /// May return Err Result if:
32- /// 1) Can not create/retrieve prepared statement
33- /// 2) Can not execute statement
34- pub async fn inner_execute < ' a > (
23+ #[ pymethods]
24+ impl Connection {
25+ pub fn execute < ' a > (
3526 & ' a self ,
27+ py : Python < ' a > ,
3628 querystring : String ,
37- parameters : Vec < PythonDTO > ,
38- ) -> RustPSQLDriverPyResult < PSQLDriverPyQueryResult > {
29+ parameters : Option < & ' a PyAny > ,
30+ ) -> RustPSQLDriverPyResult < & PyAny > {
3931 let db_client_arc = self . db_client . clone ( ) ;
4032
41- let db_client_guard = db_client_arc. read ( ) . await ;
42-
43- let mut vec_parameters: Vec < & ( dyn ToSql + Sync ) > = Vec :: with_capacity ( parameters. len ( ) ) ;
44- for param in parameters. iter ( ) {
45- vec_parameters. push ( param) ;
33+ let mut params: Vec < PythonDTO > = vec ! [ ] ;
34+ if let Some ( parameters) = parameters {
35+ params = convert_parameters ( parameters) ?
4636 }
4737
48- let statement: tokio_postgres:: Statement =
49- db_client_guard. prepare_cached ( & querystring) . await ?;
50-
51- let result = db_client_guard
52- . query ( & statement, & vec_parameters. into_boxed_slice ( ) )
53- . await ?;
54-
55- Ok ( PSQLDriverPyQueryResult :: new ( result) )
38+ rustengine_future ( py, async move {
39+ let mut vec_parameters: Vec < & ( dyn ToSql + Sync ) > = Vec :: with_capacity ( params. len ( ) ) ;
40+ for param in params. iter ( ) {
41+ vec_parameters. push ( param) ;
42+ }
43+ let db_client_guard = db_client_arc. read ( ) . await ;
44+ let statement: tokio_postgres:: Statement =
45+ db_client_guard. prepare_cached ( & querystring) . await ?;
46+
47+ let result = db_client_guard
48+ . query ( & statement, & vec_parameters. into_boxed_slice ( ) )
49+ . await ?;
50+
51+ Ok ( PSQLDriverPyQueryResult :: new ( result) )
52+ } )
5653 }
5754
58- pub fn inner_transaction < ' a > (
55+ pub fn transaction < ' a > (
5956 & ' a self ,
6057 isolation_level : Option < IsolationLevel > ,
6158 read_variant : Option < ReadVariant > ,
@@ -75,51 +72,3 @@ impl RustConnection {
7572 }
7673 }
7774}
78-
79- #[ pyclass]
80- pub struct Connection ( pub Arc < tokio:: sync:: RwLock < RustConnection > > ) ;
81-
82- #[ pymethods]
83- impl Connection {
84- /// Execute querystring with parameters.
85- ///
86- /// It converts incoming parameters to rust readable
87- /// and then execute the query with them.
88- ///
89- /// # Errors:
90- ///
91- /// May return Err Result if:
92- /// 1) Cannot convert python parameters
93- /// 2) Cannot execute querystring.
94- pub fn execute < ' a > (
95- & ' a self ,
96- py : Python < ' a > ,
97- querystring : String ,
98- parameters : Option < & ' a PyAny > ,
99- ) -> RustPSQLDriverPyResult < & PyAny > {
100- let connection_arc = self . 0 . clone ( ) ;
101- let mut params: Vec < PythonDTO > = vec ! [ ] ;
102- if let Some ( parameters) = parameters {
103- params = convert_parameters ( parameters) ?
104- }
105-
106- rustengine_future ( py, async move {
107- let connection_guard = connection_arc. read ( ) . await ;
108- Ok ( connection_guard. inner_execute ( querystring, params) . await ?)
109- } )
110- }
111-
112- pub fn transaction < ' a > (
113- & ' a self ,
114- py : Python < ' a > ,
115- isolation_level : Option < IsolationLevel > ,
116- read_variant : Option < ReadVariant > ,
117- ) -> RustPSQLDriverPyResult < & PyAny > {
118- let connection_arc = self . 0 . clone ( ) ;
119-
120- rustengine_future ( py, async move {
121- let connection_guard = connection_arc. read ( ) . await ;
122- Ok ( connection_guard. inner_transaction ( isolation_level, read_variant) )
123- } )
124- }
125- }
0 commit comments