package ppx_mysql

  1. Overview
  2. Docs

Source file ppx_mysql_runtime.ml

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
type deserialization_error = {
  idx : int;
  name : string;
  func : string;
  value : string;
  message : string
}

type column_error =
  [ `Expected_non_null_column of int * string
  | `Deserialization_error of deserialization_error ]

type 'a deserializer = string -> ('a, string) result

let wrap_failure : (string -> 'a) -> 'a deserializer =
 fun of_string s ->
  match of_string s with
  | v -> Ok v
  | exception Failure _ -> Error "cannot parse number"

let string_of_string str = Ok str

let int_of_string = wrap_failure Pervasives.int_of_string

let int32_of_string = wrap_failure Int32.of_string

let int64_of_string = wrap_failure Int64.of_string

let bool_of_string str =
  match Pervasives.int_of_string str with
  | v -> Ok (v <> 0)
  | exception Failure _ -> Error "cannot parse boolean"

external identity : 'a -> 'a = "%identity"

let deserialize_non_nullable_column idx name of_string func err_accum = function
  | None ->
      let err = `Expected_non_null_column (idx, name) in
      None, err :: err_accum
  | Some value -> (
    match of_string value with
    | Ok ok -> Some ok, err_accum
    | Error message ->
        let err = `Deserialization_error {idx; name; func; value; message} in
        None, err :: err_accum )

let deserialize_nullable_column idx name of_string func err_accum = function
  | None -> Some None, err_accum
  | Some value -> (
    match of_string value with
    | Ok ok -> Some (Some ok), err_accum
    | Error message ->
        let err = `Deserialization_error {idx; name; func; value; message} in
        None, err :: err_accum )

module type SERIALIZABLE = sig
  type t

  val of_mysql : string -> (t, string) result

  val to_mysql : t -> string
end

module type PPX_MYSQL_CONTEXT_ARG = sig
  module IO : sig
    type 'a t

    val return : 'a -> 'a t

    val bind : 'a t -> ('a -> 'b t) -> 'b t
  end

  module Prepared : sig
    type dbh

    type stmt

    type stmt_result

    type error

    val create : dbh -> string -> (stmt, error) result IO.t

    val close : stmt -> (unit, error) result IO.t

    val execute_null : stmt -> string option array -> (stmt_result, error) result IO.t

    val fetch : stmt_result -> (string option array option, error) result IO.t
  end
end

module type PPX_MYSQL_CONTEXT = sig
  module IO : sig
    type 'a t

    val return : 'a -> 'a t

    val bind : 'a t -> ('a -> 'b t) -> 'b t

    val ( >>= ) : 'a t -> ('a -> 'b t) -> 'b t
  end

  module IO_result : sig
    type ('a, 'e) t = ('a, 'e) result IO.t

    val return : 'a -> ('a, 'e) t

    val bind : ('a, 'e) t -> ('a -> ('b, 'e) t) -> ('b, 'e) t

    val ( >>= ) : ('a, 'e) t -> ('a -> ('b, 'e) t) -> ('b, 'e) t
  end

  module Prepared : sig
    type dbh

    type stmt

    type stmt_result

    type error

    type wrapped_dbh

    type wrapped_error = [`Mysql_error of error]

    val init : dbh -> wrapped_dbh

    val execute_null
      :  stmt ->
      string option array ->
      (stmt_result, [> wrapped_error]) result IO.t

    val fetch
      :  stmt_result ->
      (string option array option, [> wrapped_error]) result IO.t

    val with_stmt_cached
      :  wrapped_dbh ->
      string ->
      (stmt -> ('a, ([> wrapped_error] as 'e)) result IO.t) ->
      ('a, 'e) result IO.t

    val with_stmt_uncached
      :  wrapped_dbh ->
      string ->
      (stmt -> ('a, ([> wrapped_error] as 'e)) result IO.t) ->
      ('a, 'e) result IO.t
  end
end

module Make_context (M : PPX_MYSQL_CONTEXT_ARG) :
  PPX_MYSQL_CONTEXT
  with type 'a IO.t = 'a M.IO.t
   and type Prepared.dbh = M.Prepared.dbh
   and type Prepared.error = M.Prepared.error = struct
  module IO = struct
    include M.IO

    let ( >>= ) = bind
  end

  module IO_result = struct
    type ('a, 'e) t = ('a, 'e) result IO.t

    let return x = IO.return @@ Ok x

    let bind x f =
      IO.bind x (function
          | Ok v -> f v
          | Error _ as e -> IO.return e )

    let ( >>= ) = bind
  end

  module Prepared = struct
    type dbh = M.Prepared.dbh

    type stmt = M.Prepared.stmt

    type stmt_result = M.Prepared.stmt_result

    type error = M.Prepared.error

    type wrapped_dbh = {
      dbh : dbh;
      stmt_cache : (string, stmt) Hashtbl.t
    }

    type wrapped_error = [`Mysql_error of error]

    let wrap f x =
      IO.bind (f x) @@ function
      | Ok _ as ok -> IO.return ok
      | Error err -> IO.return @@ Error (`Mysql_error err)

    let init dbh = {dbh; stmt_cache = Hashtbl.create 16}

    let create dbh sql = wrap (M.Prepared.create dbh) sql

    let create_or_reuse {dbh; stmt_cache} sql =
      match Hashtbl.find_opt stmt_cache sql with
      | Some stmt -> IO_result.return stmt
      | None ->
          IO_result.bind (create dbh sql) @@ fun stmt ->
          Hashtbl.replace stmt_cache sql stmt;
          IO_result.return stmt

    let close stmt = wrap M.Prepared.close stmt

    let execute_null stmt args = wrap (M.Prepared.execute_null stmt) args

    let fetch stmt_res = wrap M.Prepared.fetch stmt_res

    let with_stmt_cached wrapped_dbh sql f =
      IO_result.bind (create_or_reuse wrapped_dbh sql) @@ fun stmt -> f stmt

    let with_stmt_uncached {dbh; stmt_cache = _} sql f =
      IO_result.bind (create dbh sql) @@ fun stmt ->
      IO.bind (f stmt) @@ fun res ->
      IO.bind (close stmt) @@ function
      | Ok () -> IO.return res
      | Error _ as e -> IO.return e
  end
end

module Stdlib = struct
  module Array = Array
  module List = List

  module Option = struct
    type 'a t = 'a option =
      | None
      | Some of 'a

    let map f = function
      | Some x -> Some (f x)
      | None -> None

    let get = function
      | Some x -> x
      | None -> invalid_arg "Option.get"
  end

  module Result = struct
    type ('a, 'e) t = ('a, 'e) result =
      | Ok of 'a
      | Error of 'e

    let bind r f =
      match r with
      | Ok x -> f x
      | Error _ as e -> e

    let ( >>= ) = bind
  end

  module String = struct
    include String

    let append = ( ^ )
  end

  let ( = ) = ( = )
end
OCaml

Innovation. Community. Security.