PTD/SQLite.pm untested

filter: Statementstmt 0.0% 0 / 144 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 214 CC 13 SCAR 19.5
CC
13
Cov
0%
_save
20.0
trans
12.0
count
12.0
CRAP 7.1
1 package Lingua::PTD::SQLite;
2 $Lingua::PTD::SQLite::VERSION = '1.15';
3 use strict;
4 use warnings;
5  
6 use parent 'Lingua::PTD';
7 use DBI;
8  
9 =encoding UTF-8
10  
11 =head1 NAME
12  
13 Lingua::PTD::SQLite - Sub-module to handle PTD files in sqlite format
14  
15 =head1 SYNOPSIS
16  
17 use Lingua::PTD;
18  
19 $ptd = Lingua::PTD->new( "file.sqlite" );
20  
21 =head1 DESCRIPTION
22  
23 Check L<<Lingua::PTD>> for complete reference.
24  
25 =head1 SEE ALSO
26  
27 NATools(3), perl(1)
28  
29 =head1 AUTHOR
30  
31 Alberto Manuel Brandão Simões, E<lt>ambs@cpan.orgE<gt>
32  
33 =head1 COPYRIGHT AND LICENSE
34  
35 Copyright (C) 2010-2014 by Alberto Manuel Brandão Simões
36  
37 =cut
38  
39 sub new {
40 my ($class, $filename) = @_;
41 my $dbh = DBI->connect("dbi:SQLite:dbname=$filename", "", "",
42 { sqlite_unicode => 1} ) or die "Cant connect to database";
43 my $self = {
44 dbh => $dbh,
45 get_meta => $dbh->prepare("SELECT v FROM meta WHERE k = ?;"),
46 get_occs => $dbh->prepare("SELECT occ FROM occs WHERE w = ?;"),
47 exists => $dbh->prepare("SELECT w FROM trans WHERE w = ?;"),
48 };
49 bless $self => $class # amen
50 }
51  
52 sub trans {
53 my ($self, $word, $trans) = @_;
54 if ($trans) {
55 my $sth = $self->{dbh}->prepare("SELECT p FROM trans WHERE w = ? AND t = ?;");
56 $sth->execute($word, $trans);
57 my @row = $sth->fetchrow_array;
58 return (@row)?1:0;
59 } else {
60 my $sth = $self->{dbh}->prepare("SELECT t FROM trans WHERE w = ?");
61 $sth->execute($word);
62 my @ans;
63 my @row;
64 while (@row = $sth->fetchrow_array) { push @ans, $row[0] };
65 return @ans;
66 }
67 }
68  
69  
70 sub prob {
71 my ($self, $word, $trans) = @_;
72 my $sth = $self->{dbh}->prepare("SELECT p FROM trans WHERE w = ? AND t = ?;");
73 $sth->execute($word, $trans);
74 my @row = $sth->fetchrow_array;
75 return (@row)?$row[0]:0;
76 }
77  
78 sub words {
79 my $self = shift;
80 my $sort = $_[0] ? "ORDER BY w ASC" : "";
81  
82 my $sth = $self->{dbh}->prepare("SELECT w FROM occs $sort;");
83 $sth->execute;
84 my @answer = ();
85 my @row;
86 while (@row = $sth->fetchrow_array()) { push @answer, $row[0] };
87 return @answer;
88 }
89  
90 sub exists {
91 my ($self, $word) = @_;
92 $self->{exists}->execute($word);
93 if ($self->{exists}->fetchrow_array()) {
94 return 1;
95 } else {
96 return 0;
97 }
98 }
99  
100 sub _calculate_sizes {
101 my $self = shift;
102 my $sth = $self->{dbh}->prepare("SELECT COUNT(w) FROM occs");
103 $sth->execute;
104 my @row = $sth->fetchrow_array;
105 $self->_update_meta("count", $row[0]);
106  
107 $sth = $self->{dbh}->prepare("SELECT SUM(occ) FROM occs");
108 $sth->execute;
109 @row = $sth->fetchrow_array;
110 $self->_update_meta("size", $row[0]);
111 }
112  
113 sub _set_word_translation {
114 my ($self, $w, $t, $p) = @_;
115 my $sth = $self->{dbh}->prepare("INSERT OR REPLACE INTO trans VALUES(?,?,?);");
116 $sth->execute($w, $t, $p);
117 }
118  
119 sub _delete_word_translation {
120 my ($self, $w, $t) = @_;
121 my $sth = $self->{dbh}->prepare("DELETE FROM trans WHERE w = ? AND t = ?");
122 $sth->execute($w, $t);
123 }
124  
125 sub _set_word_count {
126 my ($self, $w, $c) = @_;
127 my $sth = $self->{dbh}->prepare("INSERT OR REPLACE INTO occs VALUES(?,?);");
128 $sth->execute($w, $c);
129 }
130  
131 sub _update_meta {
132 my ($self, $k, $v) = @_;
133 my $sth = $self->{dbh}->prepare("INSERT OR REPLACE INTO meta VALUES(?,?);");
134 $sth->execute($k,$v);
135 }
136  
137  
138 sub size {
139 my $self = shift;
140 $self->{get_meta}->execute("size");
141 my @row = $self->{get_meta}->fetchrow_array;
142 return @row ? $row[0] : 0;
143 }
144  
145 sub count {
146 my ($self, $word) = @_;
147 if ($word) {
148 $self->{get_occs}->execute($word);
149 my @row = $self->{get_occs}->fetchrow_array;
150 return (@row)?$row[0]:0;
151 } else {
152 $self->{get_meta}->execute("count");
153 my @row = $self->{get_meta}->fetchrow_array;
154 return $row[0];
155 }
156 }
157  
158 sub _init_transaction {
159 my $self = shift;
160 $self->{dbh}->begin_work;
161 }
162  
163 sub _commit {
164 my $self = shift;
165 $self->{dbh}->commit;
166 }
167  
168 sub _update_word {
169 my ($self, $word, $entry) = @_;
170  
171 my ($k) = keys %$entry;
172 $entry = $entry->{$k};
173  
174 my $sth;
175  
176 if ($k ne $word) {
177 $sth = $self->{dbh}->prepare("DELETE FROM occs WHERE w = ?");
178 $sth->execute($word);
179  
180 $sth = $self->{dbh}->prepare("DELETE FROM trans WHERE w = ?");
181 $sth->execute($word);
182  
183 $word = $k;
184 }
185  
186 $sth = $self->{dbh}->prepare("DELETE FROM occs WHERE w = ?");
187 $sth->execute($word);
188  
189 $sth = $self->{dbh}->prepare("INSERT INTO occs VALUES(?, ?);");
190 $sth->execute($word, $entry->{count}, );
191  
192 $sth = $self->{dbh}->prepare("DELETE FROM trans WHERE w = ?");
193 $sth->execute($word);
194  
195 $sth = $self->{dbh}->prepare("INSERT INTO trans VALUES (?,?,?)");
196 for my $t (keys %{$entry->{trans}}) {
197 $sth->execute($word, $t, $entry->{trans}{$t});
198 }
199 }
200  
201 sub _delete_word {
202 my ($self, $word) = @_;
203 my $sth = $self->{dbh}->prepare("DELETE FROM trans WHERE w = ?");
204 $sth->execute($word);
205 $sth = $self->{dbh}->prepare("DELETE FROM occs WHERE w = ?");
206 $sth->execute($word);
207 }
208  
209 sub _trans_hash {
210 my ($self, $trans) = @_;
211 my $sth = $self->{dbh}->prepare("SELECT t, p FROM trans WHERE w = ?");
212 $sth->execute($trans);
213 my %t;
214 my @row;
215 while (@row = $sth->fetchrow_array) {
216 $t{$row[0]} = $row[1];
217 }
218 return %t;
219 }
220  
221 sub _save {
222 my ($self, $filename) = @_;
223  
224 unlink $filename if -f $filename;
225 warn "Cant write on $filename" and return 0 if -f $filename;
226  
227 my $dbh = DBI->connect("dbi:SQLite:dbname=$filename", "", "", {sqlite_unicode => 1});
228 warn "Cant create sqlite file" and return 0 unless $dbh;
229  
230 $dbh->do("CREATE TABLE trans (w, t, p REAL);");
231 $dbh->do("CREATE TABLE occs (w, occ INTEGER);");
232 $dbh->do("CREATE TABLE meta (k PRIMARY KEY, v);");
233  
234 my $insert_meta = $dbh->prepare("INSERT INTO meta VALUES (?, ?);");
235 my $insert_trans = $dbh->prepare("INSERT INTO trans VALUES (?,?,?);");
236 my $insert_occs = $dbh->prepare("INSERT INTO occs VALUES (?, ?);");
237  
238 $dbh->begin_work;
239  
240 $insert_meta->execute("size", $self->size);
241 $insert_meta->execute("count", $self->count);
242  
243 $self->downtr( sub {
244 my ($w, $c, %t) = @_;
245 $insert_occs->execute($w, $c);
246 for my $t (keys %t) {
247 $insert_trans->execute($w, $t, $t{$t});
248 }
249 });
250  
251 $dbh->commit;
252  
253 $dbh->do("CREATE INDEX transPM ON trans (w,t)");
254 $dbh->do("CREATE INDEX occsPM ON occs (w)");
255  
256 return 1;
257 }
258  
259  
260 "This isn't right. This isn't even wrong.";
261 __END__