1
1
use crate :: client:: { InnerClient , Responses } ;
2
2
use crate :: codec:: FrontendMessage ;
3
3
use crate :: connection:: RequestMessages ;
4
- use crate :: types:: { IsNull , ToSql } ;
4
+ use crate :: types:: { BorrowToSql , IsNull } ;
5
5
use crate :: { Error , Portal , Row , Statement } ;
6
6
use bytes:: { Bytes , BytesMut } ;
7
7
use futures:: { ready, Stream } ;
8
8
use log:: { debug, log_enabled, Level } ;
9
9
use pin_project_lite:: pin_project;
10
10
use postgres_protocol:: message:: backend:: Message ;
11
11
use postgres_protocol:: message:: frontend;
12
+ use std:: fmt;
12
13
use std:: marker:: PhantomPinned ;
13
14
use std:: pin:: Pin ;
14
15
use std:: task:: { Context , Poll } ;
15
16
16
- pub async fn query < ' a , I > (
17
+ struct BorrowToSqlParamsDebug < ' a , T : BorrowToSql > ( & ' a [ T ] ) ;
18
+ impl < ' a , T : BorrowToSql > std:: fmt:: Debug for BorrowToSqlParamsDebug < ' a , T > {
19
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
20
+ f. debug_list ( )
21
+ . entries ( self . 0 . iter ( ) . map ( |x| x. borrow_to_sql ( ) ) )
22
+ . finish ( )
23
+ }
24
+ }
25
+
26
+ pub async fn query < P , I > (
17
27
client : & InnerClient ,
18
28
statement : Statement ,
19
29
params : I ,
20
30
) -> Result < RowStream , Error >
21
31
where
22
- I : IntoIterator < Item = & ' a dyn ToSql > ,
32
+ P : BorrowToSql ,
33
+ I : IntoIterator < Item = P > ,
23
34
I :: IntoIter : ExactSizeIterator ,
24
35
{
25
36
let buf = if log_enabled ! ( Level :: Debug ) {
26
37
let params = params. into_iter ( ) . collect :: < Vec < _ > > ( ) ;
27
38
debug ! (
28
39
"executing statement {} with parameters: {:?}" ,
29
40
statement. name( ) ,
30
- params,
41
+ BorrowToSqlParamsDebug ( params. as_slice ( ) ) ,
31
42
) ;
32
43
encode ( client, & statement, params) ?
33
44
} else {
@@ -61,21 +72,22 @@ pub async fn query_portal(
61
72
} )
62
73
}
63
74
64
- pub async fn execute < ' a , I > (
75
+ pub async fn execute < P , I > (
65
76
client : & InnerClient ,
66
77
statement : Statement ,
67
78
params : I ,
68
79
) -> Result < u64 , Error >
69
80
where
70
- I : IntoIterator < Item = & ' a dyn ToSql > ,
81
+ P : BorrowToSql ,
82
+ I : IntoIterator < Item = P > ,
71
83
I :: IntoIter : ExactSizeIterator ,
72
84
{
73
85
let buf = if log_enabled ! ( Level :: Debug ) {
74
86
let params = params. into_iter ( ) . collect :: < Vec < _ > > ( ) ;
75
87
debug ! (
76
88
"executing statement {} with parameters: {:?}" ,
77
89
statement. name( ) ,
78
- params,
90
+ BorrowToSqlParamsDebug ( params. as_slice ( ) ) ,
79
91
) ;
80
92
encode ( client, & statement, params) ?
81
93
} else {
@@ -114,9 +126,10 @@ async fn start(client: &InnerClient, buf: Bytes) -> Result<Responses, Error> {
114
126
Ok ( responses)
115
127
}
116
128
117
- pub fn encode < ' a , I > ( client : & InnerClient , statement : & Statement , params : I ) -> Result < Bytes , Error >
129
+ pub fn encode < P , I > ( client : & InnerClient , statement : & Statement , params : I ) -> Result < Bytes , Error >
118
130
where
119
- I : IntoIterator < Item = & ' a dyn ToSql > ,
131
+ P : BorrowToSql ,
132
+ I : IntoIterator < Item = P > ,
120
133
I :: IntoIter : ExactSizeIterator ,
121
134
{
122
135
client. with_buf ( |buf| {
@@ -127,14 +140,15 @@ where
127
140
} )
128
141
}
129
142
130
- pub fn encode_bind < ' a , I > (
143
+ pub fn encode_bind < P , I > (
131
144
statement : & Statement ,
132
145
params : I ,
133
146
portal : & str ,
134
147
buf : & mut BytesMut ,
135
148
) -> Result < ( ) , Error >
136
149
where
137
- I : IntoIterator < Item = & ' a dyn ToSql > ,
150
+ P : BorrowToSql ,
151
+ I : IntoIterator < Item = P > ,
138
152
I :: IntoIter : ExactSizeIterator ,
139
153
{
140
154
let params = params. into_iter ( ) ;
@@ -152,7 +166,7 @@ where
152
166
statement. name ( ) ,
153
167
Some ( 1 ) ,
154
168
params. zip ( statement. params ( ) ) . enumerate ( ) ,
155
- |( idx, ( param, ty) ) , buf| match param. to_sql_checked ( ty, buf) {
169
+ |( idx, ( param, ty) ) , buf| match param. borrow_to_sql ( ) . to_sql_checked ( ty, buf) {
156
170
Ok ( IsNull :: No ) => Ok ( postgres_protocol:: IsNull :: No ) ,
157
171
Ok ( IsNull :: Yes ) => Ok ( postgres_protocol:: IsNull :: Yes ) ,
158
172
Err ( e) => {
0 commit comments