open StdLabels let name = __FILE__ let description = "Insert rows in the lists and display their tables" let speclist () = [] type t = (string list, Configuration.Read_conf.error) result let pp : Format.formatter -> t -> unit = Format.pp_print_result ~error:Configuration.Read_conf.pp_error ~ok: (Format.pp_print_list ~pp_sep:Format.pp_print_newline Format.pp_print_string) let ( let* ) = Lwt.bind let ( let+ ) opt cont = Result.map cont opt (** Build a request returning each row of the list table as a json object. Reference columns are followed by a [_table] literal naming the targeted table, so that the output is self-describing. *) let list_as_json : Model.list -> (unit, string, _) Caqti.Template.Request.t = fun list -> let column field = let name = Model.Field.name field in let (Model.Field.T ty) = Model.Field.type_ field in let key = Model.Identifier.to_string name in match ty with | Model.Typ.MultiReference _ -> [] (* in a dedicated table *) | Model.Typ.MonoReference reference -> [ (key, Model.Identifier.quote name); ( key ^ "_table", Croquette_common.Sql.quote_literal (Common.target_table reference.Model.Typ.t) ); ] | _ -> [ (key, Model.Identifier.quote name) ] in let columns = ("id", "\"id\"") :: List.concat_map list.Model.DataList.fields ~f:column in let open Caqti.Templater in direct T.(unit -->* string) (Common.json_select_sql list.Model.DataList.id columns) let external_table_as_json : Model.Identifier.t -> Model.Identifier.t -> (unit, string, _) Caqti.Template.Request.t = fun table target_table -> let open Caqti.Templater in direct T.(unit -->* string) (Common.json_select_sql table [ ("parent", "\"parent\""); ("target", "\"target\""); ( "target_table", Croquette_common.Sql.quote_literal (Model.Identifier.to_string target_table) ); ]) (** A provider giving a value for each field of the list, depending on its type. The references point to the row of id 1 of their target list. *) let provider_for (list : Model.list) (row : Int64.t) : string -> Value.value = fun field_name -> let field = List.find list.Model.DataList.fields ~f:(fun field -> String.equal field_name (Model.Identifier.to_string (Model.Field.name field))) in let (Model.Field.T ty) = Model.Field.type_ field in match ty with | Model.Typ.Text -> Value.single (Text, Some (field_name ^ "-" ^ Int64.to_string row)) | Model.Typ.Integer -> Value.single (Integer, Some row) | Model.Typ.MonoReference reference -> Value.single (MonoReference reference, Some 1L) | Model.Typ.MultiReference reference -> (* Several values per row, to highlight the multivalued data *) let values = if Int64.equal row 1L then [ 1L; 2L ] else [ 2L ] in Value.single (MultiReference reference, List.to_seq values) let insert_rows db (list : Model.list) = let* _id1 = Query.insert_list db list (provider_for list 1L) in let* _id2 = Query.insert_list db list (provider_for list 2L) in Lwt.return_unit let exec : string list -> t = fun _args -> let stdin = Common.read_stdin () in let+ configuration = Configuration.Read_conf.read_string_conf stdin in Common.with_db configuration (fun db -> let module Db = (val db) in let lists = Configuration.Ast.lists configuration in (* Insert two rows per list, in identifier order *) let* () = Lwt_list.iter_s (insert_rows db) lists in (* Display the list tables, each under a header naming the table *) let* rows = Lwt_list.map_s (fun list -> let table = Model.Identifier.to_string list.Model.DataList.id in let* res = Db.collect_list (list_as_json list) () in let* json = Caqti_lwt.or_fail res in Lwt.return ((table ^ ":") :: json)) lists in (* Display the dedicated tables of the multivalued fields, with the table targeted by the reference *) let external_tables = List.concat_map lists ~f:(fun list -> List.filter_map list.Model.DataList.fields ~f:(fun field -> let (Model.Field.T ty) = Model.Field.type_ field in match ty with | Model.Typ.MultiReference reference -> Some ( Model.Identifier.dot list.Model.DataList.id (Model.Field.name field), Model.Identifier.of_string (Common.target_table reference.Model.Typ.t) ) | _ -> None)) in let* ext_rows = Lwt_list.map_s (fun (table, target) -> let name = Model.Identifier.to_string table in let* res = Db.collect_list (external_table_as_json table target) () in let* json = Caqti_lwt.or_fail res in Lwt.return ((name ^ ":") :: json)) external_tables in Lwt.return (List.concat rows @ List.concat ext_rows))