summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/query/insert.ml99
-rw-r--r--lib/query/insert.mli11
-rw-r--r--lib/query/query.ml1
-rw-r--r--lib/query/query.mli11
-rw-r--r--lib/query/tests/insert_list.t45
-rw-r--r--test/insert_list.ml152
-rw-r--r--test/tests.ml1
7 files changed, 286 insertions, 34 deletions
diff --git a/lib/query/insert.ml b/lib/query/insert.ml
index ab0dc2b..40887e8 100644
--- a/lib/query/insert.ml
+++ b/lib/query/insert.ml
@@ -5,16 +5,14 @@
let collect_fields :
?init_fields:Model.field list ->
?filter:(Model.field -> bool) ->
- Model.content ->
+ Model.field list ->
Intf.t =
- fun ?(init_fields = []) ?filter content ->
- let field_seq =
- match filter with
- | None -> Model.Content.fields content
- | Some f -> Model.Content.fields content |> Seq.filter f
+ fun ?(init_fields = []) ?filter fields ->
+ let fields =
+ match filter with None -> fields | Some f -> List.filter f fields
in
- let fields = init_fields @ List.of_seq field_seq in
+ let fields = init_fields @ fields in
Encoder.list_to_tup fields ~f:(fun field ->
let name = Model.Field.name field in
@@ -25,7 +23,8 @@ module MultiReferencesInsert = struct
type 'a t = {
db : Caqti_lwt.connection;
parent_id : int64;
- content : Model.content;
+ table : Model.Identifier.t;
+ (** The table holding the row: a content table, or a list *)
provider : string -> Model.value;
k : (unit, Caqti.Error.t) result Lwt.t;
}
@@ -57,7 +56,7 @@ module MultiReferencesInsert = struct
(** The function is called for each feature declared as multi reference. *)
let f : type any. 'a t -> any Intf.data -> 'a t =
- fun ({ db; parent_id; provider; k; content } as acc)
+ fun ({ db; parent_id; provider; k; table } as acc)
Intf.{ name; content_type } ->
let module Db = (val db) in
let module Identifier = Model.Identifier in
@@ -77,8 +76,7 @@ module MultiReferencesInsert = struct
Croquette_common.Format.format_string (fun format ->
Format.fprintf format
"INSERT INTO %s (%s, %s) VALUES (?, ?)"
- (Identifier.quote
- (Identifier.dot (Model.Content.id content) name))
+ (Identifier.quote (Identifier.dot table name))
parent target)
in
let request : request =
@@ -101,31 +99,26 @@ end
module Fold = Encoder.Fold (MultiReferencesInsert)
-(** Insert a row. This syntax use RETURNING ID to get the value we just inserted
-*)
-let insert_returning :
+(** Insert a row in the given table, then insert the multivalued fields in their
+ dedicated tables. This syntax uses RETURNING ID to get the value we just
+ inserted *)
+let insert_row :
Caqti_lwt.connection ->
- Model.content ->
+ table:Model.Identifier.t ->
+ ?init_fields:Model.field list ->
+ Model.field list ->
(string -> Model.value) ->
int64 Lwt.t =
- fun db content provider ->
+ fun db ~table ?(init_fields = []) fields provider ->
let module Db = (val db) in
- let init_fields =
- match Model.Content.parent content with
- | None -> []
- | Some _ ->
- Model.Field.
- [ create (T Integer) [] ~required:true Model.Identifier.parent ]
- in
-
- (* Remove the mulivaluated content fields from the request because they are
+ (* Remove the mulivaluated fields from the request because they are
not inserted in the table directly *)
let (T t) =
- collect_fields content
+ collect_fields ~init_fields
~filter:(fun field ->
let (T ty) = Model.Field.type_ field in
match ty with Model.Typ.MultiReference _ -> false | _ -> true)
- ~init_fields
+ fields
in
let insert_template =
match t with
@@ -141,8 +134,7 @@ let insert_returning :
let q =
Croquette_common.Format.format_string (fun format ->
Format.fprintf format "INSERT INTO %s %s VALUES %s RETURNING ID"
- (Identifier.quote (Model.Content.id content))
- columns insert_template)
+ (Identifier.quote table) columns insert_template)
in
let types_param = (Encoder.CaqtiRequest.f ()) t in
let request = Caqti.Templater.(direct T.(types_param -->! int64) q) in
@@ -152,24 +144,41 @@ let insert_returning :
(* The insert request is returning the ID. That’s why we call it with the
fuction [find] even if it is not a real find in the database. *)
let* id_result = Db.find request values in
- let* content_id = Caqti_lwt.or_fail id_result in
+ let* row_id = Caqti_lwt.or_fail id_result in
(* The value is created in the db, reuse the ID in order to insert the
multi-values fields in their own tables *)
let (T multi_valuated_fields) =
- collect_fields content ~filter:(fun field ->
+ collect_fields fields ~filter:(fun field ->
let (T ty) = Model.Field.type_ field in
match ty with Model.Typ.MultiReference _ -> true | _ -> false)
in
let acc =
MultiReferencesInsert.
- { db; provider; k = Lwt.return_ok (); parent_id = content_id; content }
+ { db; provider; k = Lwt.return_ok (); parent_id = row_id; table }
in
let MultiReferencesInsert.{ k; _ } = Fold.f acc multi_valuated_fields in
let* res = k in
let* () = Caqti_lwt.or_fail res in
- Lwt.return content_id
+ Lwt.return row_id
+
+let insert_returning :
+ Caqti_lwt.connection ->
+ Model.content ->
+ (string -> Model.value) ->
+ int64 Lwt.t =
+ fun db content provider ->
+ let init_fields =
+ match Model.Content.parent content with
+ | None -> []
+ | Some _ ->
+ Model.Field.
+ [ create (T Integer) [] ~required:true Model.Identifier.parent ]
+ in
+ insert_row db ~table:(Model.Content.id content) ~init_fields
+ (List.of_seq (Model.Content.fields content))
+ provider
(** Insert an entry for a specific content *)
let insert :
@@ -198,6 +207,22 @@ let insert :
insert_returning db content provider
| _ -> raise Not_found
+(** Insert a row in the dedicated table of a list, and return its id.
+
+ Like [insert], the [provider] is called with the name of each field of the
+ list to get its value. Multivalued fields are inserted in their own
+ dedicated table, with the parent pointing to the list row. *)
+let insert_list :
+ Caqti_lwt.connection -> Model.list -> (string -> Model.value) -> int64 Lwt.t
+ =
+ fun db list provider ->
+ let module Db = (val db) in
+ match Db.dialect with
+ | Caqti.Template.Dialect.(Sqlite _ | Pgsql _) ->
+ insert_row db ~table:list.Model.DataList.id list.Model.DataList.fields
+ provider
+ | _ -> raise Not_found
+
let insert_references :
Caqti_lwt.connection ->
Model.content ->
@@ -213,7 +238,13 @@ let insert_references :
in
let acc =
MultiReferencesInsert.
- { db; provider; k = Lwt.return_ok (); parent_id = content_id; content }
+ {
+ db;
+ provider;
+ k = Lwt.return_ok ();
+ parent_id = content_id;
+ table = Model.Content.id content;
+ }
in
let ( let* ) = Lwt.bind in
let MultiReferencesInsert.{ k; _ } = MultiReferencesInsert.f acc data in
diff --git a/lib/query/insert.mli b/lib/query/insert.mli
index ef8c5c9..32251ac 100644
--- a/lib/query/insert.mli
+++ b/lib/query/insert.mli
@@ -8,6 +8,17 @@ val insert :
int64 Lwt.t
(** See [Query.insert] *)
+val insert_list :
+ Caqti_lwt.connection ->
+ Model.list ->
+ (string -> Model.value) ->
+ int64 Lwt.t
+(** Insert a row in the dedicated table of a static list, and return its id.
+
+ The [provider] is called with the name of each field of the list to get its
+ value, like [insert]. Multivalued fields are inserted in their dedicated
+ table, with the parent pointing to the inserted list row. *)
+
val insert_references :
Caqti_lwt.connection ->
Model.content ->
diff --git a/lib/query/query.ml b/lib/query/query.ml
index 13a646a..2984ed3 100644
--- a/lib/query/query.ml
+++ b/lib/query/query.ml
@@ -6,5 +6,6 @@
matching the types given in the configuration. *)
let insert = Insert.insert
+let insert_list = Insert.insert_list
let create = Create.create
let update = Update.q
diff --git a/lib/query/query.mli b/lib/query/query.mli
index 89b114d..d4badd5 100644
--- a/lib/query/query.mli
+++ b/lib/query/query.mli
@@ -19,6 +19,17 @@ val insert :
This is a low-level function: there is no representation for the content
itself, and no functionnals controls are realized. *)
+val insert_list :
+ Caqti_lwt.connection ->
+ Model.list ->
+ (string -> Model.value) ->
+ int64 Lwt.t
+(** Insert a row in the dedicated table of a static list, and return its id.
+
+ The [provider] is called with the name of each field of the list, like
+ [insert]. This is the function used to populate the lists from their
+ source. *)
+
val create : Caqti_lwt.connection -> Configuration.Ast.setting -> unit Lwt.t
val update :
diff --git a/lib/query/tests/insert_list.t b/lib/query/tests/insert_list.t
new file mode 100644
index 0000000..3499bf8
--- /dev/null
+++ b/lib/query/tests/insert_list.t
@@ -0,0 +1,45 @@
+Insert rows in the lists declared in the configuration, then display the list
+tables. Two rows are inserted per list; text and integer fields get a value
+derived from the field name, and references point to the row of id 1:
+
+ $ tests --insert_list <<EOF
+ > {
+ > "list": [
+ > {
+ > "id": "country_list",
+ > "field": [
+ > { "name": "name", "label": {}, "type": "text" },
+ > { "name": "code", "label": {}, "type": "text" }
+ > ]
+ > },
+ > {
+ > "id": "user_type_list",
+ > "field": [
+ > { "name": "label", "label": {}, "type": "text" },
+ > { "name": "rank", "label": {}, "type": "integer" },
+ > {
+ > "name": "country",
+ > "label": {},
+ > "type": { "content": "country_list" }
+ > },
+ > {
+ > "name": "aliases",
+ > "label": {},
+ > "type": { "content": "country_list", "multivaluated": true }
+ > }
+ > ]
+ > }
+ > ],
+ > "content": [ { "id": "entity" } ]
+ > }
+ > EOF
+ country_list:
+ {"id":1,"name":"name-1","code":"code-1"}
+ {"id":2,"name":"name-2","code":"code-2"}
+ user_type_list:
+ {"id":1,"label":"label-1","rank":1,"country":1,"country_table":"country_list"}
+ {"id":2,"label":"label-2","rank":2,"country":1,"country_table":"country_list"}
+ user_type_list.aliases:
+ {"parent":1,"target":1,"target_table":"country_list"}
+ {"parent":1,"target":2,"target_table":"country_list"}
+ {"parent":2,"target":2,"target_table":"country_list"}
diff --git a/test/insert_list.ml b/test/insert_list.ml
new file mode 100644
index 0000000..e23f13b
--- /dev/null
+++ b/test/insert_list.ml
@@ -0,0 +1,152 @@
+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
+
+(** Name of the table targeted by a reference *)
+let target_table : Model.target -> string = function
+ | Model.Reference.List list ->
+ Model.Identifier.to_string list.Model.DataList.id
+ | Model.Reference.Content ref ->
+ Model.Identifier.to_string (Model.Content.id (Model.Reference.get ref))
+
+(** 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 buff = Buffer.create 64 in
+ let () = Buffer.add_string buff "json_object('id', \"id\"" in
+ let () =
+ List.iter
+ ~f:(fun field ->
+ let name = Model.Field.name field in
+ let (Model.Field.T ty) = Model.Field.type_ field in
+ let literal =
+ Croquette_common.Sql.quote_literal (Model.Identifier.to_string name)
+ in
+ match ty with
+ | Model.Typ.MultiReference _ -> () (* in a dedicated table *)
+ | Model.Typ.MonoReference reference ->
+ Buffer.add_string buff
+ (", " ^ literal ^ ", "
+ ^ Model.Identifier.quote name
+ ^ ", "
+ ^ Croquette_common.Sql.quote_literal
+ (Model.Identifier.to_string name ^ "_table")
+ ^ ", "
+ ^ Croquette_common.Sql.quote_literal
+ (target_table reference.Model.Typ.t))
+ | _ ->
+ Buffer.add_string buff
+ (", " ^ literal ^ ", " ^ Model.Identifier.quote name))
+ list.Model.DataList.fields
+ in
+ let () = Buffer.add_string buff ")" in
+ let q =
+ "SELECT " ^ Buffer.contents buff ^ " FROM "
+ ^ Model.Identifier.quote list.Model.DataList.id
+ in
+ let open Caqti.Templater in
+ direct T.(unit -->* string) q
+
+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)
+ ("SELECT json_object('parent', \"parent\", 'target', \"target\", \
+ 'target_table', "
+ ^ Croquette_common.Sql.quote_literal
+ (Model.Identifier.to_string target_table)
+ ^ ") FROM "
+ ^ Model.Identifier.quote 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 -> Model.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 ->
+ Model.Typ.V (Text, Some (field_name ^ "-" ^ Int64.to_string row))
+ | Model.Typ.Integer -> Model.Typ.V (Integer, Some row)
+ | Model.Typ.MonoReference reference ->
+ Model.Typ.V (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
+ Model.Typ.Seq (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 = In_channel.input_all stdin in
+ let+ configuration = Configuration.Read_conf.read_string_conf stdin in
+ Lwt_main.run
+ (let* db = Common.connect configuration in
+ 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
+ (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))
diff --git a/test/tests.ml b/test/tests.ml
index 0f0fdc5..15fb51e 100644
--- a/test/tests.ml
+++ b/test/tests.ml
@@ -34,6 +34,7 @@ let () =
register (module Hash);
register (module Configuration_);
register (module Insert);
+ register (module Insert_list);
register (module Update);
register (module Identifier);
]