package mssql

  1. Overview
  2. Docs

Source file row.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
open Core

type t = Db_field.t option String.Map.t [@@deriving sexp_of]

let create_exn ~month_offset row colnames =
  List.map row ~f:(Db_field.of_data ~month_offset)
  |> List.zip_exn colnames
  |> String.Map.of_alist_exn

let to_alist (t : t) =
  List.map (Map.to_alist t) ~f:(Tuple2.map_snd ~f:Db_field.to_string)

let find_map ~(f : ?column:string -> Db_field.t -> 'a) (t : t) column =
  match Map.find t column with
  | None ->
    let columns = Map.keys t |> String.concat ~sep:", " in
    failwithf "Column %s was not returned in the query, \
               columns returned are: %s" column columns ()
  | Some data ->
    Option.map data ~f:(f ~column)

let required colname = function
  | None -> failwithf "Expected data for %s but got NULL" colname ()
  | Some v -> v

let bignum =
  find_map ~f:Db_field.bignum

let bignum_exn t colname =
  required colname (bignum t colname)

let float =
  find_map ~f:Db_field.float

let float_exn t colname =
  required colname (float t colname)

let int =
  find_map ~f:Db_field.int

let int_exn t colname =
  required colname (int t colname)

let int32 =
  find_map ~f:Db_field.int32

let int32_exn t colname =
  required colname (int32 t colname)

let int64 =
  find_map ~f:Db_field.int64

let int64_exn t colname =
  required colname (int64 t colname)

let bool =
  find_map ~f:Db_field.bool

let bool_exn t colname =
  required colname (bool t colname)

let str =
  find_map ~f:Db_field.str

let str_exn t colname =
  required colname (str t colname)

let date =
  find_map ~f:Db_field.date

let date_exn t colname =
  required colname (date t colname)

let datetime =
  find_map ~f:Db_field.datetime

let datetime_exn t colname =
  required colname (datetime t colname)