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/update.ml | |
| parent | a5efd7d15b4d049a4ed539ed5d9c2f1037d00973 (diff) | |
Allow updates for existing contents
Diffstat (limited to 'lib/query/update.ml')
| -rw-r--r-- | lib/query/update.ml | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/lib/query/update.ml b/lib/query/update.ml new file mode 100644 index 0000000..4b8adfb --- /dev/null +++ b/lib/query/update.ml @@ -0,0 +1,182 @@ +(** Generate the update queries for a content *) + +type provider = string -> Model.Reference.t Model.Typ.value + +module UpdateTemplate = Encoder.Map (struct + type 'a t = string + type env = unit + + let f : type a. unit -> a Intf.data -> a t = + fun () -> function + | { name = column; content_type = Text; _ } -> column ^ "=?" + | { name = column; content_type = Integer; _ } -> column ^ "=?" + | { name = column; content_type = MonoReference _; _ } -> column ^ "=?" + | { name = _; content_type = MultiReference _; _ } -> raise Not_found + + include Encoder.ConcatParam +end) + +let update_fields : + Caqti_lwt.connection -> + 'a Model.Content.t -> + (Intf.t_type * (string * Model.Reference.t Model.Typ.value)) list -> + int64 -> + unit Lwt.t = + fun db content fields id -> + let module Db = (val db) in + let fields_list, value_list = List.split fields in + (* Update all the fields stored in the content table *) + let (T query_fields) = + Encoder.list_to_tup fields_list ~f:(fun field -> field) + in + let format_param = (UpdateTemplate.f ()) query_fields in + let types_param = + Caqti_type.(t2 ((Encoder.CaqtiRequest.f ()) query_fields) int64) + in + let q = + Croquette_common.Format.format_string (fun format -> + Format.fprintf format "UPDATE \"%s\" SET %s WHERE ID = ?" + (Model.Content.id content) format_param) + in + let request = + Caqti_request.Infix.((types_param ->. Caqti_type.unit) ~oneshot:true) q + in + let query_param = + Encoder.DataToCaqtiValues.f + (fun name -> List.assoc name value_list) + query_fields + in + + let ( let* ) = Lwt.bind in + let* result = Db.exec request (query_param, id) in + Caqti_lwt.or_fail result + +(** Encode a sequence of references into a type usable in Caqti request *) +module ReferenceListEncoder = Encoder.Map (struct + include Encoder.DefaultEncoder + + type 'a t = 'a + type env = (int64, Model.Reference.t) Model.Typ.t * Int64.t Seq.t ref + + let f : type a. env -> a Intf.data -> a t = + fun (t, env) d -> + match !env () with + | Seq.Nil -> raise Not_found + | Cons (value, tl) -> ( + env := tl; + match Model.Typ.eq t d.Intf.content_type with + | Some Type.Equal -> value + | None -> raise Encoder.TypeMismatch) +end) + +module ReferencesSet = Set.Make (Int64) + +let update_mulivaluated_field : + Caqti_lwt.connection -> + Model.Reference.t Model.Content.t -> + (string * (int64, Model.Reference.t) Model.Typ.t * int64 Seq.t) list -> + int64 -> + unit Lwt.t = + fun db content fields id -> + let module Db = (val db) in + let ( let* ) = Lwt.bind in + List.fold_left + (fun acc (field_name, t, values) -> + let* () = acc in + + let values = ReferencesSet.of_seq values in + let lvalues = ReferencesSet.to_list values in + + let data_type = Intf.{ name = field_name; content_type = t } in + + (* First delete all the values not belonging to the new set *) + let (T existing_references_t) = + Encoder.list_to_tup lvalues ~f:(fun ref -> + ignore ref; + Intf.(TT data_type)) + in + let q_delete = + Croquette_common.Format.format_string (fun f -> + Format.fprintf f + "DELETE FROM \"%s.%s\" where parent=? and target not in (%s)" + (Model.Content.id content) field_name + (Encoder.EncodeParameter.f () existing_references_t)) + in + let caqti_param = Encoder.CaqtiRequest.f () existing_references_t in + let request_delete = + Caqti_request.Infix.( + (Caqti_type.(t2 int64 caqti_param) ->. Caqti_type.unit) ~oneshot:true) + q_delete + in + let seq = ref (ReferencesSet.to_seq values) in + let* result = + Db.exec request_delete + (id, ReferenceListEncoder.f (t, seq) existing_references_t) + in + let* () = Caqti_lwt.or_fail result in + + (* Then collect all the existing remaining values *) + let q_select = + Croquette_common.Format.format_string (fun f -> + Format.fprintf f "SELECT id FROM \"%s.%s\" WHERE parent=?" + (Model.Content.id content) field_name) + in + let request_select = + Caqti_request.Infix.( + (Caqti_type.int64 ->* Caqti_type.int64) ~oneshot:true) + q_select + in + let* request_result = Db.collect_list request_select id in + let* remaining' = Caqti_lwt.or_fail request_result in + + (* Finaly insert the missing values. + + At this step, we can just use the code from the insert module. *) + let remaining = ReferencesSet.(diff values (of_list remaining')) in + let v = Model.Typ.Seq (t, ReferencesSet.to_seq remaining) in + Insert.insert_references db content id field_name v data_type) + Lwt.return_unit fields + +let q : + Caqti_lwt.connection -> + Int64.t -> + Model.Reference.t Model.Content.t -> + (string -> Model.Reference.t Model.Typ.value) -> + unit Lwt.t = + fun db id content provider -> + let module Db = (val db) in + (* Get all the single fields to update in the content *) + + (* There is a difference between + + - Typ.None + - Typ.V None + + The former will be ignored in the update, and the value will remains as is + The later will set the content to NULL in the database. *) + let fields, references = + Seq.fold_left + (fun ((fields, references) as acc) field -> + let name = Model.Field.name field in + let (Model.Field.T content_type) = Model.Field.type_ field in + let tt = Intf.TT { name; content_type } in + let value = provider name in + match value with + | Model.Typ.None -> acc + | Model.Typ.V _ as value -> ((tt, (name, value)) :: fields, references) + | Model.Typ.Seq (t, seq) -> (fields, (name, t, seq) :: references)) + ([], []) + (Model.Content.fields content) + in + let ( let* ) = Lwt.bind in + let* () = + match fields with + | [] -> Lwt.return_unit + | _ -> update_fields db content fields id + in + let* () = + match references with + | [] -> Lwt.return_unit + | _ -> update_mulivaluated_field db content references id + in + Lwt.return_unit |
