Skip to content

Commit 05a1082

Browse files
committed
Add support for 64-bit integers.
1 parent 862877a commit 05a1082

File tree

12 files changed

+43
-2
lines changed

12 files changed

+43
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ function.
202202
```ocaml
203203
type value =
204204
[ `Int of int
205+
| `Int64 of Int64.t
205206
| `Float of float
206207
| `String of string
207208
| `Bytes of bytes
@@ -218,6 +219,7 @@ provided to extract the OCaml types directly from a field:
218219

219220
```ocaml
220221
val int : Field.t -> int
222+
val int64 : Field.t -> Int64.t
221223
val float : Field.t -> float
222224
val string : Field.t -> string
223225
val bytes : Field.t -> bytes
@@ -231,6 +233,7 @@ For nullable fields, the following analogous functions are also provided:
231233

232234
```ocaml
233235
val int_opt : Field.t -> int option
236+
val int64_opt : Field.t -> Int64.t option
234237
val float_opt : Field.t -> float option
235238
val string_opt : Field.t -> string option
236239
val bytes_opt : Field.t -> bytes option

examples/async/nonblocking_async_example.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ let print_row row =
6666
printf "%20s " name;
6767
match M.Field.value field with
6868
| `Int i -> printf "%d\n%!" i
69+
| `Int64 i -> printf "%Ld\n%!" i
6970
| `Float x -> printf "%f\n%!" x
7071
| `String s -> printf "%s\n%!" s
7172
| `Bytes b -> printf "%s\n%!" (Caml_bytes.to_string b)

examples/blocking/blocking_example.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ let print_row row =
1717
printf "%20s " name;
1818
match M.Field.value field with
1919
| `Int i -> printf "%d\n%!" i
20+
| `Int64 i -> printf "%Ld\n%!" i
2021
| `Float x -> printf "%f\n%!" x
2122
| `String s -> printf "%s\n%!" s
2223
| `Bytes b -> printf "%s\n%!" (Bytes.to_string b)

examples/lwt/nonblocking_lwt_example.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ let print_row row =
5252
Lwt_io.printf "%20s " name >>= fun () ->
5353
match M.Field.value field with
5454
| `Int i -> Lwt_io.printf "%d\n%!" i
55+
| `Int64 i -> Lwt_io.printf "%Ld\n%!" i
5556
| `Float x -> Lwt_io.printf "%f\n%!" x
5657
| `String s -> Lwt_io.printf "%s\n%!" s
5758
| `Bytes b -> Lwt_io.printf "%s\n%!" (Bytes.to_string b)

examples/nonblocking/nonblocking_stress_test.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ module Make (W : Mariadb.Nonblocking.Wait) = struct
6969
let string_of_value = function
7070
| `Null -> "NULL"
7171
| `Int i -> sprintf "(%d : int)" i
72+
| `Int64 i -> sprintf "(%Ld : int64)" i
7273
| `Float x -> sprintf "(%.8g : float)" x
7374
| `String s -> sprintf "(%S : string)" s
7475
| `Bytes s -> sprintf "(%S : bytes)" (Bytes.to_string s)
@@ -89,7 +90,10 @@ module Make (W : Mariadb.Nonblocking.Wait) = struct
8990
| `Null, _ | _, `Null -> false
9091
| `Int i, `Int i' -> i = i'
9192
| `Int i, `Float x | `Float x, `Int i -> float_of_int i = x
93+
| `Int64 i, `Int x | `Int x, `Int64 i -> Int64.(equal i (of_int x))
94+
| `Int64 i, `Float x | `Float x, `Int64 i -> Int64.to_float i = x
9295
| `Int _, _ | _, `Int _ -> false
96+
| `Int64 _, _ | _, `Int64 _ -> false
9397
| `Float x, `Float x' -> equal_float x x'
9498
| `Float _, _ | _, `Float _ -> false
9599
| `String s, `String s' -> s = s'

examples/select/nonblocking_select_example.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ let print_row row =
5555
printf "%20s " name;
5656
match M.Field.value field with
5757
| `Int i -> printf "%d\n%!" i
58+
| `Int64 i -> printf "%Ld\n%!" i
5859
| `Float x -> printf "%f\n%!" x
5960
| `String s -> printf "%s\n%!" s
6061
| `Bytes b -> printf "%s\n%!" (Bytes.to_string b)

