From 9708ba26ad76cc0dd040d33d7bc58cfb8acc0b56 Mon Sep 17 00:00:00 2001 From: Guillaume Huysmans Date: Mon, 10 Jul 2023 20:17:13 +0200 Subject: [PATCH 1/2] Exhibit incorrect binding of negative int (#31) --- examples/blocking/blocking_example.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/blocking/blocking_example.ml b/examples/blocking/blocking_example.ml index c871b8f..81de5dd 100644 --- a/examples/blocking/blocking_example.ml +++ b/examples/blocking/blocking_example.ml @@ -50,9 +50,9 @@ let stream res = let main () = let mariadb = connect () |> or_die "connect" in let query = env "OCAML_MARIADB_QUERY" - "SELECT * FROM mysql.user WHERE User LIKE ?" in + "SELECT * FROM mysql.user WHERE User LIKE ? AND ? < 0" in let stmt = M.prepare mariadb query |> or_die "prepare" in - let res = M.Stmt.execute stmt [| `String "r%" |] |> or_die "exec" in + let res = M.Stmt.execute stmt [| `String "r%"; `Int (-1) |] |> or_die "exec" in assert (M.Res.affected_rows res = M.Res.num_rows res); printf "#rows: %d\n%!" (M.Res.num_rows res); let s = stream res in From 81e2a49121b92c1c3620627ae6817bd419d2cd5d Mon Sep 17 00:00:00 2001 From: Guillaume Huysmans Date: Mon, 10 Jul 2023 21:05:32 +0200 Subject: [PATCH 2/2] Fix Bind.int: use MYSQL_TYPE_LONG (fix #31) According to the manual[1], C ints are tagged with MYSQL_TYPE_LONG. Using MYSQL_TYPE_LONGLONG, the buffer got padded with zeros. While it worked for positive ints, we got the unsigned interpretation of negative ints, e.g. -1 became 4294967295 (0xFFFFFFFF00000000, LE). [1]: https://dev.mysql.com/doc/c-api/5.7/en/c-api-prepared-statement-type-codes.html --- lib/bind.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/bind.ml b/lib/bind.ml index 32b67fa..93c1f43 100644 --- a/lib/bind.ml +++ b/lib/bind.ml @@ -120,7 +120,7 @@ let int ?(unsigned = false) b param ~at = bind b ~buffer:(coerce (ptr int) (ptr void) p) ~size:(sizeof int) - ~mysql_type:T.Type.long_long + ~mysql_type:T.Type.long ~unsigned:(if unsigned then yes else no) ~at