| 5209 |
dpurdie |
1 |
#!/bin/sh
|
|
|
2 |
#
|
|
|
3 |
# Based on the file custom.d/initdb.sh
|
|
|
4 |
# Tailored to:
|
|
|
5 |
# 1) Take database name as an argument
|
|
|
6 |
# 2) Only create the database if it does not exist
|
|
|
7 |
#
|
|
|
8 |
DBNAME=${1?"First argument MUST be a database name"}
|
|
|
9 |
DBEXISTS=$(mysql -u lxr -plxrpw --batch --skip-column-names -e "SHOW DATABASES LIKE '"$DBNAME"';" | grep "$DBNAME" > /dev/null; echo "$?")
|
|
|
10 |
if [ $DBEXISTS -eq 0 ];then
|
|
|
11 |
echo "A database with the name $DBNAME already exists."
|
|
|
12 |
else
|
|
|
13 |
echo " database $DBNAME does not exist."
|
|
|
14 |
|
|
|
15 |
echo "*** MySQL - Creating tree database $DBNAME"
|
|
|
16 |
mysql -u lxr -plxrpw <<END_OF_CREATE
|
|
|
17 |
drop database if exists $DBNAME;
|
|
|
18 |
create database $DBNAME;
|
|
|
19 |
END_OF_CREATE
|
|
|
20 |
|
|
|
21 |
echo "*** MySQL - Configuring tables lxr_ in database $DBNAME"
|
|
|
22 |
mysql -u lxr -plxrpw <<END_OF_TEMPLATE
|
|
|
23 |
use $DBNAME;
|
|
|
24 |
|
|
|
25 |
drop table if exists lxr_filenum;
|
|
|
26 |
drop table if exists lxr_symnum;
|
|
|
27 |
drop table if exists lxr_typenum;
|
|
|
28 |
|
|
|
29 |
create table lxr_filenum
|
|
|
30 |
( rcd int primary key
|
|
|
31 |
, fid int
|
|
|
32 |
);
|
|
|
33 |
insert into lxr_filenum
|
|
|
34 |
(rcd, fid) VALUES (0, 0);
|
|
|
35 |
|
|
|
36 |
create table lxr_symnum
|
|
|
37 |
( rcd int primary key
|
|
|
38 |
, sid int
|
|
|
39 |
);
|
|
|
40 |
insert into lxr_symnum
|
|
|
41 |
(rcd, sid) VALUES (0, 0);
|
|
|
42 |
|
|
|
43 |
create table lxr_typenum
|
|
|
44 |
( rcd int primary key
|
|
|
45 |
, tid int
|
|
|
46 |
);
|
|
|
47 |
insert into lxr_typenum
|
|
|
48 |
(rcd, tid) VALUES (0, 0);
|
|
|
49 |
|
|
|
50 |
alter table lxr_filenum
|
|
|
51 |
engine = MyISAM;
|
|
|
52 |
alter table lxr_symnum
|
|
|
53 |
engine = MyISAM;
|
|
|
54 |
alter table lxr_typenum
|
|
|
55 |
engine = MyISAM;
|
|
|
56 |
|
|
|
57 |
|
|
|
58 |
/* Base version of files */
|
|
|
59 |
/* revision: a VCS generated unique id for this version
|
|
|
60 |
of the file
|
|
|
61 |
*/
|
|
|
62 |
create table lxr_files
|
|
|
63 |
( fileid int not null primary key
|
|
|
64 |
, filename varbinary(255) not null
|
|
|
65 |
, revision varbinary(255) not null
|
|
|
66 |
, constraint lxr_uk_files
|
|
|
67 |
unique (filename, revision)
|
|
|
68 |
, index lxr_filelookup (filename)
|
|
|
69 |
)
|
|
|
70 |
engine = MyISAM;
|
|
|
71 |
|
|
|
72 |
/* Status of files in the DB */
|
|
|
73 |
/* fileid: refers to base version
|
|
|
74 |
* relcount: number of releases associated with base version
|
|
|
75 |
* indextime: time when file was parsed for references
|
|
|
76 |
* status: set of bits with the following meaning
|
|
|
77 |
* 1 declaration have been parsed
|
|
|
78 |
* 2 references have been processed
|
|
|
79 |
* Though this table could be merged with 'files',
|
|
|
80 |
* performance is improved with access to a very small item.
|
|
|
81 |
*/
|
|
|
82 |
/* Deletion of a record automatically removes the associated
|
|
|
83 |
* base version files record.
|
|
|
84 |
*/
|
|
|
85 |
create table lxr_status
|
|
|
86 |
( fileid int not null primary key
|
|
|
87 |
, relcount int
|
|
|
88 |
, indextime int
|
|
|
89 |
, status tinyint not null
|
|
|
90 |
, constraint lxr_fk_sts_file
|
|
|
91 |
foreign key (fileid)
|
|
|
92 |
references lxr_files(fileid)
|
|
|
93 |
)
|
|
|
94 |
engine = MyISAM;
|
|
|
95 |
|
|
|
96 |
/* The following trigger deletes no longer referenced files
|
|
|
97 |
* (from releases), once status has been deleted so that
|
|
|
98 |
* foreign key constrained has been cleared.
|
|
|
99 |
*/
|
|
|
100 |
drop trigger if exists lxr_remove_file;
|
|
|
101 |
create trigger lxr_remove_file
|
|
|
102 |
after delete on lxr_status
|
|
|
103 |
for each row
|
|
|
104 |
delete from lxr_files
|
|
|
105 |
where fileid = old.fileid;
|
|
|
106 |
|
|
|
107 |
/* Aliases for files */
|
|
|
108 |
/* A base version may be known under several releaseids
|
|
|
109 |
* if it did not change in-between.
|
|
|
110 |
* fileid: refers to base version
|
|
|
111 |
* releaseid: "public" release tag
|
|
|
112 |
*/
|
|
|
113 |
create table lxr_releases
|
|
|
114 |
( fileid int not null
|
|
|
115 |
, releaseid varbinary(255) not null
|
|
|
116 |
, constraint lxr_pk_releases
|
|
|
117 |
primary key (fileid, releaseid)
|
|
|
118 |
, constraint lxr_fk_rls_fileid
|
|
|
119 |
foreign key (fileid)
|
|
|
120 |
references lxr_files(fileid)
|
|
|
121 |
)
|
|
|
122 |
engine = MyISAM;
|
|
|
123 |
|
|
|
124 |
/* The following triggers maintain relcount integrity
|
|
|
125 |
* in status table after insertion/deletion of releases
|
|
|
126 |
*/
|
|
|
127 |
drop trigger if exists lxr_add_release;
|
|
|
128 |
create trigger lxr_add_release
|
|
|
129 |
after insert on lxr_releases
|
|
|
130 |
for each row
|
|
|
131 |
update lxr_status
|
|
|
132 |
set relcount = relcount + 1
|
|
|
133 |
where fileid = new.fileid;
|
|
|
134 |
/* Note: a release is erased only when option --reindexall
|
|
|
135 |
* is given to genxref; it is thus necessary to reset status
|
|
|
136 |
* to cause reindexing, especially if the file is shared by
|
|
|
137 |
* several releases
|
|
|
138 |
*/
|
|
|
139 |
drop trigger if exists lxr_remove_release;
|
|
|
140 |
create trigger lxr_remove_release
|
|
|
141 |
after delete on lxr_releases
|
|
|
142 |
for each row
|
|
|
143 |
update lxr_status
|
|
|
144 |
set relcount = relcount - 1
|
|
|
145 |
-- , status = 0
|
|
|
146 |
where fileid = old.fileid
|
|
|
147 |
and relcount > 0;
|
|
|
148 |
|
|
|
149 |
/* Types for a language */
|
|
|
150 |
/* declaration: provided by generic.conf
|
|
|
151 |
*/
|
|
|
152 |
create table lxr_langtypes
|
|
|
153 |
( typeid smallint not null
|
|
|
154 |
, langid tinyint unsigned not null
|
|
|
155 |
, declaration varchar(255) not null
|
|
|
156 |
, constraint lxr_pk_langtypes
|
|
|
157 |
primary key (typeid, langid)
|
|
|
158 |
)
|
|
|
159 |
engine = MyISAM;
|
|
|
160 |
|
|
|
161 |
/* Symbol name dictionary */
|
|
|
162 |
/* symid: unique symbol id for name
|
|
|
163 |
* symcount: number of definitions and usages for this name
|
|
|
164 |
* symname: symbol name
|
|
|
165 |
*/
|
|
|
166 |
create table lxr_symbols
|
|
|
167 |
( symid int not null primary key
|
|
|
168 |
, symcount int
|
|
|
169 |
, symname varbinary(255) not null unique
|
|
|
170 |
)
|
|
|
171 |
engine = MyISAM;
|
|
|
172 |
|
|
|
173 |
/* The following function decrements the symbol reference count
|
|
|
174 |
* (to be used in triggers).
|
|
|
175 |
*/
|
|
|
176 |
delimiter //
|
|
|
177 |
create procedure lxr_decsym(in whichsym int)
|
|
|
178 |
begin
|
|
|
179 |
update lxr_symbols
|
|
|
180 |
set symcount = symcount - 1
|
|
|
181 |
where symid = whichsym
|
|
|
182 |
and symcount > 0;
|
|
|
183 |
end//
|
|
|
184 |
delimiter ;
|
|
|
185 |
|
|
|
186 |
/* Definitions */
|
|
|
187 |
/* symid: refers to symbol name
|
|
|
188 |
* fileid and line define the location of the declaration
|
|
|
189 |
* langid: language id
|
|
|
190 |
* typeid: language type id
|
|
|
191 |
* relid: optional id of the englobing declaration
|
|
|
192 |
* (refers to another symbol, not a definition)
|
|
|
193 |
*/
|
|
|
194 |
create table lxr_definitions
|
|
|
195 |
( symid int not null
|
|
|
196 |
, fileid int not null
|
|
|
197 |
, line int not null
|
|
|
198 |
, typeid smallint not null
|
|
|
199 |
, langid tinyint unsigned not null
|
|
|
200 |
, relid int
|
|
|
201 |
, index lxr_i_definitions (symid)
|
|
|
202 |
, constraint lxr_fk_defn_symid
|
|
|
203 |
foreign key (symid)
|
|
|
204 |
references lxr_symbols(symid)
|
|
|
205 |
, constraint lxr_fk_defn_fileid
|
|
|
206 |
foreign key (fileid)
|
|
|
207 |
references lxr_files(fileid)
|
|
|
208 |
, constraint lxr_fk_defn_type
|
|
|
209 |
foreign key (typeid, langid)
|
|
|
210 |
references lxr_langtypes(typeid, langid)
|
|
|
211 |
, constraint lxr_fk_defn_relid
|
|
|
212 |
foreign key (relid)
|
|
|
213 |
references lxr_symbols(symid)
|
|
|
214 |
)
|
|
|
215 |
engine = MyISAM;
|
|
|
216 |
|
|
|
217 |
/* The following trigger maintains correct symbol reference count
|
|
|
218 |
* after definition deletion.
|
|
|
219 |
*/
|
|
|
220 |
delimiter //
|
|
|
221 |
drop trigger if exists lxr_remove_definition;
|
|
|
222 |
create trigger lxr_remove_definition
|
|
|
223 |
after delete on lxr_definitions
|
|
|
224 |
for each row
|
|
|
225 |
begin
|
|
|
226 |
call lxr_decsym(old.symid);
|
|
|
227 |
if old.relid is not null
|
|
|
228 |
then call lxr_decsym(old.relid);
|
|
|
229 |
end if;
|
|
|
230 |
end//
|
|
|
231 |
delimiter ;
|
|
|
232 |
|
|
|
233 |
/* Usages */
|
|
|
234 |
create table lxr_usages
|
|
|
235 |
( symid int not null
|
|
|
236 |
, fileid int not null
|
|
|
237 |
, line int not null
|
|
|
238 |
, index lxr_i_usages (symid)
|
|
|
239 |
, constraint lxr_fk_use_symid
|
|
|
240 |
foreign key (symid)
|
|
|
241 |
references lxr_symbols(symid)
|
|
|
242 |
, constraint lxr_fk_use_fileid
|
|
|
243 |
foreign key (fileid)
|
|
|
244 |
references lxr_files(fileid)
|
|
|
245 |
)
|
|
|
246 |
engine = MyISAM;
|
|
|
247 |
|
|
|
248 |
/* The following trigger maintains correct symbol reference count
|
|
|
249 |
* after usage deletion.
|
|
|
250 |
*/
|
|
|
251 |
drop trigger if exists lxr_remove_usage;
|
|
|
252 |
create trigger lxr_remove_usage
|
|
|
253 |
after delete on lxr_usages
|
|
|
254 |
for each row
|
|
|
255 |
call lxr_decsym(old.symid);
|
|
|
256 |
|
|
|
257 |
delimiter //
|
|
|
258 |
create procedure lxr_PurgeAll ()
|
|
|
259 |
begin
|
|
|
260 |
set @old_check = @@session.foreign_key_checks;
|
|
|
261 |
set session foreign_key_checks = OFF;
|
|
|
262 |
truncate table lxr_filenum;
|
|
|
263 |
truncate table lxr_symnum;
|
|
|
264 |
truncate table lxr_typenum;
|
|
|
265 |
insert into lxr_filenum
|
|
|
266 |
(rcd, fid) VALUES (0, 0);
|
|
|
267 |
insert into lxr_symnum
|
|
|
268 |
(rcd, sid) VALUES (0, 0);
|
|
|
269 |
insert into lxr_typenum
|
|
|
270 |
(rcd, tid) VALUES (0, 0);
|
|
|
271 |
truncate table lxr_definitions;
|
|
|
272 |
truncate table lxr_usages;
|
|
|
273 |
truncate table lxr_langtypes;
|
|
|
274 |
truncate table lxr_symbols;
|
|
|
275 |
truncate table lxr_releases;
|
|
|
276 |
truncate table lxr_status;
|
|
|
277 |
truncate table lxr_files;
|
|
|
278 |
set session foreign_key_checks = @old_check;
|
|
|
279 |
end//
|
|
|
280 |
delimiter ;
|
|
|
281 |
END_OF_TEMPLATE
|
|
|
282 |
fi
|