Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/pr/52'
Browse files Browse the repository at this point in the history
  • Loading branch information
mmottl committed Aug 1, 2024
2 parents 4edcb6c + b8eaeec commit b37da7c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/sqlite3.ml
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ let ( let& ) db f =

external errcode : db -> Rc.t = "caml_sqlite3_errcode"
external errmsg : db -> string = "caml_sqlite3_errmsg"
external extended_errcode_int : db -> int = "caml_sqlite3_extended_errcode_int"

external last_insert_rowid : db -> (int64[@unboxed])
= "caml_sqlite3_last_insert_rowid_bc" "caml_sqlite3_last_insert_rowid"
Expand Down
8 changes: 8 additions & 0 deletions src/sqlite3.mli
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,14 @@ val errmsg : db -> string
@raise SqliteError if an invalid database handle is passed. *)

val extended_errcode_int : db -> int
(** [extended_errcode_int db]
@return
the extended error code of the last operation on the database [db] as an
integer.
@raise SqliteError if an invalid database handle is passed. *)

val last_insert_rowid : db -> int64
(** [last_insert_rowid db]
@return
Expand Down
6 changes: 6 additions & 0 deletions src/sqlite3_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,12 @@ CAMLprim value caml_sqlite3_errcode(value v_db) {
return Val_rc(sqlite3_errcode(dbw->db));
}

CAMLprim value caml_sqlite3_extended_errcode_int(value v_db) {
db_wrap *dbw = Sqlite3_val(v_db);
check_db(dbw, "extended_errcode");
return Val_int(sqlite3_extended_errcode(dbw->db));
}

CAMLprim value caml_sqlite3_errmsg(value v_db) {
db_wrap *dbw = Sqlite3_val(v_db);
check_db(dbw, "errmsg");
Expand Down
22 changes: 22 additions & 0 deletions test/test_error.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,26 @@ let%test "test_error" =
print_endline "Ok";
true
in

(* Check the extended error code. *)
exec db "CREATE TABLE erc1 (x INTEGER UNIQUE NOT NULL CHECK (x > 0))"
|> Rc.check;
exec db "CREATE TABLE erc2 (x INTEGER PRIMARY KEY, y REFERENCES erc1(x))"
|> Rc.check;
let erc_test expected_erc q =
let _ = exec db q in
let erc = extended_errcode_int db in
if erc = expected_erc then (
print_endline "Ok";
true)
else (
Printf.eprintf "Expected extended error code %d for %S, got %d.\n%!"
expected_erc q erc;
false)
in

first_test && second_test
&& erc_test 1299 "INSERT INTO erc1 (x) VALUES (NULL)"
&& erc_test 275 "INSERT INTO erc1 (x) VALUES (0)"
&& erc_test 2067 "INSERT INTO erc1 (x) VALUES (1), (1)"
&& erc_test 1555 "INSERT INTO erc2 (x) VALUES (1), (1)"

0 comments on commit b37da7c

Please sign in to comment.