ygrek / sqlgg

SQL Guided (code) Generator
https://ygrek.org/p/sqlgg/
GNU General Public License v2.0
62 stars 20 forks source link

INSERT ... SELECT nullable inference #116

Closed cyberhuman closed 1 month ago

cyberhuman commented 1 year ago
sqlgg -gen caml - <<<'
   create table foo ( bar int null );
   insert into foo ( bar ) values ( @bar ); -- bar is int option
   insert into foo ( bar ) select @bar;' -- bar is int
(* DO NOT EDIT MANUALLY *)
(*  *)
(* generated by sqlgg version/20220621-0400-31083-gdd948a1e6e7+M-develop on 2023-05-10T04:09Z *)

module Sqlgg (T : Sqlgg_traits.M) = struct

  module IO = Sqlgg_io.Blocking

  let create_foo db  =
    T.execute db ("create table foo ( bar int null )") T.no_params 

  let insert_foo_1 db ~bar =
    let set_params stmt =
      let p = T.start_params stmt (1) in
      begin match bar with None -> T.set_param_null p | Some v -> T.set_param_Int p v end;
      T.finish_params p
    in
    T.execute db ("insert into foo ( bar ) values ( @bar )") set_params 

  let insert_foo_2 db ~bar =
    let set_params stmt =
      let p = T.start_params stmt (1) in
      T.set_param_Int p bar;
      T.finish_params p
    in
    T.execute db ("insert into foo ( bar ) select @bar") set_params 

  module Fold = struct
  end (* module Fold *)

  module List = struct
  end (* module List *)
end (* module Sqlgg *)
jongleb commented 1 month ago

Currently works fine

module Make (T : Sqlgg_traits.M_io) = struct

  module IO = T.IO

  let insert_foo_0 db ~bar =
    let set_params stmt =
      let p = T.start_params stmt (1) in
      begin match bar with None -> T.set_param_null p | Some v -> T.set_param_Int p v end;
      T.finish_params p
    in
    T.execute db ("insert into foo ( bar ) values ( ? )") set_params

  let insert_foo_1 db ~bar =
    let set_params stmt =
      let p = T.start_params stmt (1) in
      begin match bar with None -> T.set_param_null p | Some v -> T.set_param_Int p v end;
      T.finish_params p
    in
    T.execute db ("insert into foo ( bar ) select ?") set_params

  module Fold = struct
  end (* module Fold *)

  module List = struct
  end (* module List *)
end (* module Make *)