Skip to content
Open
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
18 changes: 13 additions & 5 deletions postgres/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ use std::{
sync::{Arc, Mutex},
};

use xitca_io::bytes::BytesMut;
use xitca_unsafe_collection::no_hash::NoHashBuilder;

use super::{
copy::{CopyIn, CopyOut},
driver::{
DriverTx,
codec::{
Response,
encode::{self, Encode},
},
codec::{Response, encode::Encode},
},
error::Error,
query::Query,
Expand Down Expand Up @@ -131,6 +129,7 @@ where
/// [`Driver`]: crate::driver::Driver
pub struct Client {
pub(crate) tx: DriverTx,
pub(crate) buf: Mutex<BytesMut>,
pub(crate) cache: Box<ClientCache>,
}

Expand Down Expand Up @@ -260,6 +259,7 @@ impl Client {
pub(crate) fn new(tx: DriverTx, session: Session) -> Self {
Self {
tx,
buf: Mutex::new(BytesMut::with_capacity(4096)),
cache: Box::new(ClientCache {
session,
type_info: Mutex::new(CachedTypeInfo {
Expand All @@ -286,7 +286,15 @@ impl Query for Client {
where
S: Encode,
{
encode::send_encode_query(&self.tx, stmt)
let (res1, buf) = {
let mut buf = self.buf.lock().unwrap();
let len = buf.len();
let res1 = stmt.encode(&mut buf).inspect_err(|_| buf.truncate(len))?;
(res1, buf.split())
};

let res2 = self.tx.send(buf)?;
Ok((res1, res2))
}
}

Expand Down
9 changes: 6 additions & 3 deletions postgres/src/driver/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ use crate::{
types::BorrowToSql,
};

use super::DriverTx;
pub struct Request {
pub(super) tx: ResponseSender,
pub(super) buf: BytesMut,
}

pub(super) fn request_pair() -> (ResponseSender, Response) {
pub(super) fn request_pair(buf: BytesMut) -> (Request, Response) {
let (tx, rx) = unbounded_channel();
(
tx,
Request { tx, buf },
Response {
rx,
buf: BytesMut::new(),
Expand Down
9 changes: 1 addition & 8 deletions postgres/src/driver/codec/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
};

use super::{
AsParams, DriverTx, Response,
AsParams,
response::{
IntoResponse, IntoRowStreamGuard, NoOpIntoRowStream, StatementCreateResponse, StatementCreateResponseBlocking,
},
Expand Down Expand Up @@ -236,13 +236,6 @@ impl<'s> Encode for PortalQuery<'s> {
}
}

pub(crate) fn send_encode_query<S>(tx: &DriverTx, stmt: S) -> Result<(S::Output, Response), Error>
where
S: Encode,
{
tx.send(|buf| stmt.encode(buf))
}

fn encode_bind<P>(stmt: &str, types: &[Type], params: P, portal: &str, buf: &mut BytesMut) -> Result<(), Error>
where
P: AsParams,
Expand Down
Loading
Loading