summaryrefslogtreecommitdiff
path: root/lib/query
diff options
context:
space:
mode:
Diffstat (limited to 'lib/query')
-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
5 files changed, 137 insertions, 110 deletions
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