summaryrefslogtreecommitdiff
path: root/lib/query
diff options
context:
space:
mode:
Diffstat (limited to 'lib/query')
-rw-r--r--lib/query/dune1
-rw-r--r--lib/query/encoder.ml8
-rw-r--r--lib/query/insert.ml48
-rw-r--r--lib/query/insert.mli15
-rw-r--r--lib/query/query.ml1
-rw-r--r--lib/query/query.mli35
-rw-r--r--lib/query/select.ml206
-rw-r--r--lib/query/select.mli30
-rw-r--r--lib/query/tests/insert.t387
-rw-r--r--lib/query/tests/select.t48
-rw-r--r--lib/query/update.ml28
11 files changed, 398 insertions, 409 deletions
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 )))
([], [])