summaryrefslogtreecommitdiff
path: root/lib/query/encoder.ml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/query/encoder.ml')
-rw-r--r--lib/query/encoder.ml85
1 files changed, 63 insertions, 22 deletions
diff --git a/lib/query/encoder.ml b/lib/query/encoder.ml
index 2f86eab..c9314cb 100644
--- a/lib/query/encoder.ml
+++ b/lib/query/encoder.ml
@@ -5,34 +5,40 @@
open StdLabels
open Intf
+exception TypeMismatch
+(** Exception raised when the given type does not match the value *)
+
module Map (T : Encoder) = struct
- let rec f : type a. a tuple -> a T.t =
- fun t ->
+ let rec f : type a. T.env -> a tuple -> a T.t =
+ fun env t ->
match t with
| T0 -> T.unit
- | T1 t1 -> T.f t1
- | T2 (t1, t2) -> T.t2 (T.f t1) (T.f t2)
- | T3 (t1, t2, t3) -> T.t3 (T.f t1) (T.f t2) (T.f t3)
- | T4 (t1, t2, t3, t4) -> T.t4 (T.f t1) (T.f t2) (T.f t3) (T.f t4)
+ | T1 t1 -> T.f env t1
+ | T2 (t1, t2) -> T.t2 (T.f env t1) (T.f env t2)
+ | T3 (t1, t2, t3) -> T.t3 (T.f env t1) (T.f env t2) (T.f env t3)
+ | T4 (t1, t2, t3, t4) ->
+ T.t4 (T.f env t1) (T.f env t2) (T.f env t3) (T.f env t4)
| T5 (t1, t2, t3, t4, t5) ->
- T.t5 (T.f t1) (T.f t2) (T.f t3) (T.f t4) (T.f t5)
+ T.t5 (T.f env t1) (T.f env t2) (T.f env t3) (T.f env t4) (T.f env t5)
| T6 (t1, t2, t3, t4, t5, t6) ->
- T.t6 (T.f t1) (T.f t2) (T.f t3) (T.f t4) (T.f t5) (T.f t6)
+ T.t6 (T.f env t1) (T.f env t2) (T.f env t3) (T.f env t4) (T.f env t5)
+ (T.f env t6)
| T7 (t1, t2, t3, t4, t5, t6, t7) ->
- T.t7 (T.f t1) (T.f t2) (T.f t3) (T.f t4) (T.f t5) (T.f t6) (T.f t7)
+ T.t7 (T.f env t1) (T.f env t2) (T.f env t3) (T.f env t4) (T.f env t5)
+ (T.f env t6) (T.f env t7)
| T8 (t1, t2, t3, t4, t5, t6, t7, t8) ->
- T.t8 (T.f t1) (T.f t2) (T.f t3) (T.f t4) (T.f t5) (T.f t6) (T.f t7)
- (T.f t8)
+ T.t8 (T.f env t1) (T.f env t2) (T.f env t3) (T.f env t4) (T.f env t5)
+ (T.f env t6) (T.f env t7) (T.f env t8)
| T9 (t1, t2, t3, t4, t5, t6, t7, t8, t9) ->
- T.t9 (T.f t1) (T.f t2) (T.f t3) (T.f t4) (T.f t5) (T.f t6) (T.f t7)
- (T.f t8) (T.f t9)
+ T.t9 (T.f env t1) (T.f env t2) (T.f env t3) (T.f env t4) (T.f env t5)
+ (T.f env t6) (T.f env t7) (T.f env t8) (T.f env t9)
| T10 (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10) ->
- T.t10 (T.f t1) (T.f t2) (T.f t3) (T.f t4) (T.f t5) (T.f t6) (T.f t7)
- (T.f t8) (T.f t9) (T.f t10)
+ T.t10 (T.f env t1) (T.f env t2) (T.f env t3) (T.f env t4) (T.f env t5)
+ (T.f env t6) (T.f env t7) (T.f env t8) (T.f env t9) (T.f env t10)
| T_N (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t_n) ->
- let tl = f t_n in
- T.t_n (T.f t1) (T.f t2) (T.f t3) (T.f t4) (T.f t5) (T.f t6) (T.f t7)
- (T.f t8) (T.f t9) (T.f t10) tl
+ let tl = f env t_n in
+ T.t_n (T.f env t1) (T.f env t2) (T.f env t3) (T.f env t4) (T.f env t5)
+ (T.f env t6) (T.f env t7) (T.f env t8) (T.f env t9) (T.f env t10) tl
end
module Fold (T : sig
@@ -200,9 +206,12 @@ let rec list_to_tup : f:('a -> t_type) -> 'a list -> t =
module CaqtiRequest = Map (struct
include Caqti_type
+ type env = unit
+
let t_n = Caqti_type.t11
- let f : type a. a data -> a Caqti_type.t = function
+ let f : type a. unit -> a data -> a Caqti_type.t =
+ fun () -> function
| { content_type = Text; _ } -> Caqti_type.option @@ Caqti_type.string
| { content_type = Integer; _ } -> Caqti_type.option @@ Caqti_type.int64
| { content_type = MultiReference _; _ } -> Caqti_type.int64
@@ -243,10 +252,12 @@ module ConcatParam = struct
String.concat ~sep [ a; b; c; d; e; f; g; h; i; j; k ]
end
-module EncodeInsert = Map (struct
+module EncodeParameter = Map (struct
type 'a t = string
+ type env = unit
- let f : type a. a data -> a t = function
+ let f : type a. unit -> a data -> a t =
+ fun () -> function
| { content_type = Text; _ } -> "?"
| { content_type = Integer; _ } -> "?"
| { content_type = MonoReference _; _ } -> "?"
@@ -258,8 +269,10 @@ end)
module InsertTableName = Map (struct
type 'a t = string
+ type env = unit
- let f : type a. a data -> a t = function
+ let f : type a. unit -> a data -> a t =
+ fun () -> function
| { name = column; content_type = Text; _ } -> column
| { name = column; content_type = Integer; _ } -> column
| { name = column; content_type = MultiReference _; _ } -> column
@@ -267,3 +280,31 @@ module InsertTableName = Map (struct
include ConcatParam
end)
+
+(** Extract the value for caqti from the type [Intf.data] and a function mapping
+ each key to the value.
+
+ This module only care about values which can fit in a single column, and
+ will raise an error if a sequence of values is provided. *)
+module DataToCaqtiValues = Map (struct
+ include DefaultEncoder
+
+ type 'a t = 'a
+ type env = string -> Model.Reference.t Model.Typ.value
+
+ let f : type a. env -> a Intf.data -> a t =
+ fun provider 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 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_type
+ | Model.Typ.V (field_type', value) -> (
+ match Model.Typ.eq content_type field_type' with
+ | Some Equal -> value
+ | None -> raise TypeMismatch)
+end)