1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
|
package LXRng::Index::DBI;
use strict;
use DBI;
use base qw(LXRng::Index::Generic);
sub transaction {
my ($self, $code) = @_;
if ($self->dbh->{AutoCommit}) {
$self->dbh->{AutoCommit} = 0;
$code->();
$self->dbh->{AutoCommit} = 1;
}
else {
# If we're in a transaction already, don't return to
# AutoCommit state.
$code->();
}
$self->dbh->commit();
}
sub _to_task {
my ($self, $rfile_id, $task) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'_to_task_ins'} ||=
$dbh->prepare(qq{insert into ${pre}filestatus(id_rfile)
select ? where not exists
(select 1 from ${pre}filestatus
where id_rfile = ?)});
$sth->execute($rfile_id, $rfile_id);
$sth = $$self{'sth'}{'_to_task_upd'}{$task} ||=
$dbh->prepare(qq{update ${pre}filestatus set $task = 't'
where $task = 'f' and id_rfile = ?});
return $sth->execute($rfile_id) > 0;
}
sub to_index {
my ($self, $rfile_id) = @_;
return $self->_to_task($rfile_id, 'indexed');
}
sub to_reference {
my ($self, $rfile_id) = @_;
return $self->_to_task($rfile_id, 'referenced');
}
sub to_hash {
my ($self, $rfile_id) = @_;
return $self->_to_task($rfile_id, 'hashed');
}
sub _get_tree {
my ($self, $tree) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'_get_tree'} ||=
$dbh->prepare(qq{select id from ${pre}trees where name = ?});
my $id;
if ($sth->execute($tree) > 0) {
($id) = $sth->fetchrow_array();
}
$sth->finish();
return $id;
}
sub _get_release {
my ($self, $tree_id, $release) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'_get_release'} ||=
$dbh->prepare(qq{select id from ${pre}releases
where id_tree = ? and release_tag = ?});
my $id;
if ($sth->execute($tree_id, $release) > 0) {
($id) = $sth->fetchrow_array();
}
$sth->finish();
return $id;
}
sub _get_file {
my ($self, $path) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'_get_file'} ||=
$dbh->prepare(qq{select id from ${pre}files where path = ?});
my $id;
if ($sth->execute($path) > 0) {
($id) = $sth->fetchrow_array();
}
$sth->finish();
return $id;
}
sub _get_rfile_by_release {
my ($self, $rel_id, $path) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'_get_rfile_by_release'} ||=
$dbh->prepare(qq{select r.id
from ${pre}filereleases fr, ${pre}files f,
${pre}revisions r
where fr.id_rfile = r.id and r.id_file = f.id
and fr.id_release = ? and f.path = ?});
my $id;
if ($sth->execute($rel_id, $path) > 0) {
($id) = $sth->fetchrow_array();
}
$sth->finish();
return $id;
}
sub _get_symbol {
my ($self, $symbol) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'_get_symbol'} ||=
$dbh->prepare(qq{select id from ${pre}symbols where name = ?});
my $id;
if ($sth->execute($symbol) > 0) {
($id) = $sth->fetchrow_array();
}
$sth->finish();
return $id;
}
sub _add_include {
my ($self, $file_id, $inc_id) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'_add_include'} ||=
$dbh->prepare(qq{insert into ${pre}includes(id_rfile, id_include_path)
values (?, ?)});
my $id;
$sth->execute($file_id, $inc_id);
return 1;
}
sub _includes_by_id {
my ($self, $file_id) = @_;
}
sub _symbol_by_id {
my ($self, $id) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'_symbol_by_id'} ||=
$dbh->prepare(qq{select * from ${pre}symbols
where id = ?});
my @res;
if ($sth->execute($id) > 0) {
@res = $sth->fetchrow_array();
}
$sth->finish();
return @res;
}
sub _identifiers_by_name {
my ($self, $rel_id, $symbol) = @_;
my $sym_id = $self->_get_symbol($symbol);
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'_identifiers_by_name'} ||=
$dbh->prepare(qq{
select i.id, i.type, f.path, i.line, s.name, c.type, c.id,
i.id_rfile
from ${pre}identifiers i
left outer join ${pre}identifiers c on i.context = c.id
left outer join ${pre}symbols s on c.id_symbol = s.id,
${pre}files f, ${pre}filereleases r, ${pre}revisions v
where i.id_rfile = v.id and v.id = r.id_rfile
and r.id_release = ? and v.id_file = f.id
and i.id_symbol = ?});
$sth->execute($rel_id, $sym_id);
my $res = $sth->fetchall_arrayref();
use Data::Dumper;
foreach my $def (@$res) {
# warn Dumper($def);
$$def[7] = 42;
# my @files = $self->get_referring_files($rel_id, $$def[7]);
# warn Dumper(\@files);
}
return $res;
}
sub _symbols_by_file {
my ($self, $rfile_id) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'_symbols_by_file'} ||=
$dbh->prepare(qq{select distinct s.name
from ${pre}usage u, ${pre}symbols s
where id_rfile = ? and u.id_symbol = s.id});
$sth->execute($rfile_id);
my %res;
while (my ($symname) = $sth->fetchrow_array()) {
$res{$symname} = 1;
}
return \%res;
}
sub _add_usage {
my ($self, $file_id, $line, $symbol_id) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'_add_usage'} ||=
$dbh->prepare(qq{insert into ${pre}usage(id_rfile, line, id_symbol)
values (?, ?, ?)});
$sth->execute($file_id, $line, $symbol_id);
return 1;
}
sub _usage_by_file {
my ($self, $rfile_id) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'_usage_by_file'} ||=
$dbh->prepare(qq{select s.name, u.line
from ${pre}usage u, ${pre}symbols s
where id_rfile = ? and u.id_symbol = s.id});
$sth->execute($rfile_id);
die "Unimplemented";
}
sub _rfile_path_by_id {
my ($self, $rfile_id) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'_rfile_path_by_id'} ||=
$dbh->prepare(qq{select f.path from ${pre}files f, ${pre}revisions r
where f.id = r.id_file and r.id = ?});
my $path;
if ($sth->execute($rfile_id) > 0) {
($path) = $sth->fetchrow_array();
}
$sth->finish();
return $path;
}
sub _get_includes_by_file {
my ($self, $res, $rel_id, @rfile_ids) = @_;
my $placeholders = join(', ', ('?') x @rfile_ids);
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $dbh->prepare(qq{select rf.id, f.path
from ${pre}revisions rf,
${pre}filereleases v,
${pre}includes i,
${pre}revisions ri,
${pre}files f
where rf.id = i.id_rfile
and rf.id_file = f.id
and rf.id = v.id_rfile
and v.id_release = ?
and i.id_include_path = ri.id_file
and ri.id in ($placeholders)});
$sth->execute($rel_id, @rfile_ids);
my $files = $sth->fetchall_arrayref();
$sth->finish();
my @recurse;
foreach my $r (@$files) {
push(@recurse, $$r[0]) unless exists($$res{$$r[0]});
$$res{$$r[0]} = $$r[1];
}
$self->_get_includes_by_file($res, $rel_id, @recurse) if @recurse;
return 1;
}
sub add_hashed_document {
my ($self, $rfile_id, $doc_id) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'add_hashed_document'} ||=
$dbh->prepare(qq{insert into ${pre}hashed_documents(id_rfile, doc_id)
values (?, ?)});
$sth->execute($rfile_id, $doc_id);
return 1;
}
sub get_hashed_document {
my ($self, $rfile_id) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'get_hashed_document'} ||=
$dbh->prepare(qq{select doc_id from ${pre}hashed_documents
where id_rfile = ?});
my $doc_id;
if ($sth->execute($rfile_id) > 0) {
($doc_id) = $sth->fetchrow_array();
}
$sth->finish();
return $doc_id;
}
sub get_symbol_usage {
my ($self, $rel_id, $symid) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'get_symbol_usage'} ||=
$dbh->prepare(qq{
select u.id_rfile, u.line
from ${pre}usage u, ${pre}filereleases fr
where u.id_symbol = ?
and u.id_rfile = fr.id_rfile and fr.id_release = ?});
$sth->execute($symid, $rel_id);
my $res = $sth->fetchall_arrayref();
$sth->finish();
return $res;
}
sub get_identifier_info {
my ($self, $ident, $rel_id) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'get_identifier_info'} ||=
$dbh->prepare(qq{
select s.name, s.id,
i.id, i.type, f.path, i.line, cs.name, c.type, c.id,
i.id_rfile
from ${pre}identifiers i
left outer join ${pre}identifiers c on i.context = c.id
left outer join ${pre}symbols cs on c.id_symbol = cs.id,
${pre}symbols s, ${pre}revisions r, ${pre}files f
where i.id = ? and i.id_symbol = s.id
and i.id_rfile = r.id and r.id_file = f.id});
# select i.id_rfile, f.path, i.line, i.type, i.context, s.id, s.name
# from identifiers i, symbols s, revisions r, files f
# where i.id = ? and i.id_symbol = s.id
# and i.id_rfile = r.id and r.id_file = f.id});
unless ($sth->execute($ident) == 1) {
return undef;
}
my ($symname, $symid,
$iid, $type, $path, $line, $cname, $ctype, $cid, $rfile_id) =
$sth->fetchrow_array();
$sth->finish();
my $refs = {$rfile_id => $path};
$self->get_referring_files($rel_id, $rfile_id, $refs);
my $usage = $self->get_symbol_usage($rel_id, $symid);
my %reflines;
foreach my $u (@$usage) {
next unless $$refs{$$u[0]};
$reflines{$$refs{$$u[0]}} ||= [];
push(@{$reflines{$$refs{$$u[0]}}}, $$u[1]);
}
return ($symname, $symid,
[$iid, $type, $path, $line, $cname, $ctype, $cid, $rfile_id],
\%reflines);
}
sub get_rfile_timestamp {
my ($self, $rfile_id) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
my $sth = $$self{'sth'}{'get_rfile_timestamp'} ||=
$dbh->prepare(qq{
select extract(epoch from last_modified_gmt)::integer,
last_modified_tz
from ${pre}revisions where id = ?});
unless ($sth->execute($rfile_id) == 1) {
return undef;
}
my ($epoch, $tz) = $sth->fetchrow_array();
$sth->finish();
return ($epoch, $tz);
}
1;
|