tobogganSe connecter

packages

The Louise packages, each released on its own

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

384 lignes · 10197 octetsBrut · Historique
1/// A row's typed accessors, by column name (issue gabla108/louise#145 §2). Errors name
2/// the column and the wanted and found types. Values may be in text or
3/// binary format (the field's format code); each accessor reads both.
4
5import std/bytes
6import std/dec as decs
7import std/float as floats
8import std/list
9import std/result
10import std/string
11import std/time.{type Timestamp}
12
13import binary
14import client.{type Row, Row}
15import numeric
16import protocol.{type Field}
17import uuid
18
19/// The value of column `name` as text. `Error` on no such column; `None`
20/// is SQL `NULL`.
21pub fn text :: row: Row name: String -> Result<Option<String>, String>
22{
23 use (found, _) <- result.try (column row name)
24 match found
25 { None -> Ok(None)
26 , Some(b) -> match bytes.to_string b
27 { Ok(s) -> Ok(Some(s)), Error(e) -> Error("column {name}: {e}") }
28 }
29}
30
31/// The value of column `name` as an `Int`.
32pub fn int :: row: Row name: String -> Result<Option<Int>, String>
33{
34 use (found, field) <- result.try (column row name)
35 match found
36 { None -> Ok(None)
37 , Some(b) -> if field.format == 1
38 {
39 // Binary: big-endian, the width by type.
40 match field.type_oid
41 { 21 -> Ok(Some(binary.i16_at b 0)) // int2
42 , 23 -> Ok(Some(binary.i32_at b 0)) // int4
43 , 20 -> Ok(Some(binary.i64_at b 0)) // int8
44 , _ ->
45 Error("column {name}: a binary integer of type oid {field.type_oid}")
46 }
47 }
48 else
49 {
50 match bytes.to_string b
51 { Ok(s) ->
52 string.to_int s
53 |> result.map fn n { Some(n) }
54 |> result.map_error fn e { "column {name}: {e}" }
55 , Error(e) -> Error("column {name}: {e}")
56 }
57 }
58 }
59}
60
61/// The value of column `name` as a `Float`.
62pub fn float :: row: Row name: String -> Result<Option<Float>, String>
63{
64 use (found, field) <- result.try (column row name)
65 match found
66 { None -> Ok(None)
67 , Some(b) -> if field.format == 1
68 {
69 match field.type_oid
70 { 700 -> Ok(Some(binary.f32_at b 0)) // float4
71 , 701 -> Ok(Some(binary.f64_at b 0)) // float8
72 , _ -> Error("column {name}: a binary float of type oid {field.type_oid}")
73 }
74 }
75 else
76 {
77 match bytes.to_string b
78 { Ok(s) ->
79 floats.parse s
80 |> result.map fn f { Some(f) }
81 |> result.map_error fn e { "column {name}: {e}" }
82 , Error(e) -> Error("column {name}: {e}")
83 }
84 }
85 }
86}
87
88/// The value of column `name` as a `Bool`.
89pub fn bool :: row: Row name: String -> Result<Option<Bool>, String>
90{
91 use (found, field) <- result.try (column row name)
92 match found
93 { None -> Ok(None)
94 , Some(b) -> // One byte, read in place ('t'/'f' in text, 1/0 in binary).
95 match (field.format == 1, bytes.length b == 1, bytes.read_u8 b 0)
96 { (True, True, 0) -> Ok(Some(False))
97 , (True, True, 1) -> Ok(Some(True))
98 , (True, _, _) -> Error("column {name}: not a binary bool")
99 , (False, True, 116) -> Ok(Some(True)) // t
100 , (False, True, 102) -> Ok(Some(False)) // f
101 , (False, _, _) -> Error("column {name}: not a bool")
102 }
103 }
104}
105
106/// The value of column `name` as bytes (binary `bytea` is the raw bytes;
107/// text is the `\x` hex form, decoded).
108pub fn bytea :: row: Row name: String -> Result<Option<Bytes>, String>
109{
110 use (found, field) <- result.try (column row name)
111 match found
112 { None -> Ok(None)
113 , Some(b) -> if field.format == 1
114 {
115 Ok(Some(b))
116 }
117 else
118 {
119 match bytes.to_string b
120 { Ok
121 ( s
122 ) -> if string.starts_with s "\\x"
123 {
124 use decoded <-
125 result.try
126 ( unhex (string_tail s 2)
127 |> result.map_error fn e { "column {name}: {e}" }
128 )
129 Ok(Some(decoded))
130 }
131 else
132 {
133 Error("column {name}: bytea not in hex form")
134 }
135 , Error(e) -> Error("column {name}: {e}")
136 }
137 }
138 }
139}
140
141/// The value of column `name` as a `uuid.Uuid`.
142pub fn uuid :: row: Row name: String -> Result<Option<uuid.Uuid>, String>
143{
144 use (found, field) <- result.try (column row name)
145 match found
146 { None -> Ok(None)
147 , Some(b) -> if field.format == 1
148 {
149 match uuid.from_bytes b
150 { Some(id) -> Ok(Some(id))
151 , None -> Error("column {name}: not 16 bytes of uuid")
152 }
153 }
154 else
155 {
156 use s <- result.try (text_of name b)
157 match uuid.from_rfc s
158 { Some(id) -> Ok(Some(id)), None -> Error("column {name}: not a UUID") }
159 }
160 }
161}
162
163/// The value of column `name` as a `time.Timestamp`. PostgreSQL's text is
164/// nearly RFC 3339; its `+00` zone suffix becomes `+00:00`. Binary is
165/// microseconds since 2000-01-01 UTC.
166pub fn timestamptz :: row: Row name: String -> Result<Option<Timestamp>, String>
167{
168 use (found, field) <- result.try (column row name)
169 match found
170 { None -> Ok(None)
171 , Some(b) -> if field.format == 1
172 {
173 let micros = binary.i64_at b 0
174 Ok(Some(time.from_unix_ns ((micros + 946684800000000) * 1000)))
175 }
176 else
177 {
178 use s <- result.try (text_of name b)
179 time.parse (rfc3339 s)
180 |> result.map fn t { Some(t) }
181 |> result.map_error fn e { "column {name}: {e}" }
182 }
183 }
184}
185
186/// The value of column `name` as a `decs.Dec` (PostgreSQL `NUMERIC`,
187/// OID 1700). Past 18 places the value rounds half-even (the rule of
188/// `std/dec`); NaN, an infinity, or an out-of-range value is an `Error`.
189pub fn dec :: row: Row name: String -> Result<Option<decs.Dec>, String>
190{
191 use (found, field) <- result.try (column row name)
192 match found
193 { None -> Ok(None)
194 , Some(b) -> if field.format == 1
195 {
196 if field.type_oid == 1700
197 {
198 numeric.to_dec b
199 |> result.map fn d { Some(d) }
200 |> result.map_error fn e { "column {name}: {e}" }
201 }
202 else
203 {
204 Error("column {name}: a binary numeric of type oid {field.type_oid}")
205 }
206 }
207 else
208 {
209 match bytes.to_string b
210 { Ok(s) ->
211 decs.parse s
212 |> result.map fn d { Some(d) }
213 |> result.map_error fn e { "column {name}: {e}" }
214 , Error(e) -> Error("column {name}: {e}")
215 }
216 }
217 }
218}
219
220// ---- helpers --------------------------------------------------------------------
221
222/// The column's raw value and its field, by name.
223fn column :: row: Row name: String -> Result<(Option<Bytes>, Field), String>
224{
225 let Row(columns, values) = row
226 match find columns values name
227 { None -> Error("no column named {name}"), Some(found) -> Ok(found) }
228}
229
230/// The value and its field, walking the columns and values together.
231fn find ::
232 columns: List<Field>
233 values: List<Option<Bytes>>
234 name: String
235 -> Option<(Option<Bytes>, Field)>
236{
237 match (columns, values)
238 { ([f, ..fs], [v, ..vs]) -> if f.name == name
239 {
240 Some((v, f))
241 }
242 else
243 {
244 find fs vs name
245 }
246 , _ -> None
247 }
248}
249
250/// The bytes as text, or an error naming the column.
251fn text_of :: name: String b: Bytes -> Result<String, String>
252{
253 match bytes.to_string b
254 { Ok(s) -> Ok(s), Error(e) -> Error("column {name}: {e}") }
255}
256
257/// PostgreSQL's timestamptz text is nearly RFC 3339: a `+00` zone suffix
258/// becomes `+00:00`.
259fn rfc3339 :: s: String -> String
260{
261 let b = bytes.from_string s
262 match bytes.get b (bytes.length b - 3)
263 { Some(43) -> s <> ":00" // +
264 , Some(45) -> s <> ":00" // -
265 , _ -> s
266 }
267}
268
269/// `s` without its first `n` characters (ASCII prefixes).
270fn string_tail :: s: String n: Int -> String
271{
272 let b = bytes.from_string s
273 match bytes.to_string (bytes.slice b n (bytes.length b))
274 { Ok(t) -> t, Error(_) -> s }
275}
276
277/// The bytes of a hex string (both cases), decoded pairwise into one
278/// buffer — not per-byte lists, which cost ~77 bytes per byte on a large
279/// `bytea` (issue gabla108/louise#311).
280fn unhex :: hex: String -> Result<Bytes, String>
281{
282 let b = bytes.from_string hex
283 unhex_loop b 0 (bytes.builder_with_capacity (bytes.length b / 2))
284}
285
286fn unhex_loop :: b: Bytes at: Int acc: bytes.Builder -> Result<Bytes, String>
287{
288 if at >= bytes.length b
289 {
290 Ok(bytes.build acc)
291 }
292 else if at + 1 >= bytes.length b
293 {
294 Error("odd hex length")
295 }
296 else
297 {
298 use hi <- result.try (hex_value (bytes.read_u8 b at))
299 use lo <- result.try (hex_value (bytes.read_u8 b (at + 1)))
300 unhex_loop b (at + 2) (bytes.append_byte acc (hi * 16 + lo))
301 }
302}
303
304fn hex_value :: c: Int -> Result<Int, String>
305{
306 if c >= 48 && c <= 57
307 {
308 Ok(c - 48)
309 }
310 else if c >= 65 && c <= 70
311 {
312 Ok(c - 65 + 10)
313 }
314 else if c >= 97 && c <= 102
315 {
316 Ok(c - 97 + 10)
317 }
318 else
319 {
320 Error("not a hex digit")
321 }
322}
323
324// ---- tests ------------------------------------------------------------------------
325
326import std/test.{assert, assert_equal}
327
328@test
329fn unhex_decodes_both_cases_and_rejects_bad_input ::
330{
331 assert (unhex "" == Ok(bytes.empty ())) "empty"
332 assert
333 (unhex "deadbeef" == Ok(bytes.from_list [222, 173, 190, 239]))
334 "lowercase"
335 assert
336 (unhex "DEADBEEF" == Ok(bytes.from_list [222, 173, 190, 239]))
337 "uppercase"
338 assert
339 (unhex "00010f10ff" == Ok(bytes.from_list [0, 1, 15, 16, 255]))
340 "digits"
341 assert (unhex "abc" == Error("odd hex length")) "odd length"
342 assert (unhex "zz" == Error("not a hex digit")) "not hex"
343}
344
345@test
346fn unhex_decodes_a_large_string_in_one_pass ::
347{
348 // 200_000 bytes as 400_000 hex chars: a per-byte-lists decode would not
349 // fit the test budget (issue gabla108/louise#311).
350 let text =
351 bytes.build_string
352 ( list.fold
353 (list.range 1 200_000)
354 (bytes.builder_with_capacity 400_000)
355 fn b i
356 {
357 let b = bytes.append_string b (hex_pair (i % 251 / 16))
358 bytes.append_string b (hex_pair (i % 251 % 16))
359 }
360 )
361 match unhex text
362 { Error(e) -> panic e
363 , Ok(decoded) ->
364 {
365 assert_equal (bytes.length decoded) 200_000
366 assert_equal (bytes.read_u8 decoded 0) 1
367 assert_equal (bytes.read_u8 decoded 199_999) (200_000 % 251)
368 }
369 }
370}
371
372/// A nibble as a hex character (the test's encoder).
373fn hex_pair :: d: Int -> String
374{
375 match d
376 { 10 -> "a"
377 , 11 -> "b"
378 , 12 -> "c"
379 , 13 -> "d"
380 , 14 -> "e"
381 , 15 -> "f"
382 , _ -> "{d}"
383 }
384}