summaryrefslogtreecommitdiff
path: root/lib/query
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2026-04-30 09:16:03 +0200
committerSébastien Dailly <sebastien@dailly.me>2026-05-06 16:45:25 +0200
commit960d5b8f2728f71dd9ce038d527844d82ca2f0f2 (patch)
tree9a15dc43f91a0b58a3aaa10223bba35abf7ef5f2 /lib/query
parenta5efd7d15b4d049a4ed539ed5d9c2f1037d00973 (diff)
Allow updates for existing contents
Diffstat (limited to 'lib/query')
-rw-r--r--lib/query/common.ml0
-rw-r--r--lib/query/dune1
-rw-r--r--lib/query/encoder.ml3
-rw-r--r--lib/query/insert.ml45
-rw-r--r--lib/query/insert.mli11
-rw-r--r--lib/query/query.ml1
-rw-r--r--lib/query/query.mli7
-rw-r--r--lib/query/tests/update.t99
-rw-r--r--lib/query/update.ml182
9 files changed, 332 insertions, 17 deletions
diff --git a/lib/query/common.ml b/lib/query/common.ml
deleted file mode 100644
index e69de29..0000000
--- a/lib/query/common.ml
+++ /dev/null
diff --git a/lib/query/dune b/lib/query/dune
index b9a862a..307319a 100644
--- a/lib/query/dune
+++ b/lib/query/dune
@@ -7,5 +7,6 @@
lwt.unix
lwt
uri
+ croquette_common
model
configuration))
diff --git a/lib/query/encoder.ml b/lib/query/encoder.ml
index c9314cb..ae34c3e 100644
--- a/lib/query/encoder.ml
+++ b/lib/query/encoder.ml
@@ -261,8 +261,7 @@ module EncodeParameter = Map (struct
| { content_type = Text; _ } -> "?"
| { content_type = Integer; _ } -> "?"
| { content_type = MonoReference _; _ } -> "?"
- | { content_type = MultiReference _; _ } ->
- failwith "Cannot encode multireference field"
+ | { content_type = MultiReference _; _ } -> "?"
include ConcatParam
end)
diff --git a/lib/query/insert.ml b/lib/query/insert.ml
index 1ee1549..ecb0efb 100644
--- a/lib/query/insert.ml
+++ b/lib/query/insert.ml
@@ -21,15 +21,6 @@ let collect_fields :
let (Model.Field.T content_type) = Model.Field.type_ field in
TT { name; content_type })
-let format_string : (Format.formatter -> unit) -> string =
- fun f ->
- let buff = Buffer.create 16 in
- let format = Format.formatter_of_buffer buff in
- let () = f format in
-
- Format.pp_print_flush format ();
- Buffer.contents buff
-
module MultiReferencesInsert = struct
type 'a t = {
db : Caqti_lwt.connection;
@@ -80,7 +71,7 @@ module MultiReferencesInsert = struct
| None -> raise Encoder.TypeMismatch
| Some Equal ->
let q =
- format_string (fun format ->
+ Croquette_common.Format.format_string (fun format ->
Format.fprintf format
"INSERT INTO \"%s.%s\" (\"parent\", \"target\") VALUES \
(?, ?)"
@@ -96,7 +87,9 @@ module MultiReferencesInsert = struct
let* res =
Seq.fold_left (accumulate db request parent_id) k values
in
- let* _ = Db.deallocate request 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
@@ -141,7 +134,7 @@ let insert_returning :
in
let q =
- format_string (fun format ->
+ Croquette_common.Format.format_string (fun format ->
Format.fprintf format "INSERT INTO \"%s\" %s VALUES %s RETURNING ID"
(Model.Content.id content) columns insert_template)
in
@@ -169,9 +162,9 @@ let insert_returning :
MultiReferencesInsert.
{ db; provider; k = Lwt.return_ok (); parent_id = content_id; content }
in
- let ret = Fold.f acc multi_valuated_fields in
- let* k = ret.k in
- let* () = Caqti_lwt.or_fail k in
+ let MultiReferencesInsert.{ k; _ } = Fold.f acc multi_valuated_fields in
+ let* res = k in
+ let* () = Caqti_lwt.or_fail res in
Lwt.return content_id
(** Insert an entry for a specific content *)
@@ -199,3 +192,25 @@ let insert :
match Caqti_driver_info.dialect_tag Db.driver_info with
| `Sqlite | `Pgsql -> insert_returning db content provider
| _ -> raise Not_found
+
+let insert_references :
+ Caqti_lwt.connection ->
+ Model.Reference.t Model.Content.t ->
+ Int64.t ->
+ string ->
+ Model.Reference.t Model.Typ.value ->
+ Int64.t Intf.data ->
+ unit Lwt.t =
+ fun db content content_id field_name value data ->
+ let provider : string -> Model.Reference.t Model.Typ.value = function
+ | n when String.equal n field_name -> value
+ | _ -> Model.Typ.None
+ in
+ let acc =
+ MultiReferencesInsert.
+ { db; provider; k = Lwt.return_ok (); parent_id = content_id; content }
+ in
+ let ( let* ) = Lwt.bind in
+ let MultiReferencesInsert.{ k; _ } = MultiReferencesInsert.f acc data in
+ let* res = k in
+ Caqti_lwt.or_fail res
diff --git a/lib/query/insert.mli b/lib/query/insert.mli
index f19ff11..798c37b 100644
--- a/lib/query/insert.mli
+++ b/lib/query/insert.mli
@@ -7,3 +7,14 @@ val insert :
(string -> Model.Reference.t Model.Typ.value) ->
int64 Lwt.t
(** See [Query.insert] *)
+
+val insert_references :
+ Caqti_lwt.connection ->
+ Model.Reference.t Model.Content.t ->
+ Int64.t ->
+ string ->
+ Model.Reference.t Model.Typ.value ->
+ 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/query.ml b/lib/query/query.ml
index 4aaf467..13a646a 100644
--- a/lib/query/query.ml
+++ b/lib/query/query.ml
@@ -7,3 +7,4 @@
let insert = Insert.insert
let create = Create.create
+let update = Update.q
diff --git a/lib/query/query.mli b/lib/query/query.mli
index 9dcef17..24e0591 100644
--- a/lib/query/query.mli
+++ b/lib/query/query.mli
@@ -20,3 +20,10 @@ val insert :
itself, and no functionnals controls are realized. *)
val create : Caqti_lwt.connection -> Configuration.Ast.setting -> unit Lwt.t
+
+val update :
+ Caqti_lwt.connection ->
+ Int64.t ->
+ Model.Reference.t Model.Content.t ->
+ (string -> Model.Reference.t Model.Typ.value) ->
+ unit Lwt.t
diff --git a/lib/query/tests/update.t b/lib/query/tests/update.t
new file mode 100644
index 0000000..32c04bb
--- /dev/null
+++ b/lib/query/tests/update.t
@@ -0,0 +1,99 @@
+Test the update method of a content.
+
+The test create a content with four features:
+- text
+- integer
+- mono reference
+- multi references
+and each test start with a new content empty.
+
+ $ tests --update --help
+ Start the tests
+ --text <v> Set field with the value <v>
+ --integer <n> Set field with the value <n>
+ --reference <id> Set field with the id
+ --references <id,..> Set this list of values
+ --commit Commit this group of update
+ --clear <f> Clear the field <f>
+ -help Display this list of options
+ --help Display this list of options
+
+After each invocation (or after a commit), the test displays the values for the
+content. By default it is empty:
+
+ $ tests --update \
+ > --commit
+ {"reference":null,"integer":null,"text":null}
+
+The multivaluated field references is not displayed when there is no values.
+
+Any feature can be set using an argument with the same name:
+
+ $ tests --update \
+ > --integer 123
+ {"reference":null,"integer":123,"text":null}
+
+ $ tests --update \
+ > --text abc
+ {"reference":null,"integer":null,"text":"abc"}
+
+ $ tests --update \
+ > --reference 1
+ {"reference":1,"integer":null,"text":null}
+
+The multireferences values are displayed only when they are set.
+
+ $ tests --update \
+ > --references 1,2,3 \
+ {"reference":null,"integer":null,"text":null}
+ {"references":1}
+ {"references":2}
+ {"references":3}
+
+The "commit" argument can be used to update the content at a given state
+
+ $ tests --update \
+ > --integer 123 \
+ > --text abc \
+ > --references 1 \
+ > --commit \
+ > --reference 1
+ {"reference":null,"integer":123,"text":"abc"}
+ {"references":1}
+ {"reference":1,"integer":123,"text":"abc"}
+ {"references":1}
+
+A field can be explicitally set empty. There is a difference between not
+updating a field (changing nothing) and setting the field to empty:
+
+ $ tests --update \
+ > --integer 123 \
+ > --text abc \
+ > --commit \
+ > --clear integer
+ {"reference":null,"integer":123,"text":"abc"}
+ {"reference":null,"integer":null,"text":"abc"}
+
+ $ tests --update \
+ > --references 1,2,3 \
+ > --commit \
+ > --clear references
+ {"reference":null,"integer":null,"text":null}
+ {"references":1}
+ {"references":2}
+ {"references":3}
+ {"reference":null,"integer":null,"text":null}
+
+There is no duplicates values in the external references, a new update replace
+the existing values.
+
+ $ tests --update \
+ > --references 1,2 \
+ > --commit \
+ > --references 1,3
+ {"reference":null,"integer":null,"text":null}
+ {"references":1}
+ {"references":2}
+ {"reference":null,"integer":null,"text":null}
+ {"references":1}
+ {"references":3}
diff --git a/lib/query/update.ml b/lib/query/update.ml
new file mode 100644
index 0000000..4b8adfb
--- /dev/null
+++ b/lib/query/update.ml
@@ -0,0 +1,182 @@
+(** Generate the update queries for a content *)
+
+type provider = string -> Model.Reference.t Model.Typ.value
+
+module UpdateTemplate = Encoder.Map (struct
+ type 'a t = string
+ type env = unit
+
+ let f : type a. unit -> a Intf.data -> a t =
+ fun () -> function
+ | { name = column; content_type = Text; _ } -> column ^ "=?"
+ | { name = column; content_type = Integer; _ } -> column ^ "=?"
+ | { name = column; content_type = MonoReference _; _ } -> column ^ "=?"
+ | { name = _; content_type = MultiReference _; _ } -> raise Not_found
+
+ include Encoder.ConcatParam
+end)
+
+let update_fields :
+ Caqti_lwt.connection ->
+ 'a Model.Content.t ->
+ (Intf.t_type * (string * Model.Reference.t Model.Typ.value)) list ->
+ int64 ->
+ unit Lwt.t =
+ fun db content fields id ->
+ let module Db = (val db) in
+ let fields_list, value_list = List.split fields in
+ (* Update all the fields stored in the content table *)
+ let (T query_fields) =
+ Encoder.list_to_tup fields_list ~f:(fun field -> field)
+ in
+ let format_param = (UpdateTemplate.f ()) query_fields in
+ let types_param =
+ Caqti_type.(t2 ((Encoder.CaqtiRequest.f ()) query_fields) int64)
+ in
+ let q =
+ Croquette_common.Format.format_string (fun format ->
+ Format.fprintf format "UPDATE \"%s\" SET %s WHERE ID = ?"
+ (Model.Content.id content) format_param)
+ in
+ let request =
+ Caqti_request.Infix.((types_param ->. Caqti_type.unit) ~oneshot:true) q
+ in
+ let query_param =
+ Encoder.DataToCaqtiValues.f
+ (fun name -> List.assoc name value_list)
+ query_fields
+ in
+
+ let ( let* ) = Lwt.bind in
+ let* result = Db.exec request (query_param, id) in
+ Caqti_lwt.or_fail result
+
+(** Encode a sequence of references into a type usable in Caqti request *)
+module ReferenceListEncoder = Encoder.Map (struct
+ include Encoder.DefaultEncoder
+
+ type 'a t = 'a
+ type env = (int64, Model.Reference.t) Model.Typ.t * Int64.t Seq.t ref
+
+ let f : type a. env -> a Intf.data -> a t =
+ fun (t, env) d ->
+ match !env () with
+ | Seq.Nil -> raise Not_found
+ | Cons (value, tl) -> (
+ env := tl;
+ match Model.Typ.eq t d.Intf.content_type with
+ | Some Type.Equal -> value
+ | None -> raise Encoder.TypeMismatch)
+end)
+
+module ReferencesSet = Set.Make (Int64)
+
+let update_mulivaluated_field :
+ Caqti_lwt.connection ->
+ Model.Reference.t Model.Content.t ->
+ (string * (int64, Model.Reference.t) Model.Typ.t * int64 Seq.t) list ->
+ int64 ->
+ unit Lwt.t =
+ fun db content fields id ->
+ let module Db = (val db) in
+ let ( let* ) = Lwt.bind in
+ List.fold_left
+ (fun acc (field_name, t, values) ->
+ let* () = acc in
+
+ let values = ReferencesSet.of_seq values in
+ let lvalues = ReferencesSet.to_list values in
+
+ let data_type = Intf.{ name = field_name; content_type = t } in
+
+ (* First delete all the values not belonging to the new set *)
+ let (T existing_references_t) =
+ Encoder.list_to_tup lvalues ~f:(fun ref ->
+ ignore ref;
+ Intf.(TT data_type))
+ in
+ let q_delete =
+ Croquette_common.Format.format_string (fun f ->
+ Format.fprintf f
+ "DELETE FROM \"%s.%s\" where parent=? and target not in (%s)"
+ (Model.Content.id content) field_name
+ (Encoder.EncodeParameter.f () existing_references_t))
+ in
+ let caqti_param = Encoder.CaqtiRequest.f () existing_references_t in
+ let request_delete =
+ Caqti_request.Infix.(
+ (Caqti_type.(t2 int64 caqti_param) ->. Caqti_type.unit) ~oneshot:true)
+ q_delete
+ in
+ let seq = ref (ReferencesSet.to_seq values) in
+ let* result =
+ Db.exec request_delete
+ (id, ReferenceListEncoder.f (t, seq) existing_references_t)
+ in
+ let* () = Caqti_lwt.or_fail result in
+
+ (* Then collect all the existing remaining values *)
+ let q_select =
+ Croquette_common.Format.format_string (fun f ->
+ Format.fprintf f "SELECT id FROM \"%s.%s\" WHERE parent=?"
+ (Model.Content.id content) field_name)
+ in
+ let request_select =
+ Caqti_request.Infix.(
+ (Caqti_type.int64 ->* Caqti_type.int64) ~oneshot:true)
+ q_select
+ in
+ let* request_result = Db.collect_list request_select id in
+ let* remaining' = Caqti_lwt.or_fail request_result in
+
+ (* Finaly insert the missing values.
+
+ 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
+ Insert.insert_references db content id field_name v data_type)
+ Lwt.return_unit fields
+
+let q :
+ Caqti_lwt.connection ->
+ Int64.t ->
+ Model.Reference.t Model.Content.t ->
+ (string -> Model.Reference.t Model.Typ.value) ->
+ unit Lwt.t =
+ fun db id content provider ->
+ let module Db = (val db) in
+ (* Get all the single fields to update in the content *)
+
+ (* 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. *)
+ let fields, references =
+ Seq.fold_left
+ (fun ((fields, references) as acc) 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 name in
+ match value with
+ | Model.Typ.None -> acc
+ | Model.Typ.V _ as value -> ((tt, (name, value)) :: fields, references)
+ | Model.Typ.Seq (t, seq) -> (fields, (name, t, seq) :: references))
+ ([], [])
+ (Model.Content.fields content)
+ in
+ let ( let* ) = Lwt.bind in
+ let* () =
+ match fields with
+ | [] -> Lwt.return_unit
+ | _ -> update_fields db content fields id
+ in
+ let* () =
+ match references with
+ | [] -> Lwt.return_unit
+ | _ -> update_mulivaluated_field db content references id
+ in
+ Lwt.return_unit