]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/matita/matitaDb.ml
ocaml 3.09 transition
[helm.git] / helm / matita / matitaDb.ml
index 93a1abaa5a731d34c81f044fbb8cc76787d0b3fc..e3c7041ddadd927d4f6b86fe0b0e4e3d0c42b306 100644 (file)
@@ -27,7 +27,7 @@ open Printf ;;
 
 let instance =
   let dbd = lazy (
-    Mysql.quick_connect
+    HMysql.quick_connect
       ~host:(Helm_registry.get "db.host")
       ~user:(Helm_registry.get "db.user")
       ~database:(Helm_registry.get "db.database")
@@ -37,6 +37,7 @@ let instance =
 
 
 let xpointer_RE = Pcre.regexp "#.*$"
+let file_scheme_RE = Pcre.regexp "^file://"
 
 let clean_owner_environment () =
   let dbd = instance () in
@@ -45,31 +46,19 @@ let clean_owner_environment () =
   let sort_tbl = MetadataTypes.sort_tbl () in
   let rel_tbl = MetadataTypes.rel_tbl () in
   let name_tbl =  MetadataTypes.name_tbl () in
-  let conclno_tbl = MetadataTypes.conclno_tbl () in
-  let conclno_hyp_tbl = MetadataTypes.fullno_tbl () in
-  let statements = [
-    sprintf "DROP TABLE %s ;" obj_tbl;
-    sprintf "DROP TABLE %s ;" sort_tbl;
-    sprintf "DROP TABLE %s ;" rel_tbl;
-    sprintf "DROP TABLE %s ;" name_tbl;
-    sprintf "DROP TABLE %s ;" conclno_tbl;
-    sprintf "DROP TABLE %s ;" conclno_hyp_tbl ] in
-(*
-DROP INDEX refObj_source ON refObj (source);
-DROP INDEX refObj_target ON refObj (h_occurrence);
-DROP INDEX refObj_position ON refObj (h_position);
-DROP INDEX refSort_source ON refSort (source);
-DROP INDEX objectName_value ON objectName (value);
-DROP INDEX no_inconcl_aux_source ON no_inconcl_aux (source);
-DROP INDEX no_inconcl_aux_no ON no_inconcl_aux (no);
-DROP INDEX no_concl_hyp_source ON no_concl_hyp (source);
-DROP INDEX no_concl_hyp_no ON no_concl_hyp (no);
-*)
+  let count_tbl = MetadataTypes.count_tbl () in
+  let tbls = [ 
+    (obj_tbl,`RefObj) ; (sort_tbl,`RefSort) ; (rel_tbl,`RefRel) ;
+    (name_tbl,`ObjectName) ; (count_tbl,`Count) ] 
+  in
+  let statements = 
+    (SqlStatements.drop_tables tbls) @ (SqlStatements.drop_indexes tbls)
+  in
   let owned_uris =
     try
       MetadataDb.clean ~dbd
     with Mysql.Error _ as exn ->
-      match Mysql.errno dbd with 
+      match HMysql.errno dbd with 
       | Mysql.No_such_table -> []
       | _ -> raise exn
   in
@@ -77,77 +66,46 @@ DROP INDEX no_concl_hyp_no ON no_concl_hyp (no);
     (fun uri ->
       let uri = Pcre.replace ~rex:xpointer_RE ~templ:"" uri in
       List.iter
-        (fun suffix -> Http_getter.unregister (uri ^ suffix))
+        (fun suffix ->
+          try
+           MatitaMisc.safe_remove (Http_getter.resolve (uri ^ suffix))
+          with Http_getter_types.Key_not_found _ -> ())
         [""; ".body"; ".types"])
     owned_uris;
   List.iter (fun statement -> 
     try
-      ignore (Mysql.exec dbd statement)
+      ignore (HMysql.exec dbd statement)
     with Mysql.Error _ as exn ->
-      match Mysql.errno dbd with 
-      | Mysql.Bad_table_error -> () 
+      match HMysql.errno dbd with 
+      | Mysql.Bad_table_error 
+      | Mysql.No_such_index | Mysql.No_such_table -> () 
       | _ -> raise exn
     ) statements;
 ;;
 
 let create_owner_environment () = 
   let dbd = instance () in
-  let owner = (Helm_registry.get "matita.owner") in
   let obj_tbl = MetadataTypes.obj_tbl () in
   let sort_tbl = MetadataTypes.sort_tbl () in
   let rel_tbl = MetadataTypes.rel_tbl () in
   let name_tbl =  MetadataTypes.name_tbl () in
-  let conclno_tbl = MetadataTypes.conclno_tbl () in
-  let conclno_hyp_tbl = MetadataTypes.fullno_tbl () in
-  let statements = [
-    sprintf "CREATE TABLE %s (
-                  source varchar(255) binary not null,
-                  h_occurrence varchar(255) binary not null,
-                  h_position varchar(255) binary not null,
-                  h_depth integer
-              );" obj_tbl;
-    sprintf "CREATE TABLE %s (
-                  source varchar(255) binary not null,
-                  h_position varchar(255) binary not null,
-                  h_depth integer not null,
-                  h_sort varchar(255) binary not null
-              );" sort_tbl;
-    sprintf "CREATE TABLE %s (
-                  source varchar(255) binary not null,
-                  h_position varchar(255) binary not null,
-                  h_depth integer not null
-              );" rel_tbl;
-    sprintf "CREATE TABLE %s (
-                  source varchar(255) binary not null,
-                  value varchar(255) binary not null
-              );" name_tbl;
-    sprintf "CREATE TABLE %s (
-                  source varchar(255) binary not null,
-                  no tinyint(4) not null
-              );" conclno_tbl;
-    sprintf "CREATE TABLE %s (
-                  source varchar(255) binary not null,
-                  no tinyint(4) not null
-              );" conclno_hyp_tbl ] in
-(*
-CREATE INDEX refObj_source ON refObj (source);
-CREATE INDEX refObj_target ON refObj (h_occurrence);
-CREATE INDEX refObj_position ON refObj (h_position);
-CREATE INDEX refSort_source ON refSort (source);
-CREATE INDEX objectName_value ON objectName (value);
-CREATE INDEX no_inconcl_aux_source ON no_inconcl_aux (source);
-CREATE INDEX no_inconcl_aux_no ON no_inconcl_aux (no);
-CREATE INDEX no_concl_hyp_source ON no_concl_hyp (source);
-CREATE INDEX no_concl_hyp_no ON no_concl_hyp (no);
-*)
+  let count_tbl = MetadataTypes.count_tbl () in
+  let tbls = [ 
+    (obj_tbl,`RefObj) ; (sort_tbl,`RefSort) ; (rel_tbl,`RefRel) ;
+    (name_tbl,`ObjectName) ; (count_tbl,`Count) ] 
+  in
+  let statements = 
+    (SqlStatements.create_tables tbls) @ (SqlStatements.create_indexes tbls)
+  in
   List.iter (fun statement -> 
     try
-      ignore (Mysql.exec dbd statement)
+      ignore (HMysql.exec dbd statement)
     with
       exn -> 
-         let status = Mysql.status dbd in
+         let status = HMysql.status dbd in
          match status with 
          | Mysql.StatusError Mysql.Table_exists_error -> ()
+         | Mysql.StatusError Mysql.Dup_keyname -> ()
          | Mysql.StatusError _ -> raise exn
          | _ -> ()
       ) statements
@@ -156,7 +114,7 @@ CREATE INDEX no_concl_hyp_no ON no_concl_hyp (no);
 (* removes uri from the ownerized tables, and returns the list of other objects
  * (theyr uris) that ref the one removed. 
  * AFAIK there is no need to return it, since the MatitaTypes.staus should
- * contain all defined objects. but to double ckeck we do not garbage the
+ * contain all defined objects. but to double check we do not garbage the
  * metadata...
  *)
 let remove_uri uri =
@@ -164,35 +122,45 @@ let remove_uri uri =
   let sort_tbl = MetadataTypes.sort_tbl () in
   let rel_tbl = MetadataTypes.rel_tbl () in
   let name_tbl =  MetadataTypes.name_tbl () in
-  let conclno_tbl = MetadataTypes.conclno_tbl () in
-  let conclno_hyp_tbl = MetadataTypes.fullno_tbl () in
+  (*let conclno_tbl = MetadataTypes.conclno_tbl () in
+  let conclno_hyp_tbl = MetadataTypes.fullno_tbl () in*)
+  let count_tbl = MetadataTypes.count_tbl () in
+  
   let dbd = instance () in
   let suri = UriManager.string_of_uri uri in 
   let query table suri = sprintf 
-    "DELETE FROM %s WHERE source LIKE '%s%%'" table (Mysql.escape suri)
+    "DELETE FROM %s WHERE source LIKE '%s%%'" table (HMysql.escape suri)
   in
   List.iter (fun t -> 
     try 
-      ignore (Mysql.exec dbd (query t suri))
+      ignore (HMysql.exec dbd (query t suri))
     with
       exn -> raise exn (* no errors should be accepted *)
-    ) [obj_tbl;sort_tbl;rel_tbl;name_tbl;conclno_tbl;conclno_hyp_tbl];
+    )
+  [obj_tbl;sort_tbl;rel_tbl;name_tbl;(*conclno_tbl;conclno_hyp_tbl*)count_tbl];
   (* and now the debug job *)  
   let dbg_q = 
