forked from transact-rs/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.rs
More file actions
237 lines (189 loc) 路 7.62 KB
/
Copy patharray.rs
File metadata and controls
237 lines (189 loc) 路 7.62 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use bytes::Buf;
use crate::decode::Decode;
use crate::encode::{Encode, IsNull};
use crate::error::BoxDynError;
use crate::postgres::type_info::PgType;
use crate::postgres::{PgArgumentBuffer, PgTypeInfo, PgValueFormat, PgValueRef, Postgres};
use crate::types::Type;
impl<T> Type<Postgres> for [Option<T>]
where
[T]: Type<Postgres>,
{
fn type_info() -> PgTypeInfo {
<[T] as Type<Postgres>>::type_info()
}
}
impl<T> Type<Postgres> for Vec<Option<T>>
where
Vec<T>: Type<Postgres>,
{
fn type_info() -> PgTypeInfo {
<Vec<T> as Type<Postgres>>::type_info()
}
}
impl<'q, T> Encode<'q, Postgres> for Vec<T>
where
for<'a> &'a [T]: Encode<'q, Postgres>,
T: Encode<'q, Postgres>,
Self: Type<Postgres>,
{
#[inline]
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
self.as_slice().encode_by_ref(buf)
}
}
impl<'q, T> Encode<'q, Postgres> for &'_ [T]
where
T: Encode<'q, Postgres>,
Self: Type<Postgres>,
{
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
buf.extend(&1_i32.to_be_bytes()); // number of dimensions
buf.extend(&0_i32.to_be_bytes()); // flags
// element type
match T::type_info().0 {
PgType::DeclareWithName(name) => buf.push_type_hole(&name),
ty => {
buf.extend(&ty.oid().to_be_bytes());
}
}
buf.extend(&(self.len() as i32).to_be_bytes()); // len
buf.extend(&1_i32.to_be_bytes()); // lower bound
for element in self.iter() {
// allocate space for the length of the encoded element
let el_len_offset = buf.len();
buf.extend(&0_i32.to_be_bytes());
let el_start = buf.len();
if let IsNull::Yes = element.encode_by_ref(buf) {
// NULL is encoded as -1 for a length
buf[el_len_offset..el_start].copy_from_slice(&(-1_i32).to_be_bytes());
} else {
let el_end = buf.len();
let el_len = el_end - el_start;
// now we can go back and update the length
buf[el_len_offset..el_start].copy_from_slice(&(el_len as i32).to_be_bytes());
}
}
IsNull::No
}
}
// TODO: Array decoding in PostgreSQL *could* allow 'r (row) lifetime of elements if we can figure
// out a way for the TEXT encoding to use some shared memory somewhere.
impl<'r, T> Decode<'r, Postgres> for Vec<T>
where
T: for<'a> Decode<'a, Postgres>,
Self: Type<Postgres>,
{
fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
let element_type_info = T::type_info();
let format = value.format();
match format {
PgValueFormat::Binary => {
// https://github.com/postgres/postgres/blob/a995b371ae29de2d38c4b7881cf414b1560e9746/src/backend/utils/adt/arrayfuncs.c#L1548
let mut buf = value.as_bytes()?;
// number of dimensions in the array
let ndim = buf.get_i32();
if ndim == 0 {
// zero dimensions is an empty array
return Ok(Vec::new());
}
if ndim != 1 {
return Err(format!("encountered an array of {} dimensions; only one-dimensional arrays are supported", ndim).into());
}
// appears to have been used in the past to communicate potential NULLS
// but reading source code back through our supported postgres versions (9.5+)
// this is never used for anything
let _flags = buf.get_i32();
// the OID of the element
let _element_type = buf.get_u32();
// length of the array axis
let len = buf.get_i32();
// the lower bound, we only support arrays starting from "1"
let lower = buf.get_i32();
if lower != 1 {
return Err(format!("encountered an array with a lower bound of {} in the first dimension; only arrays starting at one are supported", lower).into());
}
let mut elements = Vec::with_capacity(len as usize);
for _ in 0..len {
let mut element_len = buf.get_i32();
let element_val = if element_len == -1 {
element_len = 0;
None
} else {
Some(&buf[..(element_len as usize)])
};
elements.push(T::decode(PgValueRef {
value: element_val,
row: None,
type_info: element_type_info.clone(),
format,
})?);
buf.advance(element_len as usize);
}
Ok(elements)
}
PgValueFormat::Text => {
let s = value.as_str()?;
// https://github.com/postgres/postgres/blob/a995b371ae29de2d38c4b7881cf414b1560e9746/src/backend/utils/adt/arrayfuncs.c#L718
// trim the wrapping braces
let s = &s[1..(s.len() - 1)];
if s.is_empty() {
// short-circuit empty arrays up here
return Ok(Vec::new());
}
// NOTE: Nearly *all* types use ',' as the sequence delimiter. Yes, there is one
// that does not. The BOX (not PostGIS) type uses ';' as a delimiter.
// TODO: When we add support for BOX we need to figure out some way to make the
// delimiter selection
let delimiter = ',';
let mut done = false;
let mut in_quotes = false;
let mut in_escape = false;
let mut value = String::with_capacity(10);
let mut chars = s.chars();
let mut elements = Vec::with_capacity(4);
while !done {
loop {
match chars.next() {
Some(ch) => match ch {
_ if in_escape => {
value.push(ch);
in_escape = false;
}
'"' => {
in_quotes = !in_quotes;
}
'\\' => {
in_escape = true;
}
_ if ch == delimiter && !in_quotes => {
break;
}
_ => {
value.push(ch);
}
},
None => {
done = true;
break;
}
}
}
let value_opt = if value == "NULL" {
None
} else {
Some(value.as_bytes())
};
elements.push(T::decode(PgValueRef {
value: value_opt,
row: None,
type_info: element_type_info.clone(),
format,
})?);
value.clear();
}
Ok(elements)
}
}
}
}