diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-04-30 09:16:03 +0200 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-05-06 16:45:25 +0200 |
| commit | 960d5b8f2728f71dd9ce038d527844d82ca2f0f2 (patch) | |
| tree | 9a15dc43f91a0b58a3aaa10223bba35abf7ef5f2 /lib/query/insert.ml | |
| parent | a5efd7d15b4d049a4ed539ed5d9c2f1037d00973 (diff) | |
Allow updates for existing contents
Diffstat (limited to 'lib/query/insert.ml')
| -rw-r--r-- | lib/query/insert.ml | 45 |
1 files changed, 30 insertions, 15 deletions
diff --git a/lib/query/insert.ml b/lib/query/insert.ml index 1ee1549..ecb0efb 100644 --- a/lib/query/insert.ml +++ b/lib/query/insert.ml @@ -21,15 +21,6 @@ let collect_fields : let (Model.Field.T content_type) = Model.Field.type_ field in TT { name; content_type }) -let format_string : (Format.formatter -> unit) -> string = - fun f -> - let buff = Buffer.create 16 in - let format = Format.formatter_of_buffer buff in - let () = f format in - - Format.pp_print_flush format (); - Buffer.contents buff - module MultiReferencesInsert = struct type 'a t = { db : Caqti_lwt.connection; @@ -80,7 +71,7 @@ module MultiReferencesInsert = struct | None -> raise Encoder.TypeMismatch | Some Equal -> let q = - format_string (fun format -> + Croquette_common.Format.format_string (fun format -> Format.fprintf format "INSERT INTO \"%s.%s\" (\"parent\", \"target\") VALUES \ (?, ?)" @@ -96,7 +87,9 @@ module MultiReferencesInsert = struct let* res = Seq.fold_left (accumulate db request parent_id) k values in - let* _ = Db.deallocate request in + let* () = Caqti_lwt.or_fail res in + let* res = Db.deallocate request in + let* () = Caqti_lwt.or_fail res in Lwt.return res in @@ -141,7 +134,7 @@ let insert_returning : in let q = - format_string (fun format -> + Croquette_common.Format.format_string (fun format -> Format.fprintf format "INSERT INTO \"%s\" %s VALUES %s RETURNING ID" (Model.Content.id content) columns insert_template) in @@ -169,9 +162,9 @@ let insert_returning : MultiReferencesInsert. { db; provider; k = Lwt.return_ok (); parent_id = content_id; content } in - let ret = Fold.f acc multi_valuated_fields in - let* k = ret.k in - let* () = Caqti_lwt.or_fail k in + let MultiReferencesInsert.{ k; _ } = Fold.f acc multi_valuated_fields in + let* res = k in + let* () = Caqti_lwt.or_fail res in Lwt.return content_id (** Insert an entry for a specific content *) @@ -199,3 +192,25 @@ let insert : match Caqti_driver_info.dialect_tag Db.driver_info with | `Sqlite | `Pgsql -> insert_returning db content provider | _ -> raise Not_found + +let insert_references : + Caqti_lwt.connection -> + Model.Reference.t Model.Content.t -> + Int64.t -> + string -> + Model.Reference.t Model.Typ.value -> + Int64.t Intf.data -> + unit Lwt.t = + fun db content content_id field_name value data -> + let provider : string -> Model.Reference.t Model.Typ.value = function + | n when String.equal n field_name -> value + | _ -> Model.Typ.None + in + let acc = + MultiReferencesInsert. + { db; provider; k = Lwt.return_ok (); parent_id = content_id; content } + in + let ( let* ) = Lwt.bind in + let MultiReferencesInsert.{ k; _ } = MultiReferencesInsert.f acc data in + let* res = k in + Caqti_lwt.or_fail res |
