blib/lib/Biblio/Thesaurus/SQLite.pm

filter: Statementstmt 34.0% 46 / 135 Branchbran 8.1% 7 / 86 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 56.2% 9 / 16 Podpod 62.5% 5 / 8 totaltotal 27.3% 67 / 245 CC 53 SCAR 37.4
CC
53
Cov
24%
ISOthe2TheSql
82.7
getTermAsPerl
56.0
changeTerm
56.0
CRAP 42.1
↑ Summary
1 package Biblio::Thesaurus::SQLite;
2  
3 2 ▶use 5.008006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 require Exporter;
8 2 ▶use DBIx::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Biblio::Thesaurus;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our @ISA = qw(Exporter);
14 2 our %EXPORT_TAGS = ( 'all' => [
15 qw(ISOthe2TheSql
16 TheSql2ISOthe
17 getTermAsXHTML
18 getTermAsISOthe
19 getTermAsPerl
20 setTerm
21 deleteTerm
22 changeTerm
23 ) ] );
24  
25 2 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
26 2 our @EXPORT = qw();
27 2 our $VERSION = '0.10';
28  
29 2 our ($rel,@terms,$term);
30  
31 ##
32 # This method converts a ISO Thesaurus file in a SQLite database
33 # @param ficheiro de texto iso thesaurus
34 # @param ficheiro com base de dados sqlite
35 1 ▶sub ISOthe2TheSql {
Subroutine ISOthe2TheSql: called CC=11 SCAR=44.2
Pod documented
36 1 ▶ my $file = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $file = shift() or die 1 0 1
37 1 ▶ my $dbfile = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $dbfile = shift() or die 1 0 1
38  
39 # parse the thesaurus file
40 1 my $the = thesaurusLoad($file);
41 # connect to the database
42 1 ▶ my $db = DBIx::Simple->connect('dbi:SQLite:' . $dbfile)
Branch: 1 branch
BranchTrueFalseTotal
my $db = "DBIx::Simple"->connect('dbi:SQLite:' . $dbfile) or die "DBIx::Simple"->error 1 0 1
43 or die DBIx::Simple->error;
44  
45 # clear the database! TODO: check if the database exists and try
46 # to append the new data
47 1 $db->query('DROP TABLE rel');
48 0 $db->query('CREATE TABLE rel (term, rel, def)');
49 0 $db->query('DROP TABLE meta');
50 0 $db->query('CREATE TABLE meta (term, val)');
51 0 $db->query('DROP TABLE lang');
52 0 $db->query('CREATE TABLE lang (ori, lang, dest)');
53  
54 # parse metadata (we need this cause I dunno if Biblio::Thesaurus
55 # handles this the right way O:-))
56 0 ▶ open(F, "<$file") or die;
Branch: 1 branch
BranchTrueFalseTotal
open F, "<$file" or die 0 0 0
57 0 $db->begin_work;
58 0 my $lang_dest = '';
59 0 ▶ while(<F>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline F)) 0 0 0
60 0 chomp;
61 # this is metadata (starting with %)
62 0 ▶ if($_ =~ /^\%([^\s]+)\s+(.*)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ =~ /^\%([^\s]+)\s+(.*)/l) { } 0 0 0
elsif ($_ =~ /(.*)==(.*)/l) { } 0 0 0
63 0 my @vals = split(/\s+/, $2);
64 0 for (@vals) {
65 0 $db->query(
66 'INSERT INTO meta VALUES (?, ?)',
67 $1, $_
68 );
69 }
70 }
71 # this is language data
72 elsif($_ =~ /(.*)==(.*)/) {
73 0 ▶ if($1 eq $the->baselang) {
Branch: 2 branches
BranchTrueFalseTotal
if ($1 eq $the->baselang) { } 0 0 0
elsif ($lang_dest ne '') { } 0 0 0
74 0 $lang_dest = $2;
75 }
76 elsif($lang_dest ne '') {
77 0 $db->query(
78 'INSERT INTO lang VALUES (?, ?, ?)',
79 $1, $lang_dest, $2
80 );
81 }
82 }
83 }
84 0 $db->commit;
85 0 close(F);
86
87 # parse all terms :D (the hard work is handed by Biblio::Thesaurus
88 0 $db->begin_work;
89 print $the->downtr (
90 {
91 0 ▶ -default => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
92 # ignore language data...
93 0 ▶ return '' if ($term.$rel) =~ /.*==.*/;
Branch: 1 branch
BranchTrueFalseTotal
if ($Biblio::Thesaurus::SQLite::term . $Biblio::Thesaurus::SQLite::rel) =~ /.*==.*/l 0 0 0
94 0 for (@terms) {
95 0 $db->query(
96 'INSERT INTO rel VALUES (?, ?, ?)',
97 $term, $rel, $_
98 );
99 }
100 },
101 }
102 0 );
103 0 $db->commit;
104  
105 }
106  
107 ##
108 # This method convert a SQLite database to a ISO thesaurus text file
109 # @param The SQLite database
110 # @param The output ISO Thesaurus file
111 # @note This method is VERY VERY slow! I tried to know why, run a profiller
112 # and saw that most of the time we are consuming CPU in the ->hashes
113 # function of the DBIx::Simple module.... TODO: get this think faster :D
114 2 ▶sub TheSql2ISOthe {
Subroutine TheSql2ISOthe: called CC=9 SCAR=25.5
Pod documented
115 2 ▶ my $dbfile = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $dbfile = shift() or die 2 0 2
116 2 ▶ my $file = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $file = shift() or die 2 0 2
117  
118 # ok so this is easy :D
119 # connect to the database
120 2 ▶ my $db = DBIx::Simple->connect('dbi:SQLite:' . $dbfile)
Branch: 1 branch
BranchTrueFalseTotal
my $db = "DBIx::Simple"->connect('dbi:SQLite:' . $dbfile) or die "DBIx::Simple"->error 2 0 2
121 or die DBIx::Simple->error;
122  
123 # process meta-data
124 2 open(F, ">$file");
125 2 for my $row ($db->query('SELECT DISTINCT term FROM meta')->flat) {
126 0 print F '%' . $row . ' ' .
127 join(' ',
128 $db->query(
129 'SELECT val FROM META WHERE term = ?',
130 $row
131 )->flat
132 ), "\n";
133 }
134  
135 # process translations
136 2 $db->query('SELECT val FROM meta WHERE term = ?', 'baselang')->into(my $baselang);
137 2 ▶ if(defined($baselang)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $baselang) 0 2 2
138 0 $db->query('SELECT lang FROM lang LIMIT 1')->into(my $lang);
139 0 print F $baselang, '==', $lang, "\n\n";
140 0 for my $row ($db->query('SELECT * FROM lang')->hashes) {
141 0 print F $row->{ori}, '==', $row->{dest}, "\n";
142 }
143 }
144  
145 # process the main data
146 2 for my $row ($db->query('SELECT DISTINCT term FROM rel')->flat) {
147 4 print F "\n\n$row\n";
148 4 for my $row2 ($db->query('SELECT rel, def FROM rel WHERE term = ?', $row)->hashes) {
149 4 print F $row2->{rel}, ' ', $row2->{def}, "\n";
150 }
151 }
152 2 close(F);
153 }
154  
155 ##
156 # this method tries to output the result of a term as a xhtml table
157 # maybe to use with a cgi module
158 # @param the term to find data
159 # @param the sqlite database file
160 0 ▶sub getTermAsXHTML {
Subroutine getTermAsXHTML: not called CC=6 SCAR=37.4
Pod undocumented
161 0 ▶ my $termo = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $termo = shift() or die 0 0 0
162 0 ▶ my $dbfile = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $dbfile = shift() or die 0 0 0
163  
164 # connect to the database
165 0 ▶ my $db = DBIx::Simple->connect('dbi:SQLite:' . $dbfile)
Branch: 1 branch
BranchTrueFalseTotal
my $db = "DBIx::Simple"->connect('dbi:SQLite:' . $dbfile) or die "DBIx::Simple"->error 0 0 0
166 or die DBIx::Simple->error;
167  
168 # try to see if we got any results avaiable
169 0 my $count;
170 0 $db->query(
171 'SELECT COUNT(term) FROM rel WHERE term = ?'
172 , $termo
173 )->into($count);
174  
175 0 ▶ return ( '<h3>Termo <emph>' . $termo .
Branch: 1 branch
BranchTrueFalseTotal
if $count == 0 0 0 0
176 '</emph> nao encontrado</h3>' )
177 if $count == 0;
178  
179 # now starting the output of the table
180 0 my $res = '<b1>' . $termo . '<b1><table><th><td>Relacao</td>' .
181 '<td>Definição</td></th>' . "\n";
182 # this is ugly....
183 0 for my $row ($db->query('SELECT * FROM rel WHERE term = ?',
184 $termo)->hashes) {
185 $res .= '<tr><td>' . $row->{rel} .
186 '</td><td>' . $row->{def} .
187 0 '</td></tr>' . "\n";
188 }
189 0 $res .= '</table>';
190  
191 0 return $res;
192 }
193  
194 ##
195 # Does the same thing as the previous method, but outputs the data in an
196 # ISO Thesaurus format
197 # @param .....
198 # @param guess w00t ?
199 0 ▶sub getTermAsISOthe {
Subroutine getTermAsISOthe: not called CC=6 SCAR=37.4
Pod undocumented
200 0 ▶ my $termo = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $termo = shift() or die 0 0 0
201 0 ▶ my $dbfile = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $dbfile = shift() or die 0 0 0
202  
203 # connect to the database
204 0 ▶ my $db = DBIx::Simple->connect('dbi:SQLite:' . $dbfile)
Branch: 1 branch
BranchTrueFalseTotal
my $db = "DBIx::Simple"->connect('dbi:SQLite:' . $dbfile) or die "DBIx::Simple"->error 0 0 0
205 or die DBIx::Simple->error;
206  
207 0 my $count;
208 0 $db->query(
209 'SELECT COUNT(term) FROM rel WHERE term = ?'
210 , $termo
211 )->into($count);
212  
213 0 ▶ return '' if $count == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count == 0 0 0 0
214
215 0 my $res = $termo . "\n";
216 0 for my $row ($db->query('SELECT * FROM rel WHERE term = ?',
217 $termo)->hashes) {
218 $res .= '- ' . $row->{rel} . ' -> ' .
219 0 $row->{def} . "\n";
220 }
221  
222 0 chomp($res);
223 0 return $res;
224 }
225  
226 ##
227 # bla bla bla (i'm tired of this...)
228 # ....
229 # ....
230 0 ▶sub getTermAsPerl {
Subroutine getTermAsPerl: not called CC=7 SCAR=40.3
Pod undocumented
231 0 ▶ my $termo = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $termo = shift() or die 0 0 0
232 0 ▶ my $dbfile = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $dbfile = shift() or die 0 0 0
233  
234 0 my %res; # our data!
235 0 $res{$termo} = {};
236  
237 # connect to the database
238 0 ▶ my $db = DBIx::Simple->connect('dbi:SQLite:' . $dbfile)
Branch: 1 branch
BranchTrueFalseTotal
my $db = "DBIx::Simple"->connect('dbi:SQLite:' . $dbfile) or die "DBIx::Simple"->error 0 0 0
239 or die DBIx::Simple->error;
240  
241 0 my $count;
242 0 $db->query(
243 'SELECT COUNT(term) FROM rel WHERE term = ?'
244 , $termo
245 )->into($count);
246  
247 0 ▶ return Dumper \%res if $count == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count == 0 0 0 0
248
249 0 for my $row ($db->query('SELECT * FROM rel WHERE term = ?',
250 $termo)->hashes) {
251 0 my $mainhash = $res{$termo};
252 0 my $termoarray = $mainhash->{$row->{rel}};
253 0 ▶ $termoarray = [] unless defined $termoarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $termoarray 0 0 0
254 0 push @$termoarray, $row->{def};
255 0 $mainhash->{$row->{rel}} = $termoarray;
256 0 $res{$termo} = $mainhash;
257
258 }
259  
260 0 return Dumper \%res;
261 }
262  
263 ##
264 # Well, a new method! Add the new term to the sqlite database
265 # TODO: do some cheking before blinding insert the data?
266 # @param the term to insert
267 # @param the relation
268 # @param the definition
269 # @param the database file
270 0 ▶sub setTerm {
Subroutine setTerm: not called CC=6 SCAR=37.4
Pod documented
271 0 ▶ my $termo = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $termo = shift() or die 0 0 0
272 0 ▶ my $rel = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $rel = shift() or die 0 0 0
273 0 ▶ my $def = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $def = shift() or die 0 0 0
274 0 ▶ my $dbfile = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $dbfile = shift() or die 0 0 0
275  
276 # connect to the database
277 0 ▶ my $db = DBIx::Simple->connect('dbi:SQLite:' . $dbfile)
Branch: 1 branch
BranchTrueFalseTotal
my $db = "DBIx::Simple"->connect('dbi:SQLite:' . $dbfile) or die "DBIx::Simple"->error 0 0 0
278 or die DBIx::Simple->error;
279  
280 0 $db->query('INSERT INTO rel VALUES (?, ?, ?)', $termo, $rel, $def);
281 }
282  
283 ##
284 # Delete the term
285 0 ▶sub deleteTerm {
Subroutine deleteTerm: not called CC=6 SCAR=37.4
Pod documented
286 0 ▶ my $termo = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $termo = shift() or die 0 0 0
287 0 ▶ my $rel = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $rel = shift() or die 0 0 0
288 0 ▶ my $def = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $def = shift() or die 0 0 0
289 0 ▶ my $dbfile = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $dbfile = shift() or die 0 0 0
290
291 # connect to the database
292 0 ▶ my $db = DBIx::Simple->connect('dbi:SQLite:' . $dbfile)
Branch: 1 branch
BranchTrueFalseTotal
my $db = "DBIx::Simple"->connect('dbi:SQLite:' . $dbfile) or die "DBIx::Simple"->error 0 0 0
293 or die DBIx::Simple->error;
294  
295 0 $db->query('DELETE FROM rel WHERE term = ? AND rel = ? AND def = ?',
296 $termo, $rel, $def);
297 0 $db->query('DELETE FROM rel WHERE term = ? AND rel = ? AND def = ?',
298 $def, $rel, $termo);
299 }
300  
301 ##
302 # Change the term in the database
303 # @param term to change
304 # @param old relation
305 # @param old definition
306 # @param new relation
307 # @param new definition
308 # @param the sqlite database file
309 0 ▶sub changeTerm {
Subroutine changeTerm: not called CC=7 SCAR=40.3
Pod documented
310 0 ▶ my $termo = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $termo = shift() or die 0 0 0
311 0 ▶ my $oldrel = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $oldrel = shift() or die 0 0 0
312 0 ▶ my $olddef = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $olddef = shift() or die 0 0 0
313 0 ▶ my $newrel = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $newrel = shift() or die 0 0 0
314 0 ▶ my $newdef = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $newdef = shift() or die 0 0 0
315 0 ▶ my $dbfile = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $dbfile = shift() or die 0 0 0
316  
317 0 deleteTerm($termo, $oldrel, $olddef, $dbfile);
318 # use our beautiful setTerm :)
319 0 setTerm($termo, $newrel, $newdef, $dbfile);
320 }
321  
322 2 1;
323  
324 __END__
↑ Summary