@@ -5,23 +5,25 @@ import pf.Stdout
55import pf.Sqlite
66import 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
1923main ! : List Arg => Result {} _
2024main ! = |_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|
200206encode_status : TodoStatus -> [String Str ]
201207encode_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
0 commit comments