@@ -4,7 +4,7 @@ use std::{collections::HashSet, sync::Arc, vec};
44use tokio_postgres:: types:: ToSql ;
55
66use crate :: {
7- common:: rustengine_future ,
7+ common:: rustdriver_future ,
88 exceptions:: rust_errors:: RustPSQLDriverPyResult ,
99 query_result:: PSQLDriverPyQueryResult ,
1010 value_converter:: { convert_parameters, PythonDTO } ,
@@ -15,85 +15,14 @@ use super::{
1515 transaction_options:: { IsolationLevel , ReadVariant } ,
1616} ;
1717
18- #[ allow( clippy:: module_name_repetitions) ]
19- pub struct RustConnection {
20- pub db_client : Arc < Object > ,
21- }
22-
23- impl RustConnection {
24- #[ must_use]
25- pub fn new ( db_client : Arc < Object > ) -> Self {
26- RustConnection { db_client }
27- }
28- /// Execute statement with or witout parameters.
29- ///
30- /// # Errors
31- ///
32- /// May return Err Result if
33- /// 1) Cannot convert incoming parameters
34- /// 2) Cannot prepare statement
35- /// 3) Cannot execute query
36- pub async fn inner_execute (
37- & self ,
38- querystring : String ,
39- params : Vec < PythonDTO > ,
40- prepared : bool ,
41- ) -> RustPSQLDriverPyResult < PSQLDriverPyQueryResult > {
42- let db_client = & self . db_client ;
43- let mut vec_parameters: Vec < & ( dyn ToSql + Sync ) > = Vec :: with_capacity ( params. len ( ) ) ;
44- for param in & params {
45- vec_parameters. push ( param) ;
46- }
47-
48- let result = if prepared {
49- db_client
50- . query (
51- & db_client. prepare_cached ( & querystring) . await ?,
52- & vec_parameters. into_boxed_slice ( ) ,
53- )
54- . await ?
55- } else {
56- db_client
57- . query ( & querystring, & vec_parameters. into_boxed_slice ( ) )
58- . await ?
59- } ;
60-
61- Ok ( PSQLDriverPyQueryResult :: new ( result) )
62- }
63-
64- /// Return new instance of transaction.
65- #[ must_use]
66- pub fn inner_transaction (
67- & self ,
68- isolation_level : Option < IsolationLevel > ,
69- read_variant : Option < ReadVariant > ,
70- deferrable : Option < bool > ,
71- ) -> Transaction {
72- let inner_transaction = RustTransaction :: new (
73- self . db_client . clone ( ) ,
74- false ,
75- false ,
76- Arc :: new ( tokio:: sync:: RwLock :: new ( HashSet :: new ( ) ) ) ,
77- isolation_level,
78- read_variant,
79- deferrable,
80- ) ;
81-
82- Transaction :: new (
83- Arc :: new ( tokio:: sync:: RwLock :: new ( inner_transaction) ) ,
84- Default :: default ( ) ,
85- )
86- }
87- }
88-
8918#[ pyclass( ) ]
9019pub struct Connection {
91- pub inner_connection : Arc < RustConnection > ,
20+ pub inner_connection : Arc < Object > ,
9221}
9322
9423impl Connection {
9524 #[ must_use]
96- pub fn new ( inner_connection : Arc < RustConnection > ) -> Self {
25+ pub fn new ( inner_connection : Arc < Object > ) -> Self {
9726 Connection { inner_connection }
9827 }
9928}
@@ -121,10 +50,27 @@ impl Connection {
12150 if let Some ( parameters) = parameters {
12251 params = convert_parameters ( parameters) ?;
12352 }
124- rustengine_future ( py, async move {
125- connection_arc
126- . inner_execute ( querystring, params, prepared. unwrap_or ( true ) )
127- . await
53+ let is_prepared = prepared. unwrap_or ( true ) ;
54+ rustdriver_future ( py, async move {
55+ let mut vec_parameters: Vec < & ( dyn ToSql + Sync ) > = Vec :: with_capacity ( params. len ( ) ) ;
56+ for param in & params {
57+ vec_parameters. push ( param) ;
58+ }
59+
60+ let result = if is_prepared {
61+ connection_arc
62+ . query (
63+ & connection_arc. prepare_cached ( & querystring) . await ?,
64+ & vec_parameters. into_boxed_slice ( ) ,
65+ )
66+ . await ?
67+ } else {
68+ connection_arc
69+ . query ( & querystring, & vec_parameters. into_boxed_slice ( ) )
70+ . await ?
71+ } ;
72+
73+ Ok ( PSQLDriverPyQueryResult :: new ( result) )
12874 } )
12975 }
13076
@@ -136,7 +82,19 @@ impl Connection {
13682 read_variant : Option < ReadVariant > ,
13783 deferrable : Option < bool > ,
13884 ) -> Transaction {
139- self . inner_connection
140- . inner_transaction ( isolation_level, read_variant, deferrable)
85+ let inner_transaction = RustTransaction :: new (
86+ self . inner_connection . clone ( ) ,
87+ false ,
88+ false ,
89+ Arc :: new ( tokio:: sync:: RwLock :: new ( HashSet :: new ( ) ) ) ,
90+ isolation_level,
91+ read_variant,
92+ deferrable,
93+ ) ;
94+
95+ Transaction :: new (
96+ Arc :: new ( tokio:: sync:: RwLock :: new ( inner_transaction) ) ,
97+ Default :: default ( ) ,
98+ )
14199 }
142100}
0 commit comments