summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2026-11-20 18:47:26 +0100
committerSébastien Dailly <sebastien@dailly.me>2026-11-20 18:47:26 +0100
commitfa4970bf1b74f3fa0b2f63cbd461d98c251dc0f0 (patch)
tree577a41415e67510f08f43fc48c794c36a10f6e80 /lib
parentb05e2d2be7be28ee6c577671002797cf9abaf177 (diff)
Updated the model for representing ocaml types in the application type
Diffstat (limited to 'lib')
-rw-r--r--lib/configuration/ast.ml42
-rw-r--r--lib/configuration/ast.mli4
-rw-r--r--lib/configuration/read_conf.ml24
-rw-r--r--lib/configuration/tests/errors.t44
-rw-r--r--lib/model/model.ml2
-rw-r--r--lib/model/model.mli6
-rw-r--r--lib/model/typ.ml76
-rw-r--r--lib/model/typ.mli92
-rw-r--r--lib/query/encoder.ml26
-rw-r--r--lib/query/insert.ml65
-rw-r--r--lib/query/insert.mli2
-rw-r--r--lib/query/intf.ml100
-rw-r--r--lib/query/update.ml54
-rw-r--r--lib/value/data.ml22
-rw-r--r--lib/value/data.mli42
-rw-r--r--lib/value/value.ml38
-rw-r--r--lib/value/value.mli37
17 files changed, 475 insertions, 201 deletions
diff --git a/lib/configuration/ast.ml b/lib/configuration/ast.ml
index edf129f..8ea92b2 100644
--- a/lib/configuration/ast.ml
+++ b/lib/configuration/ast.ml
@@ -19,16 +19,20 @@ let get_content : setting -> Model.Identifier.t -> Model.content option =
let get_list : setting -> Model.Identifier.t -> Model.list option =
fun setting name -> Splay.find name setting.lists
-(** All the lists declared in the setting, in a deterministic (identifier)
- order *)
+(** All the lists declared in the setting, in a deterministic (identifier) order
+*)
let lists : setting -> Model.list list =
fun setting ->
Splay.fold (fun acc (_, list) -> list :: acc) [] setting.lists
|> List.sort ~cmp:(fun a b ->
- Model.Identifier.compare a.Model.DataList.id b.Model.DataList.id)
+ Model.Identifier.compare a.Model.DataList.id b.Model.DataList.id)
exception BrokenLink : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn
+exception ConflictingFields of Model.Identifier.t * Model.Identifier.t
+(** [ConflictingFields (owner, field)]: the content or list [owner] declares two
+ fields named [field] with different types *)
+
(** Register a content and all the associated subcontent in the top level,
giving the possibility to fetch any content or subcontent by its name
without traversing the whole tree each time. *)
@@ -110,6 +114,23 @@ let tree : ?required:bool -> setting -> Graph_tree.t =
let root_contents : setting -> Model.content list =
fun setting -> Graph_tree.fold (fun a acc -> a :: acc) (tree setting) []
+(** Ensure that two fields with the same name in a content or a list always have
+ the same type — otherwise reading the field by name would be ambiguous. *)
+let check_fields : Model.Identifier.t -> Model.field Seq.t -> unit =
+ fun owner fields ->
+ let seen : (Model.Identifier.t, Model.field) Hashtbl.t = Hashtbl.create 16 in
+ Seq.iter
+ (fun field ->
+ let name = Model.Field.name field in
+ match Hashtbl.find_opt seen name with
+ | None -> Hashtbl.replace seen name field
+ | Some first ->
+ let (T t1) = Model.Field.type_ first in
+ let (T t2) = Model.Field.type_ field in
+ if Option.is_none (Model.Typ.eq t1 t2) then
+ raise (ConflictingFields (owner, name)))
+ fields
+
let check : setting -> (unit, exn) result =
fun setting ->
(* Ensure each content referenced exists.
@@ -122,5 +143,20 @@ let check : setting -> (unit, exn) result =
(* Check cycle dependencies *)
let required_tree = tree ~required:true setting in
Graph_tree.check_cycle required_tree;
+
+ (* Check that fields sharing a name have the same type *)
+ let () =
+ Splay.fold
+ (fun () (_, content) ->
+ check_fields (Model.Content.id content) (Model.Content.fields content))
+ () setting.contents
+ in
+ let () =
+ Splay.fold
+ (fun () (_, list) ->
+ check_fields list.Model.DataList.id
+ (List.to_seq list.Model.DataList.fields))
+ () setting.lists
+ in
Ok ()
with e -> Error e
diff --git a/lib/configuration/ast.mli b/lib/configuration/ast.mli
index b014c9b..404c26e 100644
--- a/lib/configuration/ast.mli
+++ b/lib/configuration/ast.mli
@@ -25,6 +25,10 @@ exception BrokenLink : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn
(** Exception raised when a reference in the configuration targets neither an
existing content nor a declared list *)
+exception ConflictingFields of Model.Identifier.t * Model.Identifier.t
+(** Exception raised when a content or a list declares two fields with the same
+ name but different types *)
+
val register_list : setting -> Model.list -> setting
(** Register a list in the setting, replacing any previous list with the same
id. Lists are registered while they are decoded, so that a list can
diff --git a/lib/configuration/read_conf.ml b/lib/configuration/read_conf.ml
index 67703da..4c57699 100644
--- a/lib/configuration/read_conf.ml
+++ b/lib/configuration/read_conf.ml
@@ -41,17 +41,16 @@ module Make (S : Decoders.Decode.S) = struct
the error messages). *)
(** Build the reference target for a field. Lists are decoded before the
- contents and registered as soon as they are decoded, so a declared list
- is matched eagerly; a content target is a lazy thunk closing over the
- setting, only forced by [Ast.check] once every content has been
- registered — this is what allows forward and cyclic references between
- contents. The raw value is kept to report broken links.
- *)
+ contents and registered as soon as they are decoded, so a declared list is
+ matched eagerly; a content target is a lazy thunk closing over the
+ setting, only forced by [Ast.check] once every content has been registered
+ — this is what allows forward and cyclic references between contents. The
+ raw value is kept to report broken links. *)
let link (env : env) (value : S.value) (name : Model.Identifier.t) :
Model.target S.decoder =
match Ast.get_list env.settings name with
| Some list -> S.succeed (Model.Reference.List list)
- | None -> (
+ | None ->
if env.allow_content then
S.succeed
(Model.Reference.Content
@@ -63,7 +62,7 @@ module Make (S : Decoders.Decode.S) = struct
else
S.fail_with
(Decoders.Error.make ~context:value
- "References in a list can only target a list declared before it"))
+ "References in a list can only target a list declared before it")
let field_type (env : env) : Model.field_ex S.decoder =
let text_type : Model.field_ex S.decoder =
@@ -187,6 +186,15 @@ module Make (S : Decoders.Decode.S) = struct
("Dependency circle found between:" :: content_name)
in
S.fail_with (Decoders.Error.make message)
+ | Error (Ast.ConflictingFields (owner, name)) ->
+ let message =
+ "Two fields named \""
+ ^ Model.Identifier.to_string name
+ ^ "\" with different types in \""
+ ^ Model.Identifier.to_string owner
+ ^ "\""
+ in
+ S.fail_with (Decoders.Error.make message)
| Error other -> raise other
end
diff --git a/lib/configuration/tests/errors.t b/lib/configuration/tests/errors.t
index c4552d3..b12f29f 100644
--- a/lib/configuration/tests/errors.t
+++ b/lib/configuration/tests/errors.t
@@ -369,3 +369,47 @@ A field cannot be named "id" (reserved: it is the primary key of every row):
in field "name":
the name "id" is reserved: it is the primary key of every row, but got
"id"
+
+Two fields with the same name but different types are rejected (in a content,
+even across sections):
+
+ $ tests --configuration <<EOF
+ > {
+ > "content": [
+ > {
+ > "id": "entity",
+ > "section": [
+ > {
+ > "field": [
+ > { "name": "code", "label": {}, "type": "text" }
+ > ]
+ > },
+ > {
+ > "field": [
+ > { "name": "code", "label": {}, "type": "integer" }
+ > ]
+ > }
+ > ]
+ > }
+ > ]
+ > }
+ > EOF
+ Two fields named "code" with different types in "entity"
+
+Same for a list:
+
+ $ tests --configuration <<EOF
+ > {
+ > "list": [
+ > {
+ > "id": "country_list",
+ > "field": [
+ > { "name": "code", "label": {}, "type": "text" },
+ > { "name": "code", "label": {}, "type": "integer" }
+ > ]
+ > }
+ > ],
+ > "content": []
+ > }
+ > EOF
+ Two fields named "code" with different types in "country_list"
diff --git a/lib/model/model.ml b/lib/model/model.ml
index 2e58d0c..0f016ad 100644
--- a/lib/model/model.ml
+++ b/lib/model/model.ml
@@ -22,4 +22,4 @@ type section = target Section.t
type field = target Field.t
type field_ex = target Field.field_ex
type reference = target Typ.reference
-type 'db typ = ('db, target) Typ.t
+type ('db, 'ocaml) typ = ('db, 'ocaml, target) Typ.t
diff --git a/lib/model/model.mli b/lib/model/model.mli
index 6cf2b11..a8941d9 100644
--- a/lib/model/model.mli
+++ b/lib/model/model.mli
@@ -39,8 +39,8 @@
- {!Identifier} — the names of contents and fields, validated so that they
are always safe to embed as SQL identifiers.
- {!Content}, {!Section}, {!Field} — the structure of the model.
- - {!Typ} — the field types (the nature of the data; the data itself lives
- in the value package as [Value.value]).
+ - {!Typ} — the field types (the nature of the data; the data itself lives in
+ the value package as [Value.value]).
- {!Reference} — a link from a reference field to its target content.
- {!DataList} — a static list declared in the configuration: referenceable,
but not editable by the user.
@@ -103,7 +103,7 @@ module Typ = Typ
the value package): [None] (leave as is), [V] (a single value) or [Seq] (a
sequence of references, for multivalued fields). *)
-type 'db typ = ('db, target) Typ.t
+type ('db, 'ocaml) typ = ('db, 'ocaml, target) Typ.t
(** A field type descriptor ; ['db] is the database representation of the field
(e.g. [string option] for a text field, [int64] for a multivalued reference)
*)
diff --git a/lib/model/typ.ml b/lib/model/typ.ml
index 173f08f..a13653e 100644
--- a/lib/model/typ.ml
+++ b/lib/model/typ.ml
@@ -4,7 +4,7 @@ type 'a reference = {
{!Reference.target} (aliased as {!Model.target}). *)
constraints : string list Dsl.t option;
(** Optionally restrict the values allowed for this field, as a DSL
- expression read from the optional ["constraints"] key of the
+ expression read from the optional [["constraints"]] key of the
configuration. Currently carried through to the consumers (e.g. to
filter the proposed values); [None] means no restriction. *)
}
@@ -17,25 +17,51 @@ type 'a reference = {
(** Each of the field types available in the application.
- - The first type parameter match the DB data for the content.
- - The second is the type of the target in references fields *)
-type ('db, 't) t =
- | Text : (string option, 't) t
- | Integer : (int64 option, 't) t
- | MonoReference : 't reference -> (int64 option, 't) t
- | MultiReference : 't reference -> (int64, 't) t
+ - ['db] is the representation of the data in the database (the column type).
+ - ['ocaml] is the representation of the data in the code — usually the same
+ as ['db], except for multivalued references, stored as one row per value
+ in a dedicated table (['db = int64]) but manipulated as a sequence in code
+ (['ocaml = int64 Seq.t]).
+ - ['t] is the type of the target in references fields *)
+type ('db, 'ocaml, 't) t =
+ | Text : (string option, string option, 't) t
+ | Integer : (int64 option, int64 option, 't) t
+ | MonoReference : 't reference -> (int64 option, int64 option, 't) t
+ | MultiReference : 't reference -> (int64, int64 Seq.t, 't) t
(** Multireferences properties allows only one kind of content.
They are not encoded in the content table but in another table *)
+type 'target monoreference = (int64 option, int64 option, 'target) t
+(** The type of a reference field: the [int64] id of the target row in the
+ database, an [int64 option] in code. In practice ['target] is always
+ {!Model.target}. *)
+
+type 'target multireference = (int64, int64 Seq.t, 'target) t
+(** The type of a multivalued reference field: one [int64] per row in the
+ dedicated table (['db]), an [int64 Seq.t] in code (['ocaml]). In practice
+ ['target] is always {!Model.target}. *)
+
(** Get the value to insert in the database when the field is empty *)
-let default_value : type a. (a, _) t -> a = function
+let default_value : type a b c. (a, b, c) t -> a = function
| Integer -> None
| Text -> None
| MonoReference _ -> None
| MultiReference _ -> raise Not_found
-let eq : type a b c. (a, c) t -> (b, c) t -> (a, b) Type.eq option =
+(** Constructor-level equality on the ['db] representation *)
+let eq : type a b x y c. (a, x, c) t -> (b, y, c) t -> (a, b) Type.eq option =
+ fun t1 t2 ->
+ match (t1, t2) with
+ | MultiReference _, MultiReference _ -> Some Type.Equal
+ | MonoReference _, MonoReference _ -> Some Type.Equal
+ | Integer, Integer -> Some Type.Equal
+ | Text, Text -> Some Type.Equal
+ | _ -> None
+
+(** Constructor-level equality on the ['ocaml] representation *)
+let eq_ocaml : type a b x y c.
+ (a, x, c) t -> (b, y, c) t -> (x, y) Type.eq option =
fun t1 t2 ->
match (t1, t2) with
| MultiReference _, MultiReference _ -> Some Type.Equal
@@ -44,3 +70,33 @@ let eq : type a b c. (a, c) t -> (b, c) t -> (a, b) Type.eq option =
| Text, Text -> Some Type.Equal
| _ -> None
+type 'typ named = {
+ name : Identifier.t; (** The internal name of the field *)
+ typ : 'typ; (** The type of the field, e.g. a ['target multireference] *)
+}
+(** A named field type: a {!t} together with the field's name.
+
+ Named types are built on the fly with {!named} (e.g. from a field, see
+ {!Field.named_of}) and used locally as typed handles — e.g. to read the data
+ of a field from a value. A pre-built instance exists for the reserved field
+ [["id"]] (see [Value.id_ref]). *)
+
+(** Build a named type from a name and a type — no uniqueness involved, so
+ values built on the fly denote the same field *)
+let named : name:Identifier.t -> typ:'typ -> 'typ named =
+ fun ~name ~typ -> { name; typ }
+
+let name : 'typ named -> Identifier.t = fun n -> n.name
+let typ : 'typ named -> 'typ = fun n -> n.typ
+
+(** [provably_equal n1 n2] is [Some Equal] iff [n1] and [n2] denote the same
+ field — same name — and their types match, which also proves that their
+ ['ocaml] representations are equal *)
+let provably_equal (type a b x y c) (n1 : (a, x, c) t named)
+ (n2 : (b, y, c) t named) : (x, y) Type.eq option =
+ if Identifier.equal n1.name n2.name then eq_ocaml n1.typ n2.typ else None
+
+(** [same n1 n2] is [true] iff [n1] and [n2] denote the same field with matching
+ types *)
+let same : ('a, 'x, 'c) t named -> ('b, 'y, 'c) t named -> bool =
+ fun n1 n2 -> provably_equal n1 n2 <> None
diff --git a/lib/model/typ.mli b/lib/model/typ.mli
new file mode 100644
index 0000000..42350a3
--- /dev/null
+++ b/lib/model/typ.mli
@@ -0,0 +1,92 @@
+(** The field types of the application model.
+
+ A field type describes the nature of the data a field can carry, at two
+ levels:
+
+ - ['db] is the representation in the database (the column type);
+ - ['ocaml] is the representation in the code — usually equal to ['db],
+ except for multivalued references, stored as one row per value in a
+ dedicated table (['db = int64]) but manipulated as a sequence in code
+ (['ocaml = int64 Seq.t]).
+
+ The third parameter ['t] is the target of the field when it is a
+ reference (a content or a static list, i.e. {!Model.target}). *)
+
+type 'a reference = {
+ t : 'a;
+ (** The target of the reference: at the resolved level a
+ {!Reference.target} (aliased as {!Model.target}). *)
+ constraints : string list Dsl.t option;
+ (** Optionally restrict the values allowed for this field, as a DSL
+ expression read from the optional ["constraints"] key of the
+ configuration. Currently carried through to the consumers (e.g. to
+ filter the proposed values); [None] means no restriction. *)
+}
+(** A reference from a field to a target (a content or a static list).
+
+ The type stays parameterized over ['a] only to break the module dependency
+ cycle ([Reference] depends on [Content], which depends on [Field]/[Typ]);
+ in practice ['a] is always {!Model.target}. *)
+
+type ('db, 'ocaml, 't) t =
+ | Text : (string option, string option, 't) t
+ | Integer : (int64 option, int64 option, 't) t
+ | MonoReference : 't reference -> (int64 option, int64 option, 't) t
+ | MultiReference : 't reference -> (int64, int64 Seq.t, 't) t
+ (** Multireference fields allow only one kind of target.
+
+ They are not encoded in the content table but in another table *)
+
+type 'target monoreference = (int64 option, int64 option, 'target) t
+(** The type of a reference field: the [int64] id of the target row in the
+ database, an [int64 option] in code. In practice ['target] is always
+ {!Model.target}. *)
+
+type 'target multireference = (int64, int64 Seq.t, 'target) t
+(** The type of a multivalued reference field: one [int64] per row in the
+ dedicated table (['db]), an [int64 Seq.t] in code (['ocaml]). In practice
+ ['target] is always {!Model.target}. *)
+
+val default_value : ('a, 'b, 'c) t -> 'a
+(** The value to insert in the database when the field is empty *)
+
+val eq : ('a, 'x, 'c) t -> ('b, 'y, 'c) t -> ('a, 'b) Type.eq option
+(** Constructor-level equality on the ['db] representation: [eq t1 t2] is
+ [Some Equal] when [t1] and [t2] have the same constructor, which also proves
+ ['a = 'b] *)
+
+val eq_ocaml : ('a, 'x, 'c) t -> ('b, 'y, 'c) t -> ('x, 'y) Type.eq option
+(** Constructor-level equality on the ['ocaml] representation: [eq_ocaml t1 t2]
+ is [Some Equal] when [t1] and [t2] have the same constructor, which also
+ proves ['x = 'y] *)
+
+type 'typ named = {
+ name : Identifier.t; (** The internal name of the field *)
+ typ : 'typ; (** The type of the field, e.g. a ['target multireference] *)
+}
+(** A named field type: a {!t} together with the field's name.
+
+ Named types are built on the fly with {!named} (e.g. from a field, see
+ {!Field.named_of}) and used locally as typed handles — e.g. to read the
+ data of a field from a value. A pre-built instance exists for the reserved
+ field ["id"] (see [Value.id_ref]). *)
+
+val named : name:Identifier.t -> typ:'typ -> 'typ named
+(** Build a named type from a name and a type — no uniqueness involved, so
+ values built on the fly denote the same field *)
+
+val name : 'typ named -> Identifier.t
+(** The name of the field *)
+
+val typ : 'typ named -> 'typ
+(** The type of the named field *)
+
+val provably_equal :
+ ('a, 'x, 'c) t named -> ('b, 'y, 'c) t named -> ('x, 'y) Type.eq option
+(** [provably_equal n1 n2] is [Some Equal] iff [n1] and [n2] denote the same
+ field — same name — and their types match, which also proves that their
+ ['ocaml] representations are equal *)
+
+val same : ('a, 'x, 'c) t named -> ('b, 'y, 'c) t named -> bool
+(** [same n1 n2] is [true] iff [n1] and [n2] denote the same field with matching
+ types *)
diff --git a/lib/query/encoder.ml b/lib/query/encoder.ml
index 64464fd..2174e59 100644
--- a/lib/query/encoder.ml
+++ b/lib/query/encoder.ml
@@ -44,7 +44,7 @@ end
module Fold (T : sig
type 'a t
- val f : 'a t -> 'any data -> 'a t
+ val f : 'a t -> (_, _) data -> 'a t
end) =
struct
let rec f : type a. 'acc -> a tuple -> 'acc =
@@ -210,7 +210,7 @@ module CaqtiRequest = Map (struct
let t_n = t11
- let f : type a. unit -> a data -> a t =
+ let f : type a b. unit -> (a, b) data -> a t =
fun () -> function
| { content_type = Text; _ } -> option string
| { content_type = Integer; _ } -> option int64
@@ -255,7 +255,7 @@ module EncodeParameter = Map (struct
type 'a t = string
type env = unit
- let f : type a. unit -> a data -> a t =
+ let f : type a b. unit -> (a, b) data -> a t =
fun () -> function
| { content_type = Text; _ } -> "?"
| { content_type = Integer; _ } -> "?"
@@ -269,7 +269,7 @@ module InsertTableName = Map (struct
type 'a t = string
type env = unit
- let f : type a. unit -> a data -> a t =
+ let f : type a b. unit -> (a, b) data -> a t =
fun () -> function
| { name = column; content_type = Text; _ } -> Model.Identifier.quote column
| { name = column; content_type = Integer; _ } ->
@@ -293,7 +293,7 @@ module DataToCaqtiValues = Map (struct
type 'a t = 'a
type env = string -> Value.value
- let f : type a. env -> a Intf.data -> a t =
+ let f : type a b. env -> (a, b) Intf.data -> a t =
fun provider Intf.{ name; content_type } ->
let result = provider (Model.Identifier.to_string name) in
(* Check the value given by the provider and ensure it’s match the
@@ -301,13 +301,21 @@ module DataToCaqtiValues = Map (struct
match result with
| Value.Data.Any d -> (
match d with
- | Value.Data.Seq _ -> raise TypeMismatch
| Value.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) -> (
- match Model.Typ.eq content_type field_type' with
- | Some Equal -> value
- | None -> raise TypeMismatch))
+ (* 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 =
+ match Model.Typ.eq_ocaml ct field_type' with
+ | Some Equal -> value
+ | None -> raise TypeMismatch
+ in
+ match content_type with
+ | Model.Typ.MultiReference _ -> raise TypeMismatch
+ | Model.Typ.Text as ct -> check ct
+ | Model.Typ.Integer as ct -> check ct
+ | Model.Typ.MonoReference _ as ct -> check ct))
end)
diff --git a/lib/query/insert.ml b/lib/query/insert.ml
index 7b7619e..01b2dae 100644
--- a/lib/query/insert.ml
+++ b/lib/query/insert.ml
@@ -55,7 +55,7 @@ module MultiReferencesInsert = struct
acc
(** The function is called for each feature declared as multi reference. *)
- let f : type any. 'a t -> any Intf.data -> 'a t =
+ let f : type any b. 'a t -> (any, b) Intf.data -> 'a t =
fun ({ db; parent_id; provider; k; table } as acc)
Intf.{ name; content_type } ->
let module Db = (val db) in
@@ -69,33 +69,32 @@ module MultiReferencesInsert = struct
| Value.Data.Any d -> (
match d with
| Value.Data.None -> acc
- | Value.Data.V _ -> raise Encoder.TypeMismatch
- | Value.Data.Seq (field_type', values) -> (
- match Model.Typ.eq content_type field_type' with
- | None -> raise Encoder.TypeMismatch
- | Some Equal ->
- let q =
- Croquette_common.Format.format_string (fun format ->
- Format.fprintf format
- "INSERT INTO %s (%s, %s) VALUES (?, ?)"
- (Identifier.quote (Identifier.dot table name))
- parent target)
- in
- let request : request =
- Caqti.Templater.(dynamic T.(t2 int64 int64 -->. unit) q)
- 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* () = Caqti_lwt.or_fail res in
- let* res = Db.deallocate request in
- let* () = Caqti_lwt.or_fail res in
- Lwt.return res
- in
+ | Value.Data.V (field_type', values) -> (
+ match Model.Typ.eq_ocaml content_type field_type' with
+ | None -> raise Encoder.TypeMismatch
+ | Some Equal ->
+ let q =
+ Croquette_common.Format.format_string (fun format ->
+ Format.fprintf format
+ "INSERT INTO %s (%s, %s) VALUES (?, ?)"
+ (Identifier.quote (Identifier.dot table name))
+ parent target)
+ in
+ let request : request =
+ Caqti.Templater.(dynamic T.(t2 int64 int64 -->. unit) q)
+ 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* () = Caqti_lwt.or_fail res in
+ let* res = Db.deallocate request in
+ let* () = Caqti_lwt.or_fail res in
+ Lwt.return res
+ in
- { acc with k })))
+ { acc with k })))
| _ -> raise Encoder.TypeMismatch
end
@@ -203,7 +202,11 @@ let insert :
raise Encoder.TypeMismatch
| Some v, Some _ -> (
(* Otherwise update the provider to give a value for the parent *)
- let parent_id = Value.id v in
+ 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)
| other -> provider other)
@@ -227,13 +230,13 @@ let insert_list :
fun db list provider ->
let module Db = (val db) in
match Db.dialect with
- | Caqti.Template.Dialect.(Sqlite _ | Pgsql _) -> (
+ | Caqti.Template.Dialect.(Sqlite _ | Pgsql _) ->
let ( let* ) = Lwt.bind in
let* id =
insert_row db ~table:list.Model.DataList.id list.Model.DataList.fields
provider
in
- Lwt.return (Value.v list id))
+ Lwt.return (Value.v list id)
| _ -> raise Not_found
let insert_references :
@@ -242,7 +245,7 @@ let insert_references :
Int64.t ->
Model.Identifier.t ->
Value.value ->
- Int64.t Intf.data ->
+ (Int64.t, _) Intf.data ->
unit Lwt.t =
fun db content content_id field_name value data ->
let provider : string -> Value.value = function
diff --git a/lib/query/insert.mli b/lib/query/insert.mli
index 583c568..a0ab6e9 100644
--- a/lib/query/insert.mli
+++ b/lib/query/insert.mli
@@ -26,7 +26,7 @@ val insert_references :
Int64.t ->
Model.Identifier.t ->
Value.value ->
- Int64.t Intf.data ->
+ (Int64.t, _) Intf.data ->
unit Lwt.t
(** Insert a list of references for a content. This function is used during the
update but is not exposed outside of the module *)
diff --git a/lib/query/intf.ml b/lib/query/intf.ml
index 45fec11..d00c17b 100644
--- a/lib/query/intf.ml
+++ b/lib/query/intf.ml
@@ -1,70 +1,74 @@
-type 'a data = { name : Model.Identifier.t; content_type : 'a Model.typ }
-(** The data is the name of the field and the type associated with *)
+type ('a, 'b) data = {
+ name : Model.Identifier.t;
+ content_type : ('a, 'b) Model.typ;
+}
+(** The data is the name of the field and its type: ['a] is the DB
+ representation, ['b] the code representation of the data. *)
-type t_type = TT : 'a data -> t_type [@@unboxed]
+type t_type = TT : ('a, 'b) data -> t_type [@@unboxed]
(** Keep a track of the number in the fields in the content. Each property in
the content is mapped as an optionnal field in the DB, as each value can be
empty. *)
type _ tuple =
| T0 : unit tuple
- | T1 : 'a data -> 'a tuple
- | T2 : 'a data * 'b data -> ('a * 'b) tuple
- | T3 : 'a data * 'b data * 'c data -> ('a * 'b * 'c) tuple
- | T4 : 'a data * 'b data * 'c data * 'd data -> ('a * 'b * 'c * 'd) tuple
+ | T1 : ('a, _) data -> 'a tuple
+ | T2 : ('a, _) data * ('b, _) data -> ('a * 'b) tuple
+ | T3 : ('a, _) data * ('b, _) data * ('c, _) data -> ('a * 'b * 'c) tuple
+ | T4 : ('a, _) data * ('b, _) data * ('c, _) data * ('d, _) data -> ('a * 'b * 'c * 'd) tuple
| T5 :
- 'a data * 'b data * 'c data * 'd data * 'e data
+ ('a, _) data * ('b, _) data * ('c, _) data * ('d, _) data * ('e, _) data
-> ('a * 'b * 'c * 'd * 'e) tuple
| T6 :
- 'a data * 'b data * 'c data * 'd data * 'e data * 'f data
+ ('a, _) data * ('b, _) data * ('c, _) data * ('d, _) data * ('e, _) data * ('f, _) data
-> ('a * 'b * 'c * 'd * 'e * 'f) tuple
| T7 :
- 'a data * 'b data * 'c data * 'd data * 'e data * 'f data * 'g data
+ ('a, _) data * ('b, _) data * ('c, _) data * ('d, _) data * ('e, _) data * ('f, _) data * ('g, _) data
-> ('a * 'b * 'c * 'd * 'e * 'f * 'g) tuple
| T8 :
- 'a data
- * 'b data
- * 'c data
- * 'd data
- * 'e data
- * 'f data
- * 'g data
- * 'h data
+ ('a, _) data
+ * ('b, _) data
+ * ('c, _) data
+ * ('d, _) data
+ * ('e, _) data
+ * ('f, _) data
+ * ('g, _) data
+ * ('h, _) data
-> ('a * 'b * 'c * 'd * 'e * 'f * 'g * 'h) tuple
| T9 :
- 'a data
- * 'b data
- * 'c data
- * 'd data
- * 'e data
- * 'f data
- * 'g data
- * 'h data
- * 'i data
+ ('a, _) data
+ * ('b, _) data
+ * ('c, _) data
+ * ('d, _) data
+ * ('e, _) data
+ * ('f, _) data
+ * ('g, _) data
+ * ('h, _) data
+ * ('i, _) data
-> ('a * 'b * 'c * 'd * 'e * 'f * 'g * 'h * 'i) tuple
| T10 :
- 'a data
- * 'b data
- * 'c data
- * 'd data
- * 'e data
- * 'f data
- * 'g data
- * 'h data
- * 'i data
- * 'j data
+ ('a, _) data
+ * ('b, _) data
+ * ('c, _) data
+ * ('d, _) data
+ * ('e, _) data
+ * ('f, _) data
+ * ('g, _) data
+ * ('h, _) data
+ * ('i, _) data
+ * ('j, _) data
-> ('a * 'b * 'c * 'd * 'e * 'f * 'g * 'h * 'i * 'j) tuple
| T_N :
- 'a data
- * 'b data
- * 'c data
- * 'd data
- * 'e data
- * 'f data
- * 'g data
- * 'h data
- * 'i data
- * 'j data
+ ('a, _) data
+ * ('b, _) data
+ * ('c, _) data
+ * ('d, _) data
+ * ('e, _) data
+ * ('f, _) data
+ * ('g, _) data
+ * ('h, _) data
+ * ('i, _) data
+ * ('j, _) data
* 'tl tuple
-> ('a * 'b * 'c * 'd * 'e * 'f * 'g * 'h * 'i * 'j * 'tl) tuple
@@ -74,7 +78,7 @@ module type Encoder = sig
type 'a t
type env
- val f : env -> 'a data -> 'a t
+ val f : env -> ('a, _) data -> 'a t
val unit : unit t
val t2 : 'a t -> 'b t -> ('a * 'b) t
val t3 : 'a t -> 'b t -> 'c t -> ('a * 'b * 'c) t
diff --git a/lib/query/update.ml b/lib/query/update.ml
index 90db29d..ec1510b 100644
--- a/lib/query/update.ml
+++ b/lib/query/update.ml
@@ -6,7 +6,7 @@ module UpdateTemplate = Encoder.Map (struct
type 'a t = string
type env = unit
- let f : type a. unit -> a Intf.data -> a t =
+ let f : type a b. unit -> (a, b) Intf.data -> a t =
fun () -> function
| { name = column; content_type = Text; _ } ->
Model.Identifier.quote column ^ "=?"
@@ -60,9 +60,9 @@ module ReferenceListEncoder = Encoder.Map (struct
include Encoder.DefaultEncoder
type 'a t = 'a
- type env = (int64, Model.target) Model.Typ.t * Int64.t Seq.t ref
+ type env = Model.target Model.Typ.multireference * Int64.t Seq.t ref
- let f : type a. env -> a Intf.data -> a t =
+ let f : type a b. env -> (a, b) Intf.data -> a t =
fun (t, env) d ->
match !env () with
| Seq.Nil -> raise Not_found
@@ -78,8 +78,7 @@ module ReferencesSet = Set.Make (Int64)
let update_mulivaluated_field :
Caqti_lwt.connection ->
Model.content ->
- (Model.Identifier.t * (int64, Model.target) Model.Typ.t * int64 Seq.t)
- list ->
+ (Model.Identifier.t * Model.target Model.Typ.multireference * int64 Seq.t) list ->
int64 ->
unit Lwt.t =
fun db content fields id ->
@@ -141,7 +140,7 @@ 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.multi (t, ReferencesSet.to_seq remaining) in
+ let v = Value.single (t, ReferencesSet.to_seq remaining) in
Insert.insert_references db content id field_name v data_type)
Lwt.return_unit fields
@@ -157,29 +156,42 @@ let q :
(* There is a difference between
- - Typ.None
- - Typ.V None
-
- The former will be ignored in the update, and the value will remains as is
- The later will set the content to NULL in the database. *)
+ - Value.Data.none
+ - Value.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) field ->
+ (fun ((fields, references) as acc :
+ (Intf.t_type * (string * Value.value)) list
+ * (Model.Identifier.t * Model.target Model.Typ.multireference * int64 Seq.t) list)
+ field ->
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 = provider (Model.Identifier.to_string name) in
match value with
| Value.Data.Any d -> (
- match d with
- | Value.Data.None -> acc
- | Value.Data.V _ as dv ->
- ( ( tt,
- (Model.Identifier.to_string name, Value.Data.any dv) )
- :: fields,
- references )
- | Value.Data.Seq (t, seq) ->
- (fields, (name, t, seq) :: references)))
+ match content_type with
+ | Model.Typ.MultiReference _ -> (
+ match d with
+ | Value.Data.None -> acc
+ | Value.Data.V (t, seq) -> (
+ match
+ ( Model.Typ.eq content_type t,
+ Model.Typ.eq_ocaml content_type t )
+ with
+ | Some Type.Equal, Some Type.Equal ->
+ (fields, (name, t, seq) :: references)
+ | _ -> 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))
+ :: fields,
+ references ))))
([], [])
(Model.Content.fields content)
in
diff --git a/lib/value/data.ml b/lib/value/data.ml
index 7cbc00f..f0e321e 100644
--- a/lib/value/data.ml
+++ b/lib/value/data.ml
@@ -7,22 +7,18 @@
The type is parameterized over the type of the data held (['a]: the DB
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
+ 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. *)
-type ('a, 't) data =
+type ('ocaml, 't) data =
| None
- | V : ('a, 't) Model.Typ.t * 'a -> ('a, 't) data
- | Seq : (int64, 't) Model.Typ.t * int64 Seq.t -> (int64 Seq.t, 't) data
+ | V : ('db, 'ocaml, 't) Model.Typ.t * 'ocaml -> ('ocaml, 't) data
-let none : ('a, 't) data = None
+let none : ('ocaml, 't) data = None
-let v : ('a, 't) Model.Typ.t * 'a -> ('a, 't) data =
- fun (typ, value) -> V (typ, value)
+let v : ('db, 'ocaml, 't) Model.Typ.t * 'ocaml -> ('ocaml, 't) data =
+ fun (typ, value) -> V (typ, value)
-let seq : (int64, 't) Model.Typ.t * int64 Seq.t -> (int64 Seq.t, 't) data =
- fun (typ, values) -> Seq (typ, values)
+type 't any = Any : ('ocaml, 't) data -> 't any [@@unboxed]
-type 't any = Any : ('a, 't) data -> 't any [@@unboxed]
-
-let any : ('a, 't) data -> 't any = fun data -> Any data
+let any : ('ocaml, 't) data -> 't any = fun data -> Any data
diff --git a/lib/value/data.mli b/lib/value/data.mli
index 0cb4856..7b7a5cc 100644
--- a/lib/value/data.mli
+++ b/lib/value/data.mli
@@ -1,41 +1,37 @@
(** The data carried by a field of a value.
- [none] tells the field is empty (or left as is, on update), [v] carries a
- single value together with the type of its field, and [seq] carries a
+ [none] tells the field is empty (or left as is, on update), and [v] carries
+ the value of the field together with its type — a single value, or a
sequence of references for a multivalued field.
- The type is parameterized over the type of the data held (['a]: the DB
- 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 type is parameterized over ['ocaml], the in-code representation of the
+ 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. *)
-type ('a, 't) data = private
+type ('ocaml, 't) data = private
| None
- | V : ('a, 't) Model.Typ.t * 'a -> ('a, 't) data
- | Seq : (int64, 't) Model.Typ.t * int64 Seq.t -> (int64 Seq.t, 't) data
- (** The constructors are private: a [data] can be pattern-matched from the
- outside, but it can only be built with [none], [v] and [seq]. *)
+ | V : ('db, 'ocaml, 't) Model.Typ.t * 'ocaml -> ('ocaml, 't) data
-val none : ('a, 't) data
+val none : ('ocaml, 't) data
(** [none] tells the field is empty in the database (or left as is, on update)
*)
-val v : ('a, 't) Model.Typ.t * 'a -> ('a, 't) data
-(** [v (typ, value)] is the value for the field. The field type [typ] is also
- reported in order to give the compiler the nature of the data, and the
- result type records the type of the data held *)
-
-val seq : (int64, 't) Model.Typ.t * int64 Seq.t -> (int64 Seq.t, 't) data
-(** [seq (typ, values)] gets a sequence of values for a multivalued field *)
+val v : ('db, 'ocaml, 't) Model.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
+ payload to the field and determines the result type. Whether the value is a
+ sequence is carried by the type, not by a separate constructor. *)
(** Existential pack over the held type: the data of a field whose type is not
known statically (e.g. the data of any field of a content, as returned by a
provider function). Matchable from the outside, but built with [any]. *)
-type 't any = private Any : ('a, 't) data -> 't any [@@unboxed]
+type 't any = Any : ('ocaml, 't) data -> 't any [@@unboxed]
-val any : ('a, 't) data -> 't any
+val any : ('ocaml, 't) data -> 't any
(** Pack a typed [data] into an [any], hiding the type of the data held *)
(** Example for inserting data — build the data of each field, then pack it:
diff --git a/lib/value/value.ml b/lib/value/value.ml
index 38c2240..3012667 100644
--- a/lib/value/value.ml
+++ b/lib/value/value.ml
@@ -1,34 +1,36 @@
-module Fields = Fields
-
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],
- [Data.seq] *)
+ the data held, built with the smart constructors [Data.none], [Data.v] *)
+type value = Model.target Data.any
(** A resolved field data: the data of a field of a content or a list, whatever
the type of the data held *)
-type value = Model.target Data.any
let none : value = Data.any Data.none
-let single : ('a, Model.target) Model.Typ.t * 'a -> value =
- fun data -> Data.any (Data.v data)
-
-let multi : (int64, Model.target) Model.Typ.t * int64 Seq.t -> value =
- fun data -> Data.any (Data.seq data)
+let single : ('db, 'ocaml, Model.target) Model.Typ.t * 'ocaml -> value =
+ fun data -> Data.any (Data.v data)
+type 'a t = {
+ content : 'a; (** The content or list whose table holds the row *)
+ id : int64; (** The primary key of the row *)
+}
(** A value: one row of a content or list table in the database.
- The type parameter ['a] is the content or the static list whose table
- holds the row: [Model.content] for contents ([Model.content Value.t]) and
+ The type parameter ['a] is the content or the static list whose table holds
+ the row: [Model.content] for contents ([Model.content Value.t]) and
[Model.list] for lists ([Model.list Value.t]). *)
-type 'a t = {
- content : 'a;
- (** The content or list whose table holds the row *)
- id : int64;
- (** The primary key of the row *)
-}
let v : 'a -> int64 -> 'a t = fun content id -> { content; id }
let content : 'a t -> 'a = fun t -> t.content
let id : 'a t -> int64 = fun t -> t.id
+
+let id_ref : (Model.target Model.Typ.monoreference) Model.Typ.named =
+ Model.Typ.named ~name:Model.Identifier.id ~typ:Model.Typ.Integer
+
+let get : type ocaml.
+ 'a t -> (('db, ocaml, Model.target) Model.Typ.t) Model.Typ.named -> ocaml =
+ fun t ref ->
+ match Model.Typ.provably_equal id_ref ref with
+ | Some Type.Equal -> Some t.id
+ | None -> raise Not_found
diff --git a/lib/value/value.mli b/lib/value/value.mli
index 42482f7..5c493aa 100644
--- a/lib/value/value.mli
+++ b/lib/value/value.mli
@@ -2,14 +2,11 @@
A value is one row of a content or list table in the database. *)
-module Fields = Fields
-(** The fields of a value, each with its associated data *)
-
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],
- [Data.seq]); and {!Data.any}, the existential pack used when the held type
- is not known statically *)
+ 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
(** The data of a field of a content or a list, whatever the type of the data
@@ -18,12 +15,10 @@ type value = Model.target Data.any
val none : value
(** [none]: the field is empty (or left as is, on update) *)
-val single : ('a, Model.target) Model.Typ.t * 'a -> value
-(** [single (typ, data)]: a single value for the field, together with the type
- [typ] of the field *)
-
-val multi : (int64, Model.target) Model.Typ.t * int64 Seq.t -> value
-(** [multi (typ, ids)]: a sequence of references for a multivalued field *)
+val single : ('db, 'ocaml, Model.target) Model.Typ.t * 'ocaml -> value
+(** [single (typ, data)]: the value for the field, in its code representation
+ ['ocaml] — a single value, or an [int64 Seq.t] for a multivalued reference —
+ together with the type [typ] of the field *)
type 'a t
(** A value: a row of the table of the content or list ['a], identified by its
@@ -43,4 +38,22 @@ val content : 'a t -> 'a
(** The content or list the value belongs to *)
val id : 'a t -> int64
+[@@deprecated
+ "Use [Value.get v Value.id_ref] to read the primary key as typed data"]
(** The primary key of the value in its table *)
+
+val id_ref : Model.target Model.Typ.monoreference Model.Typ.named
+(** The pre-built {!Model.Typ.named} handle of the reserved field ["id"]
+ ({!Model.Identifier.id}) — an integer field present in every row, content or
+ list. This is the key to read the primary key of any value. *)
+
+val get :
+ 'a t -> ('db, 'ocaml, Model.target) Model.Typ.t Model.Typ.named -> 'ocaml
+(** Read a field of the value and return its data directly in its code
+ representation ['ocaml] — e.g. [get v id_ref : int64 option] is the primary
+ 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]. *)