-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgo.gleam
43 lines (40 loc) · 1.05 KB
/
pgo.gleam
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
import kreator/query.{type Query}
import gleam/dynamic.{type Decoder}
import gleam/list
import gleam/result
import kreator/value.{type Value, Bool, Float, Int, List, Null, Raw, String}
import gleam/pgo.{type Connection, type QueryError, type Returned}
fn convert_values(values: List(Value)) -> List(pgo.Value) {
list.flat_map(values, fn(value) {
case value {
String(v) -> [pgo.text(v)]
Int(v) -> [pgo.int(v)]
Float(v) -> [pgo.float(v)]
Bool(v) -> [pgo.bool(v)]
Raw(v) -> [pgo.text(v)]
List(list) -> convert_values(list)
Null -> [pgo.null()]
}
})
}
pub fn run(
query: Query,
on conn: Connection,
expecting dynamic: Decoder(t),
) -> Result(Returned(t), QueryError) {
pgo.execute(
query.sql,
with: convert_values(query.bindings),
on: conn,
expecting: dynamic,
)
}
pub fn run_nil(query: Query, on conn: Connection) -> Result(Nil, QueryError) {
pgo.execute(
query.sql,
with: convert_values(query.bindings),
on: conn,
expecting: dynamic.dynamic,
)
|> result.map(fn(_) { Nil })
}