Skip to content

Commit e9f9ac7

Browse files
authored
Merge pull request #66 from paurkedal/64bit-integers-rebased
Support for 64bit integers (rebased)
2 parents 1993263 + 4c63a92 commit e9f9ac7

File tree

12 files changed

+92
-2
lines changed

12 files changed

+92
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ function.
205205
```ocaml
206206
type value =
207207
[ `Int of int
208+
| `Int64 of Int64.t
209+
| `UInt64 of Unsigned.UInt64.t
208210
| `Float of float
209211
| `String of string
210212
| `Bytes of bytes
@@ -221,6 +223,8 @@ provided to extract the OCaml types directly from a field:
221223

222224
```ocaml
223225
val int : Field.t -> int
226+
val int64 : Field.t -> Int64.t
227+
val uint64 : Field.t -> Unsigned.Int64.t
224228
val float : Field.t -> float
225229
val string : Field.t -> string
226230
val bytes : Field.t -> bytes
@@ -234,6 +238,8 @@ For nullable fields, the following analogous functions are also provided:
234238

235239
```ocaml
236240
val int_opt : Field.t -> int option
241+
val int64_opt : Field.t -> Int64.t option
242+
val uint64_opt : Field.t -> Unsigned.UInt64.t option
237243
val float_opt : Field.t -> float option
238244
val string_opt : Field.t -> string option
239245
val bytes_opt : Field.t -> bytes option

examples/async/nonblocking_async_example.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ 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
70+
| `UInt64 i -> printf "%s\n%!" (Unsigned.UInt64.to_string i)
6971
| `Float x -> printf "%f\n%!" x
7072
| `String s -> printf "%s\n%!" s
7173
| `Bytes b -> printf "%s\n%!" (Caml_bytes.to_string b)

examples/blocking/blocking_example.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ 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
21+
| `UInt64 i -> printf "%s\n%!" (Unsigned.UInt64.to_string i)
2022
| `Float x -> printf "%f\n%!" x
2123
| `String s -> printf "%s\n%!" s
2224
| `Bytes b -> printf "%s\n%!" (Bytes.to_string b)

examples/lwt/nonblocking_lwt_example.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ 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
56+
| `UInt64 i -> Lwt_io.printf "%s\n%!" (Unsigned.UInt64.to_string i)
5557
| `Float x -> Lwt_io.printf "%f\n%!" x
5658
| `String s -> Lwt_io.printf "%s\n%!" s
5759
| `Bytes b -> Lwt_io.printf "%s\n%!" (Bytes.to_string b)

examples/select/nonblocking_select_example.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ 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
59+
| `UInt64 i -> printf "%s\n%!" (Unsigned.UInt64.to_string i)
5860
| `Float x -> printf "%f\n%!" x
5961
| `String s -> printf "%s\n%!" s
6062
| `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
@@ -126,6 +126,15 @@ let int ?(unsigned = false) b param ~at =
126126
~unsigned:(if unsigned then yes else no)
127127
~at
128128

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

lib/common.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ module Stmt = struct
335335
match arg with
336336
| `Null -> Bind.null b ~at
337337
| `Int i -> Bind.int b i ~at
338+
| `Int64 i -> Bind.int64 b i ~at
339+
| `UInt64 i -> Bind.int64 ~unsigned:true b (Unsigned.UInt64.to_int64 i) ~at
338340
| `Float x -> Bind.float b x ~at
339341
| `String s -> Bind.string b s ~at
340342
| `Bytes s -> Bind.blob b s ~at

