File tree 7 files changed +64
-0
lines changed
7 files changed +64
-0
lines changed Original file line number Diff line number Diff line change @@ -305,6 +305,9 @@ module Bindings (F : Cstubs.FOREIGN) = struct
305
305
let mysql_ping = foreign " mysql_ping"
306
306
(mysql @-> returning int )
307
307
308
+ let mysql_sqlstate = foreign " mysql_sqlstate"
309
+ (mysql @-> returning string )
310
+
308
311
let mysql_stmt_prepare = foreign " mysql_stmt_prepare"
309
312
(stmt @-> ptr char @-> ulong @-> returning int )
310
313
@@ -317,6 +320,9 @@ module Bindings (F : Cstubs.FOREIGN) = struct
317
320
let mysql_stmt_fetch = foreign " mysql_stmt_fetch"
318
321
(stmt @-> returning int )
319
322
323
+ let mysql_stmt_sqlstate = foreign " mysql_stmt_sqlstate"
324
+ (stmt @-> returning string )
325
+
320
326
let mysql_stmt_close = foreign " mysql_stmt_close"
321
327
(stmt @-> returning my_bool)
322
328
Original file line number Diff line number Diff line change @@ -48,6 +48,37 @@ let stream res =
48
48
try Ok (Stream. from next)
49
49
with F. E e -> Error e
50
50
51
+ let test_sqlstate mariadb =
52
+ assert (M. sqlstate mariadb = " 00000" );
53
+ (match M. prepare mariadb " SELECT * FROM inexistent_table" with
54
+ | Error _ -> assert (M. sqlstate mariadb <> " 00000" ) (* actually "42S02" *)
55
+ | Ok _ -> assert false );
56
+ begin
57
+ let stmt =
58
+ M. prepare mariadb
59
+ " CREATE TEMPORARY TABLE test_sqlstate (i integer PRIMARY KEY)"
60
+ |> or_die " prepare CREATE TABLE test_sqlstate"
61
+ in
62
+ let _ =
63
+ M.Stmt. execute stmt [||]
64
+ |> or_die " exec CREATE TABLE test_sqlstate"
65
+ in
66
+ M.Stmt. close stmt |> or_die " stmt close CREATE TABLE test_sqlstate"
67
+ end ;
68
+ for i = 0 to 1 do
69
+ let stmt =
70
+ M. prepare mariadb " INSERT INTO test_sqlstate VALUES (?)"
71
+ |> or_die " prepare in test_sqlstate"
72
+ in
73
+ (match M.Stmt. execute stmt [|`Int 1 |] with
74
+ | Error (_ , msg ) ->
75
+ assert (i = 1 );
76
+ assert (M.Stmt. sqlstate stmt <> " 00000" ) (* actually "23000" *)
77
+ | Ok _ -> assert (i = 0 ));
78
+
79
+ M.Stmt. close stmt |> or_die " stmt close in test_sqlstate"
80
+ done
81
+
51
82
let main () =
52
83
let mariadb = connect () |> or_die " connect" in
53
84
let query = env " OCAML_MARIADB_QUERY"
@@ -59,6 +90,7 @@ let main () =
59
90
let s = stream res |> or_die " stream" in
60
91
Stream. iter print_row s;
61
92
M.Stmt. close stmt |> or_die " stmt close" ;
93
+ test_sqlstate mariadb;
62
94
M. close mariadb;
63
95
M. library_end () ;
64
96
printf " done\n %!"
Original file line number Diff line number Diff line change @@ -152,6 +152,8 @@ let prepare mariadb query =
152
152
| Some raw -> build_stmt raw
153
153
| None -> Error (2008 , " out of memory" )
154
154
155
+ let sqlstate = Common. sqlstate
156
+
155
157
module Res = struct
156
158
type t = [`Blocking ] Common.Res .t
157
159
@@ -198,6 +200,8 @@ module Stmt = struct
198
200
else
199
201
Error (Common.Stmt. error stmt)
200
202
203
+ let sqlstate = Common.Stmt. sqlstate
204
+
201
205
let close stmt =
202
206
let raw = stmt.Common.Stmt. raw in
203
207
if B. mysql_stmt_free_result raw && B. mysql_stmt_close raw then
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
@@ -280,6 +283,9 @@ module Stmt = struct
280
283
let error stmt =
281
284
(B. mysql_stmt_errno stmt.raw, B. mysql_stmt_error stmt.raw)
282
285
286
+ let sqlstate stmt =
287
+ B. mysql_stmt_sqlstate stmt.raw
288
+
283
289
let fetch_field res i =
284
290
coerce (ptr void) (ptr T.Field. t) (B. mysql_fetch_field_direct res i)
285
291
Original file line number Diff line number Diff line change @@ -79,6 +79,7 @@ module type S = sig
79
79
80
80
val execute : t -> Field .value array -> Res .t result
81
81
val reset : t -> unit result
82
+ val sqlstate : t -> string
82
83
val close : t -> unit result
83
84
end
84
85
@@ -162,6 +163,7 @@ module type S = sig
162
163
val commit : t -> unit result
163
164
val rollback : t -> unit result
164
165
val prepare : t -> string -> Stmt .t result
166
+ val sqlstate : t -> string
165
167
end
166
168
167
169
module B = Binding_wrappers
Original file line number Diff line number Diff line change @@ -154,6 +154,12 @@ module type S = sig
154
154
were after [stmt] was prepared, and frees up any {!Res.t} produced by
155
155
[stmt]. *)
156
156
157
+ val sqlstate : t -> string
158
+ (* * [sqlstate stmt] is the SQLSTATE with MariaDB extensions indicating the
159
+ status of the previous execution of the statement. The string
160
+ ["00000"] is returned if no error occurred or if the statement has not
161
+ been executed. *)
162
+
157
163
val close : t -> unit result
158
164
(* * [close stmt] closes the prepapred statement [stmt] and frees
159
165
any allocated memory associated with it and its result. *)
@@ -275,6 +281,10 @@ module type S = sig
275
281
(* * [prepare mariadb query] creates a prepared statement for [query].
276
282
The query may contain [?] as placeholders for parameters that
277
283
can be bound by calling [Stmt.execute]. *)
284
+
285
+ val sqlstate : t -> string
286
+ (* [sqlstate mariadb] is the SQLSTATE with MariaDB extensions of the last
287
+ * operation on [mariadb]. Returns ["00000"] if no error occurred. *)
278
288
end
279
289
280
290
(* * The module for blocking MariaDB API calls. It should be possible to call
Original file line number Diff line number Diff line change @@ -211,6 +211,8 @@ let prepare mariadb query =
211
211
`Ok (prepare_start mariadb stmt query, prepare_cont mariadb stmt)
212
212
| None -> `Error (Common. error mariadb)
213
213
214
+ let sqlstate = Common. sqlstate
215
+
214
216
module Res = struct
215
217
type t = [`Nonblocking ] Common.Res .t
216
218
@@ -344,6 +346,8 @@ module Stmt = struct
344
346
345
347
let next_result_cont stmt status =
346
348
handle_next stmt (B. mysql_stmt_next_result_cont stmt.Common.Stmt. raw status)
349
+
350
+ let sqlstate = Common.Stmt. sqlstate
347
351
end
348
352
349
353
module type Wait = sig
You can’t perform that action at this time.
0 commit comments