(* Copyright (C) 2004, HELM Team. * * This file is part of HELM, an Hypertextual, Electronic * Library of Mathematics, developed at the Computer Science * Department, University of Bologna, Italy. * * HELM is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * HELM is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HELM; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. * * For details, see the HELM World-Wide-Web page, * http://helm.cs.unibo.it/ *) open Printf module DB = Dbi_mysql let sort_tbl = "refSort" let rel_tbl = "refRel" let obj_tbl = "refObj" let owners_tbl = "owners" let conclno_tbl = "no_inconcl_aux" let conclno_hyp_tbl = "no_concl_hyp" let name_tbl = "objectName" (* let baseuri = "http://www.cs.unibo.it/helm/schemas/schema-helm#" *) let baseuri = "" let inconcl_uri = baseuri ^ "InConclusion" let mainconcl_uri = baseuri ^ "MainConclusion" let mainhyp_uri = baseuri ^ "MainHypothesis" let inhyp_uri = baseuri ^ "InHypothesis" let inbody_uri = baseuri ^ "InBody" let prepare_insert (dbh: Dbi.connection) = let insert_owner = dbh#prepare (sprintf "INSERT INTO %s VALUES (\"?\",\"?\")" owners_tbl) in let insert_sort = dbh#prepare (sprintf "INSERT INTO %s VALUES (\"?\",\"?\",?,\"?\")" sort_tbl) in let insert_rel = dbh#prepare (sprintf "INSERT INTO %s VALUES (\"?\",\"?\",?)" rel_tbl) in let insert_obj = dbh#prepare (sprintf "INSERT INTO %s VALUES (\"?\",\"?\",\"?\",?)" obj_tbl) in (insert_owner, insert_sort, insert_rel, insert_obj) let execute_insert dbh (insert_owner, insert_sort, insert_rel, insert_obj) uri owner (sort_cols, rel_cols, obj_cols) = insert_owner#execute [`String uri; `String owner]; List.iter insert_sort#execute sort_cols; List.iter insert_rel#execute rel_cols; List.iter insert_obj#execute obj_cols let insert_const_no dbh uri = let inconcl_no = sprintf "INSERT INTO %s SELECT \"%s\", COUNT(DISTINCT h_occurrence) FROM %s WHERE (h_position=\"%s\" OR h_position=\"%s\") AND source LIKE \"%s%%\"" conclno_tbl uri obj_tbl inconcl_uri mainconcl_uri uri in let concl_hyp = sprintf "INSERT INTO %s SELECT \"%s\",COUNT(DISTINCT h_occurrence) FROM %s WHERE NOT (h_position=\"%s\") AND (source = \"%s\")" conclno_hyp_tbl uri obj_tbl inbody_uri uri in (dbh#prepare inconcl_no)#execute []; (dbh#prepare concl_hyp)#execute [] let insert_name ~dbh ~uri ~name = let query = dbh#prepare (sprintf "INSERT INTO %s VALUES (\"%s\", \"%s\")" name_tbl uri name) in query#execute [] type dbi_columns = Dbi.sql_t list list * Dbi.sql_t list list * Dbi.sql_t list list let index_constant ~dbh = let query = prepare_insert dbh in fun ~owner ~uri ~body ~ty -> let name = UriManager.name_of_uri uri in let uri = UriManager.string_of_uri uri in let metadata = MetadataExtractor.compute ~body ~ty in let columns = MetadataPp.columns_of_metadata ~about:uri metadata in execute_insert dbh query uri owner (columns :> dbi_columns); insert_const_no dbh uri; insert_name ~dbh ~uri ~name let index_inductive_def ~dbh = let query = prepare_insert dbh in fun ~owner ~uri ~types -> let metadata = MetadataExtractor.compute_ind ~uri ~types in let uri_of (a,b,c) = a in let uris = UriManager.string_of_uri uri :: List.map uri_of metadata in let uri = UriManager.string_of_uri uri in let columns = MetadataPp.columns_of_ind_metadata metadata in execute_insert dbh query uri owner (columns :> dbi_columns); List.iter (insert_const_no dbh) uris; List.iter (fun (uri, name, _) -> insert_name ~dbh ~uri ~name) metadata let clean ~(dbh: Dbi.connection) ~owner = let owned_uris = (* list of uris in list-of-columns format *) let query = dbh#prepare (sprintf "SELECT source FROM %s WHERE owner = \"%s\"" owners_tbl owner) in query#execute []; List.map (function [source_col] -> source_col | _ -> assert false) (query#fetchall ()) in let del_from tbl = let query = dbh#prepare (sprintf "DELETE FROM %s WHERE source LIKE \"?%%\"" tbl) in query#execute owned_uris in List.iter del_from [sort_tbl; rel_tbl; obj_tbl; conclno_tbl; conclno_hyp_tbl; name_tbl; owners_tbl]