| No_such_index
| Bad_table_error
| GENERIC_ERROR of string
+
exception Error
let prerr_endline s = ()(*HLog.debug s;;*)
let quick_connect ?host ?(database = "sqlite") ?port ?password ?user () =
let username = Helm_registry.get "user.name" in
- let ram_db_name =
- "/dev/shm/matita.db." ^ username ^ "." ^ string_of_int (Unix.getpid ())
+ let tmp_db_name =
+ if HExtlib.is_dir "/dev/shm/" && HExtlib.writable_dir "/dev/shm/" then
+ (HLog.debug "using /dev/shm to store the working copy of the db";
+ "/dev/shm/matita.db." ^ username ^ "." ^ string_of_int (Unix.getpid ()))
+ else
+ (HLog.debug "/dev/shm not available";
+ Helm_registry.get "matita.basedir" ^ "/matita.db." ^ username ^ "." ^
+ string_of_int (Unix.getpid ()))
in
let db_name = Helm_registry.get "matita.basedir" ^ "/" ^ database ^ ".db" in
- let cp_to_ram_cmd = "cp " ^ db_name ^ " " ^ ram_db_name in
+ (* XXX big hack XXX *)
+ let standard_db_name = "/usr/share/matita/metadata.db" in
+ let cp_to_ram_cmd =
+ if HExtlib.is_regular db_name then
+ "cp " ^ db_name ^ " " ^ tmp_db_name
+ else
+ (* we start from the standard library *)
+ "cp " ^ standard_db_name ^ " " ^ tmp_db_name
+ in
ignore (Sys.command cp_to_ram_cmd);
- let mv_to_disk_cmd _ = ignore (Sys.command ("mv " ^ ram_db_name ^ " " ^ db_name)) in
+ let mv_to_disk_cmd _ =
+ ignore (Sys.command ("mv " ^ tmp_db_name ^ " " ^ db_name))
+ in
at_exit mv_to_disk_cmd;
- Some (Sqlite3.db_open ram_db_name)
+ Some (Sqlite3.db_open tmp_db_name)
;;
let disconnect db =
match db with
| None -> ()
| Some db ->
- let b = Sqlite3.db_close db in
- if b=false then prerr_endline "No Closed DataBase"
+ let b = Sqlite3.db_close db in
+ if b=false then prerr_endline "No Closed DataBase"
;;
-let escape s = s
+(* XXX hack, sqlite has a print "%q" that should be used, but is not bound *)
+let escape s = Pcre.replace ~pat:"([^'])'([^'])" ~templ:"$1''$2" s
let exec db s =
prerr_endline s;
let rec map res ~f =
let map f = List.map f res in
profiler.HExtlib.profile map f
+;;
let iter res ~f =
let iter f = List.iter f res in
profiler.HExtlib.profile iter f
+;;
let pp_rc = function
- |Sqlite3.Rc.OK -> prerr_endline "Sqlite3.Rc.OK"
- |Sqlite3.Rc.ERROR -> prerr_endline "Sqlite3.Rc.ERROR"
- |Sqlite3.Rc.INTERNAL -> prerr_endline "Sqlite3.Rc.INTERNAL"
- |Sqlite3.Rc.PERM -> prerr_endline "Sqlite3.Rc.PERM"
- |Sqlite3.Rc.ABORT -> prerr_endline "Sqlite3.Rc.ABORT"
- |Sqlite3.Rc.BUSY -> prerr_endline "Sqlite3.Rc.BUSY"
- |Sqlite3.Rc.LOCKED -> prerr_endline "Sqlite3.Rc.LOCKED"
- |Sqlite3.Rc.NOMEM -> prerr_endline "Sqlite3.Rc.NOMEM"
- |Sqlite3.Rc.READONLY -> prerr_endline "Sqlite3.Rc.READONLY"
- |Sqlite3.Rc.INTERRUPT -> prerr_endline "Sqlite3.Rc.INTERRUPT"
- |Sqlite3.Rc.IOERR -> prerr_endline "Sqlite3.Rc.IOERR"
- |Sqlite3.Rc.CORRUPT -> prerr_endline "Sqlite3.Rc.CORRUPT"
- |Sqlite3.Rc.NOTFOUND -> prerr_endline "Sqlite3.Rc.NOTFOUND"
- |Sqlite3.Rc.FULL -> prerr_endline "Sqlite3.Rc.FULL"
- |Sqlite3.Rc.CANTOPEN -> prerr_endline "Sqlite3.Rc.CANTOPEN"
- |Sqlite3.Rc.PROTOCOL -> prerr_endline "Sqlite3.Rc.PROTOCOL"
- |Sqlite3.Rc.EMPTY -> prerr_endline "Sqlite3.Rc.EMPTY"
- |Sqlite3.Rc.SCHEMA -> prerr_endline "Sqlite3.Rc.SCHEMA"
- |Sqlite3.Rc.TOOBIG -> prerr_endline "Sqlite3.Rc.TOOBIG"
- |Sqlite3.Rc.CONSTRAINT -> prerr_endline "Sqlite3.Rc.CONSTRAINT"
- |Sqlite3.Rc.MISMATCH -> prerr_endline "Sqlite3.Rc.MISMATCH"
- |Sqlite3.Rc.MISUSE -> prerr_endline "Sqlite3.Rc.MISUSE"
- |Sqlite3.Rc.NOFLS -> prerr_endline "Sqlite3.Rc.NOFLS"
- |Sqlite3.Rc.AUTH -> prerr_endline "Sqlite3.Rc.AUTH"
- |Sqlite3.Rc.FORMAT -> prerr_endline "Sqlite3.Rc.FORMAT"
- |Sqlite3.Rc.RANGE -> prerr_endline "Sqlite3.Rc.RANGE"
- |Sqlite3.Rc.NOTADB -> prerr_endline "Sqlite3.Rc.NOTADB"
- |Sqlite3.Rc.ROW -> prerr_endline "Sqlite3.Rc.ROW"
- |Sqlite3.Rc.DONE -> prerr_endline "Sqlite3.Rc.DONE"
- |Sqlite3.Rc.UNKNOWN n ->
- prerr_endline ("Sqlite3.Rc.UNKNOWN " ^ string_of_int (Sqlite3.Rc.int_of_unknown n))
+ |Sqlite3.Rc.OK -> prerr_endline "Sqlite3.Rc.OK"
+ |Sqlite3.Rc.ERROR -> prerr_endline "Sqlite3.Rc.ERROR"
+ |Sqlite3.Rc.INTERNAL -> prerr_endline "Sqlite3.Rc.INTERNAL"
+ |Sqlite3.Rc.PERM -> prerr_endline "Sqlite3.Rc.PERM"
+ |Sqlite3.Rc.ABORT -> prerr_endline "Sqlite3.Rc.ABORT"
+ |Sqlite3.Rc.BUSY -> prerr_endline "Sqlite3.Rc.BUSY"
+ |Sqlite3.Rc.LOCKED -> prerr_endline "Sqlite3.Rc.LOCKED"
+ |Sqlite3.Rc.NOMEM -> prerr_endline "Sqlite3.Rc.NOMEM"
+ |Sqlite3.Rc.READONLY -> prerr_endline "Sqlite3.Rc.READONLY"
+ |Sqlite3.Rc.INTERRUPT -> prerr_endline "Sqlite3.Rc.INTERRUPT"
+ |Sqlite3.Rc.IOERR -> prerr_endline "Sqlite3.Rc.IOERR"
+ |Sqlite3.Rc.CORRUPT -> prerr_endline "Sqlite3.Rc.CORRUPT"
+ |Sqlite3.Rc.NOTFOUND -> prerr_endline "Sqlite3.Rc.NOTFOUND"
+ |Sqlite3.Rc.FULL -> prerr_endline "Sqlite3.Rc.FULL"
+ |Sqlite3.Rc.CANTOPEN -> prerr_endline "Sqlite3.Rc.CANTOPEN"
+ |Sqlite3.Rc.PROTOCOL -> prerr_endline "Sqlite3.Rc.PROTOCOL"
+ |Sqlite3.Rc.EMPTY -> prerr_endline "Sqlite3.Rc.EMPTY"
+ |Sqlite3.Rc.SCHEMA -> prerr_endline "Sqlite3.Rc.SCHEMA"
+ |Sqlite3.Rc.TOOBIG -> prerr_endline "Sqlite3.Rc.TOOBIG"
+ |Sqlite3.Rc.CONSTRAINT -> prerr_endline "Sqlite3.Rc.CONSTRAINT"
+ |Sqlite3.Rc.MISMATCH -> prerr_endline "Sqlite3.Rc.MISMATCH"
+ |Sqlite3.Rc.MISUSE -> prerr_endline "Sqlite3.Rc.MISUSE"
+ |Sqlite3.Rc.NOFLS -> prerr_endline "Sqlite3.Rc.NOFLS"
+ |Sqlite3.Rc.AUTH -> prerr_endline "Sqlite3.Rc.AUTH"
+ |Sqlite3.Rc.FORMAT -> prerr_endline "Sqlite3.Rc.FORMAT"
+ |Sqlite3.Rc.RANGE -> prerr_endline "Sqlite3.Rc.RANGE"
+ |Sqlite3.Rc.NOTADB -> prerr_endline "Sqlite3.Rc.NOTADB"
+ |Sqlite3.Rc.ROW -> prerr_endline "Sqlite3.Rc.ROW"
+ |Sqlite3.Rc.DONE -> prerr_endline "Sqlite3.Rc.DONE"
+ |Sqlite3.Rc.UNKNOWN n ->
+ prerr_endline
+ ("Sqlite3.Rc.UNKNOWN " ^ string_of_int (Sqlite3.Rc.int_of_unknown n))
;;
let errno = function
| None -> OK
| Some db ->
- (* pp_rc (Sqlite3.errcode db); *)
- (* prerr_endline(Sqlite3.errmsg db); *)
match Sqlite3.errcode db with
|Sqlite3.Rc.OK -> OK
|Sqlite3.Rc.ERROR ->
- if Pcre.pmatch(Sqlite3.errmsg db) ~pat:"^table .* already exists" then
+ let errmsg = (Sqlite3.errmsg db) in
+ if Pcre.pmatch errmsg ~pat:"^table .* already exists" then
Table_exists_error
else
- if Pcre.pmatch(Sqlite3.errmsg db) ~pat:"^index .* already exists" then
- Dup_keyname
- else
- if Pcre.pmatch(Sqlite3.errmsg db) ~pat:"^no such table: .*" then
- No_such_table
- else
- if Pcre.pmatch(Sqlite3.errmsg db) ~pat:"^no such index: .*" then
- No_such_index
- else
- GENERIC_ERROR "ciao"
-
- |Sqlite3.Rc.INTERNAL
- |Sqlite3.Rc.PERM
- |Sqlite3.Rc.ABORT
- |Sqlite3.Rc.BUSY
- |Sqlite3.Rc.LOCKED
- |Sqlite3.Rc.NOMEM
- |Sqlite3.Rc.READONLY
- |Sqlite3.Rc.INTERRUPT
- |Sqlite3.Rc.IOERR
- |Sqlite3.Rc.CORRUPT
- |Sqlite3.Rc.NOTFOUND
- |Sqlite3.Rc.FULL
- |Sqlite3.Rc.CANTOPEN
- |Sqlite3.Rc.PROTOCOL
- |Sqlite3.Rc.EMPTY
- |Sqlite3.Rc.SCHEMA
- |Sqlite3.Rc.TOOBIG
- |Sqlite3.Rc.CONSTRAINT
- |Sqlite3.Rc.MISMATCH
- |Sqlite3.Rc.MISUSE
- |Sqlite3.Rc.NOFLS
- |Sqlite3.Rc.AUTH
- |Sqlite3.Rc.FORMAT
- |Sqlite3.Rc.RANGE
- |Sqlite3.Rc.NOTADB
- |Sqlite3.Rc.ROW
- |Sqlite3.Rc.DONE
+ if Pcre.pmatch errmsg ~pat:"^index .* already exists" then Dup_keyname
+ else if Pcre.pmatch errmsg ~pat:"^no such table: .*" then No_such_table
+ else if Pcre.pmatch errmsg ~pat:"^no such index: .*" then No_such_index
+ else GENERIC_ERROR errmsg
+ |Sqlite3.Rc.INTERNAL |Sqlite3.Rc.PERM |Sqlite3.Rc.ABORT
+ |Sqlite3.Rc.BUSY |Sqlite3.Rc.LOCKED |Sqlite3.Rc.NOMEM
+ |Sqlite3.Rc.READONLY |Sqlite3.Rc.INTERRUPT |Sqlite3.Rc.IOERR
+ |Sqlite3.Rc.CORRUPT |Sqlite3.Rc.NOTFOUND |Sqlite3.Rc.FULL
+ |Sqlite3.Rc.CANTOPEN |Sqlite3.Rc.PROTOCOL |Sqlite3.Rc.EMPTY
+ |Sqlite3.Rc.SCHEMA |Sqlite3.Rc.TOOBIG |Sqlite3.Rc.CONSTRAINT
+ |Sqlite3.Rc.MISMATCH |Sqlite3.Rc.MISUSE |Sqlite3.Rc.NOFLS
+ |Sqlite3.Rc.AUTH |Sqlite3.Rc.FORMAT |Sqlite3.Rc.RANGE
+ |Sqlite3.Rc.NOTADB |Sqlite3.Rc.ROW |Sqlite3.Rc.DONE
|Sqlite3.Rc.UNKNOWN _ -> GENERIC_ERROR "Sqlite3_generic_error"
;;