lib/field.ml

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ module T = Ffi_generated.Types
55
type value =
66
[ `Null
77
| `Int of int
8+
| `Int64 of Int64.t
9+
| `UInt64 of Unsigned.UInt64.t
810
| `Float of float
911
| `String of string
1012
| `Bytes of bytes
@@ -84,8 +86,8 @@ let convert field typ unsigned =
8486
| `Short, false -> `Int (UInt.to_int (cast_to uint field))
8587
| (`Int24 | `Long), true -> `Int (UInt32.to_int (cast_to uint32_t field))
8688
| (`Int24 | `Long), false -> `Int (Int32.to_int (cast_to int32_t field))
87-
| `Long_long, true -> `Int (UInt64.to_int (cast_to uint64_t field))
88-
| `Long_long, false -> `Int (Int64.to_int (cast_to int64_t field))
89+
| `Long_long, true -> `UInt64 (cast_to uint64_t field)
90+
| `Long_long, false -> `Int64 (cast_to int64_t field)
8991
| `Float, _ -> `Float (cast_to float field)
9092
| `Double, _ -> `Float (cast_to double field)
9193
| #to_string, _ -> `String (Bytes.to_string (to_bytes field))
@@ -105,8 +107,21 @@ let err field ~info =
105107
let int field =
106108
match value field with
107109
| `Int i -> i
110+
| `Int64 i -> Int64.to_int i
111+
| `UInt64 i -> Unsigned.UInt64.to_int i
108112
| _ -> err field ~info:"an integer"
109113

114+
let int64 field =
115+
match value field with
116+
| `Int i -> Int64.of_int i
117+
| `Int64 i -> i
118+
| _ -> err field ~info:"a 64-bit integer"
119+
120+
let uint64 field =
121+
match value field with
122+
| `UInt64 i -> i
123+
| _ -> err field ~info:"a 64-bit unsigned integer"
124+
110125
let float field =
111126
match value field with
112127
| `Float x -> x
@@ -130,9 +145,24 @@ let time field =
130145
let int_opt field =
131146
match value field with
132147
| `Int i -> Some i
148+
| `Int64 i -> Some (Int64.to_int i)
149+
| `UInt64 i -> Some (Unsigned.UInt64.to_int i)
133150
| `Null -> None
134151
| _ -> err field ~info:"a nullable integer"
135152

153+
let int64_opt field =
154+
match value field with
155+
| `Int i -> Some (Int64.of_int i)
156+
| `Int64 i -> Some i
157+
| `Null -> None
158+
| _ -> err field ~info:"a nullable 64-bit integer"
159+
160+
let uint64_opt field =
161+
match value field with
162+
| `UInt64 i -> Some i
163+
| `Null -> None
164+
| _ -> err field ~info:"a nullable 64-bit unsigned integer"
165+
136166
let float_opt field =
137167
match value field with
138168
| `Float x -> Some x

lib/mariadb.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ module type S = sig
2929
type value =
3030
[ `Null
3131
| `Int of int
32+
| `Int64 of Int64.t
33+
| `UInt64 of Unsigned.UInt64.t
3234
| `Float of float
3335
| `String of string
3436
| `Bytes of bytes
@@ -41,12 +43,16 @@ module type S = sig
4143
val can_be_null : t -> bool
4244

4345
val int : t -> int
46+
val int64 : t -> Int64.t
47+
val uint64 : t -> Unsigned.UInt64.t
4448
val float : t -> float
4549
val string : t -> string
4650
val bytes : t -> bytes
4751
val time : t -> Time.t
4852

4953
val int_opt : t -> int option
54+
val int64_opt : t -> Int64.t option
55+
val uint64_opt : t -> Unsigned.UInt64.t option
5056
val float_opt : t -> float option
5157
val string_opt : t -> string option
5258
val bytes_opt : t -> bytes option

lib/mariadb.mli

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ module type S = sig
5151
type value =
5252
[ `Null
5353
| `Int of int
54+
| `Int64 of Int64.t
55+
| `UInt64 of Unsigned.UInt64.t
5456
| `Float of float
5557
| `String of string
5658
| `Bytes of bytes
@@ -81,12 +83,16 @@ module type S = sig
8183
*)
8284

8385
val int : t -> int
86+
val int64 : t -> Int64.t
87+
val uint64 : t -> Unsigned.UInt64.t
8488
val float : t -> float
8589
val string : t -> string
8690
val bytes : t -> bytes
8791
val time : t -> Time.t
8892

8993
val int_opt : t -> int option
94+
val int64_opt : t -> Int64.t option
95+
val uint64_opt : t -> Unsigned.UInt64.t option
9096
val float_opt : t -> float option
9197
val string_opt : t -> string option
9298
val bytes_opt : t -> bytes option
@@ -397,6 +403,8 @@ module Nonblocking : sig
397403
type value =
398404
[ `Null
399405
| `Int of int
406+
| `Int64 of Int64.t
407+
| `UInt64 of Unsigned.UInt64.t
400408
| `Float of float
401409
| `String of string
402410
| `Bytes of bytes
@@ -409,12 +417,16 @@ module Nonblocking : sig
409417
val can_be_null : t -> bool
410418

411419
val int : t -> int
420+
val int64 : t -> Int64.t
421+
val uint64 : t -> Unsigned.UInt64.t
412422
val float : t -> float
413423
val string : t -> string
414424
val bytes : t -> bytes
415425
val time : t -> Time.t
416426

417427
val int_opt : t -> int option
428+
val int64_opt : t -> Int64.t option
429+
val uint64_opt : t -> Unsigned.UInt64.t option
418430
val float_opt : t -> float option
419431
val string_opt : t -> string option
420432
val bytes_opt : t -> bytes option

0 commit comments

Comments
 (0)