Skip to content

Commit 572396b

Browse files
authored
Merge pull request #62 from paurkedal/server-properties
Server properties
2 parents c7e514a + a8c9d96 commit 572396b

File tree

7 files changed

+82
-0
lines changed

7 files changed

+82
-0
lines changed

bindings/ffi_bindings.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,18 @@ module Functions (F : Ctypes.FOREIGN) = struct
308308
let mysql_ping = foreign "mysql_ping"
309309
(mysql @-> returning int)
310310

311+
let mysql_get_server_info = foreign "mysql_get_server_info"
312+
(mysql @-> returning string)
313+
314+
let mysql_get_server_version = foreign "mysql_get_server_version"
315+
(mysql @-> returning ulong)
316+
317+
let mysql_get_host_info = foreign "mysql_get_host_info"
318+
(mysql @-> returning string)
319+
320+
let mysql_get_proto_info = foreign "mysql_get_proto_info"
321+
(mysql @-> returning uint)
322+
311323
let mysql_stmt_prepare = foreign "mysql_stmt_prepare"
312324
(stmt @-> ptr char @-> ulong @-> returning int)
313325

lib/blocking.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ let change_user mariadb user pass db =
121121
wrap_unit mariadb
122122
(B.mysql_change_user mariadb.Common.raw user pass mariadb.Common.db)
123123

124+
let get_server_info = Common.get_server_info
125+
let get_server_version = Common.get_server_version
126+
let get_host_info = Common.get_host_info
127+
let get_proto_info = Common.get_proto_info
128+
124129
let set_client_option =
125130
Common.set_client_option
126131

lib/common.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,18 @@ let int_of_flag = function
199199
let int_of_flags =
200200
List.fold_left (fun acc flag -> Int32.logor acc (int_of_flag flag)) 0l
201201

202+
let get_server_info mariadb =
203+
B.mysql_get_server_info mariadb.raw
204+
205+
let get_server_version mariadb =
206+
Unsigned.ULong.to_int (B.mysql_get_server_version mariadb.raw)
207+
208+
let get_host_info mariadb =
209+
B.mysql_get_host_info mariadb.raw
210+
211+
let get_proto_info mariadb =
212+
Unsigned.UInt.to_int (B.mysql_get_proto_info mariadb.raw)
213+
202214
module Res = struct
203215
open Ctypes
204216

lib/mariadb.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ module type S = sig
156156
val set_character_set : t -> string -> unit result
157157
val select_db : t -> string -> unit result
158158
val change_user : t -> string -> string -> string option -> unit result
159+
val get_server_info : t -> string
160+
val get_server_version : t -> int
161+
val get_host_info : t -> string
162+
val get_proto_info : t -> int
159163
val set_client_option : t -> client_option -> unit
160164
val set_server_option : t -> server_option -> unit result
161165
val ping : t -> unit result

lib/mariadb.mli

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,18 @@ module type S = sig
255255
[user] with password [password] and optionally change to database
256256
[db]. *)
257257

258+
val get_server_info : t -> string
259+
(** The version version as a string. *)
260+
261+
val get_server_version : t -> int
262+
(** The server version encoded as [major * 10000 + minor * 100 + patch]. *)
263+
264+
val get_host_info : t -> string
265+
(** A string representing the server host name and the connection type. *)
266+
267+
val get_proto_info : t -> int
268+
(** The protocol version used for the connection. *)
269+
258270
val set_client_option : t -> client_option -> unit
259271
(** Sets the given client option on the connection. *)
260272

@@ -514,6 +526,10 @@ module Nonblocking : sig
514526
val select_db : t -> string -> unit result future
515527
val change_user : t -> string -> string -> string option
516528
-> unit result future
529+
val get_server_info : t -> string
530+
val get_server_version : t -> int
531+
val get_host_info : t -> string
532+
val get_proto_info : t -> int
517533
val set_client_option : t -> client_option -> unit
518534
val set_server_option : t -> server_option -> unit result future
519535
val ping : t -> unit result future

lib/nonblocking.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,10 @@ module type S = sig
525525
val set_character_set : t -> string -> unit result future
526526
val select_db : t -> string -> unit result future
527527
val change_user : t -> string -> string -> string option -> unit result future
528+
val get_server_info : t -> string
529+
val get_server_version : t -> int
530+
val get_host_info : t -> string
531+
val get_proto_info : t -> int
528532
val set_client_option : t -> client_option -> unit
529533
val set_server_option : t -> server_option -> unit result future
530534
val ping : t -> unit result future
@@ -712,6 +716,14 @@ module Make (W : Wait) : S with type 'a future = 'a W.IO.future = struct
712716
m.Common.db <- char_ptr_opt_buffer_of_string db;
713717
nonblocking m (change_user m)
714718

719+
let get_server_info = Common.get_server_info
720+
721+
let get_server_version = Common.get_server_version
722+
723+
let get_host_info = Common.get_host_info
724+
725+
let get_proto_info = Common.get_proto_info
726+
715727
let set_client_option = Common.set_client_option
716728

717729
let set_server_option m opt = nonblocking m (set_server_option m opt)

tests/nonblocking/nonblocking_testsuite.ml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ struct
1616

1717
let env var def = try Sys.getenv var with Not_found -> def
1818

19+
let die_f ppf = ksprintf (fun msg -> eprintf "%s\n%!" msg; exit 2) ppf
20+
1921
let or_die where = function
2022
| Ok r -> return r
2123
| Error (i, e) -> eprintf "%s: (%d) %s\n%!" where i e; exit 2
@@ -134,6 +136,24 @@ struct
134136
| None -> failwith "expecting one row, no rows returned"
135137
| Some a -> a)
136138

139+
let test_server_properties () =
140+
connect () >>= or_die "connect" >>= fun dbh ->
141+
let v = M.get_server_version dbh in
142+
assert (v >= 10000 && v < 10000000); (* 1 <= major_version < 1000 *)
143+
let info = M.get_server_info dbh in
144+
let info' = sprintf "%d.%d.%d" (v / 10000) (v / 100 mod 100) (v mod 100) in
145+
assert (String.starts_with ~prefix:info' info);
146+
let host = M.get_host_info dbh in
147+
assert (String.length host < 1024);
148+
for i = 0 to String.length host - 1 do
149+
match host.[i] with
150+
| '\x20'..'\x7f' -> ()
151+
| _ -> die_f "result from get_host_info looks suspicious: %S" host
152+
done;
153+
let proto = M.get_proto_info dbh in
154+
assert (proto >= 0 && proto < 10000); (* it's 10 for MariaDB 10.11.8 *)
155+
return ()
156+
137157
let test_insert_id () =
138158
connect () >>= or_die "connect" >>= fun dbh ->
139159
M.prepare dbh
@@ -323,6 +343,7 @@ struct
323343
(test_integer, test_bigint)
324344

325345
let main () =
346+
test_server_properties () >>= fun () ->
326347
test_insert_id () >>= fun () ->
327348
test_txn () >>= fun () ->
328349
test_many_select () >>= fun () ->

0 commit comments

Comments
 (0)