diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/insert.ml | 387 |
1 files changed, 277 insertions, 110 deletions
diff --git a/test/insert.ml b/test/insert.ml index eaa132f..70b98d6 100644 --- a/test/insert.ml +++ b/test/insert.ml @@ -14,81 +14,6 @@ let pp : Format.formatter -> t -> unit = (fun formatter (content_id, id, content) -> Format.fprintf formatter "@[<v 2>%s:%Ld@;%s@]" content_id id content)) -(** Read the values to insert in the configuration *) -module Make (S : Decoders.Decode.S) = struct - let ( let* ) = S.( let* ) - let ( let+ ) = S.( let+ ) - - type value = Model.Reference.t Model.Typ.value - - (** The values are encoded in a very simple way, depending of the nature of - the field in the json, we create a property in for text or numeric *) - let value : value S.decoder = - S.one_of - [ - ( "numeric", - S.map (fun v -> Model.Typ.(V (Integer, Int64.of_int v))) S.int ); - ("text", S.map (fun v -> Model.Typ.(V (Text, v))) S.string); - ("other", S.map (fun _ -> Model.Typ.None) S.value); - ] - - type content = Model.Reference.t Model.Content.t - type properties = (string * value) list - type content_to_insert = content * properties list - - let rec properties : - Configuration.Ast.setting -> - string -> - (content * (properties * content_to_insert list) list) S.decoder = - fun conf name -> - (* We are loading the same configuration template, the content is supposed - to exists by definition and will never be None *) - let content = Configuration.Ast.get_content conf name |> Option.get in - let* properties = - S.field_opt_or ~default:[] "values" (S.list (S.key_value_pairs value)) - in - let* subcontents = - S.field_opt_or ~default:[] "values" (S.list (subcontents conf)) - in - let to_insert = List.combine properties subcontents in - S.succeed (content, to_insert) - - and subcontents : - Configuration.Ast.setting -> content_to_insert list S.decoder = - fun conf -> - let* properties = - S.key_value_pairs_seq (fun key -> - S.one_of - [ - ( "subcontent", - fun v -> - match Configuration.Ast.get_content conf key with - | None -> Error (Decoders.Error.make "no content") - | Some subcontent -> - let res = S.list (S.key_value_pairs value) v in - let v = Result.map (fun v -> Some (subcontent, v)) res in - v ); - ("other", fun _ -> Ok None); - ]) - in - let properties = List.filter_map ~f:(fun v -> v) properties in - S.succeed properties - - let content : - Configuration.Ast.setting -> - (content * (properties * content_to_insert list) list) S.decoder = - fun conf -> - let* name = S.field "id" S.string in - properties conf name - - let configuration : - Configuration.Ast.setting -> - (content * (properties * content_to_insert list) list) list S.decoder = - fun conf -> S.field "content" (S.list (content conf)) -end - -module D = Make (Configuration.Json5.Decode) - let query_columns : 'a Model.Content.t -> string = fun content -> let fields = @@ -122,13 +47,35 @@ let query_columns : 'a Model.Content.t -> string = Buffer.contents buff -let query table = +type content = Model.Reference.t Model.Content.t + +let query (table : content) = Caqti_request.Infix.(Caqti_type.(int64) ->! Caqti_type.string) ~oneshot:true ("SELECT json_object(" ^ query_columns table ^ ") from " ^ Model.Content.id table ^ " where id=?") -let insert_and_get_result ?parent db content entry = +let query_values (table : content) (feature : string) = + Caqti_request.Infix.(Caqti_type.(int64) ->* Caqti_type.string) + ~oneshot:true + ("SELECT json_object('" ^ feature ^ "', target) from \"" + ^ Model.Content.id table ^ "." ^ feature ^ "\" where parent=?") + +type value = Model.Reference.t Model.Typ.value +type properties = (string * value) list + +(** [insert_and_get_result db content entry] will insert the value in the + database and return the informations for the content just inserted. + + [content] is the type definition for the element we want to insert [entry] + contains all the values to insert in the database. *) +let insert_and_get_result : + ?parent:int64 -> + Caqti_lwt.connection -> + content -> + properties -> + (string * int64 * string) Lwt.t = + fun ?parent db content entry -> let ( let* ) = Lwt.bind in let* id = Query.insert ?parent db content (fun v -> @@ -140,47 +87,267 @@ let insert_and_get_result ?parent db content entry = let* json = Caqti_lwt.or_fail json_result in Lwt.return (content_id, id, json) +(** Read the values to insert in the configuration *) +module Make (S : Decoders.Decode.S) = struct + let ( let* ) = S.( let* ) + let ( let+ ) = S.( let+ ) + + type content_to_insert = content * properties list + + let ( let@@ ) = Lwt.bind + + (** Load the values associated with the configuration. *) + class loader (db : Caqti_lwt.connection) (conf : Configuration.Ast.setting) = + object (self) + method insert_and_return_references : + content -> properties list Lwt.t -> int64 list Lwt.t = + fun content properties -> + let ( let* ) = Lwt.bind in + let* properties = properties in + Lwt_list.map_s + (fun line -> + let* _, id, _ = insert_and_get_result db content line in + Lwt.return id) + properties + + method value : content -> string -> (string * value) Lwt.t S.decoder = + fun content key -> + ignore content; + S.one_of + [ + ( "numeric", + S.map + (fun v -> + Lwt.return + (key, Model.Typ.(V (Integer, Some (Int64.of_int v))))) + S.int ); + ( "text", + S.map + (fun v -> Lwt.return (key, Model.Typ.(V (Text, Some v)))) + S.string ); + ( "reference", + match Model.Content.find_target content key with + | None -> S.fail "Not a feature" + | Some target_ref -> + let target = Model.Reference.get target_ref in + let* properties = self#properties target in + let sequence = + let@@ properties = properties in + + let@@ _, id, _ = + insert_and_get_result db target properties + in + let reference = + Model.Typ.{ t = target_ref; constraints = None } + in + Lwt.return + ( key, + Model.Typ.( + V (Model.Typ.MonoReference reference, Some id)) ) + in + S.succeed sequence ); + ( "references", + match Model.Content.find_target content key with + | None -> S.fail "Not a feature" + | Some target_ref -> + let target = Model.Reference.get target_ref in + let* properties = self#values target in + let sequence = + let@@ ids = + self#insert_and_return_references target properties + in + let reference = + Model.Typ.{ t = target_ref; constraints = None } + in + Lwt.return + ( key, + Model.Typ.( + Seq + ( Model.Typ.MultiReference reference, + List.to_seq ids )) ) + in + + S.succeed sequence ); + ( "other", + (* If the value is a dictionnary list, consider it as external references *) + S.succeed (Lwt.return (key, Model.Typ.None)) ); + ] + (** The values are encoded in a very simple way, depending of the nature + of the field in the json, we create a property in for text or numeric + *) + + method properties : content -> properties Lwt.t S.decoder = + fun content -> + S.map Lwt.all (S.key_value_pairs_seq (self#value content)) + + method values : content -> properties list Lwt.t S.decoder = + fun content -> S.map Lwt.all (S.list (self#properties content)) + + method load_content : content_to_insert list Lwt.t S.decoder = + let* properties = + S.key_value_pairs_seq (fun key -> + S.one_of + [ + ( "content", + fun v -> + match (Configuration.Ast.get_content conf) key with + | None -> Error (Decoders.Error.make "no content") + | Some subcontent -> + let values = self#values subcontent v in + Result.map + (fun v -> + let content_to_insert = + let@@ v = v in + Lwt.return (subcontent, v) + in + Some content_to_insert) + values ); + ("other", fun _ -> Ok None); + ]) + in + let properties = List.filter_map ~f:(fun v -> v) properties in + S.succeed (Lwt.all properties) + (** Read the content and for each key, check if there is an existing + subcontent with the same name, If this subcontent exists, parse all + the subcontents with all the values and return them. *) + + method root_content : + string -> + (content * (properties * content_to_insert list) list) Lwt.t S.decoder + = + fun name -> + (* In this syntax, the content is supposed to follow the definition + and will never be None *) + let content = Configuration.Ast.get_content conf name |> Option.get in + let* properties = + S.field_opt_or ~default:(Lwt.return []) "values" + (self#values content) + in + + (* Look for subcontents in the given values. The subcontent is + identfied by having a key name equal to the subcontent + identifier. *) + let* subcontents : content_to_insert list list Lwt.t = + S.map Lwt.all + (S.field_opt_or ~default:[] "values" (S.list self#load_content)) + in + let result = + let@@ properties = properties in + let@@ subcontents = subcontents in + + let to_insert = List.combine properties subcontents in + Lwt.return (content, to_insert) + in + S.succeed result + + method content : + (content * (properties * content_to_insert list) list) Lwt.t S.decoder + = + let* name = S.field "id" S.string in + let* properties = self#root_content name in + S.succeed properties + + method configuration : + (content * (properties * content_to_insert list) list) list Lwt.t + S.decoder = + let* content = S.field "content" (S.list self#content) in + let conf = Lwt.all content in + S.succeed conf + end +end + +module D = Make (Configuration.Json5.Decode) + +(** Query all the multireferences tables for the given content and the given id. + Collect all the values and return them in the same json format as the same + content. *) +let collect_references : + Caqti_lwt.connection -> content -> int64 -> 'a list Lwt.t = + fun db content id -> + let module Db = (val db) in + let ( let* ) = Lwt.bind in + let content_id = Model.Content.id content in + + (* Only keep the multivaluated fields *) + let fields = + Seq.filter + (fun field -> + let (T ty) = Model.Field.type_ field in + match ty with Model.Typ.MultiReference _ -> true | _ -> false) + (Model.Content.fields content) + |> List.of_seq + in + let results = + Lwt_list.fold_left_s + (fun acc field -> + let field_name = Model.Field.name field in + let* json_result = + Db.collect_list (query_values content field_name) id + in + let* json = Caqti_lwt.or_fail json_result in + let line = + List.fold_left ~init:acc json ~f:(fun acc json -> + (content_id, id, json) :: acc) + in + Lwt.return line) + [] fields + in + + results + let exec : string list -> t = fun _ -> let stdin = In_channel.input_all stdin in - let ( let* ) = Result.bind in - let* configuration = Configuration.Read_conf.read_string_conf stdin in - let* inserts = - Configuration.Json5.Decode.decode_string - (D.configuration configuration) - stdin - in + let ( let** ) = Result.bind in + let** configuration = Configuration.Read_conf.read_string_conf stdin in Lwt_main.run (let ( let* ) = Lwt.bind in let* db = Common.connect configuration in let module Db = (val db) in - let* insertions = - Lwt_list.fold_left_s - (fun acc (content, entries) -> + let inserts = + Configuration.Json5.Decode.decode_string + (new D.loader db configuration)#configuration stdin + in + match inserts with + | Error err -> Lwt.return_error err + | Ok inserts -> + let* inserts = inserts in + let* insertions = Lwt_list.fold_left_s - (fun acc (entry, subcontents) -> - let* content_id, id, json = - insert_and_get_result db content entry - in - - let acc = (content_id, id, json) :: acc in - - (* Insert the subcontent associated *) + (fun acc (content, entries) -> Lwt_list.fold_left_s - (fun acc (subcontent, entries) -> - let* acc = - Lwt_list.fold_left_s - (fun acc entry -> - let* subcontent_id, id, json = - insert_and_get_result ~parent:id db subcontent entry - in - Lwt.return ((subcontent_id, id, json) :: acc)) - acc entries + (fun acc (entry, subcontents) -> + (* Insert the main content *) + let* content_id, id, json = + insert_and_get_result db content entry in + let acc = (content_id, id, json) :: acc in - Lwt.return acc) - acc subcontents) - acc entries) - [] inserts - in - Lwt.return_ok (List.rev insertions)) + (* Also query the multivaluated references *) + let* ext = collect_references db content id in + let acc = List.append ext acc in + (* Insert the subcontent associated *) + Lwt_list.fold_left_s + (fun acc (subcontent, entries) -> + let* acc = + Lwt_list.fold_left_s + (fun acc entry -> + let* subcontent_id, id, json = + insert_and_get_result ~parent:id db subcontent + entry + in + let acc = (subcontent_id, id, json) :: acc in + + (* Also query the multivaluated references *) + let* ext = collect_references db subcontent id in + let acc = List.append ext acc in + Lwt.return acc) + acc entries + in + + Lwt.return acc) + acc subcontents) + acc entries) + [] inserts + in + Lwt.return_ok (List.rev insertions)) |
