Skip to content

Commit 3608a9a

Browse files
authored
added nullable to sqlite-everything (#358)
1 parent b5dec68 commit 3608a9a

File tree

4 files changed

+41
-19
lines changed

4 files changed

+41
-19
lines changed

ci/all_tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ for roc_file in $EXAMPLES_DIR*.roc; do
104104
elif [ "$base_file" == "sqlite-basic.roc" ]; then
105105
DB_PATH=${EXAMPLES_DIR}todos.db $ROC dev $roc_file $ROC_BUILD_FLAGS
106106
elif [ "$base_file" == "sqlite-everything.roc" ]; then
107-
DB_PATH=${EXAMPLES_DIR}todos.db $ROC dev $roc_file $ROC_BUILD_FLAGS
107+
DB_PATH=${EXAMPLES_DIR}todos2.db $ROC dev $roc_file $ROC_BUILD_FLAGS
108108
elif [ "$base_file" == "temp-dir.roc" ]; then
109109
$ROC dev $roc_file $ROC_BUILD_FLAGS --linker=legacy
110110
elif [ "$base_file" == "file-accessed-modified-created-time.roc" ] && [ "$IS_MUSL" == "1" ]; then

ci/expect_scripts/sqlite-everything.exp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ set timeout 7
77

88
source ./ci/expect_scripts/shared-code.exp
99

10-
set env(DB_PATH) $env(EXAMPLES_DIR)todos.db
10+
set env(DB_PATH) $env(EXAMPLES_DIR)todos2.db
1111

1212
spawn $env(EXAMPLES_DIR)sqlite-everything
1313

1414
expect "All Todos:" {
15-
expect "\tid: 1, task: Prepare for AoC, status: Completed" {
16-
expect "\tid: 2, task: Win all the Stars!, status: InProgress" {
17-
expect "\tid: 3, task: Share my ❤️ for Roc, status: Todo" {
15+
expect "\tid: 1, task: Prepare for AoC, status: Completed, edited: Null" {
16+
expect "\tid: 2, task: Win all the Stars!, status: InProgress, edited: NotEdited" {
17+
expect "\tid: 3, task: Share my ❤️ for Roc, status: Todo, edited: NotEdited" {
1818
expect "In-progress Todos:" {
1919
expect "\tIn-progress tasks: Win all the Stars!" {
2020
expect "Todos sorted by length of task description:" {

examples/sqlite-everything.roc

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,25 @@ import pf.Stdout
55
import pf.Sqlite
66
import pf.Arg exposing [Arg]
77

8-
# To run this example: check the README.md in this folder and set `export DB_PATH=./examples/todos.db`
8+
# To run this example: check the README.md in this folder and set `export DB_PATH=./examples/todos2.db`
99

1010
# Demo of basic Sqlite usage
1111

1212
# Sql that was used to create the table:
1313
# CREATE TABLE todos (
1414
# id INTEGER PRIMARY KEY AUTOINCREMENT,
1515
# task TEXT NOT NULL,
16-
# status TEXT NOT NULL
16+
# status TEXT NOT NULL,
17+
# edited BOOLEAN,
1718
# );
19+
# Note 1: the edited column is nullable, this is for demonstration purposes only.
20+
# We recommend using `NOT NULL` when possible.
21+
# Note 2: boolean is "fake" in sqlite https://www.sqlite.org/datatype3.html
1822

1923
main! : List Arg => Result {} _
2024
main! = |_args|
2125
db_path = Env.var!("DB_PATH")?
2226

23-
# TODO demo create table, demo nullable field
24-
2527
# Example: print all rows
2628

2729
all_todos = Sqlite.query_many!({
@@ -33,15 +35,17 @@ main! = |_args|
3335
id: Sqlite.i64("id"),
3436
task: Sqlite.str("task"),
3537
status: Sqlite.str("status") |> Sqlite.map_value_result(decode_status),
38+
# bools in sqlite are actually integers
39+
edited: Sqlite.nullable_i64("edited") |> Sqlite.map_value(decode_edited),
3640
},
3741
})?
3842

3943
Stdout.line!("All Todos:")?
4044
4145
List.for_each_try!(
4246
all_todos,
43-
|{ id, task, status }|
44-
Stdout.line!("\tid: ${Num.to_str(id)}, task: ${task}, status: ${Inspect.to_str(status)}"),
47+
|{ id, task, status, edited }|
48+
Stdout.line!("\tid: ${Num.to_str(id)}, task: ${task}, status: ${Inspect.to_str(status)}, edited: ${Inspect.to_str(edited)}"),
4549
)?
4650

4751
# Example: filter rows by status
@@ -67,46 +71,48 @@ main! = |_args|
6771
6872
Sqlite.execute!({
6973
path: db_path,
70-
query: "INSERT INTO todos (task, status) VALUES (:task, :status);",
74+
query: "INSERT INTO todos (task, status, edited) VALUES (:task, :status, :edited);",
7175
bindings: [
7276
{ name: ":task", value: String("Make sql example.") },
7377
{ name: ":status", value: encode_status(InProgress) },
78+
{ name: ":edited", value: encode_edited(NotEdited) },
7479
],
7580
})?
7681
7782
# Example: insert multiple rows from a Roc list
7883
79-
todos_list : List ({task : Str, status : TodoStatus})
84+
todos_list : List ({task : Str, status : TodoStatus, edited : EditedValue})
8085
todos_list = [
81-
{ task: "Insert Roc list 1", status: Todo },
82-
{ task: "Insert Roc list 2", status: Todo },
83-
{ task: "Insert Roc list 3", status: Todo },
86+
{ task: "Insert Roc list 1", status: Todo, edited: NotEdited },
87+
{ task: "Insert Roc list 2", status: Todo, edited: NotEdited },
88+
{ task: "Insert Roc list 3", status: Todo, edited: NotEdited },
8489
]
8590
8691
values_str =
8792
todos_list
8893
|> List.map_with_index(
8994
|_, indx|
9095
indx_str = Num.to_str(indx)
91-
"(:task${indx_str}, :status${indx_str})",
96+
"(:task${indx_str}, :status${indx_str}, :edited${indx_str})",
9297
)
9398
|> Str.join_with(", ")
9499
95100
all_bindings =
96101
todos_list
97102
|> List.map_with_index(
98-
|{ task, status }, indx|
103+
|{ task, status, edited }, indx|
99104
indx_str = Num.to_str(indx)
100105
[
101106
{ name: ":task${indx_str}", value: String(task) },
102107
{ name: ":status${indx_str}", value: encode_status(status) },
108+
{ name: ":edited${indx_str}", value: encode_edited(edited) },
103109
],
104110
)
105111
|> List.join
106112
107113
Sqlite.execute!({
108114
path: db_path,
109-
query: "INSERT INTO todos (task, status) VALUES ${values_str};",
115+
query: "INSERT INTO todos (task, status, edited) VALUES ${values_str};",
110116
bindings: all_bindings,
111117
})?
112118
@@ -200,3 +206,19 @@ status_to_str = |status|
200206
encode_status : TodoStatus -> [String Str]
201207
encode_status = |status|
202208
String(status_to_str(status))
209+
210+
EditedValue : [Edited, NotEdited, Null]
211+
212+
decode_edited : [NotNull I64, Null] -> EditedValue
213+
decode_edited = |edited_val|
214+
when edited_val is
215+
NotNull 1 -> Edited
216+
NotNull 0 -> NotEdited
217+
_ -> Null
218+
219+
encode_edited : EditedValue -> [Integer I64, Null]
220+
encode_edited = |edited|
221+
when edited is
222+
Edited -> Integer(1)
223+
NotEdited -> Integer(0)
224+
Null -> Null

examples/todos2.db

12 KB
Binary file not shown.

0 commit comments

Comments
 (0)