Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example/h-sql: improvements and simplifications #368

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions example/h-sql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,29 @@ prepared statements using
[Caqti](https://paurkedal.github.io/ocaml-caqti/caqti/Caqti_connect_sig/module-type-S/module-type-CONNECTION/index.html),
a library for talking to SQL databases:

> [!TIP]
> `let%lwt` is syntactic sugar for `Lwt.bind`, so the two forms below are equivalent (a third option would be using `>>=`).
>
> For more information, see [here](https://ocsigen.org/lwt/latest/api/Lwt#3_Callbacks).

```ocaml
module type DB = Caqti_lwt.CONNECTION
module T = Caqti_type

let list_comments =
let list_comments (module Db : DB) =
let query =
let open Caqti_request.Infix in
(T.unit ->* T.(tup2 int string))
(T.unit ->* T.(t2 int string))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tup2 is deprecated.

"SELECT id, text FROM comment" in
fun (module Db : DB) ->
let%lwt comments_or_error = Db.collect_list query () in
Caqti_lwt.or_fail comments_or_error
Lwt.bind (Db.collect_list query ()) Caqti_lwt.or_fail

let add_comment =
let add_comment text (module Db : DB) =
let query =
let open Caqti_request.Infix in
(T.string ->. T.unit)
"INSERT INTO comment (text) VALUES ($1)" in
fun text (module Db : DB) ->
let%lwt unit_or_error = Db.exec query text in
Caqti_lwt.or_fail unit_or_error
let%lwt unit_or_error = Db.exec query text in
Caqti_lwt.or_fail unit_or_error

let render comments request =
<html>
Expand Down
13 changes: 5 additions & 8 deletions example/h-sql/sql.eml.ml
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
module type DB = Caqti_lwt.CONNECTION
module T = Caqti_type

let list_comments =
let list_comments (module Db : DB) =
let query =
let open Caqti_request.Infix in
(T.unit ->* T.(t2 int string))
"SELECT id, text FROM comment" in
fun (module Db : DB) ->
let%lwt comments_or_error = Db.collect_list query () in
Caqti_lwt.or_fail comments_or_error
Lwt.bind (Db.collect_list query ()) Caqti_lwt.or_fail

let add_comment =
let add_comment text (module Db : DB) =
let query =
let open Caqti_request.Infix in
(T.string ->. T.unit)
"INSERT INTO comment (text) VALUES ($1)" in
fun text (module Db : DB) ->
let%lwt unit_or_error = Db.exec query text in
Caqti_lwt.or_fail unit_or_error
let%lwt unit_or_error = Db.exec query text in
Caqti_lwt.or_fail unit_or_error

let render comments request =
<html>
Expand Down
Loading