[sqlite] always die on error

Reviewed By: ezgicicek

Differential Revision: D15120495

fbshipit-source-id: 728343433
master
Nikos Gorogiannis 6 years ago committed by Facebook Github Bot
parent 4333d5f9cc
commit 711e7d1611

@ -45,13 +45,11 @@ let merge_source_files_table ~db_file =
let merge ~db_file = let merge ~db_file =
let main_db = ResultsDatabase.get_database () in let main_db = ResultsDatabase.get_database () in
Sqlite3.exec main_db (Printf.sprintf "ATTACH '%s' AS attached" db_file) Sqlite3.exec main_db (Printf.sprintf "ATTACH '%s' AS attached" db_file)
|> SqliteUtils.check_result_code ~fatal:true main_db |> SqliteUtils.check_result_code main_db ~log:(Printf.sprintf "attaching database '%s'" db_file) ;
~log:(Printf.sprintf "attaching database '%s'" db_file) ;
merge_procedures_table ~db_file ; merge_procedures_table ~db_file ;
merge_source_files_table ~db_file ; merge_source_files_table ~db_file ;
Sqlite3.exec main_db "DETACH attached" Sqlite3.exec main_db "DETACH attached"
|> SqliteUtils.check_result_code ~fatal:true main_db |> SqliteUtils.check_result_code main_db ~log:(Printf.sprintf "detaching database '%s'" db_file) ;
~log:(Printf.sprintf "detaching database '%s'" db_file) ;
() ()

@ -9,16 +9,14 @@ module L = Logging
exception Error of string exception Error of string
let error ~fatal fmt = let error fmt = Format.kasprintf (fun err -> raise (Error err)) fmt
(if fatal then Format.kasprintf (fun err -> raise (Error err)) else L.internal_error) fmt
let check_result_code db ~log rc =
let check_result_code ?(fatal = false) db ~log rc =
match (rc : Sqlite3.Rc.t) with match (rc : Sqlite3.Rc.t) with
| OK | ROW -> | OK | ROW ->
() ()
| _ as err -> | _ as err ->
error ~fatal "%s: %s (%s)" log (Sqlite3.Rc.to_string err) (Sqlite3.errmsg db) error "%s: %s (%s)" log (Sqlite3.Rc.to_string err) (Sqlite3.errmsg db)
let exec db ~log ~stmt = let exec db ~log ~stmt =
@ -27,16 +25,16 @@ let exec db ~log ~stmt =
PerfEvent.log_begin_event logger ~name:"sql exec" ~arguments:[("stmt", `String log)] () ) ; PerfEvent.log_begin_event logger ~name:"sql exec" ~arguments:[("stmt", `String log)] () ) ;
let rc = Sqlite3.exec db stmt in let rc = Sqlite3.exec db stmt in
PerfEvent.(log (fun logger -> log_end_event logger ())) ; PerfEvent.(log (fun logger -> log_end_event logger ())) ;
try check_result_code ~fatal:true db ~log rc try check_result_code db ~log rc
with Error err -> error ~fatal:true "exec: %s (%s)" err (Sqlite3.errmsg db) with Error err -> error "exec: %s (%s)" err (Sqlite3.errmsg db)
let finalize db ~log stmt = let finalize db ~log stmt =
try check_result_code ~fatal:true db ~log (Sqlite3.finalize stmt) with try check_result_code db ~log (Sqlite3.finalize stmt) with
| Error err -> | Error err ->
error ~fatal:true "finalize: %s (%s)" err (Sqlite3.errmsg db) error "finalize: %s (%s)" err (Sqlite3.errmsg db)
| Sqlite3.Error err -> | Sqlite3.Error err ->
error ~fatal:true "finalize: %s: %s (%s)" log err (Sqlite3.errmsg db) error "finalize: %s: %s (%s)" log err (Sqlite3.errmsg db)
let result_fold_rows ?finalize:(do_finalize = true) db ~log stmt ~init ~f = let result_fold_rows ?finalize:(do_finalize = true) db ~log stmt ~init ~f =

@ -11,7 +11,7 @@ open! IStd
particular, they may raise if the [Sqlite3.Rc.t] result of certain operations is unexpected. *) particular, they may raise if the [Sqlite3.Rc.t] result of certain operations is unexpected. *)
exception Error of string exception Error of string
val check_result_code : ?fatal:bool -> Sqlite3.db -> log:string -> Sqlite3.Rc.t -> unit val check_result_code : Sqlite3.db -> log:string -> Sqlite3.Rc.t -> unit
(** Assert that the result is either [Sqlite3.Rc.OK] or [Sqlite3.Rc.ROW]. If the result is not (** Assert that the result is either [Sqlite3.Rc.OK] or [Sqlite3.Rc.ROW]. If the result is not
valid, then if [fatal] is set raise {!Error}, otherwise log the error and proceed. *) valid, then if [fatal] is set raise {!Error}, otherwise log the error and proceed. *)

Loading…
Cancel
Save