summaryrefslogtreecommitdiff
path: root/lib/query
diff options
context:
space:
mode:
Diffstat (limited to 'lib/query')
-rw-r--r--lib/query/encoder.ml2
-rw-r--r--lib/query/insert.ml2
-rw-r--r--lib/query/query.mli14
-rw-r--r--lib/query/select.ml55
-rw-r--r--lib/query/select.mli42
-rw-r--r--lib/query/update.ml10
6 files changed, 78 insertions, 47 deletions
diff --git a/lib/query/encoder.ml b/lib/query/encoder.ml
index 4c8d528..3c9484a 100644
--- a/lib/query/encoder.ml
+++ b/lib/query/encoder.ml
@@ -309,7 +309,7 @@ module DataToCaqtiValues = Map (struct
(* 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.Types.t) : x =
- match Model.Types.eq_ocaml ct field_type' with
+ match Model.Types.eq ct field_type' with
| Some Equal -> value
| None -> raise TypeMismatch
in
diff --git a/lib/query/insert.ml b/lib/query/insert.ml
index 98a5c88..c9a7eba 100644
--- a/lib/query/insert.ml
+++ b/lib/query/insert.ml
@@ -68,7 +68,7 @@ module MultiReferencesInsert = struct
match d with
| Model.Data.None -> acc
| Model.Data.V (field_type', values) -> (
- match Model.Types.eq_ocaml content_type field_type' with
+ match Model.Types.eq content_type field_type' with
| None -> raise Encoder.TypeMismatch
| Some Equal ->
let q =
diff --git a/lib/query/query.mli b/lib/query/query.mli
index 5e42f09..0265487 100644
--- a/lib/query/query.mli
+++ b/lib/query/query.mli
@@ -41,14 +41,22 @@ val update :
val select :
Caqti_lwt.connection ->
+ ?multivalued:bool ->
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] *)
+ field's name, type and value. With [~multivalued:false], the multivalued
+ reference fields are not read. See [Query.Select.select] *)
val select_list :
- Caqti_lwt.connection -> Int64.t -> Model.list -> Select.callback -> unit Lwt.t
+ Caqti_lwt.connection ->
+ ?multivalued:bool ->
+ Int64.t ->
+ Model.list ->
+ Select.callback ->
+ unit Lwt.t
(** Read a row of a list table and call the callback once per field with the
- field's named type and its value. See [Query.Select.select_list] *)
+ field's name, type and value. With [~multivalued:false], the multivalued
+ reference fields are not read. See [Query.Select.select_list] *)
diff --git a/lib/query/select.ml b/lib/query/select.ml
index cdcaeed..8cb2b2b 100644
--- a/lib/query/select.ml
+++ b/lib/query/select.ml
@@ -6,7 +6,8 @@ let ( let* ) = Lwt.bind
type callback = {
f :
'db 'ocaml.
- ('db, 'ocaml, Model.target) Model.Types.t Model.Types.named * 'ocaml -> unit;
+ (Model.Identifier.t * ('db, 'ocaml, Model.target) Model.Types.t) * 'ocaml ->
+ unit;
}
(** Call the callback for one single-column field. For those fields the database
@@ -15,15 +16,9 @@ type callback = {
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.Types.Text ->
- let named = Model.Types.named ~name ~typ:content_type in
- k.f (named, value)
- | Model.Types.Integer ->
- let named = Model.Types.named ~name ~typ:content_type in
- k.f (named, value)
- | Model.Types.MonoReference _ ->
- let named = Model.Types.named ~name ~typ:content_type in
- k.f (named, value)
+ | Model.Types.Text -> k.f ((name, content_type), value)
+ | Model.Types.Integer -> k.f ((name, content_type), value)
+ | Model.Types.MonoReference _ -> k.f ((name, content_type), value)
| Model.Types.MultiReference _ -> raise Encoder.TypeMismatch
(** Call the callback for each field of the tuple, zipping the type descriptors
@@ -135,7 +130,6 @@ let select_multireference :
let (Model.Field.T content_type) = Model.Field.type_ field in
match content_type with
| Model.Types.MultiReference _ ->
- let named = Model.Types.named ~name ~typ:content_type in
let q =
Croquette_common.Format.format_string (fun format ->
Format.fprintf format "SELECT %s FROM %s WHERE %s = ?"
@@ -146,22 +140,25 @@ let select_multireference :
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);
+ k.f ((name, content_type), List.to_seq targets);
Lwt.return_unit
| _ -> raise Encoder.TypeMismatch
(** Read the row [id] of [table], whose columns are described by [fields], and
call [callback] once per field. With [~has_parent], the reserved [parent]
- column is exposed as a field too, like on insert. *)
+ column is exposed as a field too, like on insert. With [~multivalued:false],
+ the multivalued reference fields are not read from their dedicated tables.
+*)
let select_row :
Caqti_lwt.connection ->
table:Model.Identifier.t ->
?has_parent:bool ->
+ ?multivalued:bool ->
Model.field list ->
Int64.t ->
callback ->
unit Lwt.t =
- fun db ~table ?(has_parent = false) fields id k ->
+ fun db ~table ?(has_parent = false) ?(multivalued = true) fields id k ->
let module Db = (val db) in
let module Identifier = Model.Identifier in
let init_fields =
@@ -206,20 +203,34 @@ let select_row :
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
+ if multivalued then
+ Lwt_list.iter_s
+ (fun field -> select_multireference db table id field k)
+ multi_fields
+ else Lwt.return_unit
let select :
- Caqti_lwt.connection -> Int64.t -> Model.content -> callback -> unit Lwt.t =
- fun db id content k ->
+ Caqti_lwt.connection ->
+ ?multivalued:bool ->
+ Int64.t ->
+ Model.content ->
+ callback ->
+ unit Lwt.t =
+ fun db ?(multivalued = true) id content k ->
select_row db ~table:(Model.Content.id content)
~has_parent:
(match Model.Content.parent content with None -> false | Some _ -> true)
+ ~multivalued
(List.of_seq (Model.Content.fields content))
id k
let select_list :
- Caqti_lwt.connection -> Int64.t -> Model.list -> callback -> unit Lwt.t =
- fun db id list k ->
- select_row db ~table:list.Model.DataList.id list.Model.DataList.fields id k
+ Caqti_lwt.connection ->
+ ?multivalued:bool ->
+ Int64.t ->
+ Model.list ->
+ callback ->
+ unit Lwt.t =
+ fun db ?(multivalued = true) id list k ->
+ select_row db ~multivalued ~table:list.Model.DataList.id
+ list.Model.DataList.fields id k
diff --git a/lib/query/select.mli b/lib/query/select.mli
index 68e4d00..fb7787e 100644
--- a/lib/query/select.mli
+++ b/lib/query/select.mli
@@ -3,21 +3,27 @@
type callback = {
f :
'db 'ocaml.
- ('db, 'ocaml, Model.target) Model.Types.t Model.Types.named * 'ocaml -> unit;
+ (Model.Identifier.t * ('db, 'ocaml, Model.target) Model.Types.t) * '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.
+(** A callback called once per field of the selected row, with the field's name
+ and type, and its data in the code representation ['ocaml] — a triple 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) -> ... } ]. *)
+ type: [ { f = fun ((name, typ), 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:
+ Caqti_lwt.connection ->
+ ?multivalued:bool ->
+ Int64.t ->
+ Model.content ->
+ callback ->
+ unit Lwt.t
+(** [select ?multivalued 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 name and 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
@@ -25,12 +31,22 @@ val select :
- each multivalued reference field is read from its dedicated table
([content.field]) and yields the sequence of its targets ([int64 Seq.t]).
+ [~multivalued] (default [true]) controls whether those multivalued reference
+ fields are also read: with [~multivalued:false], only the row of the content
+ table is read and the callback is not called for them.
+
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. *)
+ argument. In the callback, each [((name, typ), data)] triple can be stored
+ in a value's dictionary to make the field readable as typed data. *)
val select_list :
- Caqti_lwt.connection -> Int64.t -> Model.list -> callback -> unit Lwt.t
+ Caqti_lwt.connection ->
+ ?multivalued:bool ->
+ Int64.t ->
+ Model.list ->
+ callback ->
+ unit Lwt.t
(** Same as {!select}, for a row of the dedicated table of a static list. Lists
have no [parent] column; their multivalued reference fields are read from
- their dedicated tables ([list.field]), like for contents. *)
+ their dedicated tables ([list.field]), like for contents, unless
+ [~multivalued:false] is given. *)
diff --git a/lib/query/update.ml b/lib/query/update.ml
index 9bd1f2b..515bf2e 100644
--- a/lib/query/update.ml
+++ b/lib/query/update.ml
@@ -181,13 +181,9 @@ let q :
match d with
| Model.Data.None -> acc
| Model.Data.V (t, seq) -> (
- match
- ( Model.Types.eq content_type t,
- Model.Types.eq_ocaml content_type t )
- with
- | Some Type.Equal, Some Type.Equal ->
- (fields, (name, t, seq) :: references)
- | _ -> raise Encoder.TypeMismatch))
+ match Model.Types.eq content_type t with
+ | Some Type.Equal -> (fields, (name, t, seq) :: references)
+ | None -> raise Encoder.TypeMismatch))
| _ -> (
match d with
| Model.Data.None -> acc