Skip to content

Commit 0d2d554

Browse files
committed
Add a ToStatement trait in tokio-postgres
1 parent 286ecdb commit 0d2d554

File tree

19 files changed

+408
-405
lines changed

19 files changed

+408
-405
lines changed

postgres-native-tls/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
//! .build()?;
3939
//! let connector = MakeTlsConnector::new(connector);
4040
//!
41-
//! let mut client = postgres::Client::connect(
41+
//! let client = postgres::Client::connect(
4242
//! "host=localhost user=postgres sslmode=require",
4343
//! connector,
4444
//! )?;

postgres-openssl/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
//! builder.set_ca_file("database_cert.pem")?;
3131
//! let connector = MakeTlsConnector::new(builder.build());
3232
//!
33-
//! let mut client = postgres::Client::connect(
33+
//! let client = postgres::Client::connect(
3434
//! "host=localhost user=postgres sslmode=require",
3535
//! connector,
3636
//! )?;

postgres/src/client.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ impl Client {
8484
where
8585
T: ?Sized + ToStatement,
8686
{
87-
let statement = query.__statement(self)?;
88-
executor::block_on(self.0.execute(&statement, params))
87+
executor::block_on(self.0.execute(query, params))
8988
}
9089

9190
/// Executes a statement, returning the resulting rows.
@@ -154,14 +153,13 @@ impl Client {
154153
/// ```
155154
pub fn query_iter<'a, T>(
156155
&'a mut self,
157-
query: &T,
158-
params: &[&(dyn ToSql + Sync)],
156+
query: &'a T,
157+
params: &'a [&(dyn ToSql + Sync)],
159158
) -> Result<impl FallibleIterator<Item = Row, Error = Error> + 'a, Error>
160159
where
161160
T: ?Sized + ToStatement,
162161
{
163-
let statement = query.__statement(self)?;
164-
Ok(Iter::new(self.0.query(&statement, params)))
162+
Ok(Iter::new(self.0.query(query, params)))
165163
}
166164

167165
/// Creates a new prepared statement.
@@ -249,8 +247,7 @@ impl Client {
249247
T: ?Sized + ToStatement,
250248
R: Read + Unpin,
251249
{
252-
let statement = query.__statement(self)?;
253-
executor::block_on(self.0.copy_in(&statement, params, CopyInStream(reader)))
250+
executor::block_on(self.0.copy_in(query, params, CopyInStream(reader)))
254251
}
255252

256253
/// Executes a `COPY TO STDOUT` statement, returning a reader of the resulting data.
@@ -274,14 +271,13 @@ impl Client {
274271
/// ```
275272
pub fn copy_out<'a, T>(
276273
&'a mut self,
277-
query: &T,
278-
params: &[&(dyn ToSql + Sync)],
274+
query: &'a T,
275+
params: &'a [&(dyn ToSql + Sync)],
279276
) -> Result<impl BufRead + 'a, Error>
280277
where
281278
T: ?Sized + ToStatement,
282279
{
283-
let statement = query.__statement(self)?;
284-
let stream = self.0.copy_out(&statement, params);
280+
let stream = self.0.copy_out(query, params);
285281
CopyOutReader::new(stream)
286282
}
287283

@@ -314,7 +310,7 @@ impl Client {
314310
/// them to this method!
315311
pub fn simple_query_iter<'a>(
316312
&'a mut self,
317-
query: &str,
313+
query: &'a str,
318314
) -> Result<impl FallibleIterator<Item = SimpleQueryMessage, Error = Error> + 'a, Error> {
319315
Ok(Iter::new(self.0.simple_query(query)))
320316
}

postgres/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ use tokio::runtime::{self, Runtime};
6262

6363
#[cfg(feature = "runtime")]
6464
pub use tokio_postgres::Socket;
65-
pub use tokio_postgres::{error, row, tls, types, Column, Portal, SimpleQueryMessage, Statement};
65+
pub use tokio_postgres::{
66+
error, row, tls, types, Column, Portal, SimpleQueryMessage, Statement, ToStatement,
67+
};
6668

6769
pub use crate::client::*;
6870
#[cfg(feature = "runtime")]
@@ -73,7 +75,6 @@ pub use crate::error::Error;
7375
pub use crate::row::{Row, SimpleQueryRow};
7476
#[doc(no_inline)]
7577
pub use crate::tls::NoTls;
76-
pub use crate::to_statement::*;
7778
pub use crate::transaction::*;
7879

7980
mod client;
@@ -82,7 +83,6 @@ pub mod config;
8283
mod copy_in_stream;
8384
mod copy_out_reader;
8485
mod iter;
85-
mod to_statement;
8686
mod transaction;
8787

8888
#[cfg(feature = "runtime")]

postgres/src/to_statement.rs

Lines changed: 0 additions & 59 deletions
This file was deleted.

postgres/src/transaction.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ impl<'a> Transaction<'a> {
4747
where
4848
T: ?Sized + ToStatement,
4949
{
50-
let statement = query.__statement(self)?;
51-
executor::block_on(self.0.execute(&statement, params))
50+
executor::block_on(self.0.execute(query, params))
5251
}
5352

5453
/// Like `Client::query`.
@@ -60,16 +59,15 @@ impl<'a> Transaction<'a> {
6059
}
6160

6261
/// Like `Client::query_iter`.
63-
pub fn query_iter<T>(
64-
&mut self,
65-
query: &T,
66-
params: &[&(dyn ToSql + Sync)],
67-
) -> Result<impl FallibleIterator<Item = Row, Error = Error>, Error>
62+
pub fn query_iter<'b, T>(
63+
&'b mut self,
64+
query: &'b T,
65+
params: &'b [&(dyn ToSql + Sync)],
66+
) -> Result<impl FallibleIterator<Item = Row, Error = Error> + 'b, Error>
6867
where
6968
T: ?Sized + ToStatement,
7069
{
71-
let statement = query.__statement(self)?;
72-
Ok(Iter::new(self.0.query(&statement, params)))
70+
Ok(Iter::new(self.0.query(query, params)))
7371
}
7472

7573
/// Binds parameters to a statement, creating a "portal".
@@ -86,8 +84,7 @@ impl<'a> Transaction<'a> {
8684
where
8785
T: ?Sized + ToStatement,
8886
{
89-
let statement = query.__statement(self)?;
90-
executor::block_on(self.0.bind(&statement, params))
87+
executor::block_on(self.0.bind(query, params))
9188
}
9289

9390
/// Continues execution of a portal, returning the next set of rows.
@@ -100,11 +97,11 @@ impl<'a> Transaction<'a> {
10097

10198
/// Like `query_portal`, except that it returns a fallible iterator over the resulting rows rather than buffering
10299
/// the entire response in memory.
103-
pub fn query_portal_iter(
104-
&mut self,
105-
portal: &Portal,
100+
pub fn query_portal_iter<'b>(
101+
&'b mut self,
102+
portal: &'b Portal,
106103
max_rows: i32,
107-
) -> Result<impl FallibleIterator<Item = Row, Error = Error>, Error> {
104+
) -> Result<impl FallibleIterator<Item = Row, Error = Error> + 'b, Error> {
108105
Ok(Iter::new(self.0.query_portal(&portal, max_rows)))
109106
}
110107

@@ -119,21 +116,19 @@ impl<'a> Transaction<'a> {
119116
T: ?Sized + ToStatement,
120117
R: Read + Unpin,
121118
{
122-
let statement = query.__statement(self)?;
123-
executor::block_on(self.0.copy_in(&statement, params, CopyInStream(reader)))
119+
executor::block_on(self.0.copy_in(query, params, CopyInStream(reader)))
124120
}
125121

126122
/// Like `Client::copy_out`.
127123
pub fn copy_out<'b, T>(
128-
&'a mut self,
129-
query: &T,
130-
params: &[&(dyn ToSql + Sync)],
124+
&'b mut self,
125+
query: &'b T,
126+
params: &'b [&(dyn ToSql + Sync)],
131127
) -> Result<impl BufRead + 'b, Error>
132128
where
133129
T: ?Sized + ToStatement,
134130
{
135-
let statement = query.__statement(self)?;
136-
let stream = self.0.copy_out(&statement, params);
131+
let stream = self.0.copy_out(query, params);
137132
CopyOutReader::new(stream)
138133
}
139134

@@ -145,7 +140,7 @@ impl<'a> Transaction<'a> {
145140
/// Like `Client::simple_query_iter`.
146141
pub fn simple_query_iter<'b>(
147142
&'b mut self,
148-
query: &str,
143+
query: &'b str,
149144
) -> Result<impl FallibleIterator<Item = SimpleQueryMessage, Error = Error> + 'b, Error> {
150145
Ok(Iter::new(self.0.simple_query(query)))
151146
}

tokio-postgres/src/bind.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,25 @@ use std::sync::Arc;
1010

1111
static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
1212

13-
pub async fn bind(
14-
client: Arc<InnerClient>,
13+
pub async fn bind<'a, I>(
14+
client: &Arc<InnerClient>,
1515
statement: Statement,
16-
bind: Result<PendingBind, Error>,
17-
) -> Result<Portal, Error> {
18-
let bind = bind?;
19-
20-
let mut responses = client.send(RequestMessages::Single(FrontendMessage::Raw(bind.buf)))?;
21-
22-
match responses.next().await? {
23-
Message::BindComplete => {}
24-
_ => return Err(Error::unexpected_message()),
25-
}
26-
27-
Ok(Portal::new(&client, bind.name, statement))
28-
}
29-
30-
pub struct PendingBind {
31-
buf: Vec<u8>,
32-
name: String,
33-
}
34-
35-
pub fn encode<'a, I>(statement: &Statement, params: I) -> Result<PendingBind, Error>
16+
params: I,
17+
) -> Result<Portal, Error>
3618
where
3719
I: IntoIterator<Item = &'a dyn ToSql>,
3820
I::IntoIter: ExactSizeIterator,
3921
{
4022
let name = format!("p{}", NEXT_ID.fetch_add(1, Ordering::SeqCst));
41-
let mut buf = query::encode_bind(statement, params, &name)?;
23+
let mut buf = query::encode_bind(&statement, params, &name)?;
4224
frontend::sync(&mut buf);
4325

44-
Ok(PendingBind { buf, name })
26+
let mut responses = client.send(RequestMessages::Single(FrontendMessage::Raw(buf)))?;
27+
28+
match responses.next().await? {
29+
Message::BindComplete => {}
30+
_ => return Err(Error::unexpected_message()),
31+
}
32+
33+
Ok(Portal::new(client, name, statement))
4534
}

0 commit comments

Comments
 (0)