1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use crate::protocol::{ConnectionCodec, FormatCode, ProtocolError, RowDescription};
use bytes::{BufMut, BytesMut};
use chrono::{NaiveDate, NaiveDateTime};
use tokio_util::codec::Encoder;
pub struct DataRowBatch {
format_code: FormatCode,
num_cols: usize,
num_rows: usize,
data: BytesMut,
row: BytesMut,
}
impl DataRowBatch {
pub fn new(format_code: FormatCode, num_cols: usize) -> Self {
Self {
format_code,
num_cols,
num_rows: 0,
data: BytesMut::new(),
row: BytesMut::new(),
}
}
pub fn from_row_desc(desc: &RowDescription) -> Self {
Self::new(desc.format_code, desc.fields.len())
}
pub fn create_row(&mut self) -> DataRowWriter {
self.num_rows += 1;
DataRowWriter::new(self)
}
pub fn num_rows(&self) -> usize {
self.num_rows
}
}
macro_rules! primitive_write {
($name: ident, $type: ident) => {
#[allow(missing_docs)]
pub fn $name(&mut self, val: $type) {
match self.parent.format_code {
FormatCode::Text => self.write_value(&val.to_string().into_bytes()),
FormatCode::Binary => self.write_value(&val.to_be_bytes()),
};
}
};
}
pub struct DataRowWriter<'a> {
current_col: usize,
parent: &'a mut DataRowBatch,
}
impl<'a> DataRowWriter<'a> {
fn new(parent: &'a mut DataRowBatch) -> Self {
parent.row.put_i16(parent.num_cols as i16);
Self { current_col: 0, parent }
}
fn write_value(&mut self, data: &[u8]) {
self.current_col += 1;
self.parent.row.put_i32(data.len() as i32);
self.parent.row.put_slice(data);
}
pub fn write_null(&mut self) {
self.current_col += 1;
self.parent.row.put_i32(-1);
}
pub fn write_string(&mut self, val: &str) {
self.write_value(val.as_bytes());
}
fn pg_date_epoch() -> NaiveDate {
NaiveDate::from_ymd(2000, 1, 1)
}
pub fn write_date(&mut self, val: NaiveDate) {
match self.parent.format_code {
FormatCode::Binary => self.write_int4(val.signed_duration_since(Self::pg_date_epoch()).num_days() as i32),
FormatCode::Text => self.write_string(&val.to_string()),
}
}
pub fn write_timestamp(&mut self, val: NaiveDateTime) {
match self.parent.format_code {
FormatCode::Binary => {
self.write_int8(
val.signed_duration_since(Self::pg_date_epoch().and_hms(0, 0, 0))
.num_microseconds()
.unwrap(),
);
}
FormatCode::Text => self.write_string(&val.to_string()),
}
}
primitive_write!(write_int2, i16);
primitive_write!(write_int4, i32);
primitive_write!(write_int8, i64);
primitive_write!(write_float4, f32);
primitive_write!(write_float8, f64);
}
impl<'a> Drop for DataRowWriter<'a> {
fn drop(&mut self) {
assert_eq!(
self.parent.num_cols, self.current_col,
"dropped a row writer with an invalid number of columns"
);
self.parent.data.put_u8(b'D');
self.parent.data.put_i32((self.parent.row.len() + 4) as i32);
self.parent.data.extend(self.parent.row.split());
}
}
impl Encoder<DataRowBatch> for ConnectionCodec {
type Error = ProtocolError;
fn encode(&mut self, item: DataRowBatch, dst: &mut BytesMut) -> Result<(), Self::Error> {
dst.extend(item.data);
Ok(())
}
}