-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlight.gleam
66 lines (62 loc) · 1.49 KB
/
sqlight.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import kreator/query.{type Query}
import gleam/dynamic.{type Decoder}
import gleam/list
import gleam/result.{try}
import kreator/value.{type Value, Bool, Float, Int, List, Null, Raw, String}
import sqlight.{SqlightError}
fn convert_values(values: List(Value)) -> List(sqlight.Value) {
list.flat_map(values, fn(value) {
case value {
String(v) -> [sqlight.text(v)]
Int(v) -> [sqlight.int(v)]
Float(v) -> [sqlight.float(v)]
Bool(v) -> [sqlight.bool(v)]
Raw(v) -> [sqlight.text(v)]
List(list) -> convert_values(list)
Null -> [sqlight.null()]
}
})
}
pub fn run(
query: Query,
on conn: sqlight.Connection,
expecting dynamic: Decoder(t),
) -> Result(List(t), sqlight.Error) {
sqlight.query(
query.sql,
with: convert_values(query.bindings),
on: conn,
expecting: dynamic,
)
}
pub fn run_nil(
query: Query,
on conn: sqlight.Connection,
) -> Result(Nil, sqlight.Error) {
sqlight.query(
query.sql,
with: convert_values(query.bindings),
on: conn,
expecting: dynamic.dynamic,
)
|> result.map(fn(_) { Nil })
}
pub fn first(
query: Query,
on conn: sqlight.Connection,
expecting dynamic: Decoder(t),
) -> Result(t, sqlight.Error) {
use result <- try(sqlight.query(
query.sql,
with: convert_values(query.bindings),
on: conn,
expecting: dynamic,
))
result
|> list.first()
|> result.replace_error(SqlightError(
code: sqlight.GenericError,
message: "No rows in result set",
offset: -1,
))
}