Skip to content

Commit f1d85ea

Browse files
committed
Nullable values
1 parent e7b5739 commit f1d85ea

File tree

5 files changed

+41
-6
lines changed

5 files changed

+41
-6
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v0.8.0 - 2023-09-22
4+
5+
- The `nullable` function has been added.
6+
37
## v0.7.0 - 2023-08-03
48

59
- Updated syntax for Gleam v0.30.0.

gleam.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name = "sqlight"
2-
version = "0.7.0"
2+
version = "0.8.0"
33
licences = ["Apache-2.0"]
44
description = "Use SQLite from Gleam!"
55

manifest.toml

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
packages = [
55
{ name = "esqlite", version = "0.8.6", build_tools = ["rebar3"], requirements = [], otp_app = "esqlite", source = "hex", outer_checksum = "607E45F4DA42601D8F530979417F57A4CD629AB49085891849302057E68EA188" },
6-
{ name = "gleam_stdlib", version = "0.27.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "9DBDD21B48C654182CDD8AA15ACF85E8E74A0438583C68BD7EF08BE89F999C6F" },
7-
{ name = "gleeunit", version = "0.10.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "ECEA2DE4BE6528D36AFE74F42A21CDF99966EC36D7F25DEB34D47DD0F7977BAF" },
6+
{ name = "gleam_stdlib", version = "0.30.2", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "8D8BF3790AA31176B1E1C0B517DD74C86DA8235CF3389EA02043EE4FD82AE3DC" },
7+
{ name = "gleeunit", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "1397E5C4AC4108769EE979939AC39BF7870659C5AFB714630DEEEE16B8272AD5" },
88
]
99

1010
[requirements]
11-
esqlite = "~> 0.8"
12-
gleam_stdlib = "~> 0.25"
13-
gleeunit = "~> 0.8"
11+
esqlite = { version = "~> 0.8" }
12+
gleam_stdlib = { version = "~> 0.25" }
13+
gleeunit = { version = "~> 0.8" }

src/sqlight.gleam

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import gleam/list
22
import gleam/string
33
import gleam/result
4+
import gleam/option.{None, Option, Some}
45
import gleam/dynamic.{Decoder, Dynamic}
56

67
pub type Connection
@@ -404,6 +405,16 @@ fn coerce_value(a: a) -> Value
404405
@external(javascript, "./sqlight_ffi.js", "exec")
405406
fn exec_(a: String, b: Connection) -> Result(Nil, Error)
406407

408+
/// Convert a Gleam `Option` to an SQLite nullable value, to be used an argument
409+
/// to a query.
410+
///
411+
pub fn nullable(inner_type: fn(t) -> Value, value: Option(t)) -> Value {
412+
case value {
413+
Some(value) -> inner_type(value)
414+
None -> null()
415+
}
416+
}
417+
407418
/// Convert a Gleam `Int` to an SQLite int, to be used an argument to a
408419
/// query.
409420
///

test/sqlight_test.gleam

+20
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,23 @@ pub fn query_error_test() {
259259
dynamic.element(0, dynamic.int),
260260
)
261261
}
262+
263+
pub fn bind_nullable_test() {
264+
use conn <- connect()
265+
266+
let assert Ok([option.Some(12_345)]) =
267+
sqlight.query(
268+
"select ?",
269+
conn,
270+
[sqlight.nullable(sqlight.int, option.Some(12_345))],
271+
dynamic.element(0, dynamic.optional(dynamic.int)),
272+
)
273+
274+
let assert Ok([option.None]) =
275+
sqlight.query(
276+
"select ?",
277+
conn,
278+
[sqlight.nullable(sqlight.int, option.None)],
279+
dynamic.element(0, dynamic.optional(dynamic.int)),
280+
)
281+
}

0 commit comments

Comments
 (0)