File tree Expand file tree Collapse file tree 7 files changed +64
-0
lines changed Expand file tree Collapse file tree 7 files changed +64
-0
lines changed Original file line number Diff line number Diff line change @@ -320,6 +320,9 @@ module Functions (F : Ctypes.FOREIGN) = struct
320
320
let mysql_get_proto_info = foreign " mysql_get_proto_info"
321
321
(mysql @-> returning uint)
322
322
323
+ let mysql_sqlstate = foreign " mysql_sqlstate"
324
+ (mysql @-> returning string )
325
+
323
326
let mysql_stmt_prepare = foreign " mysql_stmt_prepare"
324
327
(stmt @-> ptr char @-> ulong @-> returning int )
325
328
@@ -332,6 +335,9 @@ module Functions (F : Ctypes.FOREIGN) = struct
332
335
let mysql_stmt_fetch = foreign " mysql_stmt_fetch"
333
336
(stmt @-> returning int )
334
337
338
+ let mysql_stmt_sqlstate = foreign " mysql_stmt_sqlstate"
339
+ (stmt @-> returning string )
340
+
335
341
let mysql_stmt_close = foreign " mysql_stmt_close"
336
342
(stmt @-> returning my_bool)
337
343
Original file line number Diff line number Diff line change @@ -47,6 +47,37 @@ let stream res =
47
47
| Error e -> raise (F. E e) in
48
48
next
49
49
50
+ let test_sqlstate mariadb =
51
+ assert (M. sqlstate mariadb = " 00000" );
52
+ (match M. prepare mariadb " SELECT * FROM inexistent_table" with
53
+ | Error _ -> assert (M. sqlstate mariadb <> " 00000" ) (* actually "42S02" *)
54
+ | Ok _ -> assert false );
55
+ begin
56
+ let stmt =
57
+ M. prepare mariadb
58
+ " CREATE TEMPORARY TABLE test_sqlstate (i integer PRIMARY KEY)"
59
+ |> or_die " prepare CREATE TABLE test_sqlstate"
60
+ in
61
+ let _ =
62
+ M.Stmt. execute stmt [||]
63
+ |> or_die " exec CREATE TABLE test_sqlstate"
64
+ in
65
+ M.Stmt. close stmt |> or_die " stmt close CREATE TABLE test_sqlstate"
66
+ end ;
67
+ for i = 0 to 1 do
68
+ let stmt =
69
+ M. prepare mariadb " INSERT INTO test_sqlstate VALUES (?)"
70
+ |> or_die " prepare in test_sqlstate"
71
+ in
72
+ (match M.Stmt. execute stmt [|`Int 1 |] with
73
+ | Error (_ , _ ) ->
74
+ assert (i = 1 );
75
+ assert (M.Stmt. sqlstate stmt <> " 00000" ) (* actually "23000" *)
76
+ | Ok _ -> assert (i = 0 ));
77
+
78
+ M.Stmt. close stmt |> or_die " stmt close in test_sqlstate"
79
+ done
80
+
50
81
let main () =
51
82
let mariadb = connect () |> or_die " connect" in
52
83
let query = env " OCAML_MARIADB_QUERY"
@@ -58,6 +89,7 @@ let main () =
58
89
let s = stream res in
59
90
Seq. iter print_row s;
60
91
M.Stmt. close stmt |> or_die " stmt close" ;
92
+ test_sqlstate mariadb;
61
93
M. close mariadb;
62
94
M. library_end () ;
63
95
printf " done\n %!"
Original file line number Diff line number Diff line change @@ -158,6 +158,8 @@ let prepare mariadb query =
158
158
let start_txn mariadb =
159
159
wrap_unit mariadb (B. mysql_real_query mariadb.Common. raw " START TRANSACTION" )
160
160
161
+ let sqlstate = Common. sqlstate
162
+
161
163
module Res = struct
162
164
type t = [`Blocking ] Common.Res .t
163
165
@@ -208,6 +210,8 @@ module Stmt = struct
208
210
else
209
211
Error (Common.Stmt. error stmt)
210
212
213
+ let sqlstate = Common.Stmt. sqlstate
214
+
211
215
let close stmt =
212
216
Common.Stmt. free_meta stmt;
213
217
let raw = stmt.Common.Stmt. raw in
Original file line number Diff line number Diff line change @@ -85,6 +85,9 @@ type error = int * string
85
85
let error mariadb =
86
86
(B. mysql_errno mariadb.raw, B. mysql_error mariadb.raw)
87
87
88
+ let sqlstate mariadb =
89
+ B. mysql_sqlstate mariadb.raw
90
+
88
91
let int_of_server_option = function
89
92
| Multi_statements true -> T.Server_options. multi_statements_on
90
93
| Multi_statements false -> T.Server_options. multi_statements_off
@@ -295,6 +298,9 @@ module Stmt = struct
295
298
let error stmt =
296
299
(B. mysql_stmt_errno stmt.raw, B. mysql_stmt_error stmt.raw)
297
300
301
+ let sqlstate stmt =
302
+ B. mysql_stmt_sqlstate stmt.raw
303
+
298
304
let fetch_field res i =
299
305
coerce (ptr void) (ptr T.Field. t) (B. mysql_fetch_field_direct res i)
300
306
Original file line number Diff line number Diff line change @@ -80,6 +80,7 @@ module type S = sig
80
80
81
81
val execute : t -> Field .value array -> Res .t result
82
82
val reset : t -> unit result
83
+ val sqlstate : t -> string
83
84
val close : t -> unit result
84
85
end
85
86
@@ -168,6 +169,7 @@ module type S = sig
168
169
val commit : t -> unit result
169
170
val rollback : t -> unit result
170
171
val prepare : t -> string -> Stmt .t result
172
+ val sqlstate : t -> string
171
173
end
172
174
173
175
module B = Binding_wrappers
Original file line number Diff line number Diff line change @@ -160,6 +160,12 @@ module type S = sig
160
160
were after [stmt] was prepared, and frees up any {!Res.t} produced by
161
161
[stmt]. *)
162
162
163
+ val sqlstate : t -> string
164
+ (* * [sqlstate stmt] is the SQLSTATE with MariaDB extensions indicating the
165
+ status of the previous execution of the statement. The string
166
+ ["00000"] is returned if no error occurred or if the statement has not
167
+ been executed. *)
168
+
163
169
val close : t -> unit result
164
170
(* * [close stmt] closes the prepapred statement [stmt] and frees any
165
171
allocated memory associated with it and its result. *)
@@ -294,6 +300,10 @@ module type S = sig
294
300
(* * [prepare mariadb query] creates a prepared statement for [query]. The
295
301
query may contain [?] as placeholders for parameters that can be bound
296
302
by calling [Stmt.execute]. *)
303
+
304
+ val sqlstate : t -> string
305
+ (* [sqlstate mariadb] is the SQLSTATE with MariaDB extensions of the last
306
+ * operation on [mariadb]. Returns ["00000"] if no error occurred. *)
297
307
end
298
308
299
309
(* * The module for blocking MariaDB API calls. It should be possible to call
Original file line number Diff line number Diff line change @@ -218,6 +218,8 @@ let prepare mariadb query =
218
218
`Ok (prepare_start mariadb stmt, prepare_cont mariadb stmt)
219
219
| None -> `Error (Common. error mariadb)
220
220
221
+ let sqlstate = Common. sqlstate
222
+
221
223
module Res = struct
222
224
type t = [`Nonblocking ] Common.Res .t
223
225
@@ -354,6 +356,8 @@ module Stmt = struct
354
356
355
357
let next_result_cont stmt status =
356
358
handle_next stmt (B. mysql_stmt_next_result_cont stmt.Common.Stmt. raw status)
359
+
360
+ let sqlstate = Common.Stmt. sqlstate
357
361
end
358
362
359
363
module type Wait = sig
You can’t perform that action at this time.
0 commit comments