use crate::connection::stream::PgConnection;
use crate::error::Result;
use crate::pipeline::{encode_pipeline, read_pipeline_responses, PipelineQuery, QueryResult};
pub struct PipelineBatch {
queries: Vec<PipelineQuery>,
}
impl PipelineBatch {
pub fn new() -> Self {
Self {
queries: Vec::new(),
}
}
pub fn add(
&mut self,
sql: impl Into<String>,
param_types: Vec<u32>,
params: Vec<Option<Vec<u8>>>,
) {
self.queries.push(PipelineQuery {
sql: sql.into(),
param_types,
params,
});
}
pub fn len(&self) -> usize {
self.queries.len()
}
pub fn is_empty(&self) -> bool {
self.queries.is_empty()
}
pub(crate) async fn execute(self, conn: &mut PgConnection) -> Result<Vec<QueryResult>> {
if self.queries.is_empty() {
return Ok(Vec::new());
}
let count = self.queries.len();
encode_pipeline(conn.write_buf(), &self.queries);
conn.send().await?;
read_pipeline_responses(conn, count).await
}
}
impl Default for PipelineBatch {
fn default() -> Self {
Self::new()
}
}