lib/Serge/DB.pm

filter: Statementstmt 75.6% 333 / 440 Branchbran 41.0% 114 / 278 Conditioncond 63.6% 7 / 11 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 80.3% 49 / 61 Podpod 0.0% 0 / 32 totaltotal 60.9% 505 / 828 SCAR 71.5
CC
145
Cov
62%
get_file_completeness_ratio
110.0
_check_file_id
30.0
_check_string_id
30.0
CRAP 1273.6
« lib/Serge/Engine/Plugin/parse_pot.pm ↑ Summary lib/Serge/Engine/Plugin/completeness.pm »
1 package Serge::DB;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Digest::MD5 qw(md5);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Encode qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $DEBUG = $ENV{CI} ne ''; # use debug mode from under CI environment to ensure better coverage
14  
15 2 my $DBD_PARAMS = {
16 'SQLite' => {
17 'options' => {
18 'sqlite_unicode' => 1,
19 },
20 'do' => 'PRAGMA cache_size = 10000', # 10 MB of cache instead of default 2 MB
21 'begin_transaction_stmt' => 'BEGIN',
22 },
23  
24 'SQLite_pre_1_26_06' => {
25 'options' => {
26 'unicode' => 1, # old version of DBD::SQLite used 'unicode' instead of 'sqlite_unicode'
27 },
28 'do' => 'PRAGMA cache_size = 10000', # 10 MB of cache instead of default 2 MB
29 'begin_transaction_stmt' => 'BEGIN',
30 },
31  
32 'mysql' => {
33 'options' => {
34 'mysql_enable_utf8' => 1,
35 'mysql_bind_type_guessing' => 1,
36 },
37 'begin_transaction_stmt' => 'START TRANSACTION',
38 },
39  
40 'Pg' => { # Postgres
41 'options' => {
42 'pg_enable_utf8' => 1,
43 },
44 'begin_transaction_stmt' => 'BEGIN',
45 }
46 };
47  
48 #
49 # Initialize object
50 #
51 sub new {
52 7 ▶ my ($class) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
53  
54 7 my $self = {
55 dbh => undef,
56 prepare_cache => {},
57 transaction_opened => undef,
58 dsn => {},
59 };
60  
61 7 bless $self, $class;
62 7 return $self;
63 }
64  
65 sub open {
66 6 ▶ my ($self, $source, $username, $password) = @_;
Subroutine open: called CC=12 SCAR=26.2
Pod undocumented
67  
68 6 ▶ $self->close if $self->{dbh}; # close previous connection
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dbh"} 0 6 6
69  
70 6 ▶ if ($ENV{SERGE_NO_TRANSACTIONS}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"SERGE_NO_TRANSACTIONS"}) 0 6 6
71 0 ▶ print "SERGE_NO_TRANSACTIONS is ON\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::DB::DEBUG 0 0 0
72 }
73  
74 6 ▶ if ($source =~ m/^DBI:(.*?):/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source =~ /^DBI:(.*?):/) { } 6 0 6
75 6 my $type = $1;
76 6 my $schema_filename = dirname(__FILE__).'/'.lc($type).'_schema.sql';
77  
78 6 ▶ if ($type eq 'SQLite') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "SQLite") 6 0 6
79 6 ▶ eval('use DBD::SQLite 1.26_06');
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
80 6 ▶ $type = 'SQLite_pre_1_26_06' if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
81 }
82  
83 # expand '~/' in file path to the value of $ENV{HOME}
84 # See https://github.com/evernote/serge/issues/1
85 6 $source =~ s!^(DBI:SQLite:dbname=)~/(.*)$!$1.$ENV{HOME}.'/'.$2!se;
86  
87 6 ▶ if (exists $DBD_PARAMS->{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $DBD_PARAMS->{$type}) { } 6 0 6
88 6 $self->{params} = $DBD_PARAMS->{$type};
89  
90 $self->{dbh} = DBI->connect(
91 $source, $username, $password,
92 $self->{params}->{options}
93 6 ▶ ) or die "Can't connect to the database [$source]: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"dbh"} = "DBI"->connect($source, $username, $password, $self->{"params"}{"options"}) or die "Can't connect to the database [$source]: $!\n" 0 6 6
94  
95 # execute driver-specific statements, if any
96 6 my $query = $self->{params}->{do};
97 6 ▶ map $self->execute($_), split(';', $query) if ($query ne '');
Branch: 1 branch
BranchTrueFalseTotal
if $query ne "" 6 0 6
98  
99 # test if 'files' table exists. If it's not,
100 # consider this a new database and run schema SQL
101 # to generate the initial structure
102 6 ▶ if (scalar($self->{dbh}->tables('%', '%', 'files', 'TABLE')) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar $self->{"dbh"}->tables("%", "%", "files", "TABLE") == 0) 6 0 6
103 6 print "Initializing the database...\n";
104  
105 6 ▶ open(SQL, $schema_filename) or die "Can't open schema file $schema_filename: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
CORE::open SQL, $schema_filename or die "Can't open schema file ${schema_filename}: $!\n" 0 6 6
106 6 my $sql = join('', <SQL>);
107 6 close(SQL);
108  
109 # remove block comments
110 6 $sql =~ s/\/\*.*?\*\///sg;
111  
112 # remove last ;
113 6 $sql =~ s/;\s+$//s;
114  
115 # normalize line breaks
116 6 $sql =~ s/\n{2,}/\n/sg;
117  
118 # execute each statement individually
119 6 map $self->execute($_), split(';', $sql);
120 }
121 } else {
122 0 die "Unknown database driver: '$type'\n";
123 }
124 } else {
125 0 die "Incorrect data source format: '$source'\n";
126 }
127  
128 6 return $self->{dbh};
129 }
130  
131 sub begin_transaction {
132 6 ▶ my ($self) = @_;
Subroutine begin_transaction: called CC=2 SCAR=7.0
Pod undocumented
133 6 my $sqlquery = $self->{params}->{begin_transaction_stmt};
134 6 ▶ print "[DB] $sqlquery\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::DB::DEBUG 0 6 6
135 6 $self->execute($sqlquery);
136 6 $self->{transaction_opened} = undef;
137 }
138  
139 sub commit_transaction {
140 12 ▶ my ($self) = @_;
Subroutine commit_transaction: called CC=3 SCAR=11.0
Pod undocumented
141 12 ▶ return unless $self->{transaction_opened};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"transaction_opened"} 6 6 12
142 6 my $sqlquery = 'COMMIT';
143 6 ▶ print "[DB] $sqlquery\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::DB::DEBUG 0 6 6
144 6 $self->execute($sqlquery);
145 6 $self->{transaction_opened} = undef;
146 }
147  
148 sub close {
149 6 ▶ my ($self) = @_;
Subroutine close: called CC=2 SCAR=7.0
Pod undocumented
150 6 ▶ if ($self->{dbh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dbh"}) 6 0 6
151 6 $self->commit_transaction;
152 6 $self->{dbh}->disconnect;
153 6 $self->{dbh} = undef;
154 }
155  
156 6 $self->{prepare_cache} = {};
157 }
158  
159 sub execute {
160 442 ▶ my ($self, $statement) = @_;
Subroutine execute: called CC=2 SCAR=7.2
Pod undocumented
161  
162 442 ▶ $self->{dbh}->do($statement) || die "Can't execute statement [$statement]: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"dbh"}->do($statement) or die "Can't execute statement [$statement]: $!\n" 0 442 442
163 }
164  
165 sub get_usn {
166 128 ▶ my ($self) = @_;
Subroutine get_usn: called CC=1 SCAR=0.0
Pod undocumented
167 # even if run from multiple clients, this will create a new row with a unique primary key (column: 'usn')
168 128 $self->execute("INSERT INTO usn (dummy) VALUES (1)");
169 128 my $usn = $self->{dbh}->last_insert_id(undef, undef, 'usn', 'usn');
170 # we no longer need the inserted record
171 128 $self->execute("DELETE FROM usn");
172 128 return $usn;
173 }
174  
175 sub get_highest_translation_usn_for_file_lang {
176 16 ▶ my ($self, $file_id, $lang) = @_;
Subroutine get_highest_translation_usn_for_file_lang: called CC=6 SCAR=18.6
Pod undocumented
177  
178 16 ▶ $self->_check_file_id($file_id) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::DB::DEBUG 0 16 16
179  
180 16 my $sqlquery =
181 "SELECT MAX(t.usn) AS n FROM translations t ".
182 "JOIN items i ON i.id = t.item_id ".
183 "JOIN files f ON f.id = i.file_id ".
184 "WHERE f.id = ? ".
185 "AND t.language = ?";
186 16 my $sth = $self->prepare($sqlquery);
187 16 ▶ $sth->bind_param(1, $file_id) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(1, $file_id) or die $sth->errstr 0 16 16
188 16 ▶ $sth->bind_param(2, $lang) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(2, $lang) or die $sth->errstr 0 16 16
189 16 ▶ $sth->execute || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 16 16
190 16 my $hr = $sth->fetchrow_hashref();
191 16 $sth->finish;
192 16 $sth = undef;
193 16 ▶ die "file_id [$file_id] is invalid" unless $hr;
Branch: 1 branch
BranchTrueFalseTotal
unless $hr 0 16 16
194 16 return $hr->{n} + 0;
195 }
196  
197 sub get_highest_item_usn_for_file {
198 16 ▶ my ($self, $file_id) = @_;
Subroutine get_highest_item_usn_for_file: called CC=5 SCAR=16.6
Pod undocumented
199  
200 16 ▶ $self->_check_file_id($file_id) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::DB::DEBUG 0 16 16
201  
202 16 my $sqlquery =
203 "SELECT MAX(i.usn) AS n FROM items i ".
204 "JOIN files f ON f.id = i.file_id ".
205 "WHERE f.id = ?";
206 16 my $sth = $self->prepare($sqlquery);
207 16 ▶ $sth->bind_param(1, $file_id) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(1, $file_id) or die $sth->errstr 0 16 16
208 16 ▶ $sth->execute || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 16 16
209 16 my $hr = $sth->fetchrow_hashref();
210 16 $sth->finish;
211 16 $sth = undef;
212 16 ▶ die "file_id [$file_id] is invalid" unless $hr;
Branch: 1 branch
BranchTrueFalseTotal
unless $hr 0 16 16
213 16 return $hr->{n} + 0;
214 }
215  
216 sub get_highest_string_usn_for_file {
217 16 ▶ my ($self, $file_id) = @_;
Subroutine get_highest_string_usn_for_file: called CC=5 SCAR=16.6
Pod undocumented
218  
219 16 ▶ $self->_check_file_id($file_id) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::DB::DEBUG 0 16 16
220  
221 16 my $sqlquery =
222 "SELECT MAX(s.usn) AS n FROM items i ".
223 "JOIN strings s ON s.id = i.string_id ".
224 "JOIN files f ON f.id = i.file_id ".
225 "WHERE f.id = ?";
226 16 my $sth = $self->prepare($sqlquery);
227 16 ▶ $sth->bind_param(1, $file_id) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(1, $file_id) or die $sth->errstr 0 16 16
228 16 ▶ $sth->execute || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 16 16
229 16 my $hr = $sth->fetchrow_hashref();
230 16 $sth->finish;
231 16 $sth = undef;
232 16 ▶ die "file_id [$file_id] is invalid" unless $hr;
Branch: 1 branch
BranchTrueFalseTotal
unless $hr 0 16 16
233 16 return $hr->{n} + 0;
234 }
235  
236 sub get_highest_usn_for_file_lang {
237 16 ▶ my ($self, $file_id, $lang) = @_;
Subroutine get_highest_usn_for_file_lang: called CC=3 SCAR=11.0
Pod undocumented
238  
239 16 my $t_usn = $self->get_highest_translation_usn_for_file_lang($file_id, $lang);
240 16 my $i_usn = $self->get_highest_item_usn_for_file($file_id);
241 16 my $s_usn = $self->get_highest_string_usn_for_file($file_id);
242  
243 16 my $result = $t_usn;
244 16 ▶ $result = $i_usn if $i_usn > $result;
Branch: 1 branch
BranchTrueFalseTotal
if $i_usn > $result 8 8 16
245 16 ▶ $result = $s_usn if $s_usn > $result;
Branch: 1 branch
BranchTrueFalseTotal
if $s_usn > $result 0 16 16
246  
247 16 return $result;
248 }
249  
250 sub prepare {
251 968 ▶ my ($self, $sqlquery) = @_;
Subroutine prepare: called CC=7 SCAR=19.6
Pod undocumented
252  
253 968 my $key = md5($sqlquery);
254 968 ▶ return $self->{prepare_cache}->{$key} if exists $self->{prepare_cache}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"prepare_cache"}{$key} 728 240 968
255  
256 240 ▶ if (!$self->{transaction_opened} && !$ENV{SERGE_NO_TRANSACTIONS} && $sqlquery =~ m/^(INSERT|UPDATE|DELETE) /i) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"transaction_opened"} || $ENV{"SERGE_NO_TRANSACTIONS"} and $sqlquery =~ /^(INSERT|UPDATE|DELETE) /i) 6 234 240
Condition83%5/6
not $self->{"transaction_opened"} || $ENV{"SERGE_NO_TRANSACTIONS"} and $sqlquery =~ /^(INSERT|UPDATE|DELETE) /i
!ll&&!rl&&r
179556
$self->{'transaction_opened'} || $ENV{'SERGE_NO_TRANSACTIONS'}
l!l&&r!l&&!r
179061
MC/DC66%2/3
not $self->{"transaction_opened"} || $ENV{"SERGE_NO_TRANSACTIONS"} and $sqlquery =~ /^(INSERT|UPDATE|DELETE) /i
$self->{'transaction_opened'} $ENV{'SERGE_NO_TRANSACTIONS'} $sqlquery =~ /^(INSERT|UPDATE|DELETE) /i
Truth table75%3/4
not(A || B) and C
A= $self->{'transaction_opened'}B= $ENV{'SERGE_NO_TRANSACTIONS'}C= $sqlquery =~ /^(INSERT|UPDATE|DELETE) /i
ABCresult
1XX0
01X0
0000
0011
257 6 $self->begin_transaction;
258 6 ▶ print "Prepare query: [$sqlquery]\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::DB::DEBUG 0 6 6
259 6 $self->{transaction_opened} = 1;
260 }
261  
262 240 ▶ my $sth = $self->{dbh}->prepare($sqlquery) || die "Query: [$sqlquery], error: ".$self->{dbh}->errstr;
Condition50%1/2
$self->{'dbh'}->prepare($sqlquery) || die("Query: [$sqlquery], error: " . $self->{'dbh'}->errstr)
l!l
2390
MC/DC0%0/1
$self->{'dbh'}->prepare($sqlquery) || die("Query: [$sqlquery], error: " . $self->{'dbh'}->errstr)
$self->{'dbh'}->prepare($sqlquery)
Truth table50%1/2
A
A= $self->{'dbh'}->prepare($sqlquery)
Aresult
11
00
263 239 $self->{prepare_cache}->{$key} = $sth;
264  
265 239 return $sth;
266 }
267  
268 sub _check_file_id {
269 0 ▶ my ($self, $file_id) = @_;
Subroutine _check_file_id: not called CC=5 SCAR=34.0
270  
271 # sanity check for provided file_id
272  
273 0 ▶ die "file_id is null" unless $file_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $file_id 0 0 0
274  
275 0 my $sqlquery =
276 "SELECT id ".
277 "FROM files ".
278 "WHERE id = ?";
279 0 my $sth = $self->prepare($sqlquery);
280 0 ▶ $sth->bind_param(1, $file_id) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(1, $file_id) or die $sth->errstr 0 0 0
281 0 ▶ $sth->execute || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 0 0
282 0 my $hr = $sth->fetchrow_hashref();
283 0 $sth->finish;
284 0 $sth = undef;
285 0 ▶ die "file_id [$file_id] is invalid" unless $hr;
Branch: 1 branch
BranchTrueFalseTotal
unless $hr 0 0 0
286 }
287  
288 sub _check_string_id {
289 0 ▶ my ($self, $string_id) = @_;
Subroutine _check_string_id: not called CC=5 SCAR=34.0
290  
291 # sanity check for provided string_id
292  
293 0 ▶ die "string_id is null" unless $string_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $string_id 0 0 0
294  
295 0 my $sqlquery =
296 "SELECT id ".
297 "FROM strings ".
298 "WHERE id = ?";
299 0 my $sth = $self->prepare($sqlquery);
300 0 ▶ $sth->bind_param(1, $string_id) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(1, $string_id) or die $sth->errstr 0 0 0
301 0 ▶ $sth->execute || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 0 0
302 0 my $hr = $sth->fetchrow_hashref();
303 0 $sth->finish;
304 0 $sth = undef;
305 0 ▶ die "string_id [$string_id] is invalid" unless $hr;
Branch: 1 branch
BranchTrueFalseTotal
unless $hr 0 0 0
306 }
307  
308 sub _check_item_id {
309 0 ▶ my ($self, $item_id) = @_;
Subroutine _check_item_id: not called CC=5 SCAR=34.0
310  
311 # sanity check for provided item_id
312  
313 0 ▶ die "item_id is null" unless $item_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $item_id 0 0 0
314  
315 0 my $sqlquery =
316 "SELECT id ".
317 "FROM items ".
318 "WHERE id = ?";
319 0 my $sth = $self->prepare($sqlquery);
320 0 ▶ $sth->bind_param(1, $item_id) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(1, $item_id) or die $sth->errstr 0 0 0
321 0 ▶ $sth->execute || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 0 0
322 0 my $hr = $sth->fetchrow_hashref();
323 0 $sth->finish;
324 0 $sth = undef;
325 0 ▶ die "item_id [$item_id] is invalid" unless $hr;
Branch: 1 branch
BranchTrueFalseTotal
unless $hr 0 0 0
326 }
327  
328 sub _check_translation_id {
329 0 ▶ my ($self, $translation_id) = @_;
Subroutine _check_translation_id: not called CC=5 SCAR=34.0
330  
331 # sanity check for provided item_id
332  
333 0 ▶ die "translation_id is null" unless $translation_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $translation_id 0 0 0
334  
335 0 my $sqlquery =
336 "SELECT id ".
337 "FROM translations ".
338 "WHERE id = ?";
339 0 my $sth = $self->prepare($sqlquery);
340 0 ▶ $sth->bind_param(1, $translation_id) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(1, $translation_id) or die $sth->errstr 0 0 0
341 0 ▶ $sth->execute || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 0 0
342 0 my $hr = $sth->fetchrow_hashref();
343 0 $sth->finish;
344 0 $sth = undef;
345 0 ▶ die "translation_id [$translation_id] is invalid" unless $hr;
Branch: 1 branch
BranchTrueFalseTotal
unless $hr 0 0 0
346 }
347  
348 sub _check_property_id {
349 0 ▶ my ($self, $property_id) = @_;
Subroutine _check_property_id: not called CC=5 SCAR=34.0
350  
351 # sanity check for provided property_id
352  
353 0 ▶ die "property_id is null" unless $property_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $property_id 0 0 0
354  
355 0 my $sqlquery =
356 "SELECT id ".
357 "FROM properties ".
358 "WHERE id = ?";
359 0 my $sth = $self->prepare($sqlquery);
360 0 ▶ $sth->bind_param(1, $property_id) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(1, $property_id) or die $sth->errstr 0 0 0
361 0 ▶ $sth->execute || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 0 0
362 0 my $hr = $sth->fetchrow_hashref();
363 0 $sth->finish;
364 0 $sth = undef;
365 0 ▶ die "property_id [$property_id] is invalid" unless $hr;
Branch: 1 branch
BranchTrueFalseTotal
unless $hr 0 0 0
366 }
367  
368 sub get_string_id {
369 40 ▶ my ($self, $string, $context, $nocreate) = @_;
Subroutine get_string_id: called CC=11 SCAR=27.2
Pod undocumented
370  
371 40 ▶ utf8::upgrade($string) if defined $string;
Branch: 1 branch
BranchTrueFalseTotal
if defined $string 40 0 40
372 40 ▶ $context = undef if ($context eq ''); # normalizing string
Branch: 1 branch
BranchTrueFalseTotal
if $context eq "" 40 0 40
373 40 ▶ utf8::upgrade($context) if defined $context; # to avoid converting undef to ''
Branch: 1 branch
BranchTrueFalseTotal
if defined $context 0 40 40
374  
375 40 ▶ my $context_sql = $context ? 'AND context = ?' : 'AND context IS NULL';
Branch: 1 branch
BranchTrueFalseTotal
$context ? : 0 40 40
376  
377 40 my $sqlquery =
378 "SELECT id ".
379 "FROM strings ".
380 "WHERE string = ? ".
381 "$context_sql";
382 40 my $sth = $self->prepare($sqlquery);
383 40 ▶ $sth->bind_param(1, $string) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(1, $string) or die $sth->errstr 0 40 40
384 40 ▶ if ($context) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context) 0 40 40
385 0 ▶ $sth->bind_param(2, $context) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(2, $context) or die $sth->errstr 0 0 0
386 }
387 40 ▶ $sth->execute || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 40 40
388 40 my $hr = $sth->fetchrow_hashref();
389 40 $sth->finish;
390 40 $sth = undef;
391 40 ▶ return $hr->{id} if $hr;
Branch: 1 branch
BranchTrueFalseTotal
if $hr 0 40 40
392  
393 40 ▶ return undef if $nocreate;
Branch: 1 branch
BranchTrueFalseTotal
if $nocreate 0 40 40
394  
395 # insert a new record and return its id
396  
397 40 return $self->_write_props('strings', undef, {
398 string => $string,
399 context => $context,
400 skip => 0,
401 }, 1); # insert mode
402  
403 }
404  
405 sub _write_props {
406 240 ▶ my ($self, $table, $id, $props, $insert_mode) = @_;
Subroutine _write_props: called CC=18 SCAR=32.2
407  
408 240 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Serge::DB::DEBUG) 0 240 240
409 0 print "::DB::_write_props ($table, $id)\n";
410 2 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
411 0 print Dumper($props);
412 }
413  
414 240 ▶ die "ERROR: _write_props: table is not defined\n" unless $table;
Branch: 1 branch
BranchTrueFalseTotal
unless $table 0 240 240
415 240 ▶ die "ERROR: _write_props: id is not defined\n" unless $id or $insert_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $id or $insert_mode 0 240 240
Condition33%1/3
$id or $insert_mode
l!l&&r!l&&!r
02400
MC/DC0%0/2
$id or $insert_mode
$id $insert_mode
Truth table33%1/3
A or B
A= $idB= $insert_mode
ABresult
1X1
011
000
416  
417 240 my @insert_params_sql;
418 my @insert_values_sql;
419 240 my @update_params_sql;
420 240 my @params;
421  
422 # for a certain set of tables, also update their usn field with each insert/update
423 240 ▶ if ($table =~ m/^(files|items|strings|translations)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table =~ /^(files|items|strings|translations)$/) 128 112 240
424 128 $props->{usn} = $self->get_usn;
425 }
426  
427 240 foreach my $key (keys %$props) {
428 814 my $value = $props->{$key};
429 814 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 748 66 814
430 748 ▶ if ($insert_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($insert_mode) { } 748 0 748
431 748 push @insert_params_sql, $key;
432 748 push @insert_values_sql, '?';
433 } else {
434 0 push @update_params_sql, "$key = ?";
435 }
436  
437 748 utf8::upgrade($value);
438 748 push @params, $value;
439 } else {
440 66 ▶ if ($insert_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($insert_mode) { } 66 0 66
441 66 push @insert_params_sql, $key;
442 66 push @insert_values_sql, 'NULL';
443 } else {
444 0 push @update_params_sql, "$key = NULL";
445 }
446 }
447 }
448  
449 240 ▶ return if ((@insert_params_sql + @update_params_sql) == 0); # no props to insert/update
Branch: 1 branch
BranchTrueFalseTotal
if @insert_params_sql + @update_params_sql == 0 0 240 240
450  
451 240 my $sqlquery;
452 240 ▶ if ($insert_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($insert_mode) { } 240 0 240
453 240 my $insert_params_sql_str = join(', ', @insert_params_sql);
454 240 my $insert_values_sql_str = join(', ', @insert_values_sql);
455 240 $sqlquery =
456 "INSERT INTO $table ".
457 "($insert_params_sql_str) ".
458 "VALUES ($insert_values_sql_str)";
459 } else {
460 0 my $update_params_sql_str = join(', ', @update_params_sql);
461 0 $sqlquery =
462 "UPDATE $table ".
463 "SET $update_params_sql_str ".
464 "WHERE id = ?";
465  
466 # the last parameter to bind is the row id (WHERE id = ?)
467 0 push @params, $id;
468 }
469  
470 240 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Serge::DB::DEBUG) 0 240 240
471 0 ▶ print "==> $sqlquery" . ($id ? " [id: $id]" : '') . "\n" .
Branch: 1 branch
BranchTrueFalseTotal
$id ? : 0 0 0
472 "with params: ('" . join("','", @params) . "')\n";
473 }
474  
475 240 my $sth = $self->prepare($sqlquery);
476  
477 # bind all params
478  
479 240 ▶ for (my $i = 0; $i <= $#params; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#params) 748 240 988
480 748 ▶ $sth->bind_param($i + 1, $params[$i]) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param($i + 1, $params[$i]) or die $sth->errstr 0 748 748
481 }
482 240 ▶ $sth->execute || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 240 240
483 240 $sth->finish;
484 240 $sth = undef;
485  
486 240 ▶ $id = $self->{dbh}->last_insert_id(undef, undef, $table, 'id') if $insert_mode;
Branch: 1 branch
BranchTrueFalseTotal
if $insert_mode 240 0 240
487  
488 240 return $id;
489 }
490  
491 sub _read_props {
492 240 ▶ my ($self, $table, $id) = @_;
Subroutine _read_props: called CC=3 SCAR=11.1
493  
494 240 my $sqlquery =
495 "SELECT * ".
496 "FROM $table ".
497 "WHERE id = ?";
498 240 my $sth = $self->prepare($sqlquery);
499 240 ▶ $sth->bind_param(1, $id) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(1, $id) or die $sth->errstr 0 240 240
500 240 ▶ $sth->execute || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 240 240
501 240 my $hr = $sth->fetchrow_hashref();
502 240 $sth->finish;
503 240 $sth = undef;
504 240 return $hr;
505 }
506  
507 sub update_string_props {
508 0 ▶ my ($self, $string_id, $props) = @_;
Subroutine update_string_props: not called CC=2 SCAR=17.9
Pod undocumented
509  
510 0 ▶ $self->_check_string_id($string_id) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::DB::DEBUG 0 0 0
511  
512 0 $self->_write_props('strings', $string_id, $props);
513 }
514  
515 sub get_string_props {
516 40 ▶ my ($self, $string_id) = @_;
Subroutine get_string_props: called CC=2 SCAR=7.1
Pod undocumented
517  
518 40 ▶ $self->_check_string_id($string_id) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::DB::DEBUG 0 40 40
519  
520 40 return $self->_read_props('strings', $string_id);
521 }
522  
523 sub update_item_props {
524 0 ▶ my ($self, $item_id, $props) = @_;
Subroutine update_item_props: not called CC=2 SCAR=17.9
Pod undocumented
525  
526 0 ▶ $self->_check_item_id($item_id) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::DB::DEBUG 0 0 0
527  
528 0 $self->_write_props('items', $item_id, $props);
529 }
530  
531 sub get_item_props {
532 54 ▶ my ($self, $item_id) = @_;
Subroutine get_item_props: called CC=2 SCAR=7.1
Pod undocumented
533  
534 54 ▶ $self->_check_item_id($item_id) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::DB::DEBUG 0 54 54
535  
536 54 return $self->_read_props('items', $item_id);
537 }
538  
539 sub update_file_props {
540 0 ▶ my ($self, $file_id, $props) = @_;
Subroutine update_file_props: not called CC=2 SCAR=17.9
Pod undocumented
541  
542 0 ▶ $self->_check_file_id($file_id) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::DB::DEBUG 0 0 0
543  
544 0 $self->_write_props('files', $file_id, $props);
545 }
546  
547 sub get_file_props {
548 8 ▶ my ($self, $file_id) = @_;
Subroutine get_file_props: called CC=2 SCAR=7.1
Pod undocumented
549  
550 8 ▶ $self->_check_file_id($file_id) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::DB::DEBUG 0 8 8
551  
552 8 return $self->_read_props('files', $file_id);
553 }
554  
555 sub update_translation_props {
556 0 ▶ my ($self, $translation_id, $props) = @_;
Subroutine update_translation_props: not called CC=2 SCAR=17.9
Pod undocumented
557  
558 0 ▶ $self->_check_translation_id($translation_id) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::DB::DEBUG 0 0 0
559  
560 0 $self->_write_props('translations', $translation_id, $props);
561 }
562  
563 sub get_translation_props {
564 26 ▶ my ($self, $translation_id) = @_;
Subroutine get_translation_props: called CC=2 SCAR=7.1
Pod undocumented
565  
566 26 ▶ $self->_check_translation_id($translation_id) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::DB::DEBUG 0 26 26
567  
568 26 return $self->_read_props('translations', $translation_id);
569 }
570  
571 sub update_property_props {
572 0 ▶ my ($self, $property_id, $props) = @_;
Subroutine update_property_props: not called CC=2 SCAR=17.9
Pod undocumented
573  
574 0 ▶ $self->_check_property_id($property_id) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::DB::DEBUG 0 0 0
575  
576 0 $self->_write_props('properties', $property_id, $props);
577 }
578  
579 sub get_property_props {
580 112 ▶ my ($self, $property_id) = @_;
Subroutine get_property_props: called CC=2 SCAR=7.1
Pod undocumented
581  
582 112 ▶ $self->_check_property_id($property_id) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::DB::DEBUG 0 112 112
583  
584 112 return $self->_read_props('properties', $property_id);
585 }
586  
587 sub get_file_id {
588 8 ▶ my ($self, $namespace, $job, $path, $nocreate) = @_;
Subroutine get_file_id: called CC=9 SCAR=23.4
Pod undocumented
589  
590 8 ▶ utf8::upgrade($namespace) if defined $namespace;
Branch: 1 branch
BranchTrueFalseTotal
if defined $namespace 8 0 8
591 8 ▶ utf8::upgrade($job) if defined $job;
Branch: 1 branch
BranchTrueFalseTotal
if defined $job 8 0 8
592 8 ▶ utf8::upgrade($path) if defined $path;
Branch: 1 branch
BranchTrueFalseTotal
if defined $path 8 0 8
593  
594 8 my $sqlquery =
595 "SELECT id ".
596 "FROM files ".
597 "WHERE namespace = ? AND path = ?";
598 8 my $sth = $self->prepare($sqlquery);
599 8 ▶ $sth->bind_param(1, $namespace) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(1, $namespace) or die $sth->errstr 0 8 8
600 8 ▶ $sth->bind_param(2, $path) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(2, $path) or die $sth->errstr 0 8 8
601 8 ▶ $sth->execute || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 8 8
602 8 my $hr = $sth->fetchrow_hashref();
603 8 $sth->finish;
604 8 $sth = undef;
605 8 ▶ return $hr->{id} if $hr;
Branch: 1 branch
BranchTrueFalseTotal
if $hr 0 8 8
606  
607 8 ▶ return undef if $nocreate;
Branch: 1 branch
BranchTrueFalseTotal
if $nocreate 0 8 8
608  
609 # insert a new record and return its id
610  
611 8 return $self->_write_props('files', undef, {
612 job => $job,
613 namespace => $namespace,
614 path => $path,
615 }, 1); # insert mode
616  
617 }
618  
619 sub get_item_id {
620 54 ▶ my ($self, $file_id, $string_id, $hint, $nocreate) = @_;
Subroutine get_item_id: called CC=9 SCAR=23.4
Pod undocumented
621  
622 54 ▶ $self->_check_file_id($file_id) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::DB::DEBUG 0 54 54
623 54 ▶ $self->_check_string_id($string_id) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::DB::DEBUG 0 54 54
624  
625 54 ▶ $hint = undef if $hint eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $hint eq "" 0 54 54
626  
627 # getting item_id if the row exists
628  
629 54 my $sqlquery =
630 "SELECT id ".
631 "FROM items ".
632 "WHERE file_id = ? AND string_id = ?";
633 54 my $sth = $self->prepare($sqlquery);
634 54 ▶ $sth->bind_param(1, $file_id) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(1, $file_id) or die $sth->errstr 0 54 54
635 54 ▶ $sth->bind_param(2, $string_id) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(2, $string_id) or die $sth->errstr 0 54 54
636 54 ▶ $sth->execute || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 54 54
637 54 my $hr = $sth->fetchrow_hashref();
638 54 $sth->finish;
639 54 $sth = undef;
640 54 ▶ return $hr->{id} if $hr;
Branch: 1 branch
BranchTrueFalseTotal
if $hr 0 54 54
641  
642 54 ▶ return undef if $nocreate;
Branch: 1 branch
BranchTrueFalseTotal
if $nocreate 0 54 54
643  
644 # insert a new record and return its id
645  
646 54 return $self->_write_props('items', undef, {
647 file_id => $file_id,
648 string_id => $string_id,
649 hint => $hint,
650 }, 1); # insert mode
651  
652 }
653  
654 sub get_property_id {
655 228 ▶ my ($self, $property, $value, $nocreate) = @_;
Subroutine get_property_id: called CC=5 SCAR=16.2
Pod undocumented
656  
657 # getting property_id if the row exists
658  
659 228 my $sqlquery =
660 "SELECT id, value ".
661 "FROM properties ".
662 "WHERE property = ?";
663 228 my $sth = $self->prepare($sqlquery);
664 228 ▶ $sth->bind_param(1, $property) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(1, $property) or die $sth->errstr 0 228 228
665 228 ▶ $sth->execute || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 228 228
666 228 my $hr = $sth->fetchrow_hashref();
667 228 $sth->finish;
668 228 $sth = undef;
669 228 ▶ return $hr->{id} if $hr;
Branch: 1 branch
BranchTrueFalseTotal
if $hr 8 220 228
670  
671 220 ▶ return undef if $nocreate;
Branch: 1 branch
BranchTrueFalseTotal
if $nocreate 108 112 220
672  
673 # insert a new record and return its id
674  
675 112 return $self->_write_props('properties', undef, {
676 property => $property,
677 value => $value,
678 }, 1); # insert mode
679  
680 }
681  
682 sub get_property {
683 54 ▶ my ($self, $property) = @_;
Subroutine get_property: called CC=2 SCAR=8.4
Pod undocumented
684  
685 54 my $property_id = $self->get_property_id($property, undef, 1); # do not create
686  
687 54 ▶ return unless $property_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $property_id 54 0 54
688  
689 0 my $props = $self->get_property_props($property_id);
690 0 return $props->{value};
691 }
692  
693 sub set_property {
694 0 ▶ my ($self, $property, $value) = @_;
Subroutine set_property: not called CC=1 SCAR=6.9
Pod undocumented
695  
696 0 my $property_id = $self->get_property_id($property, $value); # create if necessary
697 0 $self->update_property_props($property_id, {
698 value => $value
699 });
700 }
701  
702 sub get_translation_id {
703 54 ▶ my ($self, $item_id, $lang, $string, $fuzzy, $comment, $merge, $nocreate) = @_;
Subroutine get_translation_id: called CC=11 SCAR=25.0
Pod undocumented
704  
705 54 ▶ $self->_check_item_id($item_id) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::DB::DEBUG 0 54 54
706  
707 54 ▶ $string = undef if $string eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $string eq "" 28 26 54
708 54 ▶ $comment = undef if $comment eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $comment eq "" 54 0 54
709 54 ▶ $fuzzy = $fuzzy ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$fuzzy ? : 0 54 54
710 54 ▶ $merge = $merge ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$merge ? : 0 54 54
711  
712 # getting item_id if the row exists
713  
714 54 my $sqlquery =
715 "SELECT id ".
716 "FROM translations ".
717 "WHERE item_id = ? AND language = ?";
718 54 my $sth = $self->prepare($sqlquery);
719 54 ▶ $sth->bind_param(1, $item_id) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(1, $item_id) or die $sth->errstr 0 54 54
720 54 ▶ $sth->bind_param(2, $lang) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(2, $lang) or die $sth->errstr 0 54 54
721 54 ▶ $sth->execute || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 54 54
722 54 my $hr = $sth->fetchrow_hashref();
723 54 $sth->finish;
724 54 $sth = undef;
725 54 ▶ return $hr->{id} if $hr;
Branch: 1 branch
BranchTrueFalseTotal
if $hr 0 54 54
726  
727 54 ▶ return undef if $nocreate;
Branch: 1 branch
BranchTrueFalseTotal
if $nocreate 28 26 54
728  
729 # insert a new record and return its id
730  
731 26 return $self->_write_props('translations', undef, {
732 item_id => $item_id,
733 language => $lang,
734 string => $string,
735 fuzzy => $fuzzy,
736 comment => $comment,
737 merge => $merge,
738 }, 1); # insert mode
739  
740 }
741  
742 sub get_all_items_for_file {
743 8 ▶ my ($self, $file_id) = @_;
Subroutine get_all_items_for_file: called CC=5 SCAR=16.3
Pod undocumented
744  
745 8 ▶ $self->_check_file_id($file_id) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::DB::DEBUG 0 8 8
746  
747 8 my $sqlquery =
748 "SELECT id, orphaned ".
749 "FROM items ".
750 "WHERE file_id = ?";
751 8 my $sth = $self->prepare($sqlquery);
752 8 ▶ $sth->bind_param(1, $file_id) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(1, $file_id) or die $sth->errstr 0 8 8
753 8 ▶ $sth->execute || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 8 8
754  
755 8 my %items;
756 8 ▶ while (my $hr = $sth->fetchrow_hashref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hr = $sth->fetchrow_hashref) 54 8 62
757 54 $items{$hr->{id}} = $hr->{orphaned};
758 }
759 8 $sth->finish;
760 8 $sth = undef;
761  
762 8 return \%items;
763 }
764  
765 sub get_file_completeness_ratio {
766 0 ▶ my ($self, $file_id, $lang, $total) = @_;
Subroutine get_file_completeness_ratio: not called CC=10 SCAR=47.0
Pod undocumented
767  
768 0 ▶ $self->_check_file_id($file_id) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::DB::DEBUG 0 0 0
769  
770 0 ▶ if (!defined $total) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $total) 0 0 0
771 0 ▶ print "Total number of items wasn't passed, getting from DB\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::DB::DEBUG 0 0 0
772 0 my $sqlquery =
773 "SELECT COUNT(*) FROM items ".
774  
775 "JOIN strings ".
776 "ON strings.id = items.string_id ".
777  
778 "WHERE items.orphaned = 0 ".
779 "AND strings.skip = 0 ".
780 "AND items.file_id = ?";
781 0 my $sth = $self->prepare($sqlquery);
782 0 ▶ $sth->bind_param(1, $file_id) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(1, $file_id) or die $sth->errstr 0 0 0
783 0 ▶ $sth->execute || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 0 0
784  
785 0 my $row = $sth->fetchrow_arrayref();
786 0 $total = $row->[0];
787 0 $sth->finish;
788 0 $sth = undef;
789 }
790  
791 # empty files are 'fully translated'
792 0 ▶ return 1 if $total == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $total == 0 0 0 0
793  
794 0 my $sqlquery =
795 "SELECT COUNT(*) FROM items ".
796  
797 "JOIN strings ".
798 "ON strings.id = items.string_id ".
799  
800 "JOIN translations ".
801 "ON translations.item_id = items.id ".
802 "AND translations.language = ? ".
803 "AND translations.fuzzy = 0 ".
804  
805 "WHERE items.orphaned = 0 ".
806 "AND strings.skip = 0 ".
807 "AND items.file_id = ?";
808 0 my $sth = $self->prepare($sqlquery);
809 0 ▶ $sth->bind_param(1, $lang) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(1, $lang) or die $sth->errstr 0 0 0
810 0 ▶ $sth->bind_param(2, $file_id) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(2, $file_id) or die $sth->errstr 0 0 0
811 0 ▶ $sth->execute || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 0 0
812  
813 0 my $row = $sth->fetchrow_arrayref();
814 0 my $translated = $row->[0];
815 0 $sth->finish;
816 0 $sth = undef;
817  
818 0 return $translated / $total;
819 }
820  
821 sub get_all_files_for_job {
822 6 ▶ my ($self, $namespace, $job) = @_;
Subroutine get_all_files_for_job: called CC=5 SCAR=16.8
Pod undocumented
823  
824 6 my $sqlquery =
825 "SELECT id, path, orphaned ".
826 "FROM files ".
827 "WHERE namespace = ? ".
828 "AND job = ?";
829 6 my $sth = $self->prepare($sqlquery);
830 6 ▶ $sth->bind_param(1, $namespace) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(1, $namespace) or die $sth->errstr 0 6 6
831 6 ▶ $sth->bind_param(2, $job) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param(2, $job) or die $sth->errstr 0 6 6
832 6 ▶ $sth->execute || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 6 6
833  
834 6 my %items;
835 6 ▶ while (my $hr = $sth->fetchrow_hashref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hr = $sth->fetchrow_hashref) 0 6 6
836 $items{$hr->{path}} = {
837 id => $hr->{id},
838 orphaned => $hr->{orphaned}
839 0 };
840 }
841 6 $sth->finish;
842 6 $sth = undef;
843  
844 6 return \%items;
845 }
846  
847 2 1;
« lib/Serge/Engine/Plugin/parse_pot.pm ↑ Summary lib/Serge/Engine/Plugin/completeness.pm »