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 @@ -308,6 +308,9 @@ module Functions (F : Ctypes.FOREIGN) = struct
308
308
let mysql_ping = foreign " mysql_ping"
309
309
(mysql @-> returning int )
310
310
311
+ let mysql_sqlstate = foreign " mysql_sqlstate"
312
+ (mysql @-> returning string )
313
+
311
314
let mysql_stmt_prepare = foreign " mysql_stmt_prepare"
312
315
(stmt @-> ptr char @-> ulong @-> returning int )
313
316
@@ -320,6 +323,9 @@ module Functions (F : Ctypes.FOREIGN) = struct
320
323
let mysql_stmt_fetch = foreign " mysql_stmt_fetch"
321
324
(stmt @-> returning int )
322
325
326
+ let mysql_stmt_sqlstate = foreign " mysql_stmt_sqlstate"
327
+ (stmt @-> returning string )
328
+
323
329
let mysql_stmt_close = foreign " mysql_stmt_close"
324
330
(stmt @-> returning my_bool)
325
331
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 @@ -153,6 +153,8 @@ let prepare mariadb query =
153
153
let start_txn mariadb =
154
154
wrap_unit mariadb (B. mysql_real_query mariadb.Common. raw " START TRANSACTION" )
155
155
156
+ let sqlstate = Common. sqlstate
157
+
156
158
module Res = struct
157
159
type t = [`Blocking ] Common.Res .t
158
160
@@ -203,6 +205,8 @@ module Stmt = struct
203
205
else
204
206
Error (Common.Stmt. error stmt)
205
207
208
+ let sqlstate = Common.Stmt. sqlstate
209
+
206
210
let close stmt =
207
211
Common.Stmt. free_meta stmt;
208
212
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
@@ -283,6 +286,9 @@ module Stmt = struct
283
286
let error stmt =
284
287
(B. mysql_stmt_errno stmt.raw, B. mysql_stmt_error stmt.raw)
285
288
289
+ let sqlstate stmt =
290
+ B. mysql_stmt_sqlstate stmt.raw
291
+
286
292
let fetch_field res i =
287
293
coerce (ptr void) (ptr T.Field. t) (B. mysql_fetch_field_direct res i)
288
294
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
@@ -164,6 +165,7 @@ module type S = sig
164
165
val commit : t -> unit result
165
166
val rollback : t -> unit result
166
167
val prepare : t -> string -> Stmt .t result
168
+ val sqlstate : t -> string
167
169
end
168
170
169
171
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. *)
@@ -282,6 +288,10 @@ module type S = sig
282
288
(* * [prepare mariadb query] creates a prepared statement for [query]. The
283
289
query may contain [?] as placeholders for parameters that can be bound
284
290
by calling [Stmt.execute]. *)
291
+
292
+ val sqlstate : t -> string
293
+ (* [sqlstate mariadb] is the SQLSTATE with MariaDB extensions of the last
294
+ * operation on [mariadb]. Returns ["00000"] if no error occurred. *)
285
295
end
286
296
287
297
(* * 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