Skip to content

Commit c442287

Browse files
authored
Merge pull request #59 from ahrefs/start_txn
add start_txn
2 parents 50c3907 + a85ebbd commit c442287

File tree

6 files changed

+41
-0
lines changed

6 files changed

+41
-0
lines changed

bindings/ffi_bindings.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,9 @@ module Bindings (F : Cstubs.FOREIGN) = struct
326326
let mysql_stmt_free_result = foreign "mysql_stmt_free_result"
327327
(stmt @-> returning my_bool)
328328

329+
let mysql_real_query = foreign "mysql_real_query"
330+
(mysql @-> ptr char @-> ulong @-> returning int)
331+
329332
(* Nonblocking API *)
330333

331334
let mysql_close_start = foreign "mysql_close_start"
@@ -447,4 +450,10 @@ module Bindings (F : Cstubs.FOREIGN) = struct
447450

448451
let mysql_stmt_next_result_cont = foreign "mysql_stmt_next_result_cont"
449452
(ptr int @-> stmt @-> int @-> returning int)
453+
454+
let mysql_real_query_start = foreign "mysql_real_query_start"
455+
(ptr int @-> mysql @-> ptr char @-> ulong @-> returning int)
456+
457+
let mysql_real_query_cont = foreign "mysql_real_query_cont"
458+
(ptr int @-> mysql @-> int @-> returning int)
450459
end

lib/binding_wrappers.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ let mysql_stmt_store_result stmt =
105105
let mysql_stmt_free_result stmt =
106106
B.mysql_stmt_free_result stmt = '\000'
107107

108+
let mysql_real_query mysql query =
109+
let len = Unsigned.ULong.of_int (String.length query) in
110+
let query = char_ptr_buffer_of_string query in
111+
B.mysql_real_query mysql query len = 0
112+
108113
(* Nonblocking API *)
109114

110115
let mysql_real_connect_start mysql host user pass db port socket flags =
@@ -220,3 +225,11 @@ let mysql_stmt_next_result_start stmt =
220225

221226
let mysql_stmt_next_result_cont stmt status =
222227
handle_int (fun err -> B.mysql_stmt_next_result_cont err stmt status)
228+
229+
let mysql_real_query_start mysql query =
230+
let len = Unsigned.ULong.of_int (String.length query) in
231+
let query = char_ptr_buffer_of_string query in
232+
handle_int (fun err -> B.mysql_real_query_start err mysql query len)
233+
234+
let mysql_real_query_cont mysql status =
235+
handle_int (fun err -> B.mysql_real_query_cont err mysql status)

lib/blocking.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ let prepare mariadb query =
150150
| Some raw -> build_stmt raw
151151
| None -> Error (2008, "out of memory")
152152

153+
let start_txn mariadb =
154+
wrap_unit mariadb (B.mysql_real_query mariadb.Common.raw "START TRANSACTION")
155+
153156
module Res = struct
154157
type t = [`Blocking] Common.Res.t
155158

lib/mariadb.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ module type S = sig
159159
val set_server_option : t -> server_option -> unit result
160160
val ping : t -> unit result
161161
val autocommit : t -> bool -> unit result
162+
val start_txn : t -> unit result
162163
val commit : t -> unit result
163164
val rollback : t -> unit result
164165
val prepare : t -> string -> Stmt.t result

lib/mariadb.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ module type S = sig
264264
val autocommit : t -> bool -> unit result
265265
(** Sets autocommit mode on or off. *)
266266

267+
val start_txn : t -> unit result
268+
267269
val commit : t -> unit result
268270
(** Commits the current transaction. *)
269271

@@ -510,6 +512,7 @@ module Nonblocking : sig
510512
val set_server_option : t -> server_option -> unit result future
511513
val ping : t -> unit result future
512514
val autocommit : t -> bool -> unit result future
515+
val start_txn : t -> unit result future
513516
val commit : t -> unit result future
514517
val rollback : t -> unit result future
515518
val prepare : t -> string -> Stmt.t result future

lib/nonblocking.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,15 @@ let rollback_cont mariadb status =
177177
let rollback mariadb =
178178
(rollback_start mariadb, rollback_cont mariadb)
179179

180+
let start_txn_start mariadb =
181+
handle_int mariadb (B.mysql_real_query_start mariadb.Common.raw "START TRANSACTION")
182+
183+
let start_txn_cont mariadb status =
184+
handle_int mariadb (B.mysql_real_query_cont mariadb.Common.raw status)
185+
186+
let start_txn mariadb =
187+
(start_txn_start mariadb, start_txn_cont mariadb)
188+
180189
let build_stmt mariadb raw =
181190
`Ok (Common.Stmt.init mariadb raw)
182191

@@ -516,6 +525,7 @@ module type S = sig
516525
val set_server_option : t -> server_option -> unit result future
517526
val ping : t -> unit result future
518527
val autocommit : t -> bool -> unit result future
528+
val start_txn : t -> unit result future
519529
val commit : t -> unit result future
520530
val rollback : t -> unit result future
521531
val prepare : t -> string -> Stmt.t result future
@@ -704,6 +714,8 @@ module Make (W : Wait) : S with type 'a future = 'a W.IO.future = struct
704714

705715
let autocommit m b = nonblocking m (autocommit m b)
706716

717+
let start_txn m = nonblocking m (start_txn m)
718+
707719
let commit m = nonblocking m (commit m)
708720

709721
let rollback m = nonblocking m (rollback m)

0 commit comments

Comments
 (0)