Add bind and query_portal to sync API
This commit is contained in:
parent
23552b44a4
commit
919fa52a5e
@ -6,6 +6,7 @@ use tokio::runtime::{self, Runtime};
|
||||
#[cfg(feature = "runtime")]
|
||||
mod builder;
|
||||
mod client;
|
||||
mod portal;
|
||||
mod query;
|
||||
mod statement;
|
||||
mod transaction;
|
||||
@ -17,6 +18,7 @@ mod test;
|
||||
#[cfg(feature = "runtime")]
|
||||
pub use crate::builder::*;
|
||||
pub use crate::client::*;
|
||||
pub use crate::portal::*;
|
||||
pub use crate::query::*;
|
||||
pub use crate::statement::*;
|
||||
pub use crate::transaction::*;
|
||||
|
1
postgres/src/portal.rs
Normal file
1
postgres/src/portal.rs
Normal file
@ -0,0 +1 @@
|
||||
pub struct Portal(pub(crate) tokio_postgres::Portal);
|
@ -198,3 +198,33 @@ fn copy_out() {
|
||||
|
||||
client.batch_execute("SELECT 1").unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn portal() {
|
||||
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
|
||||
|
||||
client
|
||||
.batch_execute(
|
||||
"
|
||||
CREATE TEMPORARY TABLE foo (id INT);
|
||||
|
||||
INSERT INTO foo (id) VALUES (1), (2), (3);
|
||||
",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let mut transaction = client.transaction().unwrap();
|
||||
|
||||
let portal = transaction
|
||||
.bind("SELECT * FROM foo ORDER BY id", &[])
|
||||
.unwrap();
|
||||
|
||||
let rows = transaction.query_portal(&portal, 2).unwrap();
|
||||
assert_eq!(rows.len(), 2);
|
||||
assert_eq!(rows[0].get::<_, i32>(0), 1);
|
||||
assert_eq!(rows[1].get::<_, i32>(0), 2);
|
||||
|
||||
let rows = transaction.query_portal(&portal, 2).unwrap();
|
||||
assert_eq!(rows.len(), 1);
|
||||
assert_eq!(rows[0].get::<_, i32>(0), 3);
|
||||
}
|
||||
|
@ -1,8 +1,9 @@
|
||||
use futures::{Future, Stream};
|
||||
use std::io::Read;
|
||||
use tokio_postgres::types::{ToSql, Type};
|
||||
use tokio_postgres::{Error, Row};
|
||||
|
||||
use crate::{Client, CopyOutReader, Query, Statement};
|
||||
use crate::{Client, CopyOutReader, Portal, Query, Statement};
|
||||
|
||||
pub struct Transaction<'a> {
|
||||
client: &'a mut Client,
|
||||
@ -73,6 +74,26 @@ impl<'a> Transaction<'a> {
|
||||
self.client.query(query, params)
|
||||
}
|
||||
|
||||
pub fn bind<T>(&mut self, query: &T, params: &[&dyn ToSql]) -> Result<Portal, Error>
|
||||
where
|
||||
T: ?Sized + Query,
|
||||
{
|
||||
let statement = query.__statement(&mut self.client)?;
|
||||
self.client
|
||||
.get_mut()
|
||||
.bind(&statement.0, params)
|
||||
.wait()
|
||||
.map(Portal)
|
||||
}
|
||||
|
||||
pub fn query_portal(&mut self, portal: &Portal, max_rows: i32) -> Result<Vec<Row>, Error> {
|
||||
self.client
|
||||
.get_mut()
|
||||
.query_portal(&portal.0, max_rows)
|
||||
.collect()
|
||||
.wait()
|
||||
}
|
||||
|
||||
pub fn copy_in<T, R>(
|
||||
&mut self,
|
||||
query: &T,
|
||||
|
Loading…
Reference in New Issue
Block a user