Skip to content

add query_raw_txt for transaction #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 4 additions & 81 deletions tokio-postgres/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ use crate::copy_both::CopyBothDuplex;
use crate::copy_out::CopyOutStream;
#[cfg(feature = "runtime")]
use crate::keepalive::KeepaliveConfig;
use crate::prepare::get_type;
use crate::query::RowStream;
use crate::simple_query::SimpleQueryStream;
use crate::statement::Column;
#[cfg(feature = "runtime")]
use crate::tls::MakeTlsConnect;
use crate::tls::TlsConnect;
Expand All @@ -22,7 +20,7 @@ use crate::{
CopyInSink, Error, Row, SimpleQueryMessage, Statement, ToStatement, Transaction,
TransactionBuilder,
};
use bytes::{Buf, BufMut, BytesMut};
use bytes::{Buf, BytesMut};
use fallible_iterator::FallibleIterator;
use futures_channel::mpsc;
use futures_util::{future, pin_mut, ready, StreamExt, TryStreamExt};
Expand Down Expand Up @@ -380,86 +378,11 @@ impl Client {
/// to save a roundtrip
pub async fn query_raw_txt<'a, S, I>(&self, query: S, params: I) -> Result<RowStream, Error>
where
S: AsRef<str>,
S: AsRef<str> + Sync + Send,
I: IntoIterator<Item = Option<S>>,
I::IntoIter: ExactSizeIterator,
I::IntoIter: ExactSizeIterator + Sync + Send,
{
let params = params.into_iter();
let params_len = params.len();

let buf = self.inner.with_buf(|buf| {
// Parse, anonymous portal
frontend::parse("", query.as_ref(), std::iter::empty(), buf).map_err(Error::encode)?;
// Bind, pass params as text, retrieve as binary
match frontend::bind(
"", // empty string selects the unnamed portal
"", // empty string selects the unnamed prepared statement
std::iter::empty(), // all parameters use the default format (text)
params,
|param, buf| match param {
Some(param) => {
buf.put_slice(param.as_ref().as_bytes());
Ok(postgres_protocol::IsNull::No)
}
None => Ok(postgres_protocol::IsNull::Yes),
},
Some(0), // all text
buf,
) {
Ok(()) => Ok(()),
Err(frontend::BindError::Conversion(e)) => Err(Error::to_sql(e, 0)),
Err(frontend::BindError::Serialization(e)) => Err(Error::encode(e)),
}?;

// Describe portal to typecast results
frontend::describe(b'P', "", buf).map_err(Error::encode)?;
// Execute
frontend::execute("", 0, buf).map_err(Error::encode)?;
// Sync
frontend::sync(buf);

Ok(buf.split().freeze())
})?;

let mut responses = self
.inner
.send(RequestMessages::Single(FrontendMessage::Raw(buf)))?;

// now read the responses

match responses.next().await? {
Message::ParseComplete => {}
_ => return Err(Error::unexpected_message()),
}
match responses.next().await? {
Message::BindComplete => {}
_ => return Err(Error::unexpected_message()),
}
let row_description = match responses.next().await? {
Message::RowDescription(body) => Some(body),
Message::NoData => None,
_ => return Err(Error::unexpected_message()),
};

// construct statement object

let parameters = vec![Type::UNKNOWN; params_len];

let mut columns = vec![];
if let Some(row_description) = row_description {
let mut it = row_description.fields();
while let Some(field) = it.next().map_err(Error::parse)? {
// NB: for some types that function may send a query to the server. At least in
// raw text mode we don't need that info and can skip this.
let type_ = get_type(&self.inner, field.type_oid()).await?;
let column = Column::new(field.name().to_string(), type_, field);
columns.push(column);
}
}

let statement = Statement::new_text(&self.inner, "".to_owned(), parameters, columns);

Ok(RowStream::new(statement, responses))
query::query_txt(&self.inner, query, params).await
}

/// Executes a statement, returning the number of rows modified.
Expand Down
25 changes: 25 additions & 0 deletions tokio-postgres/src/generic_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ pub trait GenericClient: private::Sealed {
I: IntoIterator<Item = P> + Sync + Send,
I::IntoIter: ExactSizeIterator;

/// Like `Client::query_raw_txt`.
async fn query_raw_txt<'a, S, I>(&self, query: S, params: I) -> Result<RowStream, Error>
where
S: AsRef<str> + Sync + Send,
I: IntoIterator<Item = Option<S>> + Sync + Send,
I::IntoIter: ExactSizeIterator + Sync + Send;

/// Like `Client::prepare`.
async fn prepare(&self, query: &str) -> Result<Statement, Error>;

Expand Down Expand Up @@ -133,6 +140,15 @@ impl GenericClient for Client {
self.query_raw(statement, params).await
}

async fn query_raw_txt<'a, S, I>(&self, query: S, params: I) -> Result<RowStream, Error>
where
S: AsRef<str> + Sync + Send,
I: IntoIterator<Item = Option<S>> + Sync + Send,
I::IntoIter: ExactSizeIterator + Sync + Send,
{
self.query_raw_txt(query, params).await
}

async fn prepare(&self, query: &str) -> Result<Statement, Error> {
self.prepare(query).await
}
Expand Down Expand Up @@ -215,6 +231,15 @@ impl GenericClient for Transaction<'_> {
self.query_raw(statement, params).await
}

async fn query_raw_txt<'a, S, I>(&self, query: S, params: I) -> Result<RowStream, Error>
where
S: AsRef<str> + Sync + Send,
I: IntoIterator<Item = Option<S>> + Sync + Send,
I::IntoIter: ExactSizeIterator + Sync + Send,
{
self.query_raw_txt(query, params).await
}

async fn prepare(&self, query: &str) -> Result<Statement, Error> {
self.prepare(query).await
}
Expand Down
94 changes: 92 additions & 2 deletions tokio-postgres/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
use crate::client::{InnerClient, Responses};
use crate::codec::FrontendMessage;
use crate::connection::RequestMessages;
use crate::prepare::get_type;
use crate::types::{BorrowToSql, IsNull};
use crate::{Error, Portal, Row, Statement};
use bytes::{Bytes, BytesMut};
use crate::{Column, Error, Portal, Row, Statement};
use bytes::{BufMut, Bytes, BytesMut};
use fallible_iterator::FallibleIterator;
use futures_util::{ready, Stream};
use log::{debug, log_enabled, Level};
use pin_project_lite::pin_project;
use postgres_protocol::message::backend::Message;
use postgres_protocol::message::frontend;
use postgres_types::Type;
use std::fmt;
use std::marker::PhantomPinned;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

struct BorrowToSqlParamsDebug<'a, T>(&'a [T]);
Expand Down Expand Up @@ -57,6 +61,92 @@ where
})
}

