MToken/Store.pm

filter: Statementstmt 84.4% 185 / 219 Branchbran 46.8% 45 / 96 Conditioncond 36.3% 16 / 44 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 89.4% 34 / 38 Podpod 100% 14 / 14 totaltotal 67.5% 294 / 435 CC 71 SCAR 31.7
CC
71
Cov
69%
new
74.7
count
20.0
set
9.9
CRAP 23.7
« MToken/Server.pm ↑ Summary MToken/Server/Alpha.pm »
1 package MToken::Store; # $Id: Store.pm 116 2021-10-12 15:17:49Z minus $
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =encoding utf-8
6  
7 =head1 NAME
8  
9 MToken::Store - MToken store class
10  
11 =head1 VERSION
12  
13 Version 1.00
14  
15 =head1 SYNOPSIS
16  
17 use MToken::Store;
18  
19 my $store = MToken::Store->new(
20 file => "/tmp/test.db",
21 attributes => "RaiseError=0; PrintError=0; sqlite_unicode=1",
22 do_init => 1, # Need to try initialize the db
23 );
24  
25 my $store = MToken::Store->new(
26 dsn => "DBI:mysql:database=MToken;host=mysql.example.com",
27 user => "username",
28 password => "password",
29 set => [
30 "RaiseError 0",
31 "PrintError 0",
32 "mysql_enable_utf8 1",
33 ],
34 );
35  
36 die($store->error) unless $store->status;
37  
38 =head1 DESCRIPTION
39  
40 This module provides store methods.
41  
42 =head2 SQLITE DDL
43  
44 CREATE TABLE "mtoken" (
45 "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
46 "file" CHAR(256) NOT NULL UNIQUE,
47 "size" INTEGER NOT NULL,
48 "mtime" INTEGER NOT NULL,
49 "checksum" CHAR(64) DEFAULT NULL,
50 "tags" CHAR(256) DEFAULT NULL,
51 "subject" CHAR(1024) DEFAULT NULL,
52 "content" TEXT DEFAULT NULL
53 );
54  
55 =head2 MYSQL DDL
56  
57 CREATE DATABASE `mtoken` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin */;
58 CREATE TABLE IF NOT EXISTS `mtoken` (
59 `id` INT(11) NOT NULL AUTO_INCREMENT,
60 `file` VARCHAR(256) COLLATE utf8_bin NOT NULL, -- File name
61 `size` INT(11) NOT NULL, -- File size
62 `mtime` INT(11) NOT NULL, -- Unixtime value of modified time (mtime)
63 `checksum` VARCHAR(64) NOT NULL, -- Checksum (MD5/SHA1/SHA256)
64 `tags` VARCHAR(256) DEFAULT NULL, -- Tags
65 `subject` VARCHAR(1024) DEFAULT NULL, -- Subject
66 `content` TEXT COLLATE utf8_bin DEFAULT NULL,
67 PRIMARY KEY (`id`),
68 UNIQUE KEY `file` (`file`)
69 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
70  
71 =head2 POSTGRESQL DDL
72  
73 CREATE TABLE IF NOT EXISTS `mtoken` (
74 `id` INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
75 `file` CHAR(256) COLLATE utf8_bin NOT NULL UNIQUE,
76 `size` INTEGER NOT NULL,
77 `mtime` INTEGER NOT NULL,
78 `checksum` CHAR(64) DEFAULT NULL,
79 `tags` CHAR(256) DEFAULT NULL,
80 `subject` CHAR(1024) DEFAULT NULL,
81 `content` TEXT COLLATE utf8_bin DEFAULT NULL
82 );
83  
84 =head1 METHODS
85  
86 =head2 new
87  
88 my $store = MToken::Store->new(
89 file => "/tmp/test.db",
90 attributes => "RaiseError=0; PrintError=0; sqlite_unicode=1",
91 do_init => 1, # Need to try initialize the db
92 );
93  
94 # ... or MySQL:
95  
96 my $store = MToken::Store->new(
97 dsn => "DBI:mysql:database=mtoken;host=mysql.example.com",
98 user => "username",
99 password => "password",
100 set => [
101 "RaiseError 0",
102 "PrintError 0",
103 "mysql_enable_utf8 1",
104 ],
105 );
106  
107 Creates DBI object
108  
109 =head2 add
110  
111 $store->add(
112 file => "test.txt",
113 size => 1024,
114 mtime => 1590000000,
115 checksum => "1a6f4a41ae8eec2da84dbfa48636e02e33575dbd",
116 tags => "test, example",
117 subject => "Test file for example",
118 content => "...Content of the file...",
119 ) or die($store->error);
120  
121 Add new recored
122  
123 =head2 count
124  
125 print $store->count();
126  
127 Returns count of records
128  
129 =head2 del
130  
131 $store->del("test.txt") or die($store->error);
132  
133 Delete record by filename
134  
135 $store->del(1) or die($store->error);
136  
137 Delete record by record id
138  
139 =head2 dsn
140  
141 my $dsn = $store->dsn;
142  
143 Returns DSN string of current database connection
144  
145 =head2 error
146  
147 my $error = $store->error;
148  
149 Returns error message
150  
151 my $status = $store->error( "Error message" );
152  
153 Sets error message if argument is provided.
154 This method in "set" context returns status of the operation as status() method.
155  
156 =head2 file
157  
158 my $file = $store->file;
159  
160 Returns the file of SQLite database
161  
162 =head2 get
163  
164 my %data = $store->get("test.txt");
165  
166 Returns data from database by filename
167  
168 my %data = $store->get(1);
169  
170 Returns data from database by record id
171  
172 =head2 getall
173  
174 my @table = $store->getall();
175 my @table_100 = $store->getall(100); # row_count
176 my @table_100 = $store->getall(100, 100); # offset, row_count
177  
178 Returns data from database with limit supporting
179  
180 =head2 is_sqlite
181  
182 print $store->is_sqlite ? "Is SQLite" : "Is NOT SQLite"
183  
184 Returns true if type of current database is SQLite
185  
186 =head2 ping
187  
188 $store->ping ? 'OK' : 'Database session is expired';
189  
190 Checks the connection to database
191  
192 =head2 set
193  
194 $store->set(
195 file => "test.txt",
196 size => 1024,
197 mtime => 1590000000,
198 checksum => "1a6f4a41ae8eec2da84dbfa48636e02e33575dbd",
199 tags => "test, example",
200 subject => "Test file for example",
201 content => "...New content of the file...",
202 ) or die($store->error);
203  
204 Update recored by document number
205  
206 $store->set(
207 id => 1,
208 file => "test.txt",
209 size => 1024,
210 mtime => 1590000000,
211 checksum => "1a6f4a41ae8eec2da84dbfa48636e02e33575dbd",
212 tags => "test, example",
213 subject => "Test file for example",
214 content => "...New content of the file...",
215 ) or die($store->error);
216  
217 Update recored by record id
218  
219 =head2 status
220  
221 my $status = $store->status;
222 my $status = $store->status( 1 ); # Sets the status value and returns it
223  
224 Get/set BOOL status of the operation
225  
226 =head2 truncate
227  
228 $store->truncate or die($store->error);
229  
230 Delete all records
231  
232 =head1 HISTORY
233  
234 See C<Changes> file
235  
236 =head1 TO DO
237  
238 See C<TODO> file
239  
240 =head1 BUGS
241  
242 * none noted
243  
244 =head1 SEE ALSO
245  
246 L<CTK::DBI>, L<MToken>
247  
248 =head1 AUTHOR
249  
250 Serż Minus (Sergey Lepenkov) L<http://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
251  
252 =head1 COPYRIGHT
253  
254 Copyright (C) 1998-2021 D&D Corporation. All Rights Reserved
255  
256 =head1 LICENSE
257  
258 This program is free software; you can redistribute it and/or
259 modify it under the same terms as Perl itself.
260  
261 See C<LICENSE> file and L<https://dev.perl.org/licenses/>
262  
263 =cut
264  
265 2 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
266 2 $VERSION = '1.00';
267  
268 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
269 2 ▶use CTK::DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
270 2 ▶use CTK::Util qw/ touch /;
Subroutine BEGIN: called CC=1 SCAR=0.0
271 2 ▶use CTK::ConfGenUtil;
Subroutine BEGIN: called CC=1 SCAR=0.0
272 2 ▶use CTK::TFVals qw/ :ALL /;
Subroutine BEGIN: called CC=1 SCAR=0.0
273 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
274  
275 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
276 2 DBFILE_MOD => 0666,
277 DEFAULT_DSN_MASK => 'DBI:SQLite:dbname=%s',
278 DEFAULT_DBI_ATTR => {
279 dsn => '', # See DEFAULT_DSN_MASK
280 user => '',
281 password => '',
282 set => [
283 'RaiseError 0',
284 'PrintError 0',
285 'sqlite_unicode 1',
286 ],
287 },
288 2 };
289  
290 2 ▶use constant DDL_CREATE_TABLE => <<'DDL';
Subroutine BEGIN: called CC=1 SCAR=0.0
291 CREATE TABLE IF NOT EXISTS "mtoken" (
292 "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
293 "file" CHAR(256) NOT NULL UNIQUE,
294 "size" INTEGER NOT NULL,
295 "mtime" INTEGER NOT NULL,
296 "checksum" CHAR(64) DEFAULT NULL,
297 "tags" CHAR(256) DEFAULT NULL,
298 "subject" CHAR(1024) DEFAULT NULL,
299 "content" TEXT DEFAULT NULL
300 )
301 DDL
302 2 ▶use constant DDL_CREATE_INDEX => <<'DDL';
Subroutine BEGIN: called CC=1 SCAR=0.0
303 CREATE UNIQUE INDEX "file" ON "mtoken" (
304 "file"
305 )
306 DDL
307  
308 2 ▶use constant DML_ADD => <<'DML';
Subroutine BEGIN: called CC=1 SCAR=0.0
309 INSERT INTO `mtoken`
310 (`file`,`size`,`mtime`,`checksum`,`tags`,`subject`,`content`)
311 VALUES
312 (?,?,?,?,?,?,?)
313 DML
314  
315 2 ▶use constant DML_GET_COUNT => <<'DML';
Subroutine BEGIN: called CC=1 SCAR=0.0
316 SELECT COUNT(`id`) AS `cnt`
317 FROM `mtoken`
318 DML
319  
320 2 ▶use constant DML_GET_BY_ID => <<'DML';
Subroutine BEGIN: called CC=1 SCAR=0.0
321 SELECT `id`,`file`,`size`,`mtime`,`checksum`,`tags`,`subject`,`content`
322 FROM `mtoken`
323 WHERE `id` = ?
324 DML
325  
326 2 ▶use constant DML_GET_BY_FILE => <<'DML';
Subroutine BEGIN: called CC=1 SCAR=0.0
327 SELECT `id`,`file`,`size`,`mtime`,`checksum`,`tags`,`subject`,`content`
328 FROM `mtoken`
329 WHERE `file` = ?
330 DML
331  
332 2 ▶use constant DML_GETALL => <<'DML';
Subroutine BEGIN: called CC=1 SCAR=0.0
333 SELECT `id`,`file`,`size`,`mtime`,`checksum`,`tags`,`subject`
334 FROM `mtoken`
335 ORDER BY `file` ASC
336 DML
337  
338 2 ▶use constant DML_DEL_BY_ID => <<'DML';
Subroutine BEGIN: called CC=1 SCAR=0.0
339 DELETE FROM `mtoken` WHERE `id` = ?
340 DML
341  
342 2 ▶use constant DML_DEL_BY_FILE => <<'DML';
Subroutine BEGIN: called CC=1 SCAR=0.0
343 DELETE FROM `mtoken` WHERE `file` = ?
344 DML
345  
346 2 ▶use constant DML_TRUNCATE => <<'DML';
Subroutine BEGIN: called CC=1 SCAR=0.0
347 DELETE FROM `mtoken`
348 DML
349  
350 2 ▶use constant DML_SET_BY_ID => <<'DML';
Subroutine BEGIN: called CC=1 SCAR=0.0
351 UPDATE `mtoken`
352 SET `file` = ?, `size` = ?, `mtime` = ?, `checksum` = ?, `tags` = ?, `subject` = ?, `content` = ?
353 WHERE `id` = ?
354 DML
355  
356 2 ▶use constant DML_SET_BY_FILE => <<'DML';
Subroutine BEGIN: called CC=1 SCAR=0.0
357 UPDATE `mtoken`
358 SET `file` = ?, `size` = ?, `mtime` = ?, `checksum` = ?, `tags` = ?, `subject` = ?, `content` = ?
359 WHERE `file` = ?
360 DML
361  
362 1 ▶sub new {
Subroutine new: called CC=29 SCAR=43.1
Pod documented
363 1 my $class = shift;
364 1 my %args = @_;
365 1 my $args_set = $args{'set'};
366 1 ▶ unless ($args{dsn}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"dsn"}) 1 0 1
367 1 my $dda = DEFAULT_DBI_ATTR;
368 1 foreach (keys %$dda) {
369 4 ▶ next if $_ eq 'set';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "set" 1 3 4
370 3 ▶ $args{$_} //= $dda->{$_}
Condition33%1/3
$args{$_} //= $dda->{$_}
l!l&&r!l&&!r
030
MC/DC0%0/2
$args{$_} //= $dda->{$_}
$args{$_} $dda->{$_}
Truth table33%1/3
A //= B
A= $args{$_}B= $dda->{$_}
ABresult
1X1
011
000
371 }
372 }
373 1 ▶ my $file = $args{file} // "";
Condition50%1/2
$args{"file"} // ""
l!l
10
MC/DC0%0/1
$args{"file"} // ""
$args{"file"}
Truth table50%1/2
A
A= $args{"file"}
Aresult
11
00
374 1 ▶ my $dsn = $args{dsn} || sprintf(DEFAULT_DSN_MASK, $file);
Condition50%1/2
$args{'dsn'} || sprintf('DBI:SQLite:dbname=%s', $file)
l!l
01
MC/DC0%0/1
$args{'dsn'} || sprintf('DBI:SQLite:dbname=%s', $file)
$args{'dsn'}
Truth table50%1/2
A
A= $args{'dsn'}
Aresult
11
00
375 1 my %attrs_from_set = _set2attr($args_set);
376 1 ▶ my %attrs_from_str = $args{attributes} ? _parseDBAttributes($args{attributes}) : ();
Branch: 1 branch
BranchTrueFalseTotal
$args{'attributes'} ? : 1 0 1
377 1 my %attrs = (%attrs_from_set, %attrs_from_str);
378 1 ▶ %attrs = _set2attr(DEFAULT_DBI_ATTR()->{set}) unless %attrs;
Branch: 1 branch
BranchTrueFalseTotal
unless %attrs 0 1 1
379  
380 # DB
381 my $db = CTK::DBI->new(
382 -dsn => $dsn,
383 -debug => 0,
384 -username => $args{'user'},
385 -password => $args{'password'},
386 -attr => {%attrs},
387 $args{timeout} ? (
388 -timeout_connect => $args{timeout},
389 -timeout_request => $args{timeout},
390 1 ▶ ) : (),
Branch: 1 branch
BranchTrueFalseTotal
$args{'timeout'} ? : 0 1 1
391 );
392 1 ▶ my $dbh = $db->connect if $db;
Branch: 1 branch
BranchTrueFalseTotal
if $db 1 0 1
393  
394 # SQLite
395 1 my $fnew = 0;
396 1 my $issqlite = 0;
397 1 ▶ my $do_init = $args{do_init} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args{'do_init'} ? : 1 0 1
398 1 ▶ if ($dbh && $dsn =~ /SQLite/i) { # If SQLite (default)
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh and $dsn =~ /SQLite/i) 1 0 1
Condition33%1/3
$dbh and $dsn =~ /SQLite/i
!ll&&!rl&&r
001
MC/DC0%0/2
$dbh and $dsn =~ /SQLite/i
$dbh $dsn =~ /SQLite/i
Truth table33%1/3
A and B
A= $dbhB= $dsn =~ /SQLite/i
ABresult
0X0
100
111
399 1 $file = $dbh->sqlite_db_filename();
400 1 ▶ if ($do_init) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_init) 1 0 1
401 1 ▶ unless ($file && (-e $file) && !(-z $file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file and -e $file and not -z $file) 1 0 1
Condition33%2/6
$file and -e $file and not -z $file
!ll&&!rl&&r
010
$file and -e $file
!ll&&!rl&&r
001
MC/DC0%0/3
$file and -e $file and not -z $file
$file -e $file not -z $file
Truth table25%1/4
A and B and not(C)
A= $fileB= -e $fileC= not -z $file
ABCresult
0XX0
10X0
1100
1111
402 1 touch($file);
403 1 chmod(DBFILE_MOD, $file);
404 1 $fnew = 1;
405 }
406 }
407 1 $issqlite = 1;
408 }
409  
410 # Defaults
411 1 my $status = 1; # Ok
412 1 my $error = "";
413 1 ▶ if (!$db) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $db) { } 0 1 1
elsif (not $dbh) { } 0 1 1
elsif ($fnew and $do_init) { } 1 0 1
414 0 $error = sprintf("Can't init database \"%s\"", $dsn);
415 0 $status = 0;
416 } elsif (!$dbh) {
417 0 ▶ $error = sprintf("Can't connect to database \"%s\": %s", $dsn, $DBI::errstr || "unknown error");
Condition0%0/2
$DBI::errstr || 'unknown error'
l!l
00
MC/DC0%0/1
$DBI::errstr || 'unknown error'
$DBI::errstr
Truth table0%0/2
A
A= $DBI::errstr
Aresult
11
00
418 0 $status = 0;
419 ▶ } elsif ($fnew && $do_init) {
Condition33%1/3
$fnew and $do_init
!ll&&!rl&&r
001
MC/DC0%0/2
$fnew and $do_init
$fnew $do_init
Truth table33%1/3
A and B
A= $fnewB= $do_init
ABresult
0X0
100
111
420 1 $db->execute(DDL_CREATE_TABLE);
421 1 ▶ $db->execute(DDL_CREATE_INDEX) unless $dbh->err;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->err 1 0 1
422 1 $error = $dbh->errstr();
423 1 ▶ $status = 0 if $dbh->err;
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->err 0 1 1
424 }
425 1 ▶ unless ($error) { # No errors
Branch: 1 branch
BranchTrueFalseTotal
unless ($error) 1 0 1
426 1 ▶ unless ($dbh->ping) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh->ping) 0 1 1
427 0 ▶ $error = sprintf("Can't init database \"%s\". Ping failed: %s",
Condition0%0/2
$dbh->errstr || 'unknown error'
l!l
00
MC/DC0%0/1
$dbh->errstr || 'unknown error'
$dbh->errstr
Truth table0%0/2
A
A= $dbh->errstr
Aresult
11
00
428 $dsn, $dbh->errstr() || "unknown error");
429 0 $status = 0;
430 }
431 }
432  
433 1 ▶ my $self = bless {
Condition50%1/2
$error // ""
l!l
01
MC/DC0%0/1
$error // ""
$error
Truth table50%1/2
A
A= $error
Aresult
11
00
434 file => $file,
435 issqlite=> $issqlite,
436 dsn => $dsn,
437 error => $error // "",
438 dbi => $db,
439 status => $status,
440 }, $class;
441  
442 1 return $self;
443 }
444  
445 10 ▶sub status {
Subroutine status: called CC=3 SCAR=11.0
Pod documented
446 10 my $self = shift;
447 10 my $value = shift;
448 10 ▶ return fv2zero($self->{status}) unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 3 7 10
449 7 ▶ $self->{status} = $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 7 0 7
450 7 return $self->{status};
451 }
452 7 ▶sub error {
Subroutine error: called CC=3 SCAR=11.2
Pod documented
453 7 my $self = shift;
454 7 my $value = shift;
455 7 ▶ return uv2null($self->{error}) unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 7 7
456 7 $self->{error} = $value;
457 7 ▶ $self->status($value ne "" ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$value ne '' ? : 0 7 7
458 7 return $value;
459 }
460 8 ▶sub ping {
Subroutine ping: called CC=5 SCAR=16.8
Pod documented
461 8 my $self = shift;
462 8 ▶ return 0 unless $self->{dsn};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dsn"} 0 8 8
463 8 my $dbi = $self->{dbi};
464 8 ▶ return 0 unless $dbi;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbi 0 8 8
465 8 my $dbh = $dbi->{dbh};
466 8 ▶ return 0 unless $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 0 8 8
467 8 ▶ return 0 unless $dbh->can('ping');
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->can("ping") 0 8 8
468 8 return $dbh->ping();
469 }
470 0 ▶sub dsn {
Subroutine dsn: not called CC=1 SCAR=6.9
Pod documented
471 0 my $self = shift;
472 0 return $self->{dsn};
473 }
474 0 ▶sub is_sqlite {
Subroutine is_sqlite: not called CC=2 SCAR=17.9
Pod documented
475 0 my $self = shift;
476 0 ▶ return $self->{issqlite} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'issqlite'} ? : 0 0 0
477 }
478 0 ▶sub file {
Subroutine file: not called CC=1 SCAR=6.9
Pod documented
479 0 my $self = shift;
480 0 return $self->{file};
481 }
482  
483 # CRUD Methods
484  
485 2 ▶sub add {
Subroutine add: called CC=5 SCAR=17.8
Pod documented
486 2 my $self = shift;
487 2 my %data = @_;
488 2 ▶ return unless $self->ping;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ping 0 2 2
489 2 $self->error("");
490 2 my $dbi = $self->{dbi};
491  
492 # Add
493 $dbi->execute(DML_ADD,
494 $data{file},
495 $data{size} || 0,
496 $data{mtime} || time(),
497 $data{checksum},
498 $data{tags},
499 $data{subject},
500 $data{content},
501 2 ▶ );
Condition50%2/4
$data{'size'} || 0
l!l
20
$data{'mtime'} || time
l!l
20
MC/DC0%0/1
$data{'size'} || 0
$data{'size'}
MC/DC0%0/1
$data{'mtime'} || time
$data{'mtime'}
Truth table50%1/2
A
A= $data{'size'}
Aresult
11
00
Truth table50%1/2
A
A= $data{'mtime'}
Aresult
11
00
502 2 ▶ if ($dbi->connect->err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbi->connect->err) 0 2 2
503 0 $self->error(sprintf("Can't insert new record: %s", uv2null($dbi->connect->errstr)));
504 0 return;
505 }
506  
507 2 return 1;
508 }
509 0 ▶sub count {
Subroutine count: not called CC=4 SCAR=30.0
Pod documented
510 0 my $self = shift;
511 0 ▶ return 0 unless $self->ping;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ping 0 0 0
512 0 $self->error("");
513 0 my $dbi = $self->{dbi};
514  
515 0 ▶ my $cnt = $dbi->field(DML_GET_COUNT) || 0;
Condition0%0/2
$dbi->field("SELECT COUNT(`id`) AS `cnt`\nFROM `mtoken`\n") || 0
l!l
00
MC/DC0%0/1
$dbi->field("SELECT COUNT(`id`) AS `cnt`\nFROM `mtoken`\n") || 0
$dbi->field("SELECT COUNT(`id`) AS `cnt`\nFROM `mtoken`\n")
Truth table0%0/2
A
A= $dbi->field("SELECT COUNT(`id`) AS `cnt`\nFROM `mtoken`\n")
Aresult
11
00
516 0 ▶ if ($dbi->connect->err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbi->connect->err) 0 0 0
517 0 $self->error(sprintf("Can't get count: %s", uv2null($dbi->connect->errstr)));
518 0 return 0;
519 }
520  
521 0 return $cnt;
522 }
523 1 ▶sub get {
Subroutine get: called CC=5 SCAR=17.8
Pod documented
524 1 my $self = shift;
525 1 ▶ my $id = shift || 0;
Condition50%1/2
shift() || 0
l!l
10
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
526 1 ▶ return () unless $self->ping;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ping 0 1 1
527 1 $self->error("");
528 1 my $dbi = $self->{dbi};
529  
530 1 ▶ my %rec = $dbi->recordh(is_int($id) ? DML_GET_BY_ID : DML_GET_BY_FILE, $id);
Branch: 1 branch
BranchTrueFalseTotal
&is_int($id) ? : 0 1 1
531 1 ▶ if ($dbi->connect->err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbi->connect->err) 0 1 1
532 0 $self->error(sprintf("Can't get record: %s", uv2null($dbi->connect->errstr)));
533 0 return ();
534 }
535  
536 1 return %rec;
537 }
538 1 ▶sub getall {
Subroutine getall: called CC=5 SCAR=18.0
Pod documented
539 1 my $self = shift;
540 1 my $row_count = pop;
541 1 my $offset = pop;
542 1 ▶ return () unless $self->ping;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ping 0 1 1
543 1 $self->error("");
544 1 my $dbi = $self->{dbi};
545 1 ▶ my $limit = $row_count
Branch: 2 branches
BranchTrueFalseTotal
$row_count ? : 0 1 1
$offset ? : 0 0 0
546 ? $offset
547 ? sprintf(" LIMIT %d, %d", $offset, $row_count)
548 : sprintf(" LIMIT %d", $row_count)
549 : "";
550  
551 1 my @tbl = $dbi->table(sprintf("%s%s", DML_GETALL, $limit ));
552 1 ▶ if ($dbi->connect->err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbi->connect->err) 0 1 1
553 0 $self->error(sprintf("Can't get records: %s", uv2null($dbi->connect->errstr)));
554 0 return ();
555 }
556 1 return @tbl;
557 }
558 1 ▶sub del {
Subroutine del: called CC=5 SCAR=17.8
Pod documented
559 1 my $self = shift;
560 1 ▶ my $id = shift || 0;
Condition50%1/2
shift() || 0
l!l
10
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
561 1 ▶ return 0 unless $self->ping;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ping 0 1 1
562 1 $self->error("");
563 1 my $dbi = $self->{dbi};
564  
565 1 ▶ $dbi->execute(is_int($id) ? DML_DEL_BY_ID : DML_DEL_BY_FILE, $id);
Branch: 1 branch
BranchTrueFalseTotal
&is_int($id) ? : 1 0 1
566 1 ▶ if ($dbi->connect->err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbi->connect->err) 0 1 1
567 0 $self->error(sprintf("Can't delete record: %s", uv2null($dbi->connect->errstr)));
568 0 return 0;
569 }
570 1 return 1;
571 }
572 1 ▶sub truncate {
Subroutine truncate: called CC=3 SCAR=11.8
Pod documented
573 1 my $self = shift;
574 1 ▶ return 0 unless $self->ping;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ping 0 1 1
575 1 $self->error("");
576 1 my $dbi = $self->{dbi};
577  
578 1 $dbi->execute(DML_TRUNCATE);
579 1 ▶ if ($dbi->connect->err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbi->connect->err) 0 1 1
580 0 $self->error(sprintf("Can't truncate table: %s", uv2null($dbi->connect->errstr)));
581 0 return 0;
582 }
583 1 return 1;
584 }
585 1 ▶sub set {
Subroutine set: called CC=7 SCAR=23.0
Pod documented
586 1 my $self = shift;
587 1 my %data = @_;
588 1 ▶ return unless $self->ping;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ping 0 1 1
589 1 $self->error("");
590 1 my $dbi = $self->{dbi};
591  
592 # Set by id or num
593 $dbi->execute($data{id} ? DML_SET_BY_ID : DML_SET_BY_FILE,
594 $data{file},
595 $data{size} || 0,
596 $data{mtime} || time(),
597 $data{checksum},
598 $data{tags},
599 $data{subject},
600 $data{content},
601 $data{id} || $data{file},
602 1 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$data{'id'} ? : 0 1 1
Condition42%3/7
$data{'size'} || 0
l!l
10
$data{'mtime'} || time
l!l
10
$data{'id'} || $data{'file'}
l!l&&r!l&&!r
010
MC/DC0%0/1
$data{'size'} || 0
$data{'size'}
MC/DC0%0/1
$data{'mtime'} || time
$data{'mtime'}
MC/DC0%0/2
$data{'id'} || $data{'file'}
$data{'id'} $data{'file'}
Truth table50%1/2
A
A= $data{'size'}
Aresult
11
00
Truth table50%1/2
A
A= $data{'mtime'}
Aresult
11
00
Truth table33%1/3
A || B
A= $data{'id'}B= $data{'file'}
ABresult
1X1
011
000
603 1 ▶ if ($dbi->connect->err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbi->connect->err) 0 1 1
604 0 $self->error(sprintf("Can't update record: %s", uv2null($dbi->connect->errstr)));
605 0 return;
606 }
607  
608 1 return 1;
609 }
610  
611 1 ▶sub _set2attr {
Subroutine _set2attr: called CC=4 SCAR=16.1
612 1 my $in = shift;
613 1 ▶ my $attr = is_array($in) ? $in : array($in => "set");
Branch: 1 branch
BranchTrueFalseTotal
is_array($in) ? : 0 1 1
614 1 my %attrs;
615 1 foreach (@$attr) {
616 0 ▶ $attrs{$1} = $2 if $_ =~ /^\s*(\S+)\s+(.+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^\s*(\S+)\s+(.+)$/ 0 0 0
617 }
618 1 return %attrs;
619 }
620 1 ▶sub _parseDBAttributes { # Example: foo=123, bar=gii,baz;qux=werwe
Subroutine _parseDBAttributes: called CC=4 SCAR=14.2
621 1 ▶ my $t = shift || return ();
Condition50%1/2
shift() || (return ())
l!l
10
MC/DC0%0/1
shift() || (return ())
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
622 1 my %as = ();
623 1 ▶ for ($t ? (split /\s*[,;]\s*/, $t) : ()) {
Branch: 1 branch
BranchTrueFalseTotal
$t ? : 1 0 1
624 3 my ($k,$v) = (split /\s*=>?\s*|\s*\:\s*/, $_);
625 3 $as{$k} = $v;
626 }
627 1 return %as;
628 }
629  
630 2 1;
631  
632 __END__
« MToken/Server.pm ↑ Summary MToken/Server/Alpha.pm »