]> matita.cs.unibo.it Git - helm.git/blobdiff - components/library/libraryDb.ml
removed ugly printing
[helm.git] / components / library / libraryDb.ml
index 8c11f591f1ba0b22c47b7b628665da3b7706e364..e82e91f97f1629295d149ba1d066d11234932218 100644 (file)
 
 (* $Id$ *)
 
-open Printf ;;
+let dbtype_of_string dbtype =
+   if dbtype = "library" then HSql.Library
+   else if dbtype = "user" then HSql.User
+   else if dbtype = "legacy" then HSql.Legacy
+   else raise (HSql.Error "HSql: wrong config file format")
+
+let parse_dbd_conf _ =
+  let metadata = Helm_registry.get_list Helm_registry.string "db.metadata" in
+  List.map
+    (fun s -> 
+      match Pcre.split ~pat:"\\s+" s with
+      | [path;db;user;pwd;dbtype] -> 
+           let dbtype = dbtype_of_string dbtype in
+           let pwd = if pwd = "none" then None else Some pwd in
+           (* TODO parse port *)
+           path, None, db, user, pwd, dbtype
+      | _ -> raise (HSql.Error "HSql: Bad format in config file"))
+    metadata
+;;
+
+let parse_dbd_conf _ =
+   HSql.mk_dbspec (parse_dbd_conf ())
+;;
 
 let instance =
   let dbd = lazy (
-    HMysql.quick_connect
-      ~host:(Helm_registry.get "db.host")
-      ~user:(Helm_registry.get "db.user")
-      ~database:(Helm_registry.get "db.database")
-      ())
+    let dbconf = parse_dbd_conf () in
+    HSql.quick_connect dbconf)
   in
   fun () -> Lazy.force dbd
-
+;;
 
 let xpointer_RE = Pcre.regexp "#.*$"
 let file_scheme_RE = Pcre.regexp "^file://"
@@ -52,15 +71,19 @@ let clean_owner_environment () =
     (obj_tbl,`RefObj) ; (sort_tbl,`RefSort) ; (rel_tbl,`RefRel) ;
     (name_tbl,`ObjectName) ; (count_tbl,`Count) ] 
   in
+  let dbtype = 
+    if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
+  in
   let statements = 
-    (SqlStatements.drop_tables tbls) @ (SqlStatements.drop_indexes tbls)
+    (SqlStatements.drop_tables tbls) @ 
+    (SqlStatements.drop_indexes tbls dbtype dbd)
   in
   let owned_uris =
     try
       MetadataDb.clean ~dbd
-    with Mysql.Error _ as exn ->
-      match HMysql.errno dbd with 
-      | Mysql.No_such_table -> []
+    with (HSql.Error _) as exn ->
+      match HSql.errno dbtype dbd with 
+      | HSql.No_such_table -> []
       | _ -> raise exn
   in
   List.iter
@@ -69,17 +92,19 @@ let clean_owner_environment () =
       List.iter
         (fun suffix ->
           try
-           HExtlib.safe_remove (Http_getter.resolve (uri ^ suffix))
+           HExtlib.safe_remove 
+             (Http_getter.resolve ~local:true ~writable:true (uri ^ suffix))
           with Http_getter_types.Key_not_found _ -> ())
         [""; ".body"; ".types"])
     owned_uris;
   List.iter (fun statement -> 
     try
-      ignore (HMysql.exec dbd statement)
-    with Mysql.Error _ as exn ->
-      match HMysql.errno dbd with 
-      | Mysql.Bad_table_error 
-      | Mysql.No_such_index | Mysql.No_such_table -> () 
+      ignore (HSql.exec dbtype dbd statement)
+    with (HSql.Error _) as exn ->
+      match HSql.errno dbtype dbd with 
+      | HSql.No_such_table
+      | HSql.Bad_table_error
+      | HSql.No_such_index -> ()
       | _ -> raise exn
     ) statements;
 ;;
@@ -91,25 +116,41 @@ let create_owner_environment () =
   let rel_tbl = MetadataTypes.rel_tbl () in
   let name_tbl =  MetadataTypes.name_tbl () in
   let count_tbl = MetadataTypes.count_tbl () in
+  let l_obj_tbl = MetadataTypes.library_obj_tbl  in
+  let l_sort_tbl = MetadataTypes.library_sort_tbl  in
+  let l_rel_tbl = MetadataTypes.library_rel_tbl  in
+  let l_name_tbl =  MetadataTypes.library_name_tbl  in
+  let l_count_tbl = MetadataTypes.library_count_tbl  in
   let tbls = [ 
     (obj_tbl,`RefObj) ; (sort_tbl,`RefSort) ; (rel_tbl,`RefRel) ;
