Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ function.
```ocaml
type value =
[ `Int of int
| `Int64 of Int64.t
| `UInt64 of Unsigned.UInt64.t
| `Float of float
| `String of string
| `Bytes of bytes
Expand All @@ -221,6 +223,8 @@ provided to extract the OCaml types directly from a field:

```ocaml
val int : Field.t -> int
val int64 : Field.t -> Int64.t
val uint64 : Field.t -> Unsigned.Int64.t
val float : Field.t -> float
val string : Field.t -> string
val bytes : Field.t -> bytes
Expand All @@ -234,6 +238,8 @@ For nullable fields, the following analogous functions are also provided:

```ocaml
val int_opt : Field.t -> int option
val int64_opt : Field.t -> Int64.t option
val uint64_opt : Field.t -> Unsigned.UInt64.t option
val float_opt : Field.t -> float option
val string_opt : Field.t -> string option
val bytes_opt : Field.t -> bytes option
Expand Down
2 changes: 2 additions & 0 deletions examples/async/nonblocking_async_example.ml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ let print_row row =
printf "%20s " name;
match M.Field.value field with
| `Int i -> printf "%d\n%!" i
| `Int64 i -> printf "%Ld\n%!" i
| `UInt64 i -> printf "%s\n%!" (Unsigned.UInt64.to_string i)
| `Float x -> printf "%f\n%!" x
| `String s -> printf "%s\n%!" s
| `Bytes b -> printf "%s\n%!" (Caml_bytes.to_string b)
Expand Down
2 changes: 2 additions & 0 deletions examples/blocking/blocking_example.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ let print_row row =
printf "%20s " name;
match M.Field.value field with
| `Int i -> printf "%d\n%!" i
| `Int64 i -> printf "%Ld\n%!" i
| `UInt64 i -> printf "%s\n%!" (Unsigned.UInt64.to_string i)
| `Float x -> printf "%f\n%!" x
| `String s -> printf "%s\n%!" s
| `Bytes b -> printf "%s\n%!" (Bytes.to_string b)
Expand Down
2 changes: 2 additions & 0 deletions examples/lwt/nonblocking_lwt_example.ml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ let print_row row =
Lwt_io.printf "%20s " name >>= fun () ->
match M.Field.value field with
| `Int i -> Lwt_io.printf "%d\n%!" i
| `Int64 i -> Lwt_io.printf "%Ld\n%!" i
| `UInt64 i -> Lwt_io.printf "%s\n%!" (Unsigned.UInt64.to_string i)
| `Float x -> Lwt_io.printf "%f\n%!" x
| `String s -> Lwt_io.printf "%s\n%!" s
| `Bytes b -> Lwt_io.printf "%s\n%!" (Bytes.to_string b)
Expand Down
2 changes: 2 additions & 0 deletions examples/select/nonblocking_select_example.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ let print_row row =
printf "%20s " name;
match M.Field.value field with
| `Int i -> printf "%d\n%!" i
| `Int64 i -> printf "%Ld\n%!" i
| `UInt64 i -> printf "%s\n%!" (Unsigned.UInt64.to_string i)
| `Float x -> printf "%f\n%!" x
| `String s -> printf "%s\n%!" s
| `Bytes b -> printf "%s\n%!" (Bytes.to_string b)
Expand Down
9 changes: 9 additions & 0 deletions lib/bind.ml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ let int ?(unsigned = false) b param ~at =
~unsigned:(if unsigned then yes else no)
~at

let int64 ?(unsigned = false) b param ~at =
let p = allocate int64_t param in
bind b
~buffer:(coerce (ptr int64_t) (ptr void) p)
~size:(sizeof int64_t)
~mysql_type:T.Type.long_long
~unsigned:(if unsigned then yes else no)
~at

let float b param ~at =
let p = allocate float param in
bind b
Expand Down
2 changes: 2 additions & 0 deletions lib/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ module Stmt = struct
match arg with
| `Null -> Bind.null b ~at
| `Int i -> Bind.int b i ~at
| `Int64 i -> Bind.int64 b i ~at
| `UInt64 i -> Bind.int64 ~unsigned:true b (Unsigned.UInt64.to_int64 i) ~at
| `Float x -> Bind.float b x ~at
| `String s -> Bind.string b s ~at
| `Bytes s -> Bind.blob b s ~at
Expand Down
34 changes: 32 additions & 2 deletions lib/field.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module T = Ffi_generated.Types
type value =
[ `Null
| `Int of int
| `Int64 of Int64.t
| `UInt64 of Unsigned.UInt64.t
| `Float of float
| `String of string
| `Bytes of bytes
Expand Down Expand Up @@ -81,8 +83,8 @@ let convert field typ unsigned =
| `Short, false -> `Int (UInt.to_int (cast_to uint field))
| (`Int24 | `Long), true -> `Int (UInt32.to_int (cast_to uint32_t field))
| (`Int24 | `Long), false -> `Int (Int32.to_int (cast_to int32_t field))
| `Long_long, true -> `Int (UInt64.to_int (cast_to uint64_t field))
| `Long_long, false -> `Int (Int64.to_int (cast_to int64_t field))
| `Long_long, true -> `UInt64 (cast_to uint64_t field)
| `Long_long, false -> `Int64 (cast_to int64_t field)
| `Float, _ -> `Float (cast_to float field)
| `Double, _ -> `Float (cast_to double field)
| #to_string, _ -> `String (Bytes.to_string (to_bytes field))
Expand All @@ -102,8 +104,21 @@ let err field ~info =
let int field =
match value field with
| `Int i -> i
| `Int64 i -> Int64.to_int i
| `UInt64 i -> Unsigned.UInt64.to_int i
| _ -> err field ~info:"an integer"

let int64 field =
match value field with
| `Int i -> Int64.of_int i
| `Int64 i -> i
| _ -> err field ~info:"a 64-bit integer"

let uint64 field =
match value field with
| `UInt64 i -> i
| _ -> err field ~info:"a 64-bit unsigned integer"

let float field =
match value field with
| `Float x -> x
Expand All @@ -127,9 +142,24 @@ let time field =
let int_opt field =
match value field with
| `Int i -> Some i
| `Int64 i -> Some (Int64.to_int i)
| `UInt64 i -> Some (Unsigned.UInt64.to_int i)
| `Null -> None
| _ -> err field ~info:"a nullable integer"

let int64_opt field =
match value field with
| `Int i -> Some (Int64.of_int i)
| `Int64 i -> Some i
| `Null -> None
| _ -> err field ~info:"a nullable 64-bit integer"

let uint64_opt field =
match value field with
| `UInt64 i -> Some i
| `Null -> None
| _ -> err field ~info:"a nullable 64-bit unsigned integer"

let float_opt field =
match value field with
| `Float x -> Some x
Expand Down
6 changes: 6 additions & 0 deletions lib/mariadb.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ module type S = sig
type value =
[ `Null
| `Int of int
| `Int64 of Int64.t
| `UInt64 of Unsigned.UInt64.t
| `Float of float
| `String of string
| `Bytes of bytes
Expand All @@ -41,12 +43,16 @@ module type S = sig
val can_be_null : t -> bool

val int : t -> int
val int64 : t -> Int64.t
val uint64 : t -> Unsigned.UInt64.t
val float : t -> float
val string : t -> string
val bytes : t -> bytes
val time : t -> Time.t

val int_opt : t -> int option
val int64_opt : t -> Int64.t option
val uint64_opt : t -> Unsigned.UInt64.t option
val float_opt : t -> float option
val string_opt : t -> string option
val bytes_opt : t -> bytes option
Expand Down
12 changes: 12 additions & 0 deletions lib/mariadb.mli
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ module type S = sig
type value =
[ `Null
| `Int of int
| `Int64 of Int64.t
| `UInt64 of Unsigned.UInt64.t
| `Float of float
| `String of string
| `Bytes of bytes
Expand Down Expand Up @@ -81,12 +83,16 @@ module type S = sig
*)

val int : t -> int
val int64 : t -> Int64.t
val uint64 : t -> Unsigned.UInt64.t
val float : t -> float
val string : t -> string
val bytes : t -> bytes
val time : t -> Time.t

val int_opt : t -> int option
val int64_opt : t -> Int64.t option
val uint64_opt : t -> Unsigned.UInt64.t option
val float_opt : t -> float option
val string_opt : t -> string option
val bytes_opt : t -> bytes option
Expand Down Expand Up @@ -385,6 +391,8 @@ module Nonblocking : sig
type value =
[ `Null
| `Int of int
| `Int64 of Int64.t
| `UInt64 of Unsigned.UInt64.t
| `Float of float
| `String of string
| `Bytes of bytes
Expand All @@ -397,12 +405,16 @@ module Nonblocking : sig
val can_be_null : t -> bool

val int : t -> int
val int64 : t -> Int64.t
val uint64 : t -> Unsigned.UInt64.t
val float : t -> float
val string : t -> string
val bytes : t -> bytes
val time : t -> Time.t

val int_opt : t -> int option
val int64_opt : t -> Int64.t option
val uint64_opt : t -> Unsigned.UInt64.t option
val float_opt : t -> float option
val string_opt : t -> string option
val bytes_opt : t -> bytes option
Expand Down
6 changes: 6 additions & 0 deletions lib/nonblocking.ml
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ module type S = sig
type value =
[ `Null
| `Int of int
| `Int64 of Int64.t
| `UInt64 of Unsigned.UInt64.t
| `Float of float
| `String of string
| `Bytes of bytes
Expand All @@ -410,12 +412,16 @@ module type S = sig
val can_be_null : t -> bool

val int : t -> int
val int64 : t -> Int64.t
val uint64 : t -> Unsigned.UInt64.t
val float : t -> float
val string : t -> string
val bytes : t -> bytes
val time : t -> Time.t

val int_opt : t -> int option
val int64_opt : t -> Int64.t option
val uint64_opt : t -> Unsigned.UInt64.t option
val float_opt : t -> float option
val string_opt : t -> string option
val bytes_opt : t -> bytes option
Expand Down
9 changes: 9 additions & 0 deletions tests/nonblocking/nonblocking_testsuite.ml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ struct
let string_of_value = function
| `Null -> "NULL"
| `Int i -> sprintf "(%d : int)" i
| `Int64 i -> sprintf "(%Ld : int64)" i
| `UInt64 i -> sprintf "(%s : uint64)" (Unsigned.UInt64.to_string i)
| `Float x -> sprintf "(%.8g : float)" x
| `String s -> sprintf "(%S : string)" s
| `Bytes s -> sprintf "(%S : bytes)" (Bytes.to_string s)
Expand All @@ -106,7 +108,14 @@ struct
| `Null, _ | _, `Null -> false
| `Int i, `Int i' -> i = i'
| `Int i, `Float x | `Float x, `Int i -> float_of_int i = x
| `Int64 i, `Int x | `Int x, `Int64 i -> Int64.(equal i (of_int x))
| `Int64 i, `Float x | `Float x, `Int64 i -> Int64.to_float i = x
| `UInt64 i, `Int x | `Int x, `UInt64 i -> Unsigned.UInt64.(equal i (of_int x))
| `UInt64 i, `Float x | `Float x, `UInt64 i -> Int64.to_float (Unsigned.UInt64.to_int64 i) = x
| `UInt64 i, `Int64 x | `Int64 x, `UInt64 i -> Int64.equal (Unsigned.UInt64.to_int64 i) x
| `Int _, _ | _, `Int _ -> false
| `Int64 _, _ | _, `Int64 _ -> false
| `UInt64 _, _ | _, `UInt64 _ -> false
Comment on lines 112 to 120
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks you like are missing `Int64 _, `Int64 _ and `UInt64 _, `UIint64 _ cases here.

| `Float x, `Float x' -> equal_float x x'
| `Float _, _ | _, `Float _ -> false
| `String s, `String s' -> s = s'
Expand Down