]> matita.cs.unibo.it Git - helm.git/blobdiff - components/hmysql/hSqlite3.ml
Incompatible syntax problem between MySql e Sqlite3 fixed.
[helm.git] / components / hmysql / hSqlite3.ml
index 031c09c9af1245b8de9e785f86a3bf6205258acd..20e49451119fee9a5fca94f0db39a8f938124c47 100644 (file)
@@ -41,7 +41,7 @@ type error_code =
  | Bad_table_error
  | GENERIC_ERROR of string
 
-exception Error
+exception Error of string
 
 let prerr_endline s = ()(*HLog.debug s;;*)
 
@@ -51,16 +51,17 @@ let quick_connect ?host ?(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";
+      ((*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";
+      ((*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
-  (* XXX big hack XXX *)
-  let standard_db_name = "/usr/share/matita/metadata.db" in
+  let standard_db_name = 
+    Helm_registry.get "matita.rt_base_dir" ^ "/metadata.db" 
+  in
   let cp_to_ram_cmd = 
     if HExtlib.is_regular db_name then
       "cp " ^ db_name ^ " " ^ tmp_db_name 
@@ -73,7 +74,20 @@ let quick_connect ?host ?(database = "sqlite") ?port ?password ?user () =
     ignore (Sys.command ("mv " ^ tmp_db_name ^ " " ^ db_name)) 
   in
   at_exit mv_to_disk_cmd;
-  Some (Sqlite3.db_open tmp_db_name)
+  let db = Sqlite3.db_open tmp_db_name in
+  (* attach the REGEX function *)
+  Sqlite3.create_fun_2 db "REGEXP"
+      (fun s rex -> 
+         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'"));
+  Some db
 ;;
 
 let disconnect db =
@@ -87,6 +101,42 @@ let disconnect db =
 (* 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 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 = prerr_endline (string_of_rc rc);;
+
 let exec db s =
  prerr_endline s;
   let stored_result = ref [] in
@@ -101,7 +151,7 @@ let exec db s =
       in
       match rc with
       | Sqlite3.Rc.OK -> !stored_result
-      | _ -> raise Error
+      | _ -> raise (Error (string_of_rc rc))
 ;;
 
 let rec map res ~f = 
@@ -114,41 +164,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 +191,5 @@ let errno = function
 ;;
 
 let isMysql = false
+
+let escape_string_for_like = ("ESCAPE \"\\\"" : ('a,'b,'c,'a) format4);;