diff options
Diffstat (limited to 'lib/query/select.ml')
| -rw-r--r-- | lib/query/select.ml | 206 |
1 files changed, 206 insertions, 0 deletions
diff --git a/lib/query/select.ml b/lib/query/select.ml new file mode 100644 index 0000000..011544c --- /dev/null +++ b/lib/query/select.ml @@ -0,0 +1,206 @@ +(** Read a row of a content table from the database, and expose each of its + fields to a callback. *) + +let ( let* ) = Lwt.bind + +type callback = { + f : + 'db 'ocaml. + ('db, 'ocaml, Model.target) Model.Typ.t Model.Typ.named * 'ocaml -> unit; +} + +(** Call the callback for one single-column field. For those fields the database + and the code representations coincide, so the column value read from the + database is also the code value. *) +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.Typ.Text -> + let named = Model.Typ.named ~name ~typ:content_type in + k.f (named, value) + | Model.Typ.Integer -> + let named = Model.Typ.named ~name ~typ:content_type in + k.f (named, value) + | Model.Typ.MonoReference _ -> + let named = Model.Typ.named ~name ~typ:content_type in + k.f (named, value) + | Model.Typ.MultiReference _ -> raise Encoder.TypeMismatch + +(** Call the callback for each field of the tuple, zipping the type descriptors + with the row read from the database *) +let rec iter_fields : type a. callback -> a Intf.tuple -> a -> unit = + fun k tup row -> + match tup with + | Intf.T0 -> ( match row with () -> ()) + | Intf.T1 d1 -> apply_field k d1 row + | Intf.T2 (d1, d2) -> + let v1, v2 = row in + apply_field k d1 v1; + apply_field k d2 v2 + | Intf.T3 (d1, d2, d3) -> + let v1, v2, v3 = row in + apply_field k d1 v1; + apply_field k d2 v2; + apply_field k d3 v3 + | Intf.T4 (d1, d2, d3, d4) -> + let v1, v2, v3, v4 = row in + apply_field k d1 v1; + apply_field k d2 v2; + apply_field k d3 v3; + apply_field k d4 v4 + | Intf.T5 (d1, d2, d3, d4, d5) -> + let v1, v2, v3, v4, v5 = row in + apply_field k d1 v1; + apply_field k d2 v2; + apply_field k d3 v3; + apply_field k d4 v4; + apply_field k d5 v5 + | Intf.T6 (d1, d2, d3, d4, d5, d6) -> + let v1, v2, v3, v4, v5, v6 = row in + apply_field k d1 v1; + apply_field k d2 v2; + apply_field k d3 v3; + apply_field k d4 v4; + apply_field k d5 v5; + apply_field k d6 v6 + | Intf.T7 (d1, d2, d3, d4, d5, d6, d7) -> + let v1, v2, v3, v4, v5, v6, v7 = row in + apply_field k d1 v1; + apply_field k d2 v2; + apply_field k d3 v3; + apply_field k d4 v4; + apply_field k d5 v5; + apply_field k d6 v6; + apply_field k d7 v7 + | Intf.T8 (d1, d2, d3, d4, d5, d6, d7, d8) -> + let v1, v2, v3, v4, v5, v6, v7, v8 = row in + apply_field k d1 v1; + apply_field k d2 v2; + apply_field k d3 v3; + apply_field k d4 v4; + apply_field k d5 v5; + apply_field k d6 v6; + apply_field k d7 v7; + apply_field k d8 v8 + | Intf.T9 (d1, d2, d3, d4, d5, d6, d7, d8, d9) -> + let v1, v2, v3, v4, v5, v6, v7, v8, v9 = row in + apply_field k d1 v1; + apply_field k d2 v2; + apply_field k d3 v3; + apply_field k d4 v4; + apply_field k d5 v5; + apply_field k d6 v6; + apply_field k d7 v7; + apply_field k d8 v8; + apply_field k d9 v9 + | Intf.T10 (d1, d2, d3, d4, d5, d6, d7, d8, d9, d10) -> + let v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 = row in + apply_field k d1 v1; + apply_field k d2 v2; + apply_field k d3 v3; + apply_field k d4 v4; + apply_field k d5 v5; + apply_field k d6 v6; + apply_field k d7 v7; + apply_field k d8 v8; + apply_field k d9 v9; + apply_field k d10 v10 + | Intf.T_N (d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, tl) -> + let v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, vtl = row in + apply_field k d1 v1; + apply_field k d2 v2; + apply_field k d3 v3; + apply_field k d4 v4; + apply_field k d5 v5; + apply_field k d6 v6; + apply_field k d7 v7; + apply_field k d8 v8; + apply_field k d9 v9; + apply_field k d10 v10; + iter_fields k tl vtl + +(** Read the targets of a multivalued reference field from its dedicated table, + and call the callback with the sequence of targets *) +let select_multireference : + Caqti_lwt.connection -> + Model.Identifier.t -> + Int64.t -> + Model.field -> + callback -> + unit Lwt.t = + fun db table id field k -> + let module Db = (val db) in + let module Identifier = Model.Identifier in + let name = Model.Field.name field in + let (Model.Field.T content_type) = Model.Field.type_ field in + match content_type with + | Model.Typ.MultiReference _ -> + let named = Model.Typ.named ~name ~typ:content_type in + let q = + Croquette_common.Format.format_string (fun format -> + Format.fprintf format "SELECT %s FROM %s WHERE %s = ?" + (Identifier.quote Identifier.target) + (Identifier.quote (Identifier.dot table name)) + (Identifier.quote Identifier.parent)) + in + 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); + Lwt.return_unit + | _ -> raise Encoder.TypeMismatch + +let select : + Caqti_lwt.connection -> Int64.t -> Model.content -> callback -> unit Lwt.t = + fun db id content k -> + let module Db = (val db) in + let module Identifier = Model.Identifier in + let table = Model.Content.id content in + (* The [parent] column of a subcontent is exposed as a field, like on + insert *) + let init_fields = + match Model.Content.parent content with + | None -> [] + | Some _ -> + [ + Model.Field.create (Model.Field.T Model.Typ.Integer) [] ~required:true + Model.Identifier.parent; + ] + in + let fields = init_fields @ List.of_seq (Model.Content.fields content) in + let is_multireference field = + match Model.Field.type_ field with + | Model.Field.T (Model.Typ.MultiReference _) -> true + | _ -> false + in + let single_fields = List.filter (fun f -> not (is_multireference f)) fields in + let multi_fields = List.filter is_multireference fields in + (* The multivalued fields are not columns of the content table, they are + read separately from their dedicated tables *) + let (T single) = + Encoder.list_to_tup single_fields ~f:(fun field -> + let name = Model.Field.name field in + let (Model.Field.T content_type) = Model.Field.type_ field in + Intf.TT { name; content_type }) + in + let* () = + match single with + | Intf.T0 -> Lwt.return_unit + | _ -> + let columns = Encoder.InsertTableName.f () single in + let row_type = Encoder.CaqtiRequest.f () single in + let q = + Croquette_common.Format.format_string (fun format -> + Format.fprintf format "SELECT %s FROM %s WHERE %s = ?" columns + (Identifier.quote table) + (Identifier.quote Identifier.id)) + in + let request = Caqti.Templater.(direct T.(int64 -->! row_type) q) in + let* res = Db.find request id in + let* row = Caqti_lwt.or_fail res in + 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 |
