]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/mathql_interpreter/mQIProperty.ml
"backPointer" metadata enabled
[helm.git] / helm / ocaml / mathql_interpreter / mQIProperty.ml
index c30c18d47e16fa59b689a531abd690a05ff27514..026e11b488b608831620785a245c70a7934b065c 100644 (file)
@@ -1,4 +1,3 @@
-
 (* Copyright (C) 2000, HELM Team.
  * 
  * This file is part of HELM, an Hypertextual, Electronic
  * http://cs.unibo.it/helm/.
  *)
 
+(*  AUTOR: Ferruccio Guidi <fguidi@cs.unibo.it>
+ *)
+
 module M = MathQL
 module P = MQIPostgres
+module C = MQIConn
 module U = MQIUtil
 
 let not_supported s = 
@@ -51,7 +54,11 @@ let cl_print l =
 
 (* PostgreSQL backend  ******************************************************)
 
-let pg_query table cols ct cf =
+let pg_query h table cols ct cfl =
+   let exec q = 
+      if C.set h C.Log then C.log h (q ^ "\n");
+      P.exec (C.pgc h) q 
+   in
    let rec iter f sep = function
       | []           -> ""
       | [head]       -> f head 
@@ -62,24 +69,36 @@ let pg_query table cols ct cf =
    let pg_con (pat, col, v) = 
       if col <> "" then 
          let f s = col ^ " ~ " ^ P.quote ("^" ^ s ^ "$") in
-         if pat then "(" ^ iter f " or " v ^ ")" 
-         else col ^ " in (" ^ pg_msval v ^ ")"
+         if pat then "(" ^ iter f " or " v ^ ")"
+        else match v with 
+           | [s] -> col ^ " = " ^ (P.quote s)     
+           | v   -> col ^ " in (" ^ pg_msval v ^ ")"
       else "true"
    in
    let pg_cons l = iter pg_con " and " l in
    let pg_cons_not l = "not (" ^ pg_cons l ^ ")" in
-   let pg_where = match ct, cf with
-      | [], [] -> ""
-      | lt, [] -> " where " ^ pg_cons lt
-      | [], lf -> " where " ^ pg_cons_not lf
-      | lt, lf -> " where " ^ pg_cons lt ^ " and " ^ pg_cons_not lf
+   let pg_cons_not_l ll = iter pg_cons_not " and " ll in
+   let pg_where = match ct, cfl with
+      | [], []  -> ""
+      | lt, []  -> " where " ^ pg_cons lt
+      | [], llf -> " where " ^ pg_cons_not_l llf
+      | lt, llf -> " where " ^ pg_cons lt ^ " and " ^ pg_cons_not_l llf
    in
-   if cols = [] then [] else begin
-      let q = "select " ^ pg_cols ^ " from " ^ table ^ pg_where ^ 
-              " order by " ^ List.hd cols ^ " asc" in
-      prerr_endline q;      
-      P.exec q end
-   
+   if cols = [] then
+      let r = exec ("select count (source) from " ^ table ^ pg_where) in
+      match r with
+         | [[s]] when int_of_string s > 0 -> [[]]
+        | _                              -> []
+   else
+      exec ("select " ^ pg_cols ^ " from " ^ table ^ pg_where ^ 
+            " order by " ^ List.hd cols ^ " asc")
+
+(* Galax backend  ***********************************************************)
+
+let gx_query h table cols ct cfl = not_supported "Galax"
+
+(* Common functions  ********************************************************)
+
 let pg_result distinct subj el res =
   let compose = if distinct then List.map else fun f -> U.mql_iter (fun x -> [f x]) in 
   let get_name = function (p, None) -> p | (_, Some p) -> p in
@@ -93,7 +112,7 @@ let pg_result distinct subj el res =
   in
   compose mk_avs res
 
-let get_table mc ct cf el =
+let get_table mc ct cfl el =
    let fst = function
       | []           -> None
       | head :: tail -> Some head
@@ -112,7 +131,7 @@ let get_table mc ct cf el =
       | []             -> prev
       | (p, _) :: tail -> get_e (comp prev (fst p)) tail
    in
-   get_e (get_c (get_c (fst mc) ct) cf) el  
+   get_e (get_c (get_c (fst mc) ct) (List.concat cfl)) el  
 
 let rec decolon s =
    let l = String.length s in
@@ -121,46 +140,52 @@ let rec decolon s =
       String.sub s 0 i ^ "_" ^ decolon (String.sub s (succ i) (l - succ i))
    with Not_found -> s
 
-let conv = function
+let conv_bp = function
+   | []                  -> "h_occurrence" 
+   | [t]                 -> ""
+   | [_; "h:occurrence"] -> "source"
+   | [_; t]              -> decolon t
+   | _                   -> not_supported "conv_bp"
+
+let conv_gen = function
    | []             -> "source"
-   | ["objectname"] -> "value" 
+   | ["objectName"] -> "value" 
    | [t]            -> ""
    | [_; t]         -> decolon t
    | _              -> not_supported "conv" 
 
-let postgres_single mc ct cf el t =
-   let table = match t with Some t -> decolon t | None -> "objectname" in
+let exec_single h mc ct cfl el t =
+   let table = match t with Some t -> decolon t | None -> "objectName" in
+   let table, conv = 
+      if table = "backPointer" then "refObj", conv_bp else table, conv_gen
+   in
    let first = conv mc in
    let mk_con l = List.map (fun (pat, p, v) -> (pat, conv p, v)) l in
    let cons_true = mk_con ct in 
-   let cons_false = mk_con cf in
+   let cons_false = List.map mk_con cfl in
    let other_cols = List.map (fun (p, _) -> conv p) el in 
-   let cols = if first = "" then other_cols else first :: other_cols in 
-   pg_result false first el (pg_query table cols cons_true cons_false) 
+   let cols = if first = "" then other_cols else first :: other_cols in
+   let low_level = if C.set h C.Galax then gx_query else pg_query in
+   pg_result false first el (low_level h table cols cons_true cons_false) 
    
 let deadline = 100
 
-let postgres refine mc ct cf el =
-   if refine <> M.RefineExact then not_supported "postgres";
-   cl_print ct;   
-   let table = get_table mc ct cf el in
-   let rec postgres_aux ct = match ct with 
+let exec h refine mc ct cfl el =
+   if refine <> M.RefineExact then not_supported "exec";   
+   let table = get_table mc ct cfl el in
+   let rec exec_aux ct = match ct with 
       | (pat, p, v) :: tail when List.length v > deadline ->
-         let single s = postgres_aux ((pat, p, [s]) :: tail) in
+         let single s = exec_aux ((pat, p, [s]) :: tail) in
         U.mql_iter single v
       | _                                                 ->
-         postgres_single mc ct cf el table
-   in postgres_aux ct   
-
-(* Galax backend  ***********************************************************)
-
-let galax refine mc ct cf el = not_supported "Galax"
+         exec_single h mc ct cfl el table
+   in exec_aux ct       
 
 (* funzioni vecchie  ********************************************************)
 
-let pg_name s = 
+let pg_name s = 
    let q = "select id from registry where uri = " ^ P.quote s in
-   match P.exec q with
+   match P.exec q with
       | [[id]] -> "t" ^ id
       | _      -> ""