forked from roddyyaga/ppx_rapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'async-lwt' of ../ppx_rapper_clone into lemaetech-async-lwt
- Loading branch information
Showing
21 changed files
with
381 additions
and
57 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
(library | ||
(name examples_async) | ||
(libraries async ppx_rapper_async) | ||
(preprocess | ||
(pps ppx_rapper))) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
(* Simple queries *) | ||
type a = { username: string } | ||
|
||
type b = { id: int; username: string } | ||
|
||
let many_arg_execute = | ||
[%rapper | ||
execute | ||
{sql| | ||
UPDATE users | ||
SET (username, email, bio) = (%string{username}, %string{email}, %string?{bio}) | ||
WHERE id = %int{id} | ||
|sql}] | ||
|
||
let many_arg_get_one_repeated_arg = | ||
[%rapper | ||
get_one | ||
{sql| | ||
SELECT @string{username} | ||
FROM users | ||
WHERE id = %int{id} OR username = %string{username} OR id <> %int{id} | ||
|sql} | ||
record_out] | ||
|
||
let many_arg_get_opt = | ||
[%rapper | ||
get_opt | ||
{sql| | ||
SELECT @int{id}, @string{username} | ||
FROM users | ||
WHERE username = %string{username} AND id > %int{min_id} | ||
|sql}] | ||
|
||
(* Using list parameters *) | ||
type list_in = { versions: int list } | ||
|
||
let collect_list = | ||
[%rapper | ||
get_many | ||
{sql| SELECT @string{id} from schema_migrations where version in (%list{%int{versions}})|sql} | ||
record_in] | ||
|
||
(* Using custom types *) | ||
module Suit : Rapper.CUSTOM = struct | ||
type t = Clubs | Diamonds | Hearts | Spades | ||
|
||
let t = | ||
let encode = function | ||
| Clubs -> Ok "c" | ||
| Diamonds -> Ok "d" | ||
| Hearts -> Ok "h" | ||
| Spades -> Ok "s" | ||
in | ||
let decode = function | ||
| "c" -> Ok Clubs | ||
| "d" -> Ok Diamonds | ||
| "h" -> Ok Hearts | ||
| "s" -> Ok Spades | ||
| _ -> Error "invalid suit" | ||
in | ||
Caqti_type.(custom ~encode ~decode string) | ||
end | ||
|
||
let get_cards = | ||
[%rapper | ||
get_many | ||
{sql| SELECT @int{id}, @Suit{suit} FROM cards WHERE suit <> %Suit{suit} |sql}] | ||
|
||
(* Example showing the correspondence between rapper/Caqti types and OCaml types *) | ||
type all_types_output = { | ||
id: string; | ||
payload: string; | ||
version: int; | ||
some_int32: int32; | ||
some_int64: int64; | ||
added: bool; | ||
fl: float; | ||
date: Ptime.t; | ||
time: Ptime.t; | ||
span: Ptime.span; | ||
} | ||
|
||
let all_types = | ||
[%rapper | ||
get_many | ||
{sql| SELECT @string{id}, @octets{payload}, @int{version}, | ||
@int32{some_int32}, @int64{some_int64}, @bool{added}, | ||
@float{fl}, @pdate{date}, @ptime{time}, @ptime_span{span} | ||
FROM some_table |sql} | ||
record_out] | ||
|
||
(* Example of using [function_out] and [Rapper.load_many] *) | ||
module Twoot = struct | ||
type t = { id: int; content: string; likes: int } | ||
|
||
let make ~id ~content ~likes = { id; content; likes } | ||
end | ||
|
||
module User = struct | ||
type t = { id: int; name: string; twoots: Twoot.t list } | ||
|
||
let make ~id ~name = { id; name; twoots = [] } | ||
end | ||
|
||
let get_multiple_function_out () dbh = | ||
let open Async.Deferred.Result in | ||
[%rapper | ||
get_many | ||
{sql| | ||
SELECT @int{users.id}, @string{users.name}, | ||
@int{twoots.id}, @string{twoots.content}, @int{twoots.likes} | ||
FROM users | ||
JOIN twoots ON twoots.id = users.id | ||
ORDER BY users.id | ||
|sql} | ||
function_out] | ||
(User.make, Twoot.make) () dbh | ||
>>| Rapper.load_many | ||
(fst, fun { User.id; _ } -> id) | ||
[ (snd, fun user twoots -> { user with twoots }) ] |
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
(library | ||
(name examples_lwt) | ||
(libraries lwt ppx_rapper_lwt) | ||
(preprocess | ||
(pps ppx_rapper))) |
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
(library | ||
(name ppx_rapper_async) | ||
(public_name ppx_rapper_async) | ||
(wrapped false) | ||
(libraries async ppx_rapper.runtime caqti-async caqti)) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
include Rapper.Make_helper (struct | ||
type +'a t = 'a Async.Deferred.t | ||
|
||
let return = Async.return | ||
|
||
let map f a = Async.Deferred.map ~f a | ||
|
||
module Stream = Caqti_async.Stream | ||
end) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
include | ||
Rapper.RAPPER_HELPER | ||
with type 'a future := 'a Async.Deferred.t | ||
and module Stream = Caqti_async.Stream |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
(library | ||
(name ppx_rapper_lwt) | ||
(public_name ppx_rapper_lwt) | ||
(wrapped false) | ||
(libraries lwt ppx_rapper.runtime caqti-lwt caqti)) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
include Rapper.Make_helper (struct | ||
type +'a t = 'a Lwt.t | ||
|
||
let return = Lwt.return | ||
|
||
let map = Lwt.map | ||
|
||
module Stream = Caqti_lwt.Stream | ||
end) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
include | ||
Rapper.RAPPER_HELPER | ||
with type 'a future := 'a Lwt.t | ||
and module Stream = Caqti_lwt.Stream |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
(library | ||
(name rapper) | ||
(public_name ppx_rapper.runtime) | ||
(libraries caqti caqti-lwt)) | ||
(libraries caqti)) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# This file is generated by dune, edit dune-project instead | ||
opam-version: "2.0" | ||
version: "2.0.0" | ||
synopsis: "Async support for ppx_rapper" | ||
maintainer: ["Roddy MacSween <github@roddymacsween.co.uk>"] | ||
authors: ["Roddy MacSween <github@roddymacsween.co.uk>"] | ||
license: "MIT" | ||
homepage: "https://github.com/roddyyaga/ppx_rapper" | ||
doc: "https://github.com/roddyyaga/ppx_rapper" | ||
bug-reports: "https://github.com/roddyyaga/ppx_rapper/issues" | ||
depends: [ | ||
"ocaml" {>= "4.07"} | ||
"dune" {>= "2.0.1"} | ||
"ppx_rapper" | ||
"caqti-async" | ||
"caqti" | ||
"async" | ||
] | ||
build: [ | ||
["dune" "subst"] {pinned} | ||
[ | ||
"dune" | ||
"build" | ||
"-p" | ||
name | ||
"-j" | ||
jobs | ||
"@install" | ||
"@runtest" {with-test} | ||
"@doc" {with-doc} | ||
] | ||
] | ||
dev-repo: "git+https://github.com/roddyyaga/ppx_rapper.git" |
Oops, something went wrong.