-    sprintf "SELECT source FROM %s WHERE h_occurrence LIKE '%s%%'" obj_tbl suri
+    sprintf "SELECT source FROM %s WHERE h_occurrence LIKE '%s%%'" obj_tbl
+    (HMysql.escape suri)
   in
   try 
-    let rc = Mysql.exec dbd dbg_q in
+    let rc = HMysql.exec dbd dbg_q in
     let l = ref [] in
-    Mysql.iter rc (fun a -> match a.(0) with None ->()|Some a -> l := a:: !l);
+    HMysql.iter rc (fun a -> match a.(0) with None ->()|Some a -> l := a:: !l);
     let l = List.sort Pervasives.compare !l in
-    let rec uniq = function 
-      | [] -> []
-      | h::[] -> [h]
-      | h1::h2::tl when h1 = h2 -> uniq (h2 :: tl) 
-      | h1::tl (* when h1 <> h2 *) -> h1 :: uniq tl
-    in
-    uniq l
+    HExtlib.list_uniq l
   with
     exn -> raise exn (* no errors should be accepted *)
 
+let xpointers_of_ind uri =
+  let dbd = instance () in
+  let name_tbl =  MetadataTypes.name_tbl () in
+  let query = sprintf 
+    "SELECT source FROM %s WHERE source LIKE '%s#xpointer%%'" name_tbl 
+      (HMysql.escape (UriManager.string_of_uri uri))
+  in
+  let rc = HMysql.exec dbd query in
+  let l = ref [] in
+  HMysql.iter rc (fun a ->  match a.(0) with None ->()|Some a -> l := a:: !l);
+  List.map UriManager.uri_of_string !l
+