X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;ds=sidebyside;f=helm%2Fsoftware%2Fcomponents%2Fhmysql%2FhSqlite3.ml;h=707b482bc4d3cb5f76469324a8233d2495a272e9;hb=76ec6b95c6f462f7fdedf0bc87354fb874ef4a46;hp=031c09c9af1245b8de9e785f86a3bf6205258acd;hpb=b3ee59e07da8da351e49da955a1619a14d90d058;p=helm.git diff --git a/helm/software/components/hmysql/hSqlite3.ml b/helm/software/components/hmysql/hSqlite3.ml index 031c09c9a..707b482bc 100644 --- a/helm/software/components/hmysql/hSqlite3.ml +++ b/helm/software/components/hmysql/hSqlite3.ml @@ -29,6 +29,12 @@ type result = Mysql.result option *) + +let debug = false +let debug_print = + if debug then prerr_endline else (fun _ ->()) +;; + type result = Sqlite3.row list type dbd = Sqlite3.db option @@ -41,39 +47,80 @@ type error_code = | Bad_table_error | GENERIC_ERROR of string -exception Error - -let prerr_endline s = ()(*HLog.debug s;;*) +exception Error of string let profiler = HExtlib.profile "Sqlite3" -let quick_connect ?host ?(database = "sqlite") ?port ?password ?user () = +let quick_connect + is_library + ?(host = Helm_registry.get "matita.basedir") + ?(database = "sqlite") ?port ?password ?user () += let username = Helm_registry.get "user.name" in - 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 ())) + let host_mangled = Pcre.replace ~pat:"[/ .]" ~templ:"_" host in + let find_db _ = + let base = "matita.db." ^ username ^ "." ^ host_mangled ^ "." in + let root = "/dev/shm/" in + let files = HExtlib.find ~test:(Pcre.pmatch ~pat:(base^"[0-9]+")) root in + let rec aux = function + | [] -> + debug_print ("HSqlite3: no valid db files found in memory"); + let name = root ^ base ^ string_of_int (Unix.getpid ()) in + debug_print ("HSqlite3: memory db file name: "^name); + name, true + | x::tl -> + debug_print ("HSqlite3: found a .db in memory: " ^ x); + match Array.to_list (Pcre.extract ~pat:"\\.([0-9]+)$" x) with + | [] | _::_::_::_ -> assert false + | [_;p] when HExtlib.is_dir ("/proc/" ^ p) -> + debug_print ("HSqlite3: found valid db file: " ^ x); + x, false + | _ -> + HLog.warn ("HSqlite3: dead process db file found: " ^ x); + HLog.warn ("HSqlite3: removing: " ^ x); + ignore (Sys.command ("rm " ^ x)); + aux tl + in + aux files in - let db_name = Helm_registry.get "matita.basedir" ^ "/" ^ database ^ ".db" 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 + let db_name = host ^ "/" ^ database in + let db_to_open = + if HExtlib.is_dir "/dev/shm/" && HExtlib.writable_dir "/dev/shm/" + && (not is_library) + then ( + let tmp_db_name, first_time = find_db () in + let cp_to_ram_cmd = "cp " ^ db_name ^ " " ^ tmp_db_name in + if first_time then + if HExtlib.is_regular db_name then ignore (Sys.command cp_to_ram_cmd) + else debug_print ("HSqlite3: no initial db: " ^ db_name) + else debug_print "HSqlite3: not copying the db, already in memory"; + let mv_to_disk_cmd _ = + if first_time then ignore (Sys.command ("mv "^tmp_db_name^" "^db_name)) + else debug_print "HSqlite3: not copying back the db" + in + at_exit mv_to_disk_cmd; + tmp_db_name) else - (* we start from the standard library *) - "cp " ^ standard_db_name ^ " " ^ tmp_db_name + db_name in - ignore (Sys.command cp_to_ram_cmd); - 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 tmp_db_name) + HExtlib.mkdir (Filename.dirname db_to_open); + let db = Sqlite3.db_open db_to_open in + (* attach the REGEX function *) + Sqlite3.create_fun2 db "REGEXP" + (fun s rex -> + try + match rex, s with + | Sqlite3.Data.TEXT rex, Sqlite3.Data.BLOB s + | Sqlite3.Data.TEXT rex, Sqlite3.Data.TEXT s -> + let r = Str.regexp rex in + if Str.string_match r s 0 then + Sqlite3.Data.INT 1L + else + Sqlite3.Data.INT 0L + | _ -> raise (Error "wrong types to 'REGEXP'") + with Sys.Break -> Sqlite3.Data.INT 0L + | exn -> HLog.error (Printexc.to_string exn); raise exn); + Some db ;; let disconnect db = @@ -81,14 +128,56 @@ let disconnect db = | None -> () | Some db -> let b = Sqlite3.db_close db in - if b=false then prerr_endline "No Closed DataBase" + if b=false then debug_print "No Closed DataBase" ;; (* 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 escape s = + let s_escaped = Pcre.replace ~pat:"'" ~templ:"''" s in + (*let s_escaped = Pcre.replace ~pat:"([^'])'([^'])" ~templ:"$1''$2" s in*) + debug_print s; + debug_print s_escaped; + s_escaped +;; -let exec db s = - prerr_endline s; +let string_of_rc = function + |Sqlite3.Rc.OK -> "Sqlite3.Rc.OK" + |Sqlite3.Rc.ERROR -> "Sqlite3.Rc.ERROR" + |Sqlite3.Rc.INTERNAL -> "Sqlite3.Rc.INTERNAL" + |Sqlite3.Rc.PERM -> "Sqlite3.Rc.PERM" + |Sqlite3.Rc.ABORT -> "Sqlite3.Rc.ABORT" + |Sqlite3.Rc.BUSY -> "Sqlite3.Rc.BUSY" + |Sqlite3.Rc.LOCKED -> "Sqlite3.Rc.LOCKED" + |Sqlite3.Rc.NOMEM -> "Sqlite3.Rc.NOMEM" + |Sqlite3.Rc.READONLY -> "Sqlite3.Rc.READONLY" + |Sqlite3.Rc.INTERRUPT -> "Sqlite3.Rc.INTERRUPT" + |Sqlite3.Rc.IOERR -> "Sqlite3.Rc.IOERR" + |Sqlite3.Rc.CORRUPT -> "Sqlite3.Rc.CORRUPT" + |Sqlite3.Rc.NOTFOUND -> "Sqlite3.Rc.NOTFOUND" + |Sqlite3.Rc.FULL -> "Sqlite3.Rc.FULL" + |Sqlite3.Rc.CANTOPEN -> "Sqlite3.Rc.CANTOPEN" + |Sqlite3.Rc.PROTOCOL -> "Sqlite3.Rc.PROTOCOL" + |Sqlite3.Rc.EMPTY -> "Sqlite3.Rc.EMPTY" + |Sqlite3.Rc.SCHEMA -> "Sqlite3.Rc.SCHEMA" + |Sqlite3.Rc.TOOBIG -> "Sqlite3.Rc.TOOBIG" + |Sqlite3.Rc.CONSTRAINT -> "Sqlite3.Rc.CONSTRAINT" + |Sqlite3.Rc.MISMATCH -> "Sqlite3.Rc.MISMATCH" + |Sqlite3.Rc.MISUSE -> "Sqlite3.Rc.MISUSE" + |Sqlite3.Rc.NOFLS -> "Sqlite3.Rc.NOFLS" + |Sqlite3.Rc.AUTH -> "Sqlite3.Rc.AUTH" + |Sqlite3.Rc.FORMAT -> "Sqlite3.Rc.FORMAT" + |Sqlite3.Rc.RANGE -> "Sqlite3.Rc.RANGE" + |Sqlite3.Rc.NOTADB -> "Sqlite3.Rc.NOTADB" + |Sqlite3.Rc.ROW -> "Sqlite3.Rc.ROW" + |Sqlite3.Rc.DONE -> "Sqlite3.Rc.DONE" + |Sqlite3.Rc.UNKNOWN n -> + "Sqlite3.Rc.UNKNOWN " ^ string_of_int (Sqlite3.Rc.int_of_unknown n) +;; + +let pp_rc rc = debug_print (string_of_rc rc);; + +let exec s db = + debug_print s; let stored_result = ref [] in let store row = stored_result := row :: !stored_result @@ -101,7 +190,7 @@ let exec db s = in match rc with | Sqlite3.Rc.OK -> !stored_result - | _ -> raise Error + | _ -> raise (Error (string_of_rc rc ^ ": " ^ Sqlite3.errmsg db )) ;; let rec map res ~f = @@ -114,41 +203,6 @@ let iter res ~f = 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)) -;; - let errno = function | None -> OK | Some db -> @@ -176,3 +230,5 @@ let errno = function ;; let isMysql = false + +let escape_string_for_like = ("ESCAPE \"\\\"" : ('a,'b,'c,'a) format4);;