(** Generate the update queries for a content *) type provider = string -> Value.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; _ } -> Model.Identifier.quote column ^ "=?" | { name = column; content_type = Integer; _ } -> Model.Identifier.quote column ^ "=?" | { name = column; content_type = MonoReference _; _ } -> Model.Identifier.quote column ^ "=?" | { name = _; content_type = MultiReference _; _ } -> raise Not_found include Encoder.ConcatParam end) let update_fields : Caqti_lwt.connection -> Model.content -> (Intf.t_type * (string * Value.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.Templater.T.(t2 (Encoder.CaqtiRequest.f () query_fields) int64) in let module Identifier = Model.Identifier in let id_key = Identifier.quote Identifier.id in let q = Croquette_common.Format.format_string (fun format -> Format.fprintf format "UPDATE %s SET %s WHERE %s = ?" (Identifier.quote (Model.Content.id content)) format_param id_key) in let request = Caqti.Templater.(direct T.(types_param -->. unit) 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.target) 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.content -> (Model.Identifier.t * (int64, Model.target) 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 module Identifier = Model.Identifier in let id_key = Identifier.quote Identifier.id and parent = Identifier.quote Identifier.parent and target = Identifier.quote Identifier.target in let q_delete = Croquette_common.Format.format_string (fun f -> Format.fprintf f "DELETE FROM %s where %s=? and %s not in (%s)" (Identifier.quote (Identifier.dot (Model.Content.id content) field_name)) parent target (Encoder.EncodeParameter.f () existing_references_t)) in let caqti_param = Encoder.CaqtiRequest.f () existing_references_t in let request_delete = Caqti.Templater.(direct T.(t2 int64 caqti_param -->. unit) 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 %s FROM %s WHERE %s=?" id_key (Identifier.quote (Identifier.dot (Model.Content.id content) field_name)) parent) in let request_select = Caqti.Templater.(direct T.(int64 -->* int64) 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 = Value.multi (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.content -> (string -> Value.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 (Model.Identifier.to_string name) in match value with | Value.Data.Any d -> ( match d with | Value.Data.None -> acc | Value.Data.V _ as dv -> ( ( tt, (Model.Identifier.to_string name, Value.Data.any dv) ) :: fields, references ) | Value.Data.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