roddyyaga / ppx_rapper

Syntax extension for writing SQL in OCaml
Other
139 stars 18 forks source link

Using %list with function_out #28

Closed mudrz closed 1 year ago

mudrz commented 1 year ago

I have 2 tables, with an id column and want to join them and return the results:

  let a =
    [%rapper
      get_many
        {sql|
           SELECT
             @int{a.id},
             @string{a.name},
             @int{b.id},
             @string{b.name}
           FROM public.table1 as a
           INNER JOIN public.table2 as b
           ON a.b_id = b.id
           WHERE
             a.id IN (%list{%string{ids}})
       |sql}]

this works, but it results in a tuple, which is not great (int * string * int * string) list

record_out doesn't work, because id is the same in both tables:

Variable id is bound several times in this matching

function_out doesn't work, because I get

Unbound value loaders

Not sure where this is coming from? I noticed that not using %list compiles the ppx (WHERE a.id = %string{id}) (but doesn't work for my use case)

Is it possible to use %list and function_out somehow?

mudrz commented 1 year ago

this was answered in discuss.ocaml https://discuss.ocaml.org/t/ppx-rapper-join-2-tables-and-return-a-list/11277/2?u=mudrz

type t = {
  a_id: int;
  a_name: string;
  b_id: int;
  b_name: string;
}
 [%rapper
      get_many
        {sql|
           SELECT
             a.id as @int{a_id},
             a.name as @string{a_name},
             b.id as @int{b_id},
             b.name as @string{b_name}
           FROM public.table1 as a
           INNER JOIN public.table2 as b
           ON a.b_id = b.id
           WHERE
             a.id IN (%list{%string{ids}})
       |sql} record_out]