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 | |
| parent | 172a02b80d8dbdcfe09129d7fa76015bb5ec4d79 (diff) | |
Load values from the database
| -rw-r--r-- | lib/model/data.ml (renamed from lib/value/data.ml) | 8 | ||||
| -rw-r--r-- | lib/model/data.mli (renamed from lib/value/data.mli) | 14 | ||||
| -rw-r--r-- | lib/model/model.ml | 5 | ||||
| -rw-r--r-- | lib/model/model.mli | 6 | ||||
| -rw-r--r-- | lib/query/dune | 1 | ||||
| -rw-r--r-- | lib/query/encoder.ml | 8 | ||||
| -rw-r--r-- | lib/query/insert.ml | 48 | ||||
| -rw-r--r-- | lib/query/insert.mli | 15 | ||||
| -rw-r--r-- | lib/query/query.ml | 1 | ||||
| -rw-r--r-- | lib/query/query.mli | 35 | ||||
| -rw-r--r-- | lib/query/select.ml | 206 | ||||
| -rw-r--r-- | lib/query/select.mli | 30 | ||||
| -rw-r--r-- | lib/query/tests/insert.t | 387 | ||||
| -rw-r--r-- | lib/query/tests/select.t | 48 | ||||
| -rw-r--r-- | lib/query/update.ml | 28 | ||||
| -rw-r--r-- | lib/value/dune | 3 | ||||
| -rw-r--r-- | lib/value/tests/dune | 11 | ||||
| -rw-r--r-- | lib/value/tests/load.t | 41 | ||||
| -rw-r--r-- | lib/value/value.ml | 104 | ||||
| -rw-r--r-- | lib/value/value.mli | 36 | ||||
| -rw-r--r-- | test/common.ml | 198 | ||||
| -rw-r--r-- | test/insert.ml | 286 | ||||
| -rw-r--r-- | test/insert_list.ml | 102 | ||||
| -rw-r--r-- | test/load.ml | 37 | ||||
| -rw-r--r-- | test/select.ml | 46 | ||||
| -rw-r--r-- | test/tests.ml | 2 | ||||
| -rw-r--r-- | test/update.ml | 50 |
27 files changed, 920 insertions, 836 deletions
diff --git a/lib/value/data.ml b/lib/model/data.ml index f0e321e..9c44203 100644 --- a/lib/value/data.ml +++ b/lib/model/data.ml @@ -8,15 +8,15 @@ representation of the field, e.g. [string option] or [int64 option]) and over ['t], the target of the field for references — a reference is held as an [int64] (the id of the target row), while ['t] records what it points to. - The field type {!Model.Typ.t} stays in the model package; only the data - itself lives here. *) + The field type {!Typ.t} stays in the model package; only the data itself + lives here. *) type ('ocaml, 't) data = | None - | V : ('db, 'ocaml, 't) Model.Typ.t * 'ocaml -> ('ocaml, 't) data + | V : ('db, 'ocaml, 't) Typ.t * 'ocaml -> ('ocaml, 't) data let none : ('ocaml, 't) data = None -let v : ('db, 'ocaml, 't) Model.Typ.t * 'ocaml -> ('ocaml, 't) data = +let v : ('db, 'ocaml, 't) Typ.t * 'ocaml -> ('ocaml, 't) data = fun (typ, value) -> V (typ, value) type 't any = Any : ('ocaml, 't) data -> 't any [@@unboxed] diff --git a/lib/value/data.mli b/lib/model/data.mli index 7b7a5cc..6773589 100644 --- a/lib/value/data.mli +++ b/lib/model/data.mli @@ -8,18 +8,18 @@ data held (e.g. [string option], [int64 option], or [int64 Seq.t] for a multivalued reference), and over ['t], the target of the field for references. The DB representation ['db] appears only inside the field type - carried by the constructors. The field type {!Model.Typ.t} stays in the - model package; only the data itself lives here. *) + carried by the constructors. The field type {!Typ.t} stays in the model + package; only the data itself lives here. *) type ('ocaml, 't) data = private | None - | V : ('db, 'ocaml, 't) Model.Typ.t * 'ocaml -> ('ocaml, 't) data + | V : ('db, 'ocaml, 't) Typ.t * 'ocaml -> ('ocaml, 't) data val none : ('ocaml, 't) data (** [none] tells the field is empty in the database (or left as is, on update) *) -val v : ('db, 'ocaml, 't) Model.Typ.t * 'ocaml -> ('ocaml, 't) data +val v : ('db, 'ocaml, 't) Typ.t * 'ocaml -> ('ocaml, 't) data (** [v (typ, value)] is the value for the field, in its code representation ['ocaml] — e.g. a single [string option], or an [int64 Seq.t] for a multivalued reference. The field type [typ] is also reported: it ties the @@ -38,8 +38,8 @@ val any : ('ocaml, 't) data -> 't any [ (* Function providing the data of each field to insert in the database *) - let f : string -> Model.target any = function - | "siret" -> any (v (Model.Typ.Text, Some "siret")) - | "name" -> any (v (Model.Typ.Text, Some "name")) + let f : string -> Reference.target any = function + | "siret" -> any (v (Typ.Text, Some "siret")) + | "name" -> any (v (Typ.Text, Some "name")) | _ -> any none ] *) diff --git a/lib/model/model.ml b/lib/model/model.ml index 0f016ad..c3e5b5b 100644 --- a/lib/model/model.ml +++ b/lib/model/model.ml @@ -5,6 +5,7 @@ module Identifier = Identifier module Dsl = Dsl module Typ = Typ +module Data = Data module Reference = Reference module DataList = Data_list module Path = Path @@ -12,9 +13,9 @@ module Field = Field module Section = Section module Content = Content -(** The target of a reference field, resolved: a lazy pointer to a content, or - a static list. *) type target = Reference.target +(** The target of a reference field, resolved: a lazy pointer to a content, or a + static list. *) type list = target Data_list.t type content = target Content.t diff --git a/lib/model/model.mli b/lib/model/model.mli index a8941d9..0326eb2 100644 --- a/lib/model/model.mli +++ b/lib/model/model.mli @@ -119,6 +119,12 @@ module Reference = Reference type reference = target Typ.reference (** A reference field target: a {!target}, plus optional constraints *) +module Data = Data +(** The data carried by a field of a value: {!Data.data} (parameterized over the + in-code representation of the data held), built with the smart constructors + {!Data.none} and {!Data.v}, and {!Data.any}, the existential pack used when + the held type is not known statically. *) + module DataList = Data_list (** A static list declared in the configuration: its entries are loaded by the data provider and the list can be referenced from fields, but it is not diff --git a/lib/query/dune b/lib/query/dune index 990b572..307319a 100644 --- a/lib/query/dune +++ b/lib/query/dune @@ -9,5 +9,4 @@ uri croquette_common model - value configuration)) diff --git a/lib/query/encoder.ml b/lib/query/encoder.ml index 2174e59..5e5f402 100644 --- a/lib/query/encoder.ml +++ b/lib/query/encoder.ml @@ -291,7 +291,7 @@ module DataToCaqtiValues = Map (struct include DefaultEncoder type 'a t = 'a - type env = string -> Value.value + type env = string -> Model.target Model.Data.any let f : type a b. env -> (a, b) Intf.data -> a t = fun provider Intf.{ name; content_type } -> @@ -299,13 +299,13 @@ module DataToCaqtiValues = Map (struct (* Check the value given by the provider and ensure it’s match the expected type *) match result with - | Value.Data.Any d -> ( + | Model.Data.Any d -> ( match d with - | Value.Data.None -> + | Model.Data.None -> (* The provider does not have any value for this property, use the default value*) Model.Typ.default_value content_type - | Value.Data.V (field_type', value) -> ( + | Model.Data.V (field_type', value) -> ( (* For the single-column fields handled here, the DB and code representations coincide — match the field type to prove it *) let check (type x) (ct : (x, x, Model.target) Model.Typ.t) : x = diff --git a/lib/query/insert.ml b/lib/query/insert.ml index 703d5a9..26597d8 100644 --- a/lib/query/insert.ml +++ b/lib/query/insert.ml @@ -25,7 +25,7 @@ module MultiReferencesInsert = struct parent_id : int64; table : Model.Identifier.t; (** The table holding the row: a content table, or a list *) - provider : string -> Value.value; + provider : string -> Model.target Model.Data.any; k : (unit, Caqti.Error.t) result Lwt.t; } @@ -64,10 +64,10 @@ module MultiReferencesInsert = struct and target = Identifier.quote Identifier.target in match content_type with | Model.Typ.MultiReference _target -> ( - let (Value.Data.Any d) = provider (Model.Identifier.to_string name) in + let (Model.Data.Any d) = provider (Model.Identifier.to_string name) in match d with - | Value.Data.None -> acc - | Value.Data.V (field_type', values) -> ( + | Model.Data.None -> acc + | Model.Data.V (field_type', values) -> ( match Model.Typ.eq_ocaml content_type field_type' with | None -> raise Encoder.TypeMismatch | Some Equal -> @@ -106,7 +106,7 @@ let insert_row : table:Model.Identifier.t -> ?init_fields:Model.field list -> Model.field list -> - (string -> Value.value) -> + (string -> Model.target Model.Data.any) -> int64 Lwt.t = fun db ~table ?(init_fields = []) fields provider -> let module Db = (val db) in @@ -165,8 +165,8 @@ let insert_row : let insert_returning : Caqti_lwt.connection -> Model.content -> - (string -> Value.value) -> - Model.content Value.t Lwt.t = + (string -> Model.target Model.Data.any) -> + int64 Lwt.t = fun db content provider -> let init_fields = match Model.Content.parent content with @@ -181,15 +181,15 @@ let insert_returning : (List.of_seq (Model.Content.fields content)) provider in - Lwt.return (Value.v content id) + Lwt.return id -(** Insert an entry for a specific content *) +(** Insert an entry for a specific content, and return the id of the new row *) let insert : Caqti_lwt.connection -> Model.content -> - ?parent:Model.content Value.t -> - (string -> Value.value) -> - Model.content Value.t Lwt.t = + ?parent:Int64.t -> + (string -> Model.target Model.Data.any) -> + int64 Lwt.t = fun db content ?parent provider -> let provider = match (parent, Model.Content.parent content) with @@ -198,15 +198,11 @@ let insert : (* Raise an error right now if the parent of the subcontent is not given *) raise Encoder.TypeMismatch - | Some v, Some _ -> ( + | Some parent_id, Some _ -> ( (* Otherwise update the provider to give a value for the parent *) - let parent_id = - match Value.get v Value.id_ref with - | Some parent_id -> parent_id - | None -> raise Encoder.TypeMismatch - in function - | "parent" -> Value.single (Model.Typ.Integer, Some parent_id) + | "parent" -> + Model.Data.any (Model.Data.v (Model.Typ.Integer, Some parent_id)) | other -> provider other) in let module Db = (val db) in @@ -215,7 +211,7 @@ let insert : insert_returning db content provider | _ -> raise Not_found -(** Insert a row in the dedicated table of a list, and return it. +(** 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 @@ -223,8 +219,8 @@ let insert : let insert_list : Caqti_lwt.connection -> Model.list -> - (string -> Value.value) -> - Model.list Value.t Lwt.t = + (string -> Model.target Model.Data.any) -> + int64 Lwt.t = fun db list provider -> let module Db = (val db) in match Db.dialect with @@ -234,7 +230,7 @@ let insert_list : insert_row db ~table:list.Model.DataList.id list.Model.DataList.fields provider in - Lwt.return (Value.v list id) + Lwt.return id | _ -> raise Not_found let insert_references : @@ -242,13 +238,13 @@ let insert_references : Model.content -> Int64.t -> Model.Identifier.t -> - Value.value -> + Model.target Model.Data.any -> (Int64.t, _) Intf.data -> unit Lwt.t = fun db content content_id field_name value data -> - let provider : string -> Value.value = function + let provider : string -> Model.target Model.Data.any = function | n when String.equal n (Model.Identifier.to_string field_name) -> value - | _ -> Value.none + | _ -> Model.Data.any Model.Data.none in let acc = MultiReferencesInsert. diff --git a/lib/query/insert.mli b/lib/query/insert.mli index a0ab6e9..d3e3de9 100644 --- a/lib/query/insert.mli +++ b/lib/query/insert.mli @@ -3,18 +3,17 @@ val insert : Caqti_lwt.connection -> Model.content -> - ?parent:Model.content Value.t -> - (string -> Value.value) -> - Model.content Value.t Lwt.t + ?parent:Int64.t -> + (string -> Model.target Model.Data.any) -> + int64 Lwt.t (** See [Query.insert] *) val insert_list : Caqti_lwt.connection -> Model.list -> - (string -> Value.value) -> - Model.list Value.t Lwt.t -(** Insert a row in the dedicated table of a static list, and return it as a - value. + (string -> Model.target Model.Data.any) -> + 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 @@ -25,7 +24,7 @@ val insert_references : Model.content -> Int64.t -> Model.Identifier.t -> - Value.value -> + Model.target Model.Data.any -> (Int64.t, _) Intf.data -> unit Lwt.t (** Insert a list of references for a content. This function is used during the diff --git a/lib/query/query.ml b/lib/query/query.ml index 2984ed3..8d8006c 100644 --- a/lib/query/query.ml +++ b/lib/query/query.ml @@ -9,3 +9,4 @@ let insert = Insert.insert let insert_list = Insert.insert_list let create = Create.create let update = Update.q +let select = Select.select diff --git a/lib/query/query.mli b/lib/query/query.mli index cd7287f..ae7245e 100644 --- a/lib/query/query.mli +++ b/lib/query/query.mli @@ -1,17 +1,16 @@ val insert : Caqti_lwt.connection -> Model.content -> - ?parent:Model.content Value.t -> - (string -> Value.value) -> - Model.content Value.t Lwt.t -(** Insert a new element and return it as a value. + ?parent:Int64.t -> + (string -> Model.target Model.Data.any) -> + int64 Lwt.t +(** Insert a new element in the database and return the id of the new row. - [insert db content ~parent provider] will create the content in the database - and return the inserted row as a [Value.t] (the content together with its - new id). + [insert db content ~parent provider] creates the content in the database and + returns the primary key of the inserted row. - The parent — itself a value — is required if the content is a subcontent. - Not providing the parameter will raise the error [TypeMismatch]. + The [parent] — the id of the parent row — is required if the content is a + subcontent. Not providing the parameter will raise the error [TypeMismatch]. For each field in the content, the function [provider] will be called with the name for this property. The value returned shall match the expected type @@ -23,10 +22,9 @@ val insert : val insert_list : Caqti_lwt.connection -> Model.list -> - (string -> Value.value) -> - Model.list Value.t Lwt.t -(** Insert a row in the dedicated table of a static list, and return it as a - value. + (string -> Model.target Model.Data.any) -> + 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. @@ -38,5 +36,14 @@ val update : Caqti_lwt.connection -> Int64.t -> Model.content -> - (string -> Value.value) -> + (string -> Model.target Model.Data.any) -> unit Lwt.t + +val select : + Caqti_lwt.connection -> + Int64.t -> + Model.content -> + Select.callback -> + unit Lwt.t +(** Read a row of a content table and call the callback once per field with the + field's named type and its value. See [Query.Select.select] *) diff --git a/lib/query/select.ml b/lib/query/select.ml new file mode 100644 index 0000000..011544c --- /dev/null +++ b/lib/query/select.ml @@ -0,0 +1,206 @@ +(** Read a row of a content table from the database, and expose each of its + fields to a callback. *) + +let ( let* ) = Lwt.bind + +type callback = { + f : + 'db 'ocaml. + ('db, 'ocaml, Model.target) Model.Typ.t Model.Typ.named * 'ocaml -> unit; +} + +(** Call the callback for one single-column field. For those fields the database + and the code representations coincide, so the column value read from the + database is also the code value. *) +let apply_field : type a b. callback -> (a, b) Intf.data -> a -> unit = + fun k Intf.{ name; content_type } value -> + match content_type with + | Model.Typ.Text -> + let named = Model.Typ.named ~name ~typ:content_type in + k.f (named, value) + | Model.Typ.Integer -> + let named = Model.Typ.named ~name ~typ:content_type in + k.f (named, value) + | Model.Typ.MonoReference _ -> + let named = Model.Typ.named ~name ~typ:content_type in + k.f (named, value) + | Model.Typ.MultiReference _ -> raise Encoder.TypeMismatch + +(** Call the callback for each field of the tuple, zipping the type descriptors + with the row read from the database *) +let rec iter_fields : type a. callback -> a Intf.tuple -> a -> unit = + fun k tup row -> + match tup with + | Intf.T0 -> ( match row with () -> ()) + | Intf.T1 d1 -> apply_field k d1 row + | Intf.T2 (d1, d2) -> + let v1, v2 = row in + apply_field k d1 v1; + apply_field k d2 v2 + | Intf.T3 (d1, d2, d3) -> + let v1, v2, v3 = row in + apply_field k d1 v1; + apply_field k d2 v2; + apply_field k d3 v3 + | Intf.T4 (d1, d2, d3, d4) -> + let v1, v2, v3, v4 = row in + apply_field k d1 v1; + apply_field k d2 v2; + apply_field k d3 v3; + apply_field k d4 v4 + | Intf.T5 (d1, d2, d3, d4, d5) -> + let v1, v2, v3, v4, v5 = row in + apply_field k d1 v1; + apply_field k d2 v2; + apply_field k d3 v3; + apply_field k d4 v4; + apply_field k d5 v5 + | Intf.T6 (d1, d2, d3, d4, d5, d6) -> + let v1, v2, v3, v4, v5, v6 = row in + apply_field k d1 v1; + apply_field k d2 v2; + apply_field k d3 v3; + apply_field k d4 v4; + apply_field k d5 v5; + apply_field k d6 v6 + | Intf.T7 (d1, d2, d3, d4, d5, d6, d7) -> + let v1, v2, v3, v4, v5, v6, v7 = row in + apply_field k d1 v1; + apply_field k d2 v2; + apply_field k d3 v3; + apply_field k d4 v4; + apply_field k d5 v5; + apply_field k d6 v6; + apply_field k d7 v7 + | Intf.T8 (d1, d2, d3, d4, d5, d6, d7, d8) -> + let v1, v2, v3, v4, v5, v6, v7, v8 = row in + apply_field k d1 v1; + apply_field k d2 v2; + apply_field k d3 v3; + apply_field k d4 v4; + apply_field k d5 v5; + apply_field k d6 v6; + apply_field k d7 v7; + apply_field k d8 v8 + | Intf.T9 (d1, d2, d3, d4, d5, d6, d7, d8, d9) -> + let v1, v2, v3, v4, v5, v6, v7, v8, v9 = row in + apply_field k d1 v1; + apply_field k d2 v2; + apply_field k d3 v3; + apply_field k d4 v4; + apply_field k d5 v5; + apply_field k d6 v6; + apply_field k d7 v7; + apply_field k d8 v8; + apply_field k d9 v9 + | Intf.T10 (d1, d2, d3, d4, d5, d6, d7, d8, d9, d10) -> + let v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 = row in + apply_field k d1 v1; + apply_field k d2 v2; + apply_field k d3 v3; + apply_field k d4 v4; + apply_field k d5 v5; + apply_field k d6 v6; + apply_field k d7 v7; + apply_field k d8 v8; + apply_field k d9 v9; + apply_field k d10 v10 + | Intf.T_N (d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, tl) -> + let v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, vtl = row in + apply_field k d1 v1; + apply_field k d2 v2; + apply_field k d3 v3; + apply_field k d4 v4; + apply_field k d5 v5; + apply_field k d6 v6; + apply_field k d7 v7; + apply_field k d8 v8; + apply_field k d9 v9; + apply_field k d10 v10; + iter_fields k tl vtl + +(** Read the targets of a multivalued reference field from its dedicated table, + and call the callback with the sequence of targets *) +let select_multireference : + Caqti_lwt.connection -> + Model.Identifier.t -> + Int64.t -> + Model.field -> + callback -> + unit Lwt.t = + fun db table id field k -> + let module Db = (val db) in + let module Identifier = Model.Identifier in + let name = Model.Field.name field in + let (Model.Field.T content_type) = Model.Field.type_ field in + match content_type with + | Model.Typ.MultiReference _ -> + let named = Model.Typ.named ~name ~typ:content_type in + let q = + Croquette_common.Format.format_string (fun format -> + Format.fprintf format "SELECT %s FROM %s WHERE %s = ?" + (Identifier.quote Identifier.target) + (Identifier.quote (Identifier.dot table name)) + (Identifier.quote Identifier.parent)) + in + let request = Caqti.Templater.(direct T.(int64 -->* int64) q) in + let* res = Db.collect_list request id in + let* targets = Caqti_lwt.or_fail res in + k.f (named, List.to_seq targets); + Lwt.return_unit + | _ -> raise Encoder.TypeMismatch + +let select : + Caqti_lwt.connection -> Int64.t -> Model.content -> callback -> unit Lwt.t = + fun db id content k -> + let module Db = (val db) in + let module Identifier = Model.Identifier in + let table = Model.Content.id content in + (* The [parent] column of a subcontent is exposed as a field, like on + insert *) + let init_fields = + match Model.Content.parent content with + | None -> [] + | Some _ -> + [ + Model.Field.create (Model.Field.T Model.Typ.Integer) [] ~required:true + Model.Identifier.parent; + ] + in + let fields = init_fields @ List.of_seq (Model.Content.fields content) in + let is_multireference field = + match Model.Field.type_ field with + | Model.Field.T (Model.Typ.MultiReference _) -> true + | _ -> false + in + let single_fields = List.filter (fun f -> not (is_multireference f)) fields in + let multi_fields = List.filter is_multireference fields in + (* The multivalued fields are not columns of the content table, they are + read separately from their dedicated tables *) + let (T single) = + Encoder.list_to_tup single_fields ~f:(fun field -> + let name = Model.Field.name field in + let (Model.Field.T content_type) = Model.Field.type_ field in + Intf.TT { name; content_type }) + in + let* () = + match single with + | Intf.T0 -> Lwt.return_unit + | _ -> + let columns = Encoder.InsertTableName.f () single in + let row_type = Encoder.CaqtiRequest.f () single in + let q = + Croquette_common.Format.format_string (fun format -> + Format.fprintf format "SELECT %s FROM %s WHERE %s = ?" columns + (Identifier.quote table) + (Identifier.quote Identifier.id)) + in + let request = Caqti.Templater.(direct T.(int64 -->! row_type) q) in + let* res = Db.find request id in + let* row = Caqti_lwt.or_fail res in + let () = iter_fields k single row in + Lwt.return_unit + in + Lwt_list.iter_s + (fun field -> select_multireference db table id field k) + multi_fields diff --git a/lib/query/select.mli b/lib/query/select.mli new file mode 100644 index 0000000..338cef8 --- /dev/null +++ b/lib/query/select.mli @@ -0,0 +1,30 @@ +(** Read a row of a content table from the database *) + +type callback = { + f : + 'db 'ocaml. + ('db, 'ocaml, Model.target) Model.Typ.t Model.Typ.named * 'ocaml -> unit; +} +(** A callback called once per field of the selected row, with the field's named + type and its data in the code representation ['ocaml] — a pair that can be + stored in a value's dictionary and read back as typed data. + + The field is existentially typed: ['db] and ['ocaml] are universally + quantified in the record field, so a plain function works for a field of any + type: [ { f = fun (named, data) -> ... } ]. *) + +val select : + Caqti_lwt.connection -> Int64.t -> Model.content -> callback -> unit Lwt.t +(** [select db id content callback] reads the row [id] in the table of [content] + and calls [callback.f] once per field of the content, with the field's named + type and its value: + + - the single-column fields ([Text], [Integer], [MonoReference], plus the + reserved [parent] column of a subcontent) are read from the content table + — their database and code representations coincide; + - each multivalued reference field is read from its dedicated table + ([content.field]) and yields the sequence of its targets ([int64 Seq.t]). + + The [id] of the row itself is not passed to the callback: it is the [id] + argument. In the callback, each [(named, data)] pair can be stored in a + value's dictionary to make the field readable as typed data. *) diff --git a/lib/query/tests/insert.t b/lib/query/tests/insert.t index e060ad4..3dd2c94 100644 --- a/lib/query/tests/insert.t +++ b/lib/query/tests/insert.t @@ -1,31 +1,32 @@ -Test the capacity to insert elements in the database according a specific -schema +Test the capacity to insert elements in the database according to a specific +schema. -The data to insert is given in the json format. This format is a subset from the -configuration (for example we can’t have a property with the same name as a -subcontent) +The configuration is read on the standard input, and the name of the content to +populate is given as argument. Two rows are inserted per run, with a value +generated for each field from its type and the row number (see +[Common.provider_for]): text and integer fields get a value derived from the +field name, monovalued references point to the row of id 1, and multivalued +references get two values for the first row and one for the second. -Minimal exemple, a content without any field does not have any value: +A content without any field gets an empty row: - $ tests --insert <<EOF + $ tests --insert "entity" <<EOF > { > "content": [ > { - > "id": "entity", - > "values": [ {} ] + > "id": "entity" > } > ] > } > EOF entity:1 {} + entity:2 + {} -Ensure an error is raised if the value does not match the expected type. - -Here, the year is declared as an integer, but the value is provided as string. -This error is expected to be checked and reported. +Text and integer fields are filled with a generated value: - $ tests --insert <<EOF + $ tests --insert "entity" <<EOF > { > "content": [ > { @@ -33,366 +34,66 @@ This error is expected to be checked and reported. > "section": [ > { > "field": [ - > { - > "name": "year", - > "label": {}, - > "type": "integer" - > } + > { "name": "name", "label": {}, "type": "text" }, + > { "name": "year", "label": {}, "type": "integer" } > ] > } - > ], - > "values": [ { "year": "1234", } ] - > } - > ] - > } - > EOF - Fatal error: exception Query__Encoder.TypeMismatch - [2] - - $ tests --insert <<EOF - > { - > "content": [ - > { - > "id": "entity", - > "section": [ - > { - > "field": [ - > { - > "name": "name", - > "label": {}, - > "type": "text", - > "required": true - > }, - > { - > "name": "year", - > "label": {}, - > "type": "integer" - > } - > ] - > } - > ], - > "values": [ - > { - > "name": "name", - > "year": 1234, - > } > ] > } > ] > } > EOF entity:1 - {"name":"name","year":1234} - -Any value can be omitted and will be replaced by the default value: - $ tests --insert <<EOF - > { - > "content": [ - > { - > "id": "entity", - > "section": [ - > { - > "field": [ - > { - > "name": "year", - > "label": {}, - > "type": "integer", - > } - > ] - > } - > ], - > "values": [ {} ] - > } - > ] - > } - > EOF - entity:1 - {"year":null} - -If the field is "required" the value is still inserted: the requirement feature -is only functionnal; this shall not block the creation of the content in the -database. - - $ tests --insert <<EOF - > { - > "content": [ - > { - > "id": "entity", - > "section": [ - > { - > "field": [ - > { - > "name": "year", - > "label": {}, - > "type": "integer", - > "required": true, - > } - > ] - > } - > ], - > "values": [ {} ] - > } - > ] - > } - > EOF - entity:1 - {"year":null} + {"name":"name-1","year":1} + entity:2 + {"name":"name-2","year":2} -A subcontent is linked to the parent: +References are declared in the field type. A monovalued reference stores the +id of the target row in the content table, and each value of a multivalued +reference gets its own line, read back from the dedicated table: - $ tests --insert <<EOF + $ tests --insert "entity" <<EOF > { - > "content": [ + > "list": [ > { - > "id": "referential", - > }, - > { - > "id": "entity", - > "section": [ - > { - > "field": [ - > { - > "name": "idx", - > "label": {}, - > "type": "integer" - > }, - > ] - > } - > ], - > "subcontent": [ - > { - > "id": "details", - > "section": [ - > { - > "field": [ - > { - > "name": "comment", - > "label": {}, - > "type": "text" - > }, - > ] - > } - > ], - > } - > ], - > "values": [ - > { - > "idx": 1, - > "details": [ - > {"comment": "lorem"}, - > {"comment": "ipsum"} - > ] - > }, - > { - > "idx": 2, - > "details": [ - > {"comment": "lorem 2"}, - > ] - > } + > "id": "country_list", + > "field": [ + > { "name": "code", "label": {}, "type": "text" } > ] > } - > ] - > } - > EOF - entity:1 - {"idx":1} - details:1 - {"comment":"lorem","parent":1} - details:2 - {"comment":"ipsum","parent":1} - entity:2 - {"idx":2} - details:3 - {"comment":"lorem 2","parent":2} - -In this syntax, external references are declared inside the feature referencing -them. - - $ tests --insert <<EOF - > { + > ], > "content": [ > { - > "id": "referential", + > "id": "entity", > "section": [ > { > "field": [ + > { "name": "name", "label": {}, "type": "text" }, + > { "name": "year", "label": {}, "type": "integer" }, > { - > "name": "description", + > "name": "country", > "label": {}, - > "type": "text" + > "type": { "content": "country_list" } > }, - > ] - > } - > ], - > "values": [ - > {"description": "id2"}, - > ] - > }, - > { - > "id": "entity", - > "section": [ - > { - > "field": [ - > { - > "name": "idx", - > "label": {}, - > "type": "integer" - > }, - > { - > "name": "comment", - > "label": {}, - > "type": { - > "content": "referential", - > "multivaluated": false - > } - > } - > ] - > } - > ], - > "values": [ - > { - > "idx": 1, - > "comment": {"description": "id1"}, - > - > }, - > ] - > } - > ] - > } - > EOF - referential:2 - {"description":"id2"} - entity:1 - {"idx":1,"comment":1} - -Four contents are created for the type referential here: -- Two as contents linked referenced by the content "entity" (ids 1 and 2). -- Two as contents not referenced at all (ids 3 and 4). - - $ tests --insert <<EOF - > { - > "content": [ - > { - > "id": "referential", - > "section": [ - > { - > "field": [ > { - > "name": "description", + > "name": "aliases", > "label": {}, - > "type": "text" - > }, + > "type": { "content": "country_list", "multivaluated": true } + > } > ] > } - > ], - > "values": [ - > {"description": "id3"}, - > {"description": "id4"} - > ] - > }, - > { - > "id": "entity", - > "section": [ - > { - > "field": [ - > { - > "name": "idx", - > "label": {}, - > "type": "integer" - > }, - > { - > "name": "comment", - > "label": {}, - > "type": { - > "content": "referential", - > "multivaluated": true - > } - > } - > ] - > } - > ], - > "values": [ - > { - > "idx": 1, - > "comment": [ - > {"description": "id1"}, - > {"description": "id2"} - > ] - > }, > ] > } > ] > } > EOF - referential:3 - {"description":"id3"} - referential:4 - {"description":"id4"} - entity:1 - {"idx":1} entity:1 - {"comment":1} + {"name":"name-1","year":1,"country":1} entity:1 - {"comment":2} - -Subcontent are also managed in the same way. - - $ tests --insert <<EOF - > { - > "content": [ - > { - > "id": "entity", - > "section": [ - > { - > "field": [ ] - > } - > ], - > "subcontent": [ - > { - > "id": "details", - > "section": [ - > { - > "field": [ - > { - > "name": "reference", - > "label": {}, - > "type": { - > "content": "referential", - > "multivaluated": true - > } - > } - > ] - > } - > ], - > } - > ], - > "values": [ - > { - > "idx": 1, - > "details": [ - > { - > // Create two references here, the counter for the non-linked - > // reference is raised to 3 - > "reference": [ {}, {} ] - > } - > ] - > }, - > ] - > }, - > { - > "id": "referential", - > "values": [ {} ] - > }, - > ] - > } - > EOF + {"aliases":1} entity:1 - {} - details:1 - {"parent":1} - details:1 - {"reference":1} - details:1 - {"reference":2} - referential:3 - {} + {"aliases":2} + entity:2 + {"name":"name-2","year":2,"country":1} + entity:2 + {"aliases":2} diff --git a/lib/query/tests/select.t b/lib/query/tests/select.t new file mode 100644 index 0000000..f4a4740 --- /dev/null +++ b/lib/query/tests/select.t @@ -0,0 +1,48 @@ +Low level test of [Query.select]: two rows are inserted in the table of the +content given as argument (the configuration is read on the standard input), +then each row is read back with [Query.select]. One line is printed per field, +as received by the callback. The [id] of the row is the argument of [select]: +it is not reported by the callback. + + $ tests --select "entity" <<EOF + > { + > "list": [ + > { + > "id": "country_list", + > "field": [ + > { "name": "code", "label": {}, "type": "text" } + > ] + > } + > ], + > "content": [ + > { + > "id": "entity", + > "section": [ + > { + > "field": [ + > { "name": "name", "label": {}, "type": "text" }, + > { "name": "year", "label": {}, "type": "integer" }, + > { "name": "country", "label": {}, "type": { "content": "country_list" } }, + > { + > "name": "aliases", + > "label": {}, + > "type": { "content": "country_list", "multivaluated": true } + > } + > ] + > } + > ] + > } + > ] + > } + > EOF + row 1: + name = "name-1" + year = 1 + country -> 1 + aliases -> [1,2] + row 2: + name = "name-2" + year = 2 + country -> 1 + aliases -> [2] + diff --git a/lib/query/update.ml b/lib/query/update.ml index 5b53cb7..08a7551 100644 --- a/lib/query/update.ml +++ b/lib/query/update.ml @@ -1,6 +1,6 @@ (** Generate the update queries for a content *) -type provider = string -> Value.value +type provider = string -> Model.target Model.Data.any module UpdateTemplate = Encoder.Map (struct type 'a t = string @@ -22,7 +22,7 @@ end) let update_fields : Caqti_lwt.connection -> Model.content -> - (Intf.t_type * (string * Value.value)) list -> + (Intf.t_type * (string * Model.target Model.Data.any)) list -> int64 -> unit Lwt.t = fun db content fields id -> @@ -141,7 +141,9 @@ let update_mulivaluated_field : At this step, we can just use the code from the insert module. *) let remaining = ReferencesSet.(diff values (of_list remaining')) in - let v = Value.single (t, ReferencesSet.to_seq remaining) in + let v = + Model.Data.any (Model.Data.v (t, ReferencesSet.to_seq remaining)) + in Insert.insert_references db content id field_name v data_type) Lwt.return_unit fields @@ -149,7 +151,7 @@ let q : Caqti_lwt.connection -> Int64.t -> Model.content -> - (string -> Value.value) -> + (string -> Model.target Model.Data.any) -> unit Lwt.t = fun db id content provider -> let module Db = (val db) in @@ -157,15 +159,15 @@ let q : (* There is a difference between - - Value.Data.none - - Value.Data.v (typ, None) + - Model.Data.none + - Model.Data.v (typ, None) The former will be ignored in the update, and the value will remain as is. The latter will set the column to NULL in the database. *) let fields, references = Seq.fold_left (fun ((fields, references) as acc : - (Intf.t_type * (string * Value.value)) list + (Intf.t_type * (string * Model.target Model.Data.any)) list * (Model.Identifier.t * Model.target Model.Typ.multireference * int64 Seq.t) @@ -173,12 +175,12 @@ let q : let name = Model.Field.name field in let (Model.Field.T content_type) = Model.Field.type_ field in let tt = Intf.TT { name; content_type } in - let (Value.Data.Any d) = provider (Model.Identifier.to_string name) in + let (Model.Data.Any d) = provider (Model.Identifier.to_string name) in match content_type with | Model.Typ.MultiReference _ -> ( match d with - | Value.Data.None -> acc - | Value.Data.V (t, seq) -> ( + | Model.Data.None -> acc + | Model.Data.V (t, seq) -> ( match ( Model.Typ.eq content_type t, Model.Typ.eq_ocaml content_type t ) @@ -188,9 +190,9 @@ let q : | _ -> raise Encoder.TypeMismatch)) | _ -> ( match d with - | Value.Data.None -> acc - | Value.Data.V _ as dv -> - ( (tt, (Model.Identifier.to_string name, Value.Data.any dv)) + | Model.Data.None -> acc + | Model.Data.V _ as dv -> + ( (tt, (Model.Identifier.to_string name, Model.Data.any dv)) :: fields, references ))) ([], []) diff --git a/lib/value/dune b/lib/value/dune index bcfb9a8..761cdf7 100644 --- a/lib/value/dune +++ b/lib/value/dune @@ -1,4 +1,3 @@ (library (name value) - (libraries - model)) + (libraries model caqti-lwt lwt query)) diff --git a/lib/value/tests/dune b/lib/value/tests/dune new file mode 100644 index 0000000..c5913da --- /dev/null +++ b/lib/value/tests/dune @@ -0,0 +1,11 @@ +; Add the test library in the path + +(env + (_ + (binaries %{project_root}/test/tests.exe))) + +; And depend of it. The binary will be available in the +; cram tests under the name "tests" + +(cram + (deps %{bin:tests})) diff --git a/lib/value/tests/load.t b/lib/value/tests/load.t new file mode 100644 index 0000000..e20860a --- /dev/null +++ b/lib/value/tests/load.t @@ -0,0 +1,41 @@ +High level test of [Value.load]: two rows are inserted in the table of the +content given as argument (the configuration is read on the standard input), +loaded as values with [Value.load] and printed with [Value.pp] — the primary +key and every field, as read back as typed data with [Value.get]. + + $ tests --load "entity" <<EOF + > { + > "list": [ + > { + > "id": "country_list", + > "field": [ + > { "name": "code", "label": {}, "type": "text" } + > ] + > } + > ], + > "content": [ + > { + > "id": "entity", + > "section": [ + > { + > "field": [ + > { "name": "name", "label": {}, "type": "text" }, + > { "name": "year", "label": {}, "type": "integer" }, + > { "name": "country", "label": {}, "type": { "content": "country_list" } }, + > { + > "name": "aliases", + > "label": {}, + > "type": { "content": "country_list", "multivaluated": true } + > } + > ] + > } + > ] + > } + > ] + > } + > EOF + row 1: + {id = 1; aliases = [1, 2]; country = 1; name = "name-1"; year = 1} + row 2: + {id = 2; aliases = [2]; country = 1; name = "name-2"; year = 2} + diff --git a/lib/value/value.ml b/lib/value/value.ml index 84bed4c..ab5b5a7 100644 --- a/lib/value/value.ml +++ b/lib/value/value.ml @@ -1,19 +1,32 @@ -module Data = Data -(** The data carried by a field: {!Data.data}, parameterized over the type of - the data held, built with the smart constructors [Data.none], [Data.v] *) - -type value = Model.target Data.any +type value = Model.target Model.Data.any (** A resolved field data: the data of a field of a content or a list, whatever the type of the data held *) -let none : value = Data.any Data.none +let none : value = Model.Data.any Model.Data.none let single : ('db, 'ocaml, Model.target) Model.Typ.t * 'ocaml -> value = - fun data -> Data.any (Data.v data) + fun data -> Model.Data.any (Model.Data.v data) + +(** The key of the fields dictionary: an existential pack over the named type of + a field, hiding its ['db] and ['ocaml] parameters *) +type key = + | Named : ('db, 'ocaml, Model.target) Model.Typ.t Model.Typ.named -> key + +module Key = struct + type t = key + + let equal : t -> t -> bool = fun (Named a) (Named b) -> Model.Typ.same a b + let hash : t -> int = fun (Named n) -> Hashtbl.hash (Model.Typ.name n) +end + +module Fields = Hashtbl.Make (Key) type 'a t = { content : 'a; (** The content or list whose table holds the row *) id : int64; (** The primary key of the row *) + fields : value Fields.t; + (** Cache of the other fields and their values, keyed by their named type + — empty by default, populating it requires reading the database *) } (** A value: one row of a content or list table in the database. @@ -21,10 +34,22 @@ type 'a t = { the row: [Model.content] for contents ([Model.content Value.t]) and [Model.list] for lists ([Model.list Value.t]). *) -let v : 'a -> int64 -> 'a t = fun content id -> { content; id } +let v : 'a -> int64 -> 'a t = + fun content id -> { content; id; fields = Fields.create 0 } + let content : 'a t -> 'a = fun t -> t.content let id : 'a t -> int64 = fun t -> t.id +let add_field : type ocaml. + 'a t -> + ('db, ocaml, Model.target) Model.Typ.t Model.Typ.named -> + ocaml -> + unit = + fun t ref data -> + let typ = Model.Typ.typ ref in + Fields.replace t.fields (Named ref) + (Model.Data.any (Model.Data.v (typ, data))) + let id_ref : Model.target Model.Typ.monoreference Model.Typ.named = Model.Typ.named ~name:Model.Identifier.id ~typ:Model.Typ.Integer @@ -33,4 +58,65 @@ let get : type ocaml. fun t ref -> match Model.Typ.provably_equal id_ref ref with | Some Type.Equal -> Some t.id - | None -> raise Not_found + | None -> ( + match Fields.find_opt t.fields (Named ref) with + | Some (Model.Data.Any (Model.Data.V (typ, payload))) -> ( + match Model.Typ.eq_ocaml (Model.Typ.typ ref) typ with + | Some Type.Equal -> payload + | None -> raise Not_found) + | _ -> raise Not_found) + +let load : Caqti_lwt.connection -> Model.content t -> unit Lwt.t = + fun db v -> + let ( let* ) = Lwt.bind in + let* () = + Query.select db v.id v.content + { f = (fun (named, data) -> add_field v named data) } + in + Lwt.return_unit + +(** Pretty-print the data of a field, whatever its type *) +let pp_data : type db ocaml. + Format.formatter -> (db, ocaml, Model.target) Model.Typ.t -> ocaml -> unit = + fun fmt typ data -> + let pp_option pp fmt data = + match data with + | Some v -> pp fmt v + | None -> Format.pp_print_string fmt "NULL" + in + match typ with + | Model.Typ.Text -> + pp_option (fun fmt s -> Format.fprintf fmt "%S" s) fmt data + | Model.Typ.Integer -> + pp_option (fun fmt i -> Format.fprintf fmt "%Ld" i) fmt data + | Model.Typ.MonoReference _ -> + pp_option (fun fmt i -> Format.fprintf fmt "%Ld" i) fmt data + | Model.Typ.MultiReference _ -> + Format.fprintf fmt "[%a]" + (Format.pp_print_list + ~pp_sep:(fun fmt () -> Format.pp_print_string fmt ", ") + (fun fmt i -> Format.fprintf fmt "%Ld" i)) + (List.of_seq data) + +let pp : Format.formatter -> 'a t -> unit = + fun fmt t -> + let fields = + Fields.fold + (fun (Named named) data acc -> (Model.Typ.name named, data) :: acc) + t.fields [] + |> List.sort (fun (n1, _) (n2, _) -> Model.Identifier.compare n1 n2) + in + let pp_field fmt (name, data) = + let (Model.Data.Any d) = data in + match d with + | Model.Data.None -> + Format.fprintf fmt "%s = NULL" (Model.Identifier.to_string name) + | Model.Data.V (typ, payload) -> + Format.fprintf fmt "%s = " (Model.Identifier.to_string name); + pp_data fmt typ payload + in + Format.fprintf fmt "{@[id = %Ld;@ %a@]}" t.id + (Format.pp_print_list + ~pp_sep:(fun fmt () -> Format.pp_print_string fmt "; ") + pp_field) + fields diff --git a/lib/value/value.mli b/lib/value/value.mli index 5c493aa..5a8984d 100644 --- a/lib/value/value.mli +++ b/lib/value/value.mli @@ -2,15 +2,9 @@ A value is one row of a content or list table in the database. *) -module Data = Data -(** The data carried by a field: {!Data.data}, parameterized over the type of - the data held, built with the smart constructors ([Data.none], [Data.v]); - and {!Data.any}, the existential pack used when the held type is not known - statically *) - -type value = Model.target Data.any +type value = Model.target Model.Data.any (** The data of a field of a content or a list, whatever the type of the data - held: an existential pack over {!Data.data} *) + held: an existential pack over {!Model.Data.data} *) val none : value (** [none]: the field is empty (or left as is, on update) *) @@ -54,6 +48,26 @@ val get : key of [v], and a multivalued reference field would yield an [int64 Seq.t]. The match between the requested named type and the readable field is proved - by {!Model.Typ.provably_equal} on name and type. Only the reserved field - [id_ref] is readable for now; the other fields are read from the database - (not implemented yet) and raise [Not_found]. *) + by {!Model.Typ.provably_equal} on name and type. The reserved field [id_ref] + is always readable; the other fields are read from the value's dictionary + (see {!add_field}) and raise [Not_found] when absent. *) + +val load : Caqti_lwt.connection -> Model.content t -> unit Lwt.t +(** [load db v] reads the row of [v] in the database (one query for the content + table, plus one per multivalued reference field) and stores each field in + [v]'s dictionary with {!add_field}, so that {!get} can then read every field + as typed data. + + The value is modified in place — the dictionary is part of its state — so + the function returns [unit]. The dictionary is empty when the value is built + with {!v}: [load] is what populates it. The primary key needs no loading — + {!get} with {!id_ref} reads it directly from the value. *) + +val pp : Format.formatter -> 'a t -> unit +(** Pretty-print a value as [{ id = …; field = …; … }]: the primary key and the + fields currently held in the value's dictionary, sorted by field name (the + dictionary is a hashtable, sorting keeps the output stable). + + The content or list the value belongs to is not printed — its type ['a] is + abstract here — and neither are the fields not loaded yet: only what {!load} + (or {!add_field}) put in the dictionary is shown. *) diff --git a/test/common.ml b/test/common.ml index 2a1e070..b1032e9 100644 --- a/test/common.ml +++ b/test/common.ml @@ -10,7 +10,9 @@ let connect : Configuration.Ast.setting -> Caqti_lwt.connection Lwt.t = on PRAGMA foreign_keys: the tests insert references to rows that are not present in the referenced table. *) let config = Caqti.Connect.Config.(set tweaks_version (1, 7) default) in - let* conn = Caqti_lwt_unix.connect ~config (Uri.of_string "sqlite3::memory:") in + let* conn = + Caqti_lwt_unix.connect ~config (Uri.of_string "sqlite3::memory:") + in let* db = Caqti_lwt.or_fail conn in let module Db = (val db) in let* () = Query.create db conf in @@ -34,8 +36,8 @@ let target_table : Model.target -> string = function | Model.Reference.Content ref -> Model.Identifier.to_string (Model.Content.id (Model.Reference.get ref)) -(** Build a [SELECT json_object(...)] query returning one json object per row - of [table]. [columns] are (json key, SQL expression) pairs. *) +(** Build a [SELECT json_object(...)] query returning one json object per row of + [table]. [columns] are (json key, SQL expression) pairs. *) let json_select_sql : ?where:string -> Model.Identifier.t -> (string * string) list -> string = fun ?where table columns -> @@ -49,47 +51,81 @@ let json_select_sql : in match where with None -> q | Some w -> q ^ " where " ^ w -(** Load the given content and return the values as a json *) +(** Build the [(json key, SQL expression)] columns selecting the single-column + fields of a table: one quoted column per field, skipping multivalued + references (they live in dedicated tables). With [~annotate_references], a + monovalued reference column is followed by a [[<field>_table]] literal + naming the targeted table, so the output is self-describing. *) +let field_columns : + ?annotate_references:bool -> Model.field list -> (string * string) list = + fun ?(annotate_references = false) fields -> + List.concat_map + (fun 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 _ -> [] + | Model.Typ.MonoReference reference when annotate_references -> + [ + (key, Model.Identifier.quote name); + ( key ^ "_table", + Croquette_common.Sql.quote_literal + (target_table reference.Model.Typ.t) ); + ] + | _ -> [ (key, Model.Identifier.quote name) ]) + fields + +(** Run a [json_select_sql] query over [table] and collect one json string per + row. With [~where] and [~id], the query is restricted to the rows where the + [where] column equals [id]. *) +let collect_json : + Caqti_lwt.connection -> + ?where:Model.Identifier.t -> + ?id:Int64.t -> + Model.Identifier.t -> + (string * string) list -> + string list Lwt.t = + fun db ?where ?id table columns -> + let module Db = (val db) in + let open Caqti.Templater in + let ( let* ) = Lwt.bind in + match (where, id) with + | Some col, Some id -> + let request = + direct + T.(int64 -->* string) + (json_select_sql + ~where:(Model.Identifier.quote col ^ "=?") + table columns) + in + let* res = Db.collect_list request id in + Caqti_lwt.or_fail res + | None, None -> + let request = + direct T.(unit -->* string) (json_select_sql table columns) + in + let* res = Db.collect_list request () in + Caqti_lwt.or_fail res + | _ -> invalid_arg "collect_json: [where] and [id] must be given together" + +(** Request returning one row of a content table as a json object *) let content_as_json : Model.content -> (int64, string, [ `One ]) Caqti.Template.Request.t = fun content -> - let fields = - Model.Content.fields content - (* Remove the multivaluated fields from the query *) - |> Seq.filter (fun t -> - let (Model.Field.T ty) = Model.Field.type_ t in - match ty with Model.Typ.MultiReference _ -> false | _ -> true) - |> List.of_seq - in - let columns = - ListLabels.map fields ~f:(fun field -> - let name = Model.Field.name field in - (Model.Identifier.to_string name, Model.Identifier.quote name)) - in let columns = + field_columns (List.of_seq (Model.Content.fields content)) + @ match Model.Content.parent content with - | None -> columns - | Some _ -> - columns - @ [ ("parent", Model.Identifier.quote Model.Identifier.parent) ] + | None -> [] + | Some _ -> [ ("parent", Model.Identifier.quote Model.Identifier.parent) ] in - let q = - json_select_sql - ~where:(Model.Identifier.quote Model.Identifier.id ^ "=?") - (Model.Content.id content) columns - in - let open Caqti.Templater in - direct T.(int64 -->! string) q - -let query_values (table : Model.content) - (feature : Model.Identifier.t) = let open Caqti.Templater in - direct T.(int64 -->* string) - ("SELECT json_object(" - ^ Croquette_common.Sql.quote_literal (Model.Identifier.to_string feature) - ^ ", " ^ Model.Identifier.quote (Model.Identifier.target) ^ ") from " - ^ Model.Identifier.quote (Model.Identifier.dot (Model.Content.id table) feature) - ^ " where " ^ Model.Identifier.quote (Model.Identifier.parent) ^ "=?") + direct + T.(int64 -->! string) + (json_select_sql + ~where:(Model.Identifier.quote Model.Identifier.id ^ "=?") + (Model.Content.id content) columns) (** 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 @@ -100,7 +136,6 @@ let collect_references : int64 -> (string * int64 * string) list Lwt.t = fun db content id -> - let module Db = (val db) in let ( let* ) = Lwt.bind in let content_id = Model.Identifier.to_string (Model.Content.id content) in @@ -117,10 +152,14 @@ let collect_references : 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 + let* json = + collect_json db ~where:Model.Identifier.parent ~id + (Model.Identifier.dot (Model.Content.id content) field_name) + [ + ( Model.Identifier.to_string field_name, + Model.Identifier.quote Model.Identifier.target ); + ] in - let* json = Caqti_lwt.or_fail json_result in let line = ListLabels.fold_left ~init:acc json ~f:(fun acc json -> (content_id, id, json) :: acc) @@ -130,3 +169,80 @@ let collect_references : in results + +(** A provider giving a deterministic value for each field of a row, depending + on the field type and the row number. References point to the row of id 1 of + their target. *) +let provider_for_fields : Model.field list -> Int64.t -> string -> Value.value = + fun fields row field_name -> + match + List.find_opt + (fun field -> + String.equal field_name + (Model.Identifier.to_string (Model.Field.name field))) + fields + with + | None -> Value.none + | Some field -> ( + let (Model.Field.T ty) = Model.Field.type_ field in + match ty with + | Model.Typ.Text -> + Value.single + (Model.Typ.Text, Some (field_name ^ "-" ^ Int64.to_string row)) + | Model.Typ.Integer -> Value.single (Model.Typ.Integer, Some row) + | Model.Typ.MonoReference reference -> + Value.single (Model.Typ.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 (Model.Typ.MultiReference reference, List.to_seq values)) + +(** Same, for the fields of a content *) +let provider_for : Model.content -> Int64.t -> string -> Value.value = + fun content row field_name -> + provider_for_fields + (List.of_seq (Model.Content.fields content)) + row field_name + +(** Render a [(named, data)] pair as a single line, whatever the field type *) +let show_field : + 'db 'ocaml. + ('db, 'ocaml, Model.target) Model.Typ.t Model.Typ.named -> 'ocaml -> string + = + fun named data -> + let name = Model.Identifier.to_string (Model.Typ.name named) in + let show : type db ocaml. + (db, ocaml, Model.target) Model.Typ.t -> ocaml -> string = + fun typ data -> + match typ with + | Model.Typ.Text -> ( + match data with + | Some s -> name ^ " = \"" ^ s ^ "\"" + | None -> name ^ " = NULL") + | Model.Typ.Integer -> ( + match data with + | Some i -> name ^ " = " ^ Int64.to_string i + | None -> name ^ " = NULL") + | Model.Typ.MonoReference _ -> ( + match data with + | Some i -> name ^ " -> " ^ Int64.to_string i + | None -> name ^ " = NULL") + | Model.Typ.MultiReference _ -> + let targets = List.of_seq data |> List.sort compare in + name ^ " -> [" + ^ String.concat "," (List.map Int64.to_string targets) + ^ "]" + in + show (Model.Typ.typ named) data + +(** Read a configuration from the standard input and run [f] with a fresh + in-memory database created from it *) +let exec_with_conf : + (Configuration.Ast.setting -> Caqti_lwt.connection -> 'a Lwt.t) -> + ('a, Configuration.Read_conf.error) result = + fun f -> + let ( let+ ) opt cont = Result.map cont opt in + let+ configuration = + Configuration.Read_conf.read_string_conf (read_stdin ()) + in + with_db configuration (fun db -> f configuration db) diff --git a/test/insert.ml b/test/insert.ml index 1b12596..9133832 100644 --- a/test/insert.ml +++ b/test/insert.ml @@ -1,7 +1,9 @@ -open StdLabels - let name = __FILE__ -let description = "Insert data and check the result" + +let description = + "Insert two rows of the given content with a generated provider, and display \ + the inserted rows. The configuration is read on the standard input" + let speclist () = [] type t = @@ -14,267 +16,33 @@ let pp : Format.formatter -> t -> unit = (fun formatter (content_id, id, content) -> Format.fprintf formatter "@[<v 2>%s:%Ld@;%s@]" content_id id content)) -type content = Model.content -type value = Value.value -type properties = (string * value) list - -(* A subcontent to insert, with one entry (properties) per row *) -type content_to_insert = content * properties list - -(* One entry of the "values" list: the properties of a row, and the entries - of each subcontent (identified by a key equal to the subcontent name) *) -type entry = properties * content_to_insert 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:Model.content Value.t -> +(** Insert one row of [content], and return its json representation together + with one line per value of each multivalued reference field *) +let insert_one : Caqti_lwt.connection -> - content -> - properties -> - (string * int64 * string) Lwt.t = - fun ?parent db content entry -> + Model.content -> + Int64.t -> + (string * Int64.t * string) list Lwt.t = + fun db content row -> let ( let* ) = Lwt.bind in - let* inserted = - Query.insert ?parent db content (fun v -> - match List.assoc_opt v entry with Some v -> v | None -> Value.none) - in - let id = - match Value.get inserted Value.id_ref with - | Some id -> id - | None -> assert false - in + let* id = Query.insert db content (Common.provider_for content row) in let content_id = Model.Identifier.to_string (Model.Content.id content) in let module Db = (val db) in let* json_result = Db.find (Common.content_as_json content) id in let* json = Caqti_lwt.or_fail json_result in - Lwt.return (content_id, id, json) - -(** Insert one row and collect the result line, together with the lines of its - multivalued references. Return the accumulator and the inserted value. *) -let insert_one : - Caqti_lwt.connection -> - ?parent:Model.content Value.t -> - content -> - properties -> - (string * Int64.t * string) list -> - ((string * Int64.t * string) list * Model.content Value.t) Lwt.t = - fun db ?parent content entry acc -> - let ( let* ) = Lwt.bind in - let* content_id, id, json = insert_and_get_result ?parent db content entry in let* ext = Common.collect_references db content id in - Lwt.return (List.append ext ((content_id, id, json) :: acc), Value.v content id) - -module S = Configuration.Json5.Decode - -let ( let* ) = Lwt.bind - -(* bind in the decoder monad *) -let ( let** ) = S.( let* ) - -(* Force a list of Lwt.t sequentially: the sqlite connection does not support - concurrent usage, and some of these Lwt.t perform inserts *) -let seq_lwt : 'a Lwt.t list -> 'a list Lwt.t = - fun l -> Lwt_list.map_s (fun x -> x) l - -let is_field : content -> string -> bool = - fun content key -> - Seq.exists - (fun field -> - String.equal key Model.(Identifier.to_string (Field.name field))) - (Model.Content.fields content) - -(** Decode the value of a field: the json nature of the value tells the type - (numeric, text), or it is a reference (an integer for a list target, an - object holding the properties of a new row of the target content) *) -let rec value : - Caqti_lwt.connection -> - content -> - string -> - (string * value) Lwt.t S.decoder = - fun db content key -> - S.one_of - [ - ( "numeric", - S.map - (fun v -> - Lwt.return (key, Value.single (Model.Typ.Integer, Some (Int64.of_int v)))) - S.int ); - ( "text", - S.map (fun v -> Lwt.return (key, Value.single (Model.Typ.Text, Some v))) S.string - ); - ("reference", reference_value db content key false); - ("references", reference_value db content key true); - ("other", S.succeed (Lwt.return (key, Value.none))); - ] - -(** Decode a reference field: single value or list of values, depending on - [multivaluated]. - - A list target is referenced by the id of one of its rows (an integer). A - content target is referenced by an object holding the properties of a new - row, inserted in its own table — the inserts are chained in a single Lwt - promise, because the sqlite connection does not support concurrent usage. *) -and reference_value : - Caqti_lwt.connection -> - content -> - string -> - bool -> - (string * value) Lwt.t S.decoder = - fun db content key multivaluated -> - match Model.Content.find_target content key with - | None -> S.fail "Not a feature" - | Some t -> ( - let reference = Model.Typ.{ t; constraints = None } in - match t with - | Model.Reference.List _ -> - if multivaluated then - S.map - (fun ids -> - Lwt.return - ( key, - Value.single - ( Model.Typ.MultiReference reference, - List.to_seq (List.map ~f:Int64.of_int ids) ) )) - (S.list S.int) - else - S.map - (fun v -> - Lwt.return - ( key, - Value.single - (Model.Typ.MonoReference reference, Some (Int64.of_int v)) - )) - S.int - | Model.Reference.Content target_ref -> - let target = Model.Reference.get target_ref in - if multivaluated then - let** entries = S.list (properties db target) in - S.succeed - (let* entries = seq_lwt entries in - let* ids = - Lwt_list.map_s - (fun props -> - let* _, id, _ = insert_and_get_result db target props in - Lwt.return id) - entries - in - Lwt.return - ( key, - Value.single - (Model.Typ.MultiReference reference, List.to_seq ids) )) - else - let** props = properties db target in - S.succeed - (let* props = props in - let* _, id, _ = insert_and_get_result db target props in - Lwt.return - (key, Value.single (Model.Typ.MonoReference reference, Some id))) - ) - -and properties : Caqti_lwt.connection -> content -> properties Lwt.t S.decoder = - fun db content -> S.map seq_lwt (S.key_value_pairs_seq (value db content)) - -(** One entry of the "values" list of a content: each key is decoded once, as a - field value or as the entries of a subcontent *) -and entry : - Caqti_lwt.connection -> - Configuration.Ast.setting -> - content -> - entry Lwt.t S.decoder = - fun db conf content -> - let key_value key = - if is_field content key then S.map (fun v -> `Prop v) (value db content key) - else - match - Configuration.Ast.get_content conf (Model.Identifier.of_string key) - with - | Some subcontent -> S.map (fun v -> `Sub v) (sub_entries db subcontent) - | None -> S.map (fun v -> `Prop v) (value db content key) - in - S.map - (fun pairs -> - let props = - List.filter_map pairs ~f:(function `Prop v -> Some v | `Sub _ -> None) - in - let subs = - List.filter_map pairs ~f:(function `Sub v -> Some v | `Prop _ -> None) - in - let* properties = seq_lwt props in - let* subs = seq_lwt subs in - Lwt.return (properties, subs)) - (S.key_value_pairs_seq key_value) - -(** The entries of a subcontent: a list of plain properties, one per row *) -and sub_entries : - Caqti_lwt.connection -> content -> content_to_insert Lwt.t S.decoder = - fun db content -> - S.map - (fun entries -> - let* entries = seq_lwt entries in - Lwt.return (content, entries)) - (S.list (properties db content)) - -(** Decode one content of the data document: its id and its "values" entries *) -and content_entry : - Caqti_lwt.connection -> - Configuration.Ast.setting -> - (content * entry list) Lwt.t S.decoder = - fun db conf -> - let** name = S.field "id" S.string in - let content = - Configuration.Ast.get_content conf (Model.Identifier.of_string name) - |> Option.get - in - S.map - (fun entries -> - let* entries = seq_lwt entries in - Lwt.return (content, entries)) - (S.field_opt_or ~default:[] "values" (S.list (entry db conf content))) - -(** Decode the whole data document *) -and data_document : - Caqti_lwt.connection -> - Configuration.Ast.setting -> - (content * entry list) list Lwt.t S.decoder = - fun db conf -> - S.map seq_lwt (S.field "content" (S.list (content_entry db conf))) - -let exec : string list -> t = - fun _ -> - (* The same document holds both the configuration and the data to insert: - the configuration reader ignores the "values" keys, the data reader - ignores the "section" keys *) - let stdin = Common.read_stdin () in - let ( let** ) = Result.bind in - let** configuration = Configuration.Read_conf.read_string_conf stdin in - Common.with_db configuration (fun db -> - match S.decode_string (data_document db configuration) stdin with - | Error err -> Lwt.return_error err - | Ok inserts -> - let* inserts = inserts in - let* insertions = - Lwt_list.fold_left_s - (fun acc (content, entries) -> - Lwt_list.fold_left_s - (fun acc (entry, subcontents) -> - (* Insert the main content *) - let* acc, parent = insert_one db content entry acc in - (* Insert the subcontents associated *) - Lwt_list.fold_left_s - (fun acc (subcontent, subentries) -> - Lwt_list.fold_left_s - (fun acc entry -> - let* acc, _ = - insert_one db ~parent subcontent entry acc - in - Lwt.return acc) - acc subentries) - acc subcontents) - acc entries) - [] inserts + Lwt.return ((content_id, id, json) :: List.rev ext) + +let exec : string list -> t = function + | [ content_id ] -> + Common.exec_with_conf (fun configuration db -> + let content = + Option.get + (Configuration.Ast.get_content configuration + (Model.Identifier.of_string content_id)) in - Lwt.return_ok (List.rev insertions)) + let ( let* ) = Lwt.bind in + let* lines1 = insert_one db content 1L in + let* lines2 = insert_one db content 2L in + Lwt.return (lines1 @ lines2)) + | _ -> failwith "Invalid arguments" 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 diff --git a/test/load.ml b/test/load.ml new file mode 100644 index 0000000..8f4d2fe --- /dev/null +++ b/test/load.ml @@ -0,0 +1,37 @@ +let name = __FILE__ + +let description = + "Insert two rows of the given content, load them as values with [Value.load] \ + and print them with [Value.pp]. The configuration is read on the standard \ + input" + +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_cut Format.pp_print_string) + +let exec : string list -> t = function + | [ content_id ] -> + Common.exec_with_conf (fun configuration db -> + let content = + Option.get + (Configuration.Ast.get_content configuration + (Model.Identifier.of_string content_id)) + in + let ( let* ) = Lwt.bind in + let* id1 = Query.insert db content (Common.provider_for content 1L) in + let* id2 = Query.insert db content (Common.provider_for content 2L) in + let load_one id = + let v = Value.v content id in + let* () = Value.load db v in + Lwt.return + [ Format.sprintf "row %Ld:" id; Format.asprintf "%a" Value.pp v ] + in + let* lines1 = load_one id1 in + let* lines2 = load_one id2 in + Lwt.return (lines1 @ lines2)) + | _ -> failwith "Invalid arguments" diff --git a/test/select.ml b/test/select.ml new file mode 100644 index 0000000..d185772 --- /dev/null +++ b/test/select.ml @@ -0,0 +1,46 @@ +open StdLabels + +let name = __FILE__ + +let description = + "Insert two rows of the given content, then read them back with \ + [Query.select]. The configuration is read on the standard input" + +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_cut Format.pp_print_string) + +let exec : string list -> t = function + | [ content_id ] -> + Common.exec_with_conf (fun configuration db -> + let content = + Option.get + (Configuration.Ast.get_content configuration + (Model.Identifier.of_string content_id)) + in + let ( let* ) = Lwt.bind in + let* id1 = Query.insert db content (Common.provider_for content 1L) in + let* id2 = Query.insert db content (Common.provider_for content 2L) in + let select_one id = + let acc = ref [] in + let* () = + Query.select db id content + { + f = + (fun (named, data) -> + acc := Common.show_field named data :: !acc); + } + in + Lwt.return (List.rev !acc) + in + let* lines1 = select_one id1 in + let* lines2 = select_one id2 in + Lwt.return + (Format.sprintf "row %Ld:" id1 + :: (lines1 @ (Format.sprintf "row %Ld:" id2 :: lines2)))) + | _ -> failwith "Invalid arguments" diff --git a/test/tests.ml b/test/tests.ml index c6ab006..06f4451 100644 --- a/test/tests.ml +++ b/test/tests.ml @@ -38,6 +38,8 @@ let () = register (module Path); register (module Update); register (module Identifier); + register (module Select); + register (module Load); ] let args = ref [] diff --git a/test/update.ml b/test/update.ml index 18c3821..502ad05 100644 --- a/test/update.ml +++ b/test/update.ml @@ -67,7 +67,10 @@ let configuration = let speclist () = let (lazy configuration) = configuration in - let content = Configuration.Ast.get_content configuration (Model.Identifier.of_string "entity") in + let content = + Configuration.Ast.get_content configuration + (Model.Identifier.of_string "entity") + in let fields = Model.Content.fields (Option.get content) in Seq.fold_left (fun acc feature -> @@ -81,7 +84,10 @@ let speclist () = Arg.String (fun value -> actions := - Text (Model.Identifier.to_string (Model.Field.name feature), value) :: !actions), + Text + ( Model.Identifier.to_string (Model.Field.name feature), + value ) + :: !actions), "<v> Set field with the value <v>" ) | Model.Typ.Integer -> Some @@ -89,7 +95,10 @@ let speclist () = Arg.Int (fun value -> actions := - Integer (Model.Identifier.to_string (Model.Field.name feature), value) :: !actions), + Integer + ( Model.Identifier.to_string (Model.Field.name feature), + value ) + :: !actions), "<n> Set field with the value <n>" ) | Model.Typ.MonoReference target -> Some @@ -97,7 +106,10 @@ let speclist () = Arg.Int (fun value -> actions := - Reference (target, Model.Identifier.to_string (Model.Field.name feature), value) + Reference + ( target, + Model.Identifier.to_string (Model.Field.name feature), + value ) :: !actions), "<id> Set field with the id" ) | Model.Typ.MultiReference target -> @@ -112,7 +124,10 @@ let speclist () = in actions := - References (target, Model.Identifier.to_string (Model.Field.name feature), ids) + References + ( target, + Model.Identifier.to_string (Model.Field.name feature), + ids ) :: !actions), "<id,..> Set this list of values" ) in @@ -133,7 +148,9 @@ let exec : string list -> t = fun _ -> let (lazy configuration) = configuration in let content = - Configuration.Ast.get_content configuration (Model.Identifier.of_string "entity") |> Option.get + Configuration.Ast.get_content configuration + (Model.Identifier.of_string "entity") + |> Option.get in Lwt_main.run @@ -141,12 +158,7 @@ let exec : string list -> t = let* db = Common.connect configuration in let module Db = (val db) in (* Insert a content without any value *) - let* inserted = Query.insert db content (fun _ -> Value.none) in - let id = - match Value.get inserted Value.id_ref with - | Some id -> id - | None -> assert false - in + let* id = Query.insert db content (fun _ -> Value.none) in let group_commit : actions Seq.t Seq.t = List.rev !actions |> List.to_seq @@ -167,7 +179,9 @@ let exec : string list -> t = let field = Model.Content.fields content |> Seq.find (fun f -> - String.equal key (Model.Identifier.to_string (Model.Field.name f))) + String.equal key + (Model.Identifier.to_string + (Model.Field.name f))) |> Option.get in let (Model.Field.T t) = Model.Field.type_ field in @@ -182,13 +196,17 @@ let exec : string list -> t = | Text (k, v) when String.equal k key -> Some (Value.single (Model.Typ.Text, Some v)) | Integer (k, v) when String.equal k key -> - Some (Value.single (Model.Typ.Integer, Some (Int64.of_int v))) + Some + (Value.single + (Model.Typ.Integer, Some (Int64.of_int v))) | Reference (target, k, v) when String.equal k key -> Some (Value.single - (Model.Typ.MonoReference target, Some (Int64.of_int v))) + ( Model.Typ.MonoReference target, + Some (Int64.of_int v) )) | References (target, k, v) when String.equal k key -> - Some (Value.single (Model.Typ.MultiReference target, v)) + Some + (Value.single (Model.Typ.MultiReference target, v)) | _ -> None) actions in |
