summaryrefslogtreecommitdiff
path: root/lib/value/value.ml
blob: ab5b5a77f28f33469bf423a601e1a534ee15dcb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
type value = Model.target Model.Data.any
(** A resolved field data: the data of a field of a content or a list, whatever
    the type of the data held *)

let none : value = Model.Data.any Model.Data.none

let single : ('db, 'ocaml, Model.target) Model.Typ.t * 'ocaml -> value =
 fun data -> Model.Data.any (Model.Data.v data)

(** The key of the fields dictionary: an existential pack over the named type of
    a field, hiding its ['db] and ['ocaml] parameters *)
type key =
  | Named : ('db, 'ocaml, Model.target) Model.Typ.t Model.Typ.named -> key

module Key = struct
  type t = key

  let equal : t -> t -> bool = fun (Named a) (Named b) -> Model.Typ.same a b
  let hash : t -> int = fun (Named n) -> Hashtbl.hash (Model.Typ.name n)
end

module Fields = Hashtbl.Make (Key)

type 'a t = {
  content : 'a;  (** The content or list whose table holds the row *)
  id : int64;  (** The primary key of the row *)
  fields : value Fields.t;
      (** Cache of the other fields and their values, keyed by their named type
          — empty by default, populating it requires reading the database *)
}
(** A value: one row of a content or list table in the database.

    The type parameter ['a] is the content or the static list whose table holds
    the row: [Model.content] for contents ([Model.content Value.t]) and
    [Model.list] for lists ([Model.list Value.t]). *)

let v : 'a -> int64 -> 'a t =
 fun content id -> { content; id; fields = Fields.create 0 }

let content : 'a t -> 'a = fun t -> t.content
let id : 'a t -> int64 = fun t -> t.id

let add_field : type ocaml.
    'a t ->
    ('db, ocaml, Model.target) Model.Typ.t Model.Typ.named ->
    ocaml ->
    unit =
 fun t ref data ->
  let typ = Model.Typ.typ ref in
  Fields.replace t.fields (Named ref)
    (Model.Data.any (Model.Data.v (typ, data)))

let id_ref : Model.target Model.Typ.monoreference Model.Typ.named =
  Model.Typ.named ~name:Model.Identifier.id ~typ:Model.Typ.Integer

let get : type ocaml.
    'a t -> ('db, ocaml, Model.target) Model.Typ.t Model.Typ.named -> ocaml =
 fun t ref ->
  match Model.Typ.provably_equal id_ref ref with
  | Some Type.Equal -> Some t.id
  | None -> (
      match Fields.find_opt t.fields (Named ref) with
      | Some (Model.Data.Any (Model.Data.V (typ, payload))) -> (
          match Model.Typ.eq_ocaml (Model.Typ.typ ref) typ with
          | Some Type.Equal -> payload
          | None -> raise Not_found)
      | _ -> raise Not_found)

let load : Caqti_lwt.connection -> Model.content t -> unit Lwt.t =
 fun db v ->
  let ( let* ) = Lwt.bind in
  let* () =
    Query.select db v.id v.content
      { f = (fun (named, data) -> add_field v named data) }
  in
  Lwt.return_unit

(** Pretty-print the data of a field, whatever its type *)
let pp_data : type db ocaml.
    Format.formatter -> (db, ocaml, Model.target) Model.Typ.t -> ocaml -> unit =
 fun fmt typ data ->
  let pp_option pp fmt data =
    match data with
    | Some v -> pp fmt v
    | None -> Format.pp_print_string fmt "NULL"
  in
  match typ with
  | Model.Typ.Text ->
      pp_option (fun fmt s -> Format.fprintf fmt "%S" s) fmt data
  | Model.Typ.Integer ->
      pp_option (fun fmt i -> Format.fprintf fmt "%Ld" i) fmt data
  | Model.Typ.MonoReference _ ->
      pp_option (fun fmt i -> Format.fprintf fmt "%Ld" i) fmt data
  | Model.Typ.MultiReference _ ->
      Format.fprintf fmt "[%a]"
        (Format.pp_print_list
           ~pp_sep:(fun fmt () -> Format.pp_print_string fmt ", ")
           (fun fmt i -> Format.fprintf fmt "%Ld" i))
        (List.of_seq data)

let pp : Format.formatter -> 'a t -> unit =
 fun fmt t ->
  let fields =
    Fields.fold
      (fun (Named named) data acc -> (Model.Typ.name named, data) :: acc)
      t.fields []
    |> List.sort (fun (n1, _) (n2, _) -> Model.Identifier.compare n1 n2)
  in
  let pp_field fmt (name, data) =
    let (Model.Data.Any d) = data in
    match d with
    | Model.Data.None ->
        Format.fprintf fmt "%s = NULL" (Model.Identifier.to_string name)
    | Model.Data.V (typ, payload) ->
        Format.fprintf fmt "%s = " (Model.Identifier.to_string name);
        pp_data fmt typ payload
  in
  Format.fprintf fmt "{@[id = %Ld;@ %a@]}" t.id
    (Format.pp_print_list
       ~pp_sep:(fun fmt () -> Format.pp_print_string fmt "; ")
       pp_field)
    fields