summaryrefslogtreecommitdiff
path: root/lib/query
diff options
context:
space:
mode:
Diffstat (limited to 'lib/query')
-rw-r--r--lib/query/dune1
-rw-r--r--lib/query/encoder.ml22
-rw-r--r--lib/query/insert.ml63
-rw-r--r--lib/query/insert.mli15
-rw-r--r--lib/query/query.mli30
-rw-r--r--lib/query/update.ml23
6 files changed, 89 insertions, 65 deletions
diff --git a/lib/query/dune b/lib/query/dune
index 307319a..990b572 100644
--- a/lib/query/dune
+++ b/lib/query/dune
@@ -9,4 +9,5 @@
uri
croquette_common
model
+ value
configuration))
diff --git a/lib/query/encoder.ml b/lib/query/encoder.ml
index ba5435e..64464fd 100644
--- a/lib/query/encoder.ml
+++ b/lib/query/encoder.ml
@@ -291,7 +291,7 @@ module DataToCaqtiValues = Map (struct
include DefaultEncoder
type 'a t = 'a
- type env = string -> Model.value
+ type env = string -> Value.value
let f : type a. env -> a Intf.data -> a t =
fun provider Intf.{ name; content_type } ->
@@ -299,13 +299,15 @@ module DataToCaqtiValues = Map (struct
(* 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)
+ | 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))
end)
diff --git a/lib/query/insert.ml b/lib/query/insert.ml
index 40887e8..7b7619e 100644
--- a/lib/query/insert.ml
+++ b/lib/query/insert.ml
@@ -25,7 +25,7 @@ module MultiReferencesInsert = struct
parent_id : int64;
table : Model.Identifier.t;
(** The table holding the row: a content table, or a list *)
- provider : string -> Model.value;
+ provider : string -> Value.value;
k : (unit, Caqti.Error.t) result Lwt.t;
}
@@ -66,10 +66,12 @@ module MultiReferencesInsert = struct
| Model.Typ.MultiReference _target -> (
let result = provider (Model.Identifier.to_string name) in
match result with
- | Model.Typ.None -> acc
- | Model.Typ.V _ -> raise Encoder.TypeMismatch
- | Model.Typ.Seq (field_type', values) -> (
- match Model.Typ.eq content_type field_type' with
+ | 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 =
@@ -93,7 +95,7 @@ module MultiReferencesInsert = struct
Lwt.return res
in
- { acc with k }))
+ { acc with k })))
| _ -> raise Encoder.TypeMismatch
end
@@ -107,7 +109,7 @@ let insert_row :
table:Model.Identifier.t ->
?init_fields:Model.field list ->
Model.field list ->
- (string -> Model.value) ->
+ (string -> Value.value) ->
int64 Lwt.t =
fun db ~table ?(init_fields = []) fields provider ->
let module Db = (val db) in
@@ -166,8 +168,8 @@ let insert_row :
let insert_returning :
Caqti_lwt.connection ->
Model.content ->
- (string -> Model.value) ->
- int64 Lwt.t =
+ (string -> Value.value) ->
+ Model.content Value.t Lwt.t =
fun db content provider ->
let init_fields =
match Model.Content.parent content with
@@ -176,17 +178,21 @@ let insert_returning :
Model.Field.
[ create (T Integer) [] ~required:true Model.Identifier.parent ]
in
- insert_row db ~table:(Model.Content.id content) ~init_fields
- (List.of_seq (Model.Content.fields content))
- provider
+ let ( let* ) = Lwt.bind in
+ let* id =
+ insert_row db ~table:(Model.Content.id content) ~init_fields
+ (List.of_seq (Model.Content.fields content))
+ provider
+ in
+ Lwt.return (Value.v content id)
(** Insert an entry for a specific content *)
let insert :
Caqti_lwt.connection ->
Model.content ->
- ?parent:int64 ->
- (string -> Model.value) ->
- int64 Lwt.t =
+ ?parent:Model.content Value.t ->
+ (string -> Value.value) ->
+ Model.content Value.t Lwt.t =
fun db content ?parent provider ->
let provider =
match (parent, Model.Content.parent content) with
@@ -197,8 +203,9 @@ 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
function
- | "parent" -> Model.Typ.(V (Integer, Some v))
+ | "parent" -> Value.single (Model.Typ.Integer, Some parent_id)
| other -> provider other)
in
let module Db = (val db) in
@@ -207,20 +214,26 @@ let insert :
insert_returning db content provider
| _ -> raise Not_found
-(** Insert a row in the dedicated table of a list, and return its id.
+(** Insert a row in the dedicated table of a list, and return it.
Like [insert], the [provider] is called with the name of each field of the
list to get its value. Multivalued fields are inserted in their own
dedicated table, with the parent pointing to the list row. *)
let insert_list :
- Caqti_lwt.connection -> Model.list -> (string -> Model.value) -> int64 Lwt.t
- =
+ Caqti_lwt.connection ->
+ Model.list ->
+ (string -> Value.value) ->
+ Model.list Value.t Lwt.t =
fun db list provider ->
let module Db = (val db) in
match Db.dialect with
- | Caqti.Template.Dialect.(Sqlite _ | Pgsql _) ->
- insert_row db ~table:list.Model.DataList.id list.Model.DataList.fields
- provider
+ | 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))
| _ -> raise Not_found
let insert_references :
@@ -228,13 +241,13 @@ let insert_references :
Model.content ->
Int64.t ->
Model.Identifier.t ->
- Model.value ->
+ Value.value ->
Int64.t Intf.data ->
unit Lwt.t =
fun db content content_id field_name value data ->
- let provider : string -> Model.value = function
+ let provider : string -> Value.value = function
| n when String.equal n (Model.Identifier.to_string field_name) -> value
- | _ -> Model.Typ.None
+ | _ -> Value.none
in
let acc =
MultiReferencesInsert.
diff --git a/lib/query/insert.mli b/lib/query/insert.mli
index 32251ac..583c568 100644
--- a/lib/query/insert.mli
+++ b/lib/query/insert.mli
@@ -3,17 +3,18 @@
val insert :
Caqti_lwt.connection ->
Model.content ->
- ?parent:int64 ->
- (string -> Model.value) ->
- int64 Lwt.t
+ ?parent:Model.content Value.t ->
+ (string -> Value.value) ->
+ Model.content Value.t Lwt.t
(** See [Query.insert] *)
val insert_list :
Caqti_lwt.connection ->
Model.list ->
- (string -> Model.value) ->
- int64 Lwt.t
-(** Insert a row in the dedicated table of a static list, and return its id.
+ (string -> Value.value) ->
+ Model.list Value.t Lwt.t
+(** Insert a row in the dedicated table of a static list, and return it as a
+ value.
The [provider] is called with the name of each field of the list to get its
value, like [insert]. Multivalued fields are inserted in their dedicated
@@ -24,7 +25,7 @@ val insert_references :
Model.content ->
Int64.t ->
Model.Identifier.t ->
- Model.value ->
+ Value.value ->
Int64.t Intf.data ->
unit Lwt.t
(** Insert a list of references for a content. This function is used during the
diff --git a/lib/query/query.mli b/lib/query/query.mli
index d4badd5..cd7287f 100644
--- a/lib/query/query.mli
+++ b/lib/query/query.mli
@@ -1,16 +1,17 @@
val insert :
Caqti_lwt.connection ->
Model.content ->
- ?parent:int64 ->
- (string -> Model.value) ->
- int64 Lwt.t
-(** Insert a new element and return the ID.
+ ?parent:Model.content Value.t ->
+ (string -> Value.value) ->
+ Model.content Value.t Lwt.t
+(** Insert a new element and return it as a value.
- [insert db content ~parent provider] will create the content in the
- database.
+ [insert db content ~parent provider] will create the content in the database
+ and return the inserted row as a [Value.t] (the content together with its
+ new id).
- The parent is required if the content is a subcontent. Not providing the
- parameter will raise the error [TypeMismatch].
+ The parent — itself a value — is required if the content is a subcontent.
+ Not providing the 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
@@ -22,13 +23,14 @@ val insert :
val insert_list :
Caqti_lwt.connection ->
Model.list ->
- (string -> Model.value) ->
- int64 Lwt.t
-(** Insert a row in the dedicated table of a static list, and return its id.
+ (string -> Value.value) ->
+ Model.list Value.t Lwt.t
+(** Insert a row in the dedicated table of a static list, and return it as a
+ value.
The [provider] is called with the name of each field of the list, like
- [insert]. This is the function used to populate the lists from their
- source. *)
+ [insert]. This is the function used to populate the lists from their source.
+*)
val create : Caqti_lwt.connection -> Configuration.Ast.setting -> unit Lwt.t
@@ -36,5 +38,5 @@ val update :
Caqti_lwt.connection ->
Int64.t ->
Model.content ->
- (string -> Model.value) ->
+ (string -> Value.value) ->
unit Lwt.t
diff --git a/lib/query/update.ml b/lib/query/update.ml
index 3c0941c..90db29d 100644
--- a/lib/query/update.ml
+++ b/lib/query/update.ml
@@ -1,6 +1,6 @@
(** Generate the update queries for a content *)
-type provider = string -> Model.value
+type provider = string -> Value.value
module UpdateTemplate = Encoder.Map (struct
type 'a t = string
@@ -22,7 +22,7 @@ end)
let update_fields :
Caqti_lwt.connection ->
Model.content ->
- (Intf.t_type * (string * Model.value)) list ->
+ (Intf.t_type * (string * Value.value)) list ->
int64 ->
unit Lwt.t =
fun db content fields id ->
@@ -141,7 +141,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 = Model.Typ.Seq (t, ReferencesSet.to_seq remaining) in
+ let v = Value.multi (t, ReferencesSet.to_seq remaining) in
Insert.insert_references db content id field_name v data_type)
Lwt.return_unit fields
@@ -149,7 +149,7 @@ let q :
Caqti_lwt.connection ->
Int64.t ->
Model.content ->
- (string -> Model.value) ->
+ (string -> Value.value) ->
unit Lwt.t =
fun db id content provider ->
let module Db = (val db) in
@@ -170,11 +170,16 @@ let q :
let tt = Intf.TT { name; content_type } in
let value = provider (Model.Identifier.to_string name) in
match value with
- | Model.Typ.None -> acc
- | Model.Typ.V _ as value ->
- ( (tt, (Model.Identifier.to_string name, value)) :: fields,
- references )
- | Model.Typ.Seq (t, seq) -> (fields, (name, t, seq) :: references))
+ | 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)))
([], [])
(Model.Content.fields content)
in