pub struct Encoder<W: Write> { /* private fields */ }Expand description
Low-level encoder for binary format.
End users are required to manually call all necessary methods in a right order.
use pgcopy::Encoder;
let mut buf: Vec<u8> = vec![];
let mut encoder = Encoder::new(&mut buf);
encoder.write_header().unwrap();
encoder.write_tuple(3).unwrap(); // First tuple with three columns
encoder.write_smallint(1).unwrap(); // First column
encoder.write_bool(false).unwrap(); // Second
encoder.write_str("first").unwrap(); // Third
encoder.write_tuple(3).unwrap(); // Second tuple
encoder.write_smallint(2).unwrap();
encoder.write_bool(true).unwrap();
encoder.write_str("second").unwrap();
encoder.write_trailer().unwrap();
println!("{:?}", encoder.get_ref());Implementations§
Source§impl<W> Encoder<W>where
W: Write,
impl<W> Encoder<W>where
W: Write,
Sourcepub fn get_mut(&mut self) -> &mut W
pub fn get_mut(&mut self) -> &mut W
Acquires a mutable reference to the underlying writer.
Note that mutating the output/input state of the stream may corrupt this object, so care must be taken when using this method.
Sourcepub fn write_header(&mut self) -> Result<()>
pub fn write_header(&mut self) -> Result<()>
Writes binary format header.
Caller is required to invoke this method first before starting to write tuples data.
Sourcepub fn write_trailer(&mut self) -> Result<()>
pub fn write_trailer(&mut self) -> Result<()>
Writes binary format trailer.
Caller is required to invoke this method last immediately after writing tuples data.
Sourcepub fn write_tuple(&mut self, fields: i16) -> Result<()>
pub fn write_tuple(&mut self, fields: i16) -> Result<()>
Starts a new tuple.
Each tuple begins with a signed 16-bit integer count of the number of fields in the tuple. Presently, all tuples in a table will have the same count.
Sourcepub fn write_null(&mut self) -> Result<()>
pub fn write_null(&mut self) -> Result<()>
Writes NULL as a column value.
Sourcepub fn write_smallint(&mut self, value: i16) -> Result<()>
pub fn write_smallint(&mut self, value: i16) -> Result<()>
Writes smallint type value.
Sourcepub fn write_bigint(&mut self, value: i64) -> Result<()>
pub fn write_bigint(&mut self, value: i64) -> Result<()>
Writes bigint type value.
Sourcepub fn write_real(&mut self, value: f32) -> Result<()>
pub fn write_real(&mut self, value: f32) -> Result<()>
Writes real type value.
Sourcepub fn write_double(&mut self, value: f64) -> Result<()>
pub fn write_double(&mut self, value: f64) -> Result<()>
Writes double precision type value.
Sourcepub fn write_str<T: AsRef<str>>(&mut self, value: T) -> Result<()>
pub fn write_str<T: AsRef<str>>(&mut self, value: T) -> Result<()>
Writes character type value.
Any of character varying(n), character(n) or text column type should be handled by this method.
Sourcepub fn write_timestamp<T: Timestamp>(&mut self, value: T) -> Result<()>
pub fn write_timestamp<T: Timestamp>(&mut self, value: T) -> Result<()>
Writes timestamp type value.
See Timestamp type implementors for available options here.
Sourcepub fn write_timestamp_with_time_zone<T: TimestampWithTimeZone>(
&mut self,
value: T,
) -> Result<()>
pub fn write_timestamp_with_time_zone<T: TimestampWithTimeZone>( &mut self, value: T, ) -> Result<()>
Writes timestamp with time zone type value.
See TimestampWithTimeZone type implementors for available options here.
Sourcepub fn write_date<T: Date>(&mut self, value: T) -> Result<()>
pub fn write_date<T: Date>(&mut self, value: T) -> Result<()>
Writes date type value.
See Date type implementors for available options here.
Sourcepub fn write_time<T: Time>(&mut self, value: T) -> Result<()>
pub fn write_time<T: Time>(&mut self, value: T) -> Result<()>
Writes time type value.
See Time type implementors for available options here.
Sourcepub fn write_macaddr<T: MacAddr>(&mut self, value: T) -> Result<()>
pub fn write_macaddr<T: MacAddr>(&mut self, value: T) -> Result<()>
Writes macaddr type value.
See MacAddr type implementors for available options here.