diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-11-21 11:23:41 +0100 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-11-21 11:23:41 +0100 |
| commit | ce13c93cf14cb71894481b0303a7058a72a6f4ce (patch) | |
| tree | bd93186a65f6645cb4401f188d266d743cc2fde7 /test/insert_list.ml | |
| parent | 172a02b80d8dbdcfe09129d7fa76015bb5ec4d79 (diff) | |
Load values from the database
Diffstat (limited to 'test/insert_list.ml')
| -rw-r--r-- | test/insert_list.ml | 102 |
1 files changed, 26 insertions, 76 deletions
diff --git a/test/insert_list.ml b/test/insert_list.ml index a2425ae..88e17db 100644 --- a/test/insert_list.ml +++ b/test/insert_list.ml @@ -13,93 +13,37 @@ let pp : Format.formatter -> t -> unit = 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 [<field>_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 +(** 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 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))) + let* _id2 = + Query.insert_list db list (Common.provider_for_fields fields 2L) 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 + 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 *) + (* Display the list tables, each under a header naming the table. The + reference columns are followed by a [<field>_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* res = Db.collect_list (list_as_json list) () in - let* json = Caqti_lwt.or_fail res 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 @@ -122,10 +66,16 @@ let exec : string list -> t = 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) () + 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 = Caqti_lwt.or_fail res in + let* json = Common.collect_json db table columns in Lwt.return ((name ^ ":") :: json)) external_tables in |