lib/bind.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ let int ?(unsigned = false) b param ~at =
124124
~unsigned:(if unsigned then yes else no)
125125
~at
126126

127+
let int64 ?(unsigned = false) b param ~at =
128+
let p = allocate int64_t param in
129+
bind b
130+
~buffer:(coerce (ptr int64_t) (ptr void) p)
131+
~size:(sizeof int64_t)
132+
~mysql_type:T.Type.long_long
133+
~unsigned:(if unsigned then yes else no)
134+
~at
135+
127136
let float b param ~at =
128137
let p = allocate float param in
129138
bind b

lib/common.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ module Stmt = struct
339339
match arg with
340340
| `Null -> Bind.null b ~at
341341
| `Int i -> Bind.int b i ~at
342+
| `Int64 i -> Bind.int64 b i ~at
342343
| `Float x -> Bind.float b x ~at
343344
| `String s -> Bind.string b s ~at
344345
| `Bytes s -> Bind.blob b s ~at

lib/field.ml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module T = Ffi_bindings.Types(Ffi_generated_types)
55
type value =
66
[ `Null
77
| `Int of int
8+
| `Int64 of Int64.t
89
| `Float of float
910
| `String of string
1011
| `Bytes of bytes
@@ -81,8 +82,8 @@ let convert field typ unsigned =
8182
| `Short, false -> `Int (UInt.to_int (cast_to uint field))
8283
| (`Int24 | `Long), true -> `Int (UInt32.to_int (cast_to uint32_t field))
8384
| (`Int24 | `Long), false -> `Int (Int32.to_int (cast_to int32_t field))
84-
| `Long_long, true -> `Int (UInt64.to_int (cast_to uint64_t field))
85-
| `Long_long, false -> `Int (Int64.to_int (cast_to int64_t field))
85+
| `Long_long, true -> `Int64 (UInt64.to_int64 (cast_to uint64_t field))
86+
| `Long_long, false -> `Int64 (cast_to int64_t field)
8687
| `Float, _ -> `Float (cast_to float field)
8788
| `Double, _ -> `Float (cast_to double field)
8889
| #to_string, _ -> `String (Bytes.to_string (to_bytes field))
@@ -104,6 +105,11 @@ let int field =
104105
| `Int i -> i
105106
| _ -> err field ~info:"an integer"
106107

108+
let int64 field =
109+
match value field with
110+
| `Int64 i -> i
111+
| _ -> err field ~info:"an 64-bit integer"
112+
107113
let float field =
108114
match value field with
109115
| `Float x -> x
@@ -130,6 +136,12 @@ let int_opt field =
130136
| `Null -> None
131137
| _ -> err field ~info:"a nullable integer"
132138

139+
let int64_opt field =
140+
match value field with
141+
| `Int64 i -> Some i
142+
| `Null -> None
143+
| _ -> err field ~info:"a nullable 64-bit integer"
144+
133145
let float_opt field =
134146
match value field with
135147
| `Float x -> Some x

lib/mariadb.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module type S = sig
2929
type value =
3030
[ `Null
3131
| `Int of int
32+
| `Int64 of int64
3233
| `Float of float
3334
| `String of string
3435
| `Bytes of bytes
@@ -41,12 +42,14 @@ module type S = sig
4142
val can_be_null : t -> bool
4243

4344
val int : t -> int
45+
val int64 : t -> Int64.t
4446
val float : t -> float
4547
val string : t -> string
4648
val bytes : t -> bytes
4749
val time : t -> Time.t
4850

4951
val int_opt : t -> int option
52+
val int64_opt : t -> Int64.t option
5053
val float_opt : t -> float option
5154
val string_opt : t -> string option
5255
val bytes_opt : t -> bytes option

0 commit comments

Comments
 (0)