|
| 1 | +//! Utilities for working with the PostgreSQL binary copy format. |
| 2 | +
|
| 3 | +use crate::types::{ToSql, Type}; |
| 4 | +use crate::{CopyInWriter, CopyOutReader, Error, Rt}; |
| 5 | +use fallible_iterator::FallibleIterator; |
| 6 | +use futures::StreamExt; |
| 7 | +use std::pin::Pin; |
| 8 | +#[doc(inline)] |
| 9 | +pub use tokio_postgres::binary_copy::BinaryCopyOutRow; |
| 10 | +use tokio_postgres::binary_copy::{self, BinaryCopyOutStream}; |
| 11 | + |
| 12 | +/// A type which serializes rows into the PostgreSQL binary copy format. |
| 13 | +/// |
| 14 | +/// The copy *must* be explicitly completed via the `finish` method. If it is not, the copy will be aborted. |
| 15 | +pub struct BinaryCopyInWriter<'a> { |
| 16 | + runtime: Rt<'a>, |
| 17 | + sink: Pin<Box<binary_copy::BinaryCopyInWriter>>, |
| 18 | +} |
| 19 | + |
| 20 | +impl<'a> BinaryCopyInWriter<'a> { |
| 21 | + /// Creates a new writer which will write rows of the provided types. |
| 22 | + pub fn new(writer: CopyInWriter<'a>, types: &[Type]) -> BinaryCopyInWriter<'a> { |
| 23 | + let stream = writer |
| 24 | + .sink |
| 25 | + .into_unpinned() |
| 26 | + .expect("writer has already been written to"); |
| 27 | + |
| 28 | + BinaryCopyInWriter { |
| 29 | + runtime: writer.runtime, |
| 30 | + sink: Box::pin(binary_copy::BinaryCopyInWriter::new(stream, types)), |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + /// Writes a single row. |
| 35 | + /// |
| 36 | + /// # Panics |
| 37 | + /// |
| 38 | + /// Panics if the number of values provided does not match the number expected. |
| 39 | + pub fn write(&mut self, values: &[&(dyn ToSql + Sync)]) -> Result<(), Error> { |
| 40 | + self.runtime.block_on(self.sink.as_mut().write(values)) |
| 41 | + } |
| 42 | + |
| 43 | + /// A maximally-flexible version of `write`. |
| 44 | + /// |
| 45 | + /// # Panics |
| 46 | + /// |
| 47 | + /// Panics if the number of values provided does not match the number expected. |
| 48 | + pub fn write_raw<'b, I>(&mut self, values: I) -> Result<(), Error> |
| 49 | + where |
| 50 | + I: IntoIterator<Item = &'b dyn ToSql>, |
| 51 | + I::IntoIter: ExactSizeIterator, |
| 52 | + { |
| 53 | + self.runtime.block_on(self.sink.as_mut().write_raw(values)) |
| 54 | + } |
| 55 | + |
| 56 | + /// Completes the copy, returning the number of rows added. |
| 57 | + /// |
| 58 | + /// This method *must* be used to complete the copy process. If it is not, the copy will be aborted. |
| 59 | + pub fn finish(mut self) -> Result<u64, Error> { |
| 60 | + self.runtime.block_on(self.sink.as_mut().finish()) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/// An iterator of rows deserialized from the PostgreSQL binary copy format. |
| 65 | +pub struct BinaryCopyOutIter<'a> { |
| 66 | + runtime: Rt<'a>, |
| 67 | + stream: Pin<Box<BinaryCopyOutStream>>, |
| 68 | +} |
| 69 | + |
| 70 | +impl<'a> BinaryCopyOutIter<'a> { |
| 71 | + /// Creates a new iterator from a raw copy out reader and the types of the columns being returned. |
| 72 | + pub fn new(reader: CopyOutReader<'a>, types: &[Type]) -> BinaryCopyOutIter<'a> { |
| 73 | + let stream = reader |
| 74 | + .stream |
| 75 | + .into_unpinned() |
| 76 | + .expect("reader has already been read from"); |
| 77 | + |
| 78 | + BinaryCopyOutIter { |
| 79 | + runtime: reader.runtime, |
| 80 | + stream: Box::pin(BinaryCopyOutStream::new(stream, types)), |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl FallibleIterator for BinaryCopyOutIter<'_> { |
| 86 | + type Item = BinaryCopyOutRow; |
| 87 | + type Error = Error; |
| 88 | + |
| 89 | + fn next(&mut self) -> Result<Option<BinaryCopyOutRow>, Error> { |
| 90 | + self.runtime.block_on(self.stream.next()).transpose() |
| 91 | + } |
| 92 | +} |
0 commit comments