sfackler / rust-postgres

Native PostgreSQL driver for the Rust programming language
Apache License 2.0
3.43k stars 436 forks source link

copy x FROM STDIN format( CSV) #1050

Closed jamessewell closed 1 year ago

jamessewell commented 1 year ago

Is there still a way of doing the above from async code?

I can only seem to find working examples for the binary protocol (I don't know the column defs so I can't use this).

sfackler commented 1 year ago

copy_in just returns a Sink of bytes - you can write a CSV into that.

stevenliebregt commented 1 year ago

I'm also looking to do this, I have a Vec<u8> that I want to send to that sink but can't find a method that accepts that, which one could I use?

sfackler commented 1 year ago

The sink consumes types implementing the Buf trait. The simplest thing to do would be to convert your Vec into Bytes: Bytes::from(my_vec).

stevenliebregt commented 1 year ago

Through which method should I do that? Or can you perhaps provide a tiny example, my current code looks like:

let file = File::open("data.tsv").await.unwrap();
let mut reader = BufReader::new(file);

let copy_in = client.copy_in("COPY person (name, age) FROM STDIN WITH (HEADER MATCH)").await.unwrap();

pin_mut!(copy_in);

let mut buffer = [0; 8192];

loop {
    let bytes_read = reader.read(&mut buffer).await.unwrap();
    if bytes_read == 0 {
        break;
    }

    copy_in.<how to send here with &buffer[..bytes_read]>.await.unwrap();
}

copy_in.finish().await.unwrap();
sfackler commented 1 year ago

https://docs.rs/futures/latest/futures/sink/trait.SinkExt.html#method.send