rs_table2pgcopy/
lib.rs

1use std::io;
2
3use std::io::BufWriter;
4use std::io::Write;
5
6use postgres::GenericClient;
7
8pub fn tablename2pgcopy2writer<W, C>(
9    trusted_table_name: &str,
10    client: &mut C,
11    writer: &mut W,
12) -> Result<(), io::Error>
13where
14    C: GenericClient,
15    W: Write,
16{
17    let query: String = format!("COPY {trusted_table_name} TO STDOUT WITH BINARY");
18    let mut rdr = client.copy_out(&query).map_err(io::Error::other)?;
19    io::copy(&mut rdr, writer)?;
20    Ok(())
21}
22
23pub fn tablename2pgcopy2stdout<C>(trusted_table_name: &str, client: &mut C) -> Result<(), io::Error>
24where
25    C: GenericClient,
26{
27    let o = io::stdout();
28    let mut l = o.lock();
29    {
30        let mut bw = BufWriter::new(&mut l);
31        tablename2pgcopy2writer(trusted_table_name, client, &mut bw)?;
32        bw.flush()?;
33    }
34    l.flush()
35}