-let add_card_constr tbl (n,from,where) = function
- | None -> (n,from,where)
- | Some (Eq card) ->
- (n+1,
- (sprintf "%s as %s" tbl (tbln n) :: from),
- (sprintf "no=%d" card ::
- (if n=0 then []
- else [sprintf "table0.source = %s.source" (tbln n)]) @
- where))
- | Some (Gt card) ->
- (n+1,
- (sprintf "%s as %s" tbl (tbln n) :: from),
- (sprintf "no>%d" card ::
- (if n=0 then []
- else [sprintf "table0.source = %s.source" (tbln n)]) @
- where))
-
-let at_least ~(dbh:Dbi.connection) ?concl_card ?full_card
+let explode_card_constr = function
+ | Eq card -> "=", card
+ | Gt card -> ">", card
+ | Lt card -> "<", card
+
+let add_card_constr tbl col where = function
+ | None -> where
+ | Some constr ->
+ let op, card = explode_card_constr constr in
+ (* count(_utente).hypothesis = 3 *)
+ (sprintf "%s.%s %s %d" tbl col op card :: where)
+
+let add_diff_constr tbl where = function
+ | None -> where
+ | Some constr ->
+ let op, card = explode_card_constr constr in
+ (sprintf "%s.hypothesis - %s.conclusion %s %d" tbl tbl op card :: where)
+
+let add_all_constr ?(tbl=library_count_tbl) (n,from,where) concl full diff =
+ match (concl, full, diff) with
+ | None, None, None -> (n,from,where)
+ | _ ->
+ let cur_tbl = tbln n in
+ let from = (sprintf "%s as %s" tbl cur_tbl) :: from in
+ let where = add_card_constr cur_tbl "conclusion" where concl in
+ let where = add_card_constr cur_tbl "statement" where full in
+ let where = add_diff_constr cur_tbl where diff in
+ (n+2,from,
+ (if n > 0 then
+ sprintf "table0.source = %s.source" cur_tbl :: where
+ else
+ where))
+
+
+let add_constraint ?(start=0) ?(tables=default_tables) (n,from,where) metadata =
+ let obj_tbl,rel_tbl,sort_tbl,count_tbl = tables
+ in
+ let cur_tbl = tbln n in
+ let start_table = tbln start in
+ match metadata with
+ | `Obj (uri, positions) ->
+ let from = (sprintf "%s as %s" obj_tbl cur_tbl) :: from in
+ let where =
+ (sprintf "(%s.h_occurrence = \"%s\")" cur_tbl uri) ::
+ mk_positions positions cur_tbl ::
+ (if n=start then []
+ else [sprintf "%s.source = %s.source" start_table cur_tbl]) @
+ where
+ in
+ ((n+2), from, where)
+ | `Rel positions ->
+ let from = (sprintf "%s as %s" rel_tbl cur_tbl) :: from in
+ let where =
+ mk_positions positions cur_tbl ::
+ (if n=start then []
+ else [sprintf "%s.source = %s.source" start_table cur_tbl]) @
+ where
+ in
+ ((n+2), from, where)
+ | `Sort (sort, positions) ->
+ let sort_str = CicPp.ppsort sort in
+ let from = (sprintf "%s as %s" sort_tbl cur_tbl) :: from in
+ let where =
+ (sprintf "%s.h_sort = \"%s\"" cur_tbl sort_str ) ::
+ mk_positions positions cur_tbl ::
+ (if n=start then
+ []
+ else
+ [sprintf "%s.source = %s.source" start_table cur_tbl ]) @ where
+ in
+ ((n+2), from, where)
+
+let exec ~(dbd:Mysql.dbd) ?rating (n,from,where) =
+let from = String.concat ", " from in
+ let where = String.concat " and " where in
+ let query =
+ match rating with
+ | None -> sprintf "select table0.source from %s where %s" from where
+ | Some `Hits ->
+ sprintf
+ ("select table0.source from %s, hits where %s
+ and table0.source = hits.source order by hits.no desc")
+ from where
+ in
+ prerr_endline query;
+ let result = Mysql.exec dbd query in
+ Mysql.map result
+ (fun row -> match row.(0) with Some s -> s | _ -> assert false)
+
+
+let at_least ~(dbd:Mysql.dbd) ?concl_card ?full_card ?diff ?rating tables