pub async fn query_txt<S, I>(
client: &Arc<InnerClient>,
query: S,
params: I,
) -> Result<RowStream, Error>
where
S: AsRef<str> + Sync + Send,
I: IntoIterator<Item = Option<S>>,
I::IntoIter: ExactSizeIterator,
{
let params = params.into_iter();
let params_len = params.len();

let buf = client.with_buf(|buf| {
// Parse, anonymous portal
frontend::parse("", query.as_ref(), std::iter::empty(), buf).map_err(Error::encode)?;
// Bind, pass params as text, retrieve as binary
match frontend::bind(
"", // empty string selects the unnamed portal
"", // empty string selects the unnamed prepared statement
std::iter::empty(), // all parameters use the default format (text)
params,
|param, buf| match param {
Some(param) => {
buf.put_slice(param.as_ref().as_bytes());
Ok(postgres_protocol::IsNull::No)
}
None => Ok(postgres_protocol::IsNull::Yes),
},
Some(0), // all text
buf,
) {
Ok(()) => Ok(()),
Err(frontend::BindError::Conversion(e)) => Err(Error::to_sql(e, 0)),
Err(frontend::BindError::Serialization(e)) => Err(Error::encode(e)),
}?;

// Describe portal to typecast results
frontend::describe(b'P', "", buf).map_err(Error::encode)?;
// Execute
frontend::execute("", 0, buf).map_err(Error::encode)?;
// Sync
frontend::sync(buf);

Ok(buf.split().freeze())
})?;

let mut responses = client.send(RequestMessages::Single(FrontendMessage::Raw(buf)))?;

// now read the responses

match responses.next().await? {
Message::ParseComplete => {}
_ => return Err(Error::unexpected_message()),
}
match responses.next().await? {
Message::BindComplete => {}
_ => return Err(Error::unexpected_message()),
}
let row_description = match responses.next().await? {
Message::RowDescription(body) => Some(body),
Message::NoData => None,
_ => return Err(Error::unexpected_message()),
};

// construct statement object

let parameters = vec![Type::UNKNOWN; params_len];

let mut columns = vec![];
if let Some(row_description) = row_description {
let mut it = row_description.fields();
while let Some(field) = it.next().map_err(Error::parse)? {
// NB: for some types that function may send a query to the server. At least in
// raw text mode we don't need that info and can skip this.
let type_ = get_type(client, field.type_oid()).await?;
let column = Column::new(field.name().to_string(), type_, field);
columns.push(column);
}
}

let statement = Statement::new_text(client, "".to_owned(), parameters, columns);

Ok(RowStream::new(statement, responses))
}

pub async fn query_portal(
client: &InnerClient,
portal: &Portal,
Expand Down
10 changes: 10 additions & 0 deletions tokio-postgres/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ impl<'a> Transaction<'a> {
self.client.query_raw(statement, params).await
}

/// Like `Client::query_raw_txt`.
pub async fn query_raw_txt<S, I>(&self, query: S, params: I) -> Result<RowStream, Error>
where
S: AsRef<str> + Sync + Send,
I: IntoIterator<Item = Option<S>>,
I::IntoIter: ExactSizeIterator + Sync + Send,
{
self.client.query_raw_txt(query, params).await
}

/// Like `Client::execute`.
pub async fn execute<T>(
&self,
Expand Down