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 (** Insert two rows in the given list, with a generated provider *) let insert_rows db (list : Model.list) = let fields = list.Model.DataList.fields in let* _id1 = Query.insert_list db list (Common.provider_for_fields fields 1L) in let* _id2 = Query.insert_list db list (Common.provider_for_fields fields 2L) in Lwt.return_unit let exec : string list -> t = fun _args -> Common.exec_with_conf (fun configuration db -> 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. The reference columns are followed by a [_table] literal naming the targeted table, so that the output is self-describing. *) let* rows = Lwt_list.map_s (fun list -> let table = Model.Identifier.to_string list.Model.DataList.id in let columns = ("id", Model.Identifier.quote Model.Identifier.id) :: Common.field_columns ~annotate_references:true list.Model.DataList.fields in let* json = Common.collect_json db list.Model.DataList.id columns 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.Types.MultiReference reference -> Some ( Model.Identifier.dot list.Model.DataList.id (Model.Field.name field), Model.Identifier.of_string (Common.target_table reference.Model.Types.t) ) | _ -> None)) in let* ext_rows = Lwt_list.map_s (fun (table, target) -> let name = Model.Identifier.to_string table in let columns = [ ("parent", Model.Identifier.quote Model.Identifier.parent); ("target", Model.Identifier.quote Model.Identifier.target); ( "target_table", Croquette_common.Sql.quote_literal (Model.Identifier.to_string target) ); ] in let* json = Common.collect_json db table columns in Lwt.return ((name ^ ":") :: json)) external_tables in Lwt.return (List.concat rows @ List.concat ext_rows))