-    (name_tbl,`ObjectName) ; (count_tbl,`Count) ] 
+    (name_tbl,`ObjectName) ; (count_tbl,`Count) ]
+  in
+  let system_tbls = [ 
+    (l_obj_tbl,`RefObj) ; (l_sort_tbl,`RefSort) ; (l_rel_tbl,`RefRel) ;
+    (l_name_tbl,`ObjectName) ; (l_count_tbl,`Count) ] 
   in
+  let tag tag l = List.map (fun x -> tag, x) l in
   let statements = 
-    (SqlStatements.create_tables tbls) @ (SqlStatements.create_indexes tbls)
+    (tag HSql.Library (SqlStatements.create_tables system_tbls)) @ 
+    (tag HSql.User (SqlStatements.create_tables tbls)) @ 
+    (tag HSql.Library (SqlStatements.create_indexes system_tbls)) @
+    (tag HSql.User (SqlStatements.create_indexes tbls))
   in
-  List.iter (fun statement -> 
-    try
-      ignore (HMysql.exec dbd statement)
-    with
-      exn -> 
-         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
+  List.iter 
+    (fun (dbtype, statement) -> 
+      try
+        ignore (HSql.exec dbtype dbd statement)
+      with
+        (HSql.Error _) as exc -> 
+          let status = HSql.errno dbtype dbd in 
+          match status with 
+          | HSql.Table_exists_error -> ()
+          | HSql.Dup_keyname -> ()
+          | HSql.GENERIC_ERROR _ -> 
+              prerr_endline statement;
+              raise exc
+          | _ -> ())
+  statements
 ;;
 
 (* removes uri from the ownerized tables, and returns the list of other objects
@@ -129,39 +170,43 @@ let remove_uri uri =
   
   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 (HMysql.escape suri)
+  let dbtype = 
+    if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
+  in
+  let query table suri = 
+    if HSql.isMysql dbtype dbd then 
+      Printf.sprintf "DELETE QUICK LOW_PRIORITY FROM %s WHERE source='%s'" table 
+        (HSql.escape dbtype dbd suri)
+    else 
+      Printf.sprintf "DELETE FROM %s WHERE source='%s'" table 
+        (HSql.escape dbtype dbd suri)
   in
   List.iter (fun t -> 
     try 
-      ignore (HMysql.exec dbd (query t suri))
+      ignore (HSql.exec dbtype 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*)count_tbl];
-  (* and now the debug job *)  
-  let dbg_q = 
-    sprintf "SELECT source FROM %s WHERE h_occurrence LIKE '%s%%'" obj_tbl
-    (HMysql.escape suri)
-  in
-  try 
-    let rc = HMysql.exec dbd dbg_q in
-    let l = ref [] in
-    HMysql.iter rc (fun a -> match a.(0) with None ->()|Some a -> l := a:: !l);
-    let l = List.sort Pervasives.compare !l in
-    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))
+  let dbtype = 
+    if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
+  in
+  let escape s =
+    Pcre.replace ~pat:"([^\\\\])_" ~templ:"$1\\_" 
+      (HSql.escape dbtype dbd s)
+  in
+  let query = Printf.sprintf 
+    ("SELECT source FROM %s WHERE source LIKE '%s#xpointer%%' "
+     ^^ HSql.escape_string_for_like dbtype dbd)
+    name_tbl (escape (UriManager.string_of_uri uri))
   in
-  let rc = HMysql.exec dbd query in
+  let rc = HSql.exec dbtype dbd query in
   let l = ref [] in
-  HMysql.iter rc (fun a ->  match a.(0) with None ->()|Some a -> l := a:: !l);
+  HSql.iter rc (fun a ->  match a.(0) with None ->()|Some a -> l := a:: !l);
   List.map UriManager.uri_of_string !l