Conversation
bhansconnect
commented
Jan 3, 2025
Comment on lines
+577
to
+590
| decoder = \fn -> | ||
| \name -> | ||
| @SqlDecode \cols -> | ||
|
|
||
| found = List.findFirstIndex cols \x -> x == name | ||
| when found is | ||
| Ok index -> | ||
| \stmt -> | ||
| try column_value! stmt index | ||
| |> fn | ||
|
|
||
| Err NotFound -> | ||
| \_ -> | ||
| Err (FieldNotFound name) |
Member
Author
There was a problem hiding this comment.
Not sure why the formatter decided to change this.
|
Example re-written to work with syntax on latest (not nightly) at the time of writing: main! = |_args|
db_path = Env.var!("DB_PATH")?
query_todos_by_status! = Sqlite.prepare_query_many!({
path: db_path,
query: "SELECT id, task FROM todos WHERE status = :status;",
bindings: |status| [{ name: ":status", value: String status }],
rows: { Sqlite.decode_record <-
id: Sqlite.i64 "id" |> Sqlite.map_value Num.toStr,
task: Sqlite.str "task",
},
})?
todo = query_todos_by_status!("todo")?
Stdout.line!("Todo Tasks:")?
List.forEachTry!(todo, |{ id, task }|
Stdout.line! "\tid: ${id}, task: ${task}"
)
completed = query_todos_by_status!("completed")?
Stdout.line!("\nCompleted Tasks:")?
List.forEachTry!(completed, |{ id, task }|
Stdout.line! "\tid: ${id}, task: ${task}"
)
Ok({})and exec_transaction! = prepare_transaction!({ path: "path/to/database.db" })
exec_transaction!(|{}|
Sqlite.execute!({
path: "path/to/database.db",
query: "INSERT INTO users (first, last) VALUES (:first, :last);",
bindings: [
{ name: ":first", value: String "John" },
{ name: ":last", value: String "Smith" },
],
})?
# Oh no, hit an error. Need to rollback.
# Note: Error could be anything.
Err(NeedToRollback)
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The better query ideas from https://roc.zulipchat.com/#narrow/channel/383402-API-design/topic/Sqlite.20APIs
and