summaryrefslogtreecommitdiff
path: root/lib/query
diff options
context:
space:
mode:
Diffstat (limited to 'lib/query')
-rw-r--r--lib/query/encoder.ml25
-rw-r--r--lib/query/insert.ml125
-rw-r--r--lib/query/intf.ml5
-rw-r--r--lib/query/query.mli7
-rw-r--r--lib/query/tests/insert.t203
5 files changed, 314 insertions, 51 deletions
diff --git a/lib/query/encoder.ml b/lib/query/encoder.ml
index 624931a..2f86eab 100644
--- a/lib/query/encoder.ml
+++ b/lib/query/encoder.ml
@@ -203,10 +203,11 @@ module CaqtiRequest = Map (struct
let t_n = Caqti_type.t11
let f : type a. a data -> a Caqti_type.t = function
- | { content = Text; _ } -> Caqti_type.option @@ Caqti_type.string
- | { content = Integer; _ } -> Caqti_type.option @@ Caqti_type.int64
- | { content = MultiReference _; _ } -> Caqti_type.option @@ Caqti_type.int64
- | { content = MonoReference _; _ } -> Caqti_type.option @@ Caqti_type.int64
+ | { content_type = Text; _ } -> Caqti_type.option @@ Caqti_type.string
+ | { content_type = Integer; _ } -> Caqti_type.option @@ Caqti_type.int64
+ | { content_type = MultiReference _; _ } -> Caqti_type.int64
+ | { content_type = MonoReference _; _ } ->
+ Caqti_type.option @@ Caqti_type.int64
end)
module DefaultEncoder = struct
@@ -246,10 +247,10 @@ module EncodeInsert = Map (struct
type 'a t = string
let f : type a. a data -> a t = function
- | { content = Text; _ } -> "?"
- | { content = Integer; _ } -> "?"
- | { content = MonoReference _; _ } -> "?"
- | { content = MultiReference _; _ } ->
+ | { content_type = Text; _ } -> "?"
+ | { content_type = Integer; _ } -> "?"
+ | { content_type = MonoReference _; _ } -> "?"
+ | { content_type = MultiReference _; _ } ->
failwith "Cannot encode multireference field"
include ConcatParam
@@ -259,10 +260,10 @@ module InsertTableName = Map (struct
type 'a t = string
let f : type a. a data -> a t = function
- | { name = column; content = Text; _ } -> column
- | { name = column; content = Integer; _ } -> column
- | { name = column; content = MultiReference _; _ } -> column
- | { name = column; content = MonoReference _; _ } -> column
+ | { name = column; content_type = Text; _ } -> column
+ | { name = column; content_type = Integer; _ } -> column
+ | { name = column; content_type = MultiReference _; _ } -> column
+ | { name = column; content_type = MonoReference _; _ } -> column
include ConcatParam
end)
diff --git a/lib/query/insert.ml b/lib/query/insert.ml
index 39c32d0..b6aee0f 100644
--- a/lib/query/insert.ml
+++ b/lib/query/insert.ml
@@ -21,8 +21,86 @@ let collect_fields :
Encoder.list_to_tup fields ~f:(fun field ->
let name = Model.Field.name field in
- let (Model.Field.T content) = Model.Field.type_ field in
- TT { name; content })
+ let (Model.Field.T content_type) = Model.Field.type_ field in
+ TT { name; content_type })
+
+module MultiReferencesInsert = struct
+ type 'a t = {
+ db : Caqti_lwt.connection;
+ parent_id : int64;
+ content : Model.Reference.t Model.Content.t;
+ provider : string -> Model.Reference.t Model.Typ.value;
+ k : (unit, Caqti_error.t) result Lwt.t;
+ }
+
+ type request = (int64 * int64, unit, [ `Zero ]) Caqti_request.t
+ (** Helper for the request insert signature *)
+
+ let ( let* ) = Lwt.bind
+
+ let accumulate :
+ Caqti_lwt.connection ->
+ request ->
+ int64 ->
+ (unit, [> Caqti_error.t ]) result Lwt.t ->
+ int64 ->
+ (unit, [> Caqti_error.t ]) result Lwt.t =
+ fun db request parent k value ->
+ let module Db = (val db) in
+ let acc =
+ let* res = k in
+ match res with
+ | Error _ as err -> Lwt.return err
+ | Ok () ->
+ (* We have the type for each of the value, so we can create
+ a request directly. *)
+ Db.exec request (parent, value)
+ in
+ acc
+
+ (** The function is called for each feature declared as multi reference. *)
+ let f : type any. 'a t -> any Intf.data -> 'a t =
+ fun ({ db; parent_id; provider; k; content } as acc)
+ Intf.{ name; content_type } ->
+ let module Db = (val db) in
+ match content_type with
+ | Model.Typ.MultiReference _target -> (
+ let result = provider name in
+ match result with
+ | Model.Typ.None -> acc
+ | Model.Typ.V _ -> raise TypeMismatch
+ | Model.Typ.Seq (field_type', values) -> (
+ match Model.Typ.eq content_type field_type' with
+ | None -> raise TypeMismatch
+ | Some Equal ->
+ let buff = Buffer.create 16 in
+ let format = Format.formatter_of_buffer buff in
+ let () =
+ Format.fprintf format
+ "INSERT INTO \"%s.%s\" (\"parent\", \"target\") VALUES (?, \
+ ?)"
+ (Model.Content.id content) name
+ in
+ Format.pp_print_flush format ();
+ let request : request =
+ Caqti_request.Infix.(
+ Caqti_type.(t2 int64 int64 ->. Caqti_type.unit))
+ (Buffer.contents buff)
+ in
+ let k =
+ (* Successively insert all the values in the dedicated table *)
+ let* res =
+ Seq.fold_left (accumulate db request parent_id) k values
+ in
+ let* _ = Db.deallocate request in
+ Lwt.return res
+ in
+
+ { acc with k }))
+ | _ -> raise TypeMismatch
+end
+
+module Fold = Encoder.Fold (MultiReferencesInsert)
(** Insert a row. This syntax use RETURNING ID to get the value we just inserted
*)
@@ -39,18 +117,19 @@ let insert_returning :
type 'a t = 'a
let f : type a. a Intf.data -> a t =
- fun Intf.{ name; content } ->
+ fun Intf.{ name; content_type } ->
let result = provider name in
(* Check the value given by the provider and ensure it’s match
the expected type *)
- match (content, result) with
- | _, None ->
+ match result with
+ | Model.Typ.Seq _ -> raise TypeMismatch
+ | Model.Typ.None ->
(* The provider does not have any value for this property, use the
- default value*)
- Model.Typ.default_value content
- | _, V (field_type', value) -> (
- match Model.Typ.eq content field_type' with
- | Some Equal -> Some value
+ default value*)
+ Model.Typ.default_value content_type
+ | Model.Typ.V (field_type', value) -> (
+ match Model.Typ.eq content_type field_type' with
+ | Some Equal -> value
| None -> raise TypeMismatch)
end) in
let init_fields =
@@ -94,7 +173,7 @@ let insert_returning :
(* The insert request is returning the ID. That’s why we call it with the
fuction [find] even if it is not a real find in the database. *)
let* id_result = Db.find request values in
- let* id = Caqti_lwt.or_fail id_result in
+ let* content_id = Caqti_lwt.or_fail id_result in
(* The value is created in the db, reuse the ID in order to insert the
multi-values fields in their own tables *)
@@ -104,20 +183,14 @@ let insert_returning :
match ty with Model.Typ.MultiReference _ -> true | _ -> false)
in
- let module Fold = Encoder.Fold (struct
- type 'a t = 'a Lwt.t
-
- (** The function is called for each feature declared as multi reference. *)
- let f : 'a Lwt.t -> 'b Intf.data -> 'a Lwt.t =
- fun acc Intf.{ name; content } ->
- ignore content;
- let values = provider name in
- ignore values;
- let* acc = acc in
- Lwt.return acc
- end) in
- let* () = Fold.f Lwt.return_unit multi_valuated_fields in
- Lwt.return id
+ let acc =
+ MultiReferencesInsert.
+ { db; provider; k = Lwt.return_ok (); parent_id = content_id; content }
+ in
+ let ret = Fold.f acc multi_valuated_fields in
+ let* k = ret.k in
+ let* () = Caqti_lwt.or_fail k in
+ Lwt.return content_id
(** Insert an entry for a specific content *)
let insert :
@@ -137,7 +210,7 @@ let insert :
| Some v, Some _ -> (
(* Otherwise update the provider to give a value for the parent *)
function
- | "parent" -> Model.Typ.(V (Integer, v))
+ | "parent" -> Model.Typ.(V (Integer, Some v))
| other -> provider other)
in
let module Db = (val db) in
diff --git a/lib/query/intf.ml b/lib/query/intf.ml
index 49c51db..10e78b0 100644
--- a/lib/query/intf.ml
+++ b/lib/query/intf.ml
@@ -1,4 +1,7 @@
-type 'a data = { name : string; content : ('a, Model.Reference.t) Model.Typ.t }
+type 'a data = {
+ name : string;
+ content_type : ('a, Model.Reference.t) Model.Typ.t;
+}
(** The data is the name of the field and the type associated with *)
type t_type = TT : 'a data -> t_type [@@unboxed]
diff --git a/lib/query/query.mli b/lib/query/query.mli
index 1a26383..9dcef17 100644
--- a/lib/query/query.mli
+++ b/lib/query/query.mli
@@ -6,14 +6,15 @@ val insert :
int64 Lwt.t
(** Insert a new element and return the ID.
- [insert content ~parent provider] will create the content in the database.
+ [insert db content ~parent provider] will create the content in the
+ database.
The parent is required if the content is a subcontent. Not providing the
- parameter is a structural error in the database.
+ 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
- for this property, the execption [TypeMismatch] is raised otherwise.
+ for this property, the exception [TypeMismatch] is raised otherwise.
This is a low-level function: there is no representation for the content
itself, and no functionnals controls are realized. *)
diff --git a/lib/query/tests/insert.t b/lib/query/tests/insert.t
index 236bb97..af74436 100644
--- a/lib/query/tests/insert.t
+++ b/lib/query/tests/insert.t
@@ -110,7 +110,8 @@ Any value can be omitted and will be replaced by the default value:
{"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.
+is only functionnal; this shall not block the creation of the content in the
+database.
$ tests -insert <<EOF
> {
@@ -155,14 +156,6 @@ A subcontent is linked to the parent:
> "label": {},
> "type": "integer"
> },
- > {
- > "name": "ref",
- > "label": {},
- > "type": {
- > "content": "referential",
- > "multivaluated": true
- > }
- > }
> ]
> }
> ],
@@ -211,3 +204,195 @@ A subcontent is linked to the parent:
{"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",
+ > "section": [
+ > {
+ > "field": [
+ > {
+ > "name": "description",
+ > "label": {},
+ > "type": "text"
+ > },
+ > ]
+ > }
+ > ],
+ > "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",
+ > "label": {},
+ > "type": "text"
+ > },
+ > ]
+ > }
+ > ],
+ > "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}
+ 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
+ entity:1
+ {}
+ details:1
+ {"parent":1}
+ details:1
+ {"reference":1}
+ details:1
+ {"reference":2}
+ referential:3
+ {}