@@ -4,7 +4,9 @@ use futures_util::pin_mut;
44use postgres_types:: ToSql ;
55use pyo3:: { buffer:: PyBuffer , pyclass, pymethods, Py , PyAny , PyErr , Python } ;
66use std:: { collections:: HashSet , sync:: Arc , vec} ;
7- use tokio_postgres:: { binary_copy:: BinaryCopyInWriter , Client , CopyInSink , Row , Statement , ToStatement } ;
7+ use tokio_postgres:: {
8+ binary_copy:: BinaryCopyInWriter , Client , CopyInSink , Row , Statement , ToStatement ,
9+ } ;
810
911use crate :: {
1012 exceptions:: rust_errors:: { RustPSQLDriverError , RustPSQLDriverPyResult } ,
@@ -20,108 +22,114 @@ use super::{
2022 transaction_options:: { IsolationLevel , ReadVariant , SynchronousCommit } ,
2123} ;
2224
23- pub enum InnerConnection {
25+ #[ allow( clippy:: module_name_repetitions) ]
26+ pub enum PsqlpyConnection {
2427 PoolConn ( Object ) ,
2528 SingleConn ( Client ) ,
2629}
2730
28- impl InnerConnection {
29- pub async fn prepare_cached (
30- & self ,
31- query : & str
32- ) -> RustPSQLDriverPyResult < Statement > {
31+ impl PsqlpyConnection {
32+ /// Prepare cached statement.
33+ ///
34+ /// # Errors
35+ /// May return Err if cannot prepare statement.
36+ pub async fn prepare_cached ( & self , query : & str ) -> RustPSQLDriverPyResult < Statement > {
3337 match self {
34- InnerConnection :: PoolConn ( pconn) => {
35- return Ok ( pconn. prepare_cached ( query) . await ?)
36- }
37- InnerConnection :: SingleConn ( sconn) => {
38- return Ok ( sconn. prepare ( query) . await ?)
39- }
38+ PsqlpyConnection :: PoolConn ( pconn) => return Ok ( pconn. prepare_cached ( query) . await ?) ,
39+ PsqlpyConnection :: SingleConn ( sconn) => return Ok ( sconn. prepare ( query) . await ?) ,
4040 }
4141 }
4242
43+ /// Prepare cached statement.
44+ ///
45+ /// # Errors
46+ /// May return Err if cannot execute statement.
4347 pub async fn query < T > (
4448 & self ,
4549 statement : & T ,
4650 params : & [ & ( dyn ToSql + Sync ) ] ,
4751 ) -> RustPSQLDriverPyResult < Vec < Row > >
48- where T : ?Sized + ToStatement {
52+ where
53+ T : ?Sized + ToStatement ,
54+ {
4955 match self {
50- InnerConnection :: PoolConn ( pconn) => {
51- return Ok ( pconn. query ( statement, params) . await ?)
52- }
53- InnerConnection :: SingleConn ( sconn) => {
56+ PsqlpyConnection :: PoolConn ( pconn) => return Ok ( pconn. query ( statement, params) . await ?) ,
57+ PsqlpyConnection :: SingleConn ( sconn) => {
5458 return Ok ( sconn. query ( statement, params) . await ?)
5559 }
5660 }
5761 }
5862
63+ /// Prepare cached statement.
64+ ///
65+ /// # Errors
66+ /// May return Err if cannot execute statement.
5967 pub async fn batch_execute ( & self , query : & str ) -> RustPSQLDriverPyResult < ( ) > {
6068 match self {
61- InnerConnection :: PoolConn ( pconn) => {
62- return Ok ( pconn. batch_execute ( query) . await ?)
63- }
64- InnerConnection :: SingleConn ( sconn) => {
65- return Ok ( sconn. batch_execute ( query) . await ?)
66- }
69+ PsqlpyConnection :: PoolConn ( pconn) => return Ok ( pconn. batch_execute ( query) . await ?) ,
70+ PsqlpyConnection :: SingleConn ( sconn) => return Ok ( sconn. batch_execute ( query) . await ?) ,
6771 }
6872 }
6973
74+ /// Prepare cached statement.
75+ ///
76+ /// # Errors
77+ /// May return Err if cannot execute statement.
7078 pub async fn query_one < T > (
7179 & self ,
7280 statement : & T ,
7381 params : & [ & ( dyn ToSql + Sync ) ] ,
7482 ) -> RustPSQLDriverPyResult < Row >
75- where T : ?Sized + ToStatement
83+ where
84+ T : ?Sized + ToStatement ,
7685 {
7786 match self {
78- InnerConnection :: PoolConn ( pconn) => {
87+ PsqlpyConnection :: PoolConn ( pconn) => {
7988 return Ok ( pconn. query_one ( statement, params) . await ?)
8089 }
81- InnerConnection :: SingleConn ( sconn) => {
90+ PsqlpyConnection :: SingleConn ( sconn) => {
8291 return Ok ( sconn. query_one ( statement, params) . await ?)
8392 }
8493 }
8594 }
8695
87- pub async fn copy_in < T , U > (
88- & self ,
89- statement : & T
90- ) -> RustPSQLDriverPyResult < CopyInSink < U > >
96+ /// Prepare cached statement.
97+ ///
98+ /// # Errors
99+ /// May return Err if cannot execute copy data.
100+ pub async fn copy_in < T , U > ( & self , statement : & T ) -> RustPSQLDriverPyResult < CopyInSink < U > >
91101 where
92102 T : ?Sized + ToStatement ,
93- U : Buf + ' static + Send
103+ U : Buf + ' static + Send ,
94104 {
95105 match self {
96- InnerConnection :: PoolConn ( pconn) => {
97- return Ok ( pconn. copy_in ( statement) . await ?)
98- }
99- InnerConnection :: SingleConn ( sconn) => {
100- return Ok ( sconn. copy_in ( statement) . await ?)
101- }
106+ PsqlpyConnection :: PoolConn ( pconn) => return Ok ( pconn. copy_in ( statement) . await ?) ,
107+ PsqlpyConnection :: SingleConn ( sconn) => return Ok ( sconn. copy_in ( statement) . await ?) ,
102108 }
103109 }
104110}
105111
106112#[ pyclass( subclass) ]
107113#[ derive( Clone ) ]
108114pub struct Connection {
109- db_client : Option < Arc < InnerConnection > > ,
115+ db_client : Option < Arc < PsqlpyConnection > > ,
110116 db_pool : Option < Pool > ,
111117}
112118
113119impl Connection {
114120 #[ must_use]
115- pub fn new ( db_client : Option < Arc < InnerConnection > > , db_pool : Option < Pool > ) -> Self {
121+ pub fn new ( db_client : Option < Arc < PsqlpyConnection > > , db_pool : Option < Pool > ) -> Self {
116122 Connection { db_client, db_pool }
117123 }
118124
119- pub fn db_client ( & self ) -> Option < Arc < InnerConnection > > {
120- return self . db_client . clone ( )
125+ #[ must_use]
126+ pub fn db_client ( & self ) -> Option < Arc < PsqlpyConnection > > {
127+ self . db_client . clone ( )
121128 }
122129
130+ #[ must_use]
123131 pub fn db_pool ( & self ) -> Option < Pool > {
124- return self . db_pool . clone ( )
132+ self . db_pool . clone ( )
125133 }
126134}
127135
@@ -151,7 +159,7 @@ impl Connection {
151159 . await ??;
152160 pyo3:: Python :: with_gil ( |gil| {
153161 let mut self_ = self_. borrow_mut ( gil) ;
154- self_. db_client = Some ( Arc :: new ( InnerConnection :: PoolConn ( db_connection) ) ) ;
162+ self_. db_client = Some ( Arc :: new ( PsqlpyConnection :: PoolConn ( db_connection) ) ) ;
155163 } ) ;
156164 return Ok ( self_) ;
157165 }
@@ -277,7 +285,7 @@ impl Connection {
277285 let db_client = pyo3:: Python :: with_gil ( |gil| self_. borrow ( gil) . db_client . clone ( ) ) ;
278286
279287 if let Some ( db_client) = db_client {
280- return Ok ( db_client. batch_execute ( & querystring) . await ? ) ;
288+ return db_client. batch_execute ( & querystring) . await ;
281289 }
282290
283291 Err ( RustPSQLDriverError :: ConnectionClosedError )
0 commit comments