]> matita.cs.unibo.it Git - helm.git/blobdiff - components/metadata/metadataDb.ml
Big progress
[helm.git] / components / metadata / metadataDb.ml
index 457545deeb6e8f25826e29e3cf9b487f520d0b61..844a083474fb72af086c8c09c46c60477ada14c9 100644 (file)
@@ -29,6 +29,14 @@ open MetadataTypes
 
 open Printf
 
+let format_insert dbtype dbd tbl tuples = 
+     if HSql.isMysql dbtype dbd then 
+       [sprintf "INSERT %s VALUES %s;" tbl (String.concat "," tuples)]
+     else
+       List.map (fun tup -> 
+               sprintf "INSERT INTO %s VALUES %s;" tbl tup) tuples 
+;;
+
 let execute_insert dbd uri (sort_cols, rel_cols, obj_cols) =
   let sort_tuples = 
     List.fold_left (fun s l -> match l with
@@ -52,26 +60,29 @@ let execute_insert dbd uri (sort_cols, rel_cols, obj_cols) =
       | _ -> assert false)
     [] obj_cols
   in
+  let dbtype = 
+    if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
+  in
   if sort_tuples <> [] then
     begin
     let query_sort = 
-      sprintf "INSERT %s VALUES %s;" (sort_tbl ()) (String.concat "," sort_tuples) 
+     format_insert dbtype dbd (sort_tbl ())  sort_tuples 
     in
-    ignore (HMysql.exec dbd query_sort)
+    List.iter (fun query -> ignore (HSql.exec dbtype dbd query)) query_sort
     end;
   if rel_tuples <> [] then
     begin
     let query_rel = 
-      sprintf "INSERT %s VALUES %s;" (rel_tbl ()) (String.concat "," rel_tuples) 
+     format_insert dbtype dbd (rel_tbl ())  rel_tuples 
     in
-    ignore (HMysql.exec dbd query_rel)
+    List.iter (fun query -> ignore (HSql.exec dbtype dbd query)) query_rel
     end;
   if obj_tuples <> [] then
     begin
     let query_obj = 
-      sprintf "INSERT %s VALUES %s;" (obj_tbl ()) (String.concat "," obj_tuples) 
+     format_insert dbtype dbd (obj_tbl ())  obj_tuples 
     in
-    ignore (HMysql.exec dbd query_obj)
+    List.iter (fun query -> ignore (HSql.exec dbtype dbd query)) query_obj
     end
   
     
@@ -108,21 +119,27 @@ let insert_const_no ~dbd l =
       (sprintf "(\"%s\", %d, %d, %d)" 
        (UriManager.string_of_uri uri) no_concl no_hyp no_full) :: acc
    ) [] l in
+ let dbtype = 
+   if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
+ in
  let insert =
-  sprintf "INSERT INTO %s VALUES %s" (count_tbl ()) (String.concat "," data)
+  format_insert dbtype dbd (count_tbl ())  data
  in
-  ignore (HMysql.exec dbd insert)
+  List.iter (fun query -> ignore (HSql.exec dbtype dbd query)) insert
   
 let insert_name ~dbd l =
+ let dbtype =
+   if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
+ in
  let data =
   List.fold_left
    (fun acc (uri,name,_) -> 
       (sprintf "(\"%s\", \"%s\")" (UriManager.string_of_uri uri) name) :: acc
    ) [] l in
  let insert =
-  sprintf "INSERT INTO %s VALUES %s" (name_tbl ()) (String.concat "," data)
+   format_insert dbtype dbd (name_tbl ())  data
  in
-  ignore (HMysql.exec dbd insert)
+  List.iter (fun query -> ignore (HSql.exec dbtype dbd query)) insert
 
 type columns =
   MetadataPp.t list list * MetadataPp.t list list * MetadataPp.t list list
@@ -135,10 +152,11 @@ let analyze_index = ref 0
 let eventually_analyze dbd =
   incr analyze_index;
   if !analyze_index > 30 then
+    if  HSql.isMysql HSql.User dbd then
     begin
       let analyze t = "OPTIMIZE TABLE " ^ t ^ ";" in
       List.iter 
-        (fun table -> ignore (HMysql.exec dbd (analyze table)))
+        (fun table -> ignore (HSql.exec HSql.User dbd (analyze table)))
         [name_tbl (); rel_tbl (); sort_tbl (); obj_tbl(); count_tbl()]
     end
   
@@ -159,11 +177,11 @@ let index_obj ~dbd ~uri =
 let tables_to_clean =
   [sort_tbl; rel_tbl; obj_tbl; name_tbl; count_tbl]
 
-let clean ~(dbd:HMysql.dbd) =
+let clean ~(dbd:HSql.dbd) =
   let owned_uris =  (* list of uris in list-of-columns format *)
     let query = sprintf "SELECT source FROM %s" (name_tbl ()) in
-    let result = HMysql.exec dbd query in
-    let uris = HMysql.map result (fun cols ->
+    let result = HSql.exec HSql.User dbd query in
+    let uris = HSql.map result (fun cols ->
       match cols.(0) with
       | Some src -> src
       | None -> assert false) in
@@ -171,11 +189,17 @@ let clean ~(dbd:HMysql.dbd) =
     uris
   in
   let del_from tbl =
+    let escape s =
+      Pcre.replace ~pat:"([^\\\\])_" ~templ:"$1\\_" (HSql.escape HSql.User dbd s)
+    in
     let query s = 
-      sprintf "DELETE FROM %s WHERE source LIKE \"%s%%\"" (tbl ()) s 
+      sprintf
+       ("DELETE FROM %s WHERE source LIKE \"%s%%\" " ^^
+        HSql.escape_string_for_like HSql.User dbd)
+        (tbl ()) (escape s)
     in
     List.iter
-      (fun source_col -> ignore (HMysql.exec dbd (query source_col)))
+      (fun source_col -> ignore (HSql.exec HSql.User dbd (query source_col)))
       owned_uris
   in
   List.iter del_from tables_to_clean;
@@ -184,10 +208,17 @@ let clean ~(dbd:HMysql.dbd) =
 let unindex ~dbd ~uri =
   let uri = UriManager.string_of_uri uri in
   let del_from tbl =
+    let escape s =
+      Pcre.replace 
+        ~pat:"([^\\\\])_" ~templ:"$1\\_" (HSql.escape HSql.User dbd s)
+    in
     let query tbl =
-      sprintf "DELETE FROM %s WHERE source LIKE \"%s%%\"" (tbl ()) uri
+      sprintf
+       ("DELETE FROM %s WHERE source LIKE \"%s%%\" " ^^
+        HSql.escape_string_for_like HSql.User dbd)
+       (tbl ()) (escape uri)
     in
-    ignore (HMysql.exec dbd (query tbl))
+    ignore (HSql.exec HSql.User dbd (query tbl))
   in
   List.iter del_from tables_to_clean