1 (* Copyright (C) 2004-2005, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
29 type tbl = [ `RefObj| `RefSort| `RefRel| `ObjectName| `Hits| `Count]
33 let sprintf_refObj_format name = [
34 sprintf "CREATE TABLE %s (
35 source varchar(255) not null,
36 h_occurrence varchar(255) not null,
37 h_position varchar(62) not null,
41 let sprintf_refSort_format name = [
42 sprintf "CREATE TABLE %s (
43 source varchar(255) not null,
44 h_position varchar(62) not null,
45 h_depth integer not null,
46 h_sort varchar(5) not null
49 let sprintf_refRel_format name = [
50 sprintf "CREATE TABLE %s (
51 source varchar(255) not null,
52 h_position varchar(62) not null,
53 h_depth integer not null
56 let sprintf_objectName_format name = [
57 sprintf "CREATE TABLE %s (
58 source varchar(255) not null,
59 value varchar(255) not null
62 let sprintf_hits_format name = [
63 sprintf "CREATE TABLE %s (
64 source varchar(255) not null,
68 let sprintf_count_format name = [
69 sprintf "CREATE TABLE %s (
70 source varchar(255) unique not null,
71 conclusion smallint(6) not null,
72 hypothesis smallint(6) not null,
73 statement smallint(6) not null
76 let sprintf_refObj_drop name = [sprintf "DROP TABLE %s;" name]
78 let sprintf_refSort_drop name = [sprintf "DROP TABLE %s;" name]
80 let sprintf_refRel_drop name = [sprintf "DROP TABLE %s;" name]
82 let sprintf_objectName_drop name = [sprintf "DROP TABLE %s;" name]
84 let sprintf_hits_drop name = [sprintf "DROP TABLE %s;" name]
86 let sprintf_count_drop name = [sprintf "DROP TABLE %s;" name]
90 let sprintf_refObj_index name = [
91 sprintf "CREATE INDEX %s_index ON %s (source,h_occurrence,h_position);" name name;
92 (*sprintf "CREATE INDEX %s_index ON %s (source(219),h_occurrence(219),h_position);" name name;*)
93 sprintf "CREATE INDEX %s_occurrence ON %s (h_occurrence);" name name ]
95 let sprintf_refSort_index name = [
96 sprintf "CREATE INDEX %s_index ON %s (source,h_sort,h_position,h_depth);" name name]
98 let sprintf_objectName_index name = [
99 sprintf "CREATE INDEX %s_value ON %s (value);" name name]
101 let sprintf_hits_index name = [
102 sprintf "CREATE INDEX %s_source ON %s (source);" name name ;
103 sprintf "CREATE INDEX %s_no ON %s (no);" name name]
105 let sprintf_count_index name = [
106 sprintf "CREATE INDEX %s_conclusion ON %s (conclusion);" name name;
107 sprintf "CREATE INDEX %s_hypothesis ON %s (hypothesis);" name name;
108 sprintf "CREATE INDEX %s_statement ON %s (statement);" name name]
110 let sprintf_refRel_index name = [
111 sprintf "CREATE INDEX %s_index ON %s (source,h_position,h_depth);" name name]
113 let format_drop name sufix dtype dbd =
114 if HSql.isMysql dtype dbd then
115 (sprintf "DROP INDEX %s_%s ON %s;" name sufix name)
117 (sprintf "DROP INDEX %s_%s;" name sufix);;
119 let sprintf_refObj_index_drop name dtype dbd= [(format_drop name "index" dtype dbd)]
121 let sprintf_refSort_index_drop name dtype dbd = [(format_drop name "index" dtype dbd)]
123 let sprintf_objectName_index_drop name dtype dbd = [(format_drop name "value" dtype dbd)]
125 let sprintf_hits_index_drop name dtype dbd = [
126 (format_drop name "source" dtype dbd);
127 (format_drop name "no" dtype dbd)]
129 let sprintf_count_index_drop name dtype dbd = [
130 (format_drop name "source" dtype dbd);
131 (format_drop name "conclusion" dtype dbd);
132 (format_drop name "hypothesis" dtype dbd);
133 (format_drop name "statement" dtype dbd)]
135 let sprintf_refRel_index_drop name dtype dbd =
136 [(format_drop name "index" dtype dbd)]
138 let sprintf_rename_table oldname newname = [
139 sprintf "RENAME TABLE %s TO %s;" oldname newname
145 let get_table_format t named =
147 | `RefObj -> sprintf_refObj_format named
148 | `RefSort -> sprintf_refSort_format named
149 | `RefRel -> sprintf_refRel_format named
150 | `ObjectName -> sprintf_objectName_format named
151 | `Hits -> sprintf_hits_format named
152 | `Count -> sprintf_count_format named
154 let get_index_format t named =
156 | `RefObj -> sprintf_refObj_index named
157 | `RefSort -> sprintf_refSort_index named
158 | `RefRel -> sprintf_refRel_index named
159 | `ObjectName -> sprintf_objectName_index named
160 | `Hits -> sprintf_hits_index named
161 | `Count -> sprintf_count_index named
163 let get_table_drop t named =
165 | `RefObj -> sprintf_refObj_drop named
166 | `RefSort -> sprintf_refSort_drop named
167 | `RefRel -> sprintf_refRel_drop named
168 | `ObjectName -> sprintf_objectName_drop named
169 | `Hits -> sprintf_hits_drop named
170 | `Count -> sprintf_count_drop named
172 let get_index_drop t named dtype dbd =
174 | `RefObj -> sprintf_refObj_index_drop named dtype dbd
175 | `RefSort -> sprintf_refSort_index_drop named dtype dbd
176 | `RefRel -> sprintf_refRel_index_drop named dtype dbd
177 | `ObjectName -> sprintf_objectName_index_drop named dtype dbd
178 | `Hits -> sprintf_hits_index_drop named dtype dbd
179 | `Count -> sprintf_count_index_drop named dtype dbd
181 let create_tables l =
182 List.fold_left (fun s (name,table) -> s @ get_table_format table name) [] l
184 let create_indexes l =
185 List.fold_left (fun s (name,table) -> s @ get_index_format table name) [] l
188 List.fold_left (fun s (name,table) -> s @ get_table_drop table name) [] l
190 let drop_indexes l dtype dbd=
191 List.fold_left (fun s (name,table) -> s @ get_index_drop table name dtype dbd) [] l
193 let rename_tables l =
194 List.fold_left (fun s (o,n) -> s @ sprintf_rename_table o n) [] l
196 let fill_hits refObj hits =
199 SELECT h_occurrence, COUNT(source)
201 GROUP BY h_occurrence;"
205 let move_content (name1, tbl1) (name2, tbl2) buri dtype dbd =
207 Pcre.replace ~pat:"([^\\\\])_" ~templ:"$1\\_" (HSql.escape dtype dbd s)
209 assert (tbl1 = tbl2);
211 "INSERT INTRO %s SELECT * FROM %s WHERE source LIKE \"%s%%\";"
212 name2 name1 (escape buri)
214 let direct_deps refObj uri dtype dbd =
215 sprintf "SELECT * FROM %s WHERE source = \"%s\";"
216 refObj (HSql.escape dtype dbd (UriManager.string_of_uri uri))
218 let inverse_deps refObj uri dtype dbd =
219 sprintf "SELECT * FROM %s WHERE h_occurrence = \"%s\";"
220 refObj (HSql.escape dtype dbd (UriManager.string_of_uri uri))