| 1 | package BusyBird::StatusStorage::SQLite; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 12 | ▶ | use v5.8.0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use parent ("BusyBird::StatusStorage"); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use DBI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 12 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 12 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 12 | ▶ | use SQL::Maker 1.19; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 12 | ▶ | use SQL::QueryMaker 0.03 qw(sql_and sql_eq sql_ne sql_or sql_lt sql_le sql_raw); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 12 | ▶ | use BusyBird::DateTime::Format; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 12 | ▶ | use BusyBird::Util qw(set_param); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 12 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 12 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 12 | ▶ | use DateTime::Format::Strptime; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 12 | ▶ | use DateTime; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 12 | ▶ | no autovivification; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 12 | my @STATUSES_ORDER_BY = ('utc_acked_at DESC', 'utc_created_at DESC', 'status_id DESC'); | |||||||||||||||||||||||||||||||||||||||||
| 20 | 12 | my $DELETE_COUNT_ID = 0; | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 12 | my $UNDEF_TIMESTAMP = '9999-99-99T99:99:99'; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | { | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 12 | my $TIMESTAMP_FORMAT_STR = '%Y-%m-%dT%H:%M:%S'; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 12 | my $TIMESTAMP_FORMAT = DateTime::Format::Strptime->new( | |||||||||||||||||||||||||||||||||||||||||
| 27 | pattern => $TIMESTAMP_FORMAT_STR, | ||||||||||||||||||||||||||||||||||||||||||
| 28 | time_zone => 'UTC', | ||||||||||||||||||||||||||||||||||||||||||
| 29 | on_error => 'croak', | ||||||||||||||||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 4539 | ▶ | sub _format_datetime { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_format_datetime: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 4539 | my ($dt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 4539 | return $dt->strftime($TIMESTAMP_FORMAT_STR); | |||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 15621 | ▶ | sub _parse_datetime { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_datetime: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 15621 | my ($dt_str) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 15621 | return $TIMESTAMP_FORMAT->parse_datetime($dt_str); | |||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 12 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 183 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 183 | my $self = bless { | |||||||||||||||||||||||||||||||||||||||||
| 47 | maker => SQL::Maker->new(driver => 'SQLite', strict => 1), | ||||||||||||||||||||||||||||||||||||||||||
| 48 | in_memory_dbh => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 183 | $self->set_param(\%args, "path", undef, 1); | |||||||||||||||||||||||||||||||||||||||||
| 51 | 183 | $self->set_param(\%args, "max_status_num", 2000); | |||||||||||||||||||||||||||||||||||||||||
| 52 | 183 | $self->set_param(\%args, "hard_max_status_num", int($self->{max_status_num} * 1.2)); | |||||||||||||||||||||||||||||||||||||||||
| 53 | 183 | $self->set_param(\%args, "vacuum_on_delete", int($self->{max_status_num} * 2.0)); | |||||||||||||||||||||||||||||||||||||||||
| 54 | 183 | ▶ | croak "max_status_num must be a number" if !looks_like_number($self->{max_status_num}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 183 | ▶ | croak "hard_max_status_num must be a number" if !looks_like_number($self->{hard_max_status_num}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 183 | $self->{max_status_num} = int($self->{max_status_num}); | |||||||||||||||||||||||||||||||||||||||||
| 57 | 183 | $self->{hard_max_status_num} = int($self->{hard_max_status_num}); | |||||||||||||||||||||||||||||||||||||||||
| 58 | 183 | ▶ | croak "hard_max_status_num must be >= max_status_num" if !($self->{hard_max_status_num} >= $self->{max_status_num}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 183 | $self->_create_tables(); | |||||||||||||||||||||||||||||||||||||||||
| 60 | 182 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 821 | ▶ | sub _create_new_dbh { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_create_new_dbh: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 821 | my ($self, @connect_params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 821 | my $dbh = DBI->connect(@connect_params); | |||||||||||||||||||||||||||||||||||||||||
| 66 | 820 | $dbh->do(q{PRAGMA foreign_keys = ON}); | |||||||||||||||||||||||||||||||||||||||||
| 67 | 820 | return $dbh; | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 2649 | ▶ | sub _get_my_dbh { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_my_dbh: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 2649 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 2649 | my @connect_params = ("dbi:SQLite:dbname=$self->{path}", "", "", { | |||||||||||||||||||||||||||||||||||||||||
| 73 | RaiseError => 1, PrintError => 0, AutoCommit => 1, sqlite_unicode => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 74 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 2649 | ▶ | if($self->{path} eq ':memory:') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 2000 | ▶ | $self->{in_memory_dbh} = $self->_create_new_dbh(@connect_params) if !$self->{in_memory_dbh}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 2000 | return $self->{in_memory_dbh}; | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 649 | return $self->_create_new_dbh(@connect_params); | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 183 | ▶ | sub _create_tables { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_create_tables: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 183 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 183 | my $dbh = $self->_get_my_dbh(); | |||||||||||||||||||||||||||||||||||||||||
| 85 | 182 | $dbh->do(<<EOD); | |||||||||||||||||||||||||||||||||||||||||
| 86 | CREATE TABLE IF NOT EXISTS timelines ( | ||||||||||||||||||||||||||||||||||||||||||
| 87 | timeline_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, | ||||||||||||||||||||||||||||||||||||||||||
| 88 | name TEXT UNIQUE NOT NULL | ||||||||||||||||||||||||||||||||||||||||||
| 89 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 90 | EOD | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 182 | $dbh->do(<<EOD); | |||||||||||||||||||||||||||||||||||||||||
| 92 | CREATE TABLE IF NOT EXISTS statuses ( | ||||||||||||||||||||||||||||||||||||||||||
| 93 | timeline_id INTEGER NOT NULL | ||||||||||||||||||||||||||||||||||||||||||
| 94 | REFERENCES timelines(timeline_id) ON DELETE CASCADE ON UPDATE CASCADE, | ||||||||||||||||||||||||||||||||||||||||||
| 95 | status_id TEXT NOT NULL, | ||||||||||||||||||||||||||||||||||||||||||
| 96 | utc_acked_at TEXT NOT NULL, | ||||||||||||||||||||||||||||||||||||||||||
| 97 | utc_created_at TEXT NOT NULL, | ||||||||||||||||||||||||||||||||||||||||||
| 98 | timezone_acked_at TEXT NOT NULL, | ||||||||||||||||||||||||||||||||||||||||||
| 99 | timezone_created_at TEXT NOT NULL, | ||||||||||||||||||||||||||||||||||||||||||
| 100 | level INTEGER NOT NULL, | ||||||||||||||||||||||||||||||||||||||||||
| 101 | content TEXT NOT NULL, | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | PRIMARY KEY (timeline_id, status_id) | ||||||||||||||||||||||||||||||||||||||||||
| 104 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 105 | EOD | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 182 | $dbh->do(<<EOD); | |||||||||||||||||||||||||||||||||||||||||
| 107 | CREATE TABLE IF NOT EXISTS delete_counts ( | ||||||||||||||||||||||||||||||||||||||||||
| 108 | delete_count_id INTEGER PRIMARY KEY NOT NULL, | ||||||||||||||||||||||||||||||||||||||||||
| 109 | delete_count INTEGER NOT NULL | ||||||||||||||||||||||||||||||||||||||||||
| 110 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 111 | EOD | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 182 | my ($sql, @bind) = $self->{maker}->insert('delete_counts', [ | |||||||||||||||||||||||||||||||||||||||||
| 113 | delete_count_id => $DELETE_COUNT_ID, delete_count => 0 | ||||||||||||||||||||||||||||||||||||||||||
| 114 | ], {prefix => 'INSERT OR IGNORE INTO'}); | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 182 | $dbh->do($sql, undef, @bind); | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 3283 | ▶ | sub _record_hash_to_array { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_record_hash_to_array: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 3283 | my ($record) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 3283 | return [ map { $_, $record->{$_} } sort { $a cmp $b } keys %$record ]; | |||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 426 | ▶ | sub _put_update { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_put_update: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 426 | my ($self, $dbh, $record, $prev_sth) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 426 | my $sth = $prev_sth; | |||||||||||||||||||||||||||||||||||||||||
| 126 | my ($sql, @bind) = $self->{maker}->update('statuses', _record_hash_to_array($record), sql_and([ | ||||||||||||||||||||||||||||||||||||||||||
| 127 | sql_eq('timeline_id' => $record->{timeline_id}), sql_eq(status_id => $record->{status_id}) | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 426 | ])); | |||||||||||||||||||||||||||||||||||||||||
| 129 | 426 | ▶ | if(!$sth) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | ## Or, should we check $sql is not changed...? | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 111 | $sth = $dbh->prepare($sql); | |||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 426 | return ($sth->execute(@bind), $sth); | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 2857 | ▶ | sub _put_insert { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_put_insert: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 2857 | my ($self, $dbh, $record, $prev_sth) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 138 | 2857 | my $sth = $prev_sth; | |||||||||||||||||||||||||||||||||||||||||
| 139 | 2857 | my ($sql, @bind) = $self->{maker}->insert('statuses', _record_hash_to_array($record), { | |||||||||||||||||||||||||||||||||||||||||
| 140 | prefix => 'INSERT OR IGNORE INTO' | ||||||||||||||||||||||||||||||||||||||||||
| 141 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 2857 | ▶ | if(!$sth) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 371 | $sth = $dbh->prepare($sql); | |||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 2857 | return ($sth->execute(@bind), $sth); | |||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 68 | ▶ | sub _put_upsert { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_put_upsert: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 68 | my ($self, $dbh, $record) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 68 | my ($count) = $self->_put_update($dbh, $record); | |||||||||||||||||||||||||||||||||||||||||
| 151 | 68 | ▶ | if($count <= 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 34 | ($count) = $self->_put_insert($dbh, $record); | |||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | 68 | return ($count, undef); | |||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | 430 | ▶ | sub put_statuses { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
put_statuses: called CC=16 SCAR=28.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 430 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 159 | 430 | my $timeline = $args{timeline}; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 430 | ▶ | croak "timeline parameter is mandatory" if not defined $timeline; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 428 | my $mode = $args{mode}; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 428 | ▶ | croak "mode parameter is mandatory" if not defined $mode; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 426 | ▶ | if($mode ne 'insert' && $mode ne 'update' && $mode ne 'upsert') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$mode ne "insert" and $mode ne "update" and $mode ne "upsert"
$mode ne "insert" and $mode ne "update"
MC/DC0%0/3
$mode ne "insert" and $mode ne "update" and $mode ne "upsert"
$mode ne "insert"
$mode ne "update"
$mode ne "upsert"
Truth table75%3/4
A and B and C
A= $mode ne "insert"B= $mode ne "update"C= $mode ne "upsert"
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | croak "mode must be either insert, update or upsert"; | |||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 426 | my $statuses = $args{statuses}; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 426 | ▶ | croak "statuses parameter is mandatory" if not defined $statuses; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 424 | ▶ | if(ref($statuses) ne 'HASH' && ref($statuses) ne 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $statuses ne "HASH" and ref $statuses ne "ARRAY"
MC/DC0%0/2
ref $statuses ne "HASH" and ref $statuses ne "ARRAY"
ref $statuses ne "HASH"
ref $statuses ne "ARRAY"
Truth table66%2/3
A and B
A= ref $statuses ne "HASH"B= ref $statuses ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | croak "statuses parameter must be either a status object or an array-ref of statuses"; | |||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 424 | ▶ | if(ref($statuses) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 82 | $statuses = [$statuses]; | |||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 424 | foreach my $status (@$statuses) { | |||||||||||||||||||||||||||||||||||||||||
| 175 | 3309 | ▶ | croak "status object must be a hash-ref" if !defined($status) || !ref($status) || ref($status) ne 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
not defined $status && ref $status or ref $status ne "HASH"
defined $status && ref $status
MC/DC33%1/3
not defined $status && ref $status or ref $status ne "HASH"
defined $status
ref $status
ref $status ne "HASH"
Truth table50%2/4
not(A && B) or C
A= defined $statusB= ref $statusC= ref $status ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 3303 | ▶ | croak "status ID is missing" if not defined $status->{id}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 394 | ▶ | my $callback = $args{callback} || sub {}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$args{'callback'} || sub {
}
MC/DC0%0/2
$args{'callback'} || sub {
}
$args{'callback'}
sub {
}
Truth table33%1/3
A || B
A= $args{'callback'}B= sub {
}
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 394 | my $dbh; | |||||||||||||||||||||||||||||||||||||||||
| 180 | 394 | ▶ | my @results = try { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 394 | ▶ | return (undef, 0) if @$statuses == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 388 | $dbh = $self->_get_my_dbh(); | |||||||||||||||||||||||||||||||||||||||||
| 183 | 388 | $dbh->begin_work(); | |||||||||||||||||||||||||||||||||||||||||
| 184 | 388 | ▶ | my $timeline_id = $self->_get_timeline_id($dbh, $timeline) || $self->_create_timeline($dbh, $timeline); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->_get_timeline_id($dbh, $timeline) || $self->_create_timeline($dbh, $timeline)
MC/DC0%0/2
$self->_get_timeline_id($dbh, $timeline) || $self->_create_timeline($dbh, $timeline)
$self->_get_timeline_id($dbh, $timeline)
$self->_create_timeline($dbh, $timeline)
Truth table66%2/3
A || B
A= $self->_get_timeline_id($dbh, $timeline)B= $self->_create_timeline($dbh, $timeline)
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | 388 | ▶ | if(!defined($timeline_id)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | die "Internal error: could not create a timeline '$timeline' somehow."; | |||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 388 | my $sth; | |||||||||||||||||||||||||||||||||||||||||
| 189 | 388 | my $total_count = 0; | |||||||||||||||||||||||||||||||||||||||||
| 190 | 388 | my $put_method = "_put_$mode"; | |||||||||||||||||||||||||||||||||||||||||
| 191 | 388 | foreach my $status (@$statuses) { | |||||||||||||||||||||||||||||||||||||||||
| 192 | 3267 | my $record = _to_status_record($timeline_id, $status); | |||||||||||||||||||||||||||||||||||||||||
| 193 | 3249 | my $count; | |||||||||||||||||||||||||||||||||||||||||
| 194 | 3249 | ($count, $sth) = $self->$put_method($dbh, $record, $sth); | |||||||||||||||||||||||||||||||||||||||||
| 195 | 3249 | ▶ | if($count > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | 3211 | $total_count += $count; | |||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||
| 199 | 370 | my $exceeding_delete_count = 0; | |||||||||||||||||||||||||||||||||||||||||
| 200 | 370 | ▶ | if($mode ne "update" && $total_count > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$mode ne "update" and $total_count > 0
MC/DC100%2/2
$mode ne "update" and $total_count > 0
$mode ne "update"
$total_count > 0
Truth table100%3/3
A and B
A= $mode ne "update"B= $total_count > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 325 | $exceeding_delete_count = $self->_delete_exceeding_statuses($dbh, $timeline_id); | |||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||
| 203 | 370 | $dbh->commit(); | |||||||||||||||||||||||||||||||||||||||||
| 204 | 370 | ▶ | if($exceeding_delete_count > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | 8 | $self->_add_to_delete_count($dbh, $exceeding_delete_count); | |||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||
| 207 | 370 | return (undef, $total_count); | |||||||||||||||||||||||||||||||||||||||||
| 208 | 18 | ▶ | } catch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 18 | my $e = shift; | |||||||||||||||||||||||||||||||||||||||||
| 210 | 18 | ▶ | if($dbh) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 18 | $dbh->rollback(); | |||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||
| 213 | 18 | return ($e); | |||||||||||||||||||||||||||||||||||||||||
| 214 | 394 | }; | |||||||||||||||||||||||||||||||||||||||||
| 215 | 394 | @_ = @results; | |||||||||||||||||||||||||||||||||||||||||
| 216 | 394 | goto $callback; | |||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 2721 | ▶ | sub _get_timeline_id { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_timeline_id: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 2721 | my ($self, $dbh, $timeline_name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 221 | 2721 | my ($sql, @bind) = $self->{maker}->select('timelines', ['timeline_id'], sql_eq(name => $timeline_name)); | |||||||||||||||||||||||||||||||||||||||||
| 222 | 2721 | my $record = $dbh->selectrow_arrayref($sql, undef, @bind); | |||||||||||||||||||||||||||||||||||||||||
| 223 | 2721 | ▶ | if(!defined($record)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 644 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||
| 226 | 2077 | return $record->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | 267 | ▶ | sub _create_timeline { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_create_timeline: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | 267 | my ($self, $dbh, $timeline_name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 231 | 267 | my ($sql, @bind) = $self->{maker}->insert('timelines', [name => "$timeline_name"]); | |||||||||||||||||||||||||||||||||||||||||
| 232 | 267 | $dbh->do($sql, undef, @bind); | |||||||||||||||||||||||||||||||||||||||||
| 233 | 267 | return $self->_get_timeline_id($dbh, $timeline_name); | |||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | 3267 | ▶ | sub _to_status_record { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_to_status_record: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||
| 237 | 3267 | my ($timeline_id, $status) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 238 | 3267 | ▶ | croak "status ID must be set" if not defined $status->{id}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | 3267 | ▶ | croak "timeline_id must be defined" if not defined $timeline_id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | my $record = { | ||||||||||||||||||||||||||||||||||||||||||
| 241 | timeline_id => $timeline_id, | ||||||||||||||||||||||||||||||||||||||||||
| 242 | status_id => $status->{id}, | ||||||||||||||||||||||||||||||||||||||||||
| 243 | 3267 | ▶ | level => $status->{busybird}{level} || 0, | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$$status{'busybird'}{'level'} || 0
MC/DC100%1/1
$$status{'busybird'}{'level'} || 0
$$status{'busybird'}{'level'}
Truth table100%2/2
A
A= $$status{'busybird'}{'level'}
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 245 | 3261 | my $acked_at = $status->{busybird}{acked_at}; ## avoid autovivification | |||||||||||||||||||||||||||||||||||||||||
| 246 | 3261 | ($record->{utc_acked_at}, $record->{timezone_acked_at}) = _extract_utc_timestamp_and_timezone($acked_at); | |||||||||||||||||||||||||||||||||||||||||
| 247 | 3255 | ($record->{utc_created_at}, $record->{timezone_created_at}) = _extract_utc_timestamp_and_timezone($status->{created_at}); | |||||||||||||||||||||||||||||||||||||||||
| 248 | 3249 | $record->{content} = to_json($status); | |||||||||||||||||||||||||||||||||||||||||
| 249 | 3249 | return $record; | |||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||
| 252 | 10319 | ▶ | sub _from_status_record { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_from_status_record: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 10319 | my ($record) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 254 | 10319 | my $status = from_json($record->{content}); | |||||||||||||||||||||||||||||||||||||||||
| 255 | 10319 | $status->{id} = $record->{status_id}; | |||||||||||||||||||||||||||||||||||||||||
| 256 | 10319 | ▶ | if($record->{level} != 0 || defined($status->{busybird}{level})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$$record{"level"} != 0 or defined $$status{"busybird"}{"level"}
MC/DC100%2/2
$$record{"level"} != 0 or defined $$status{"busybird"}{"level"}
$$record{"level"} != 0
defined $$status{"busybird"}{"level"}
Truth table100%3/3
A or B
A= $$record{"level"} != 0B= defined $$status{"busybird"}{"level"}
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 2760 | $status->{busybird}{level} = $record->{level}; | |||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||
| 259 | 10319 | my $acked_at_str = _create_bb_timestamp_from_utc_timestamp_and_timezone($record->{utc_acked_at}, $record->{timezone_acked_at}); | |||||||||||||||||||||||||||||||||||||||||
| 260 | 10319 | ▶ | if(defined($acked_at_str) || defined($status->{busybird}{acked_at})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $acked_at_str or defined $$status{"busybird"}{"acked_at"}
MC/DC50%1/2
defined $acked_at_str or defined $$status{"busybird"}{"acked_at"}
defined $acked_at_str
defined $$status{"busybird"}{"acked_at"}
Truth table66%2/3
A or B
A= defined $acked_at_strB= defined $$status{"busybird"}{"acked_at"}
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | 5328 | $status->{busybird}{acked_at} = $acked_at_str; | |||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||
| 263 | 10319 | my $created_at_str = _create_bb_timestamp_from_utc_timestamp_and_timezone($record->{utc_created_at}, $record->{timezone_created_at}); | |||||||||||||||||||||||||||||||||||||||||
| 264 | 10319 | ▶ | if(defined($created_at_str) || defined($status->{created_at})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $created_at_str or defined $$status{"created_at"}
MC/DC50%1/2
defined $created_at_str or defined $$status{"created_at"}
defined $created_at_str
defined $$status{"created_at"}
Truth table66%2/3
A or B
A= defined $created_at_strB= defined $$status{"created_at"}
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | 10293 | $status->{created_at} = $created_at_str; | |||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||
| 267 | 10319 | return $status; | |||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | 6516 | ▶ | sub _extract_utc_timestamp_and_timezone { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_extract_utc_timestamp_and_timezone: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | 6516 | my ($timestamp_str) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 272 | 6516 | ▶ | if(!defined($timestamp_str) || $timestamp_str eq '') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $timestamp_str or $timestamp_str eq ""
MC/DC50%1/2
not defined $timestamp_str or $timestamp_str eq ""
not defined $timestamp_str
$timestamp_str eq ""
Truth table66%2/3
not(A) or B
A= not defined $timestamp_strB= $timestamp_str eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | 2106 | return ($UNDEF_TIMESTAMP, 'UTC'); | |||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 275 | 4410 | my $datetime = BusyBird::DateTime::Format->parse_datetime($timestamp_str); | |||||||||||||||||||||||||||||||||||||||||
| 276 | 4410 | ▶ | croak "Invalid datetime format: $timestamp_str" if not defined $datetime; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | 4398 | my $timezone_name = $datetime->time_zone->name; | |||||||||||||||||||||||||||||||||||||||||
| 278 | 4398 | $datetime->set_time_zone('UTC'); | |||||||||||||||||||||||||||||||||||||||||
| 279 | 4398 | my $utc_timestamp = _format_datetime($datetime); | |||||||||||||||||||||||||||||||||||||||||
| 280 | 4398 | return ($utc_timestamp, $timezone_name); | |||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | 20638 | ▶ | sub _create_bb_timestamp_from_utc_timestamp_and_timezone { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_create_bb_timestamp_from_utc_timestamp_and_timezone: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | 20638 | my ($utc_timestamp_str, $timezone) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 20638 | ▶ | if($utc_timestamp_str eq $UNDEF_TIMESTAMP) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 286 | 5017 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||||
| 288 | 15621 | my $dt = _parse_datetime($utc_timestamp_str); | |||||||||||||||||||||||||||||||||||||||||
| 289 | 15621 | $dt->set_time_zone($timezone); | |||||||||||||||||||||||||||||||||||||||||
| 290 | 15621 | return BusyBird::DateTime::Format->format_datetime($dt); | |||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | 1142 | ▶ | sub get_statuses { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_statuses: called CC=11 SCAR=24.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | 1142 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 295 | 1142 | my $timeline = $args{timeline}; | |||||||||||||||||||||||||||||||||||||||||
| 296 | 1142 | ▶ | croak "timeline parameter is mandatory" if not defined $timeline; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | 1140 | my $callback = $args{callback}; | |||||||||||||||||||||||||||||||||||||||||
| 298 | 1140 | ▶ | croak "callback parameter is mandatory" if not defined $callback; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 299 | 1138 | ▶ | croak "callback parameter must be a CODEREF" if ref($callback) ne "CODE"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | 1138 | ▶ | my $ack_state = defined($args{ack_state}) ? $args{ack_state} : "any"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | 1138 | ▶ | if($ack_state ne "any" && $ack_state ne "unacked" && $ack_state ne "acked") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$ack_state ne "any" and $ack_state ne "unacked" and $ack_state ne "acked"
$ack_state ne "any" and $ack_state ne "unacked"
MC/DC0%0/3
$ack_state ne "any" and $ack_state ne "unacked" and $ack_state ne "acked"
$ack_state ne "any"
$ack_state ne "unacked"
$ack_state ne "acked"
Truth table75%3/4
A and B and C
A= $ack_state ne "any"B= $ack_state ne "unacked"C= $ack_state ne "acked"
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | croak "ack_state parameter must be either 'any' or 'acked' or 'unacked'"; | |||||||||||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||||||||||
| 304 | 1138 | my $max_id = $args{max_id}; | |||||||||||||||||||||||||||||||||||||||||
| 305 | 1138 | ▶ | my $count = defined($args{count}) ? $args{count} : 'all'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 306 | 1138 | ▶ | if($count ne 'all' && !looks_like_number($count)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$count ne "all" and not &looks_like_number($count)
MC/DC0%0/2
$count ne "all" and not &looks_like_number($count)
$count ne "all"
not &looks_like_number($count)
Truth table66%2/3
A and not(B)
A= $count ne "all"B= not &looks_like_number($count)
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | 0 | croak "count parameter must be either 'all' or number"; | |||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||
| 309 | 1138 | ▶ | my @results = try { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 310 | 1138 | my $dbh = $self->_get_my_dbh(); | |||||||||||||||||||||||||||||||||||||||||
| 311 | 1138 | my $timeline_id = $self->_get_timeline_id($dbh, $timeline); | |||||||||||||||||||||||||||||||||||||||||
| 312 | 1138 | ▶ | if(!defined($timeline_id)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | 86 | return (undef, []); | |||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||
| 315 | 1052 | my $cond = $self->_create_base_condition($timeline_id, $ack_state); | |||||||||||||||||||||||||||||||||||||||||
| 316 | 1052 | ▶ | if(defined($max_id)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | 193 | my $max_id_cond = $self->_create_max_id_condition($dbh, $timeline_id, $max_id, $ack_state); | |||||||||||||||||||||||||||||||||||||||||
| 318 | 193 | ▶ | if(!defined($max_id_cond)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 319 | 78 | return (undef, []); | |||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||
| 321 | 115 | $cond = sql_and([$cond, $max_id_cond]); | |||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||
| 323 | 974 | my %maker_opt = (order_by => \@STATUSES_ORDER_BY); | |||||||||||||||||||||||||||||||||||||||||
| 324 | 974 | ▶ | if($count ne 'all') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | 204 | $maker_opt{limit} = "$count"; | |||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||
| 327 | 974 | my ($sql, @bind) = $self->{maker}->select("statuses", ['*'], $cond, \%maker_opt); | |||||||||||||||||||||||||||||||||||||||||
| 328 | 974 | my $sth = $dbh->prepare($sql); | |||||||||||||||||||||||||||||||||||||||||
| 329 | 974 | $sth->execute(@bind); | |||||||||||||||||||||||||||||||||||||||||
| 330 | 974 | my @statuses = (); | |||||||||||||||||||||||||||||||||||||||||
| 331 | 974 | ▶ | while(my $record = $sth->fetchrow_hashref('NAME_lc')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 332 | 10319 | push(@statuses, _from_status_record($record)); | |||||||||||||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||||||||||||
| 334 | 974 | return (undef, \@statuses); | |||||||||||||||||||||||||||||||||||||||||
| 335 | 0 | ▶ | }catch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | my $e = shift; | |||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | return ($e); | |||||||||||||||||||||||||||||||||||||||||
| 338 | 1138 | }; | |||||||||||||||||||||||||||||||||||||||||
| 339 | 1138 | @_ = @results; | |||||||||||||||||||||||||||||||||||||||||
| 340 | 1138 | goto $callback; | |||||||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | 1856 | ▶ | sub _create_base_condition { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_create_base_condition: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 344 | 1856 | my ($self, $timeline_id, $ack_state) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 345 | 1856 | ▶ | $ack_state ||= 'any'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$ack_state ||= "any"
MC/DC0%0/1
$ack_state ||= "any"
$ack_state
Truth table50%1/2
A
A= $ack_state
| |||||||||||||||||||||||||||||||||||||||||||
| 346 | 1856 | my $cond = sql_eq(timeline_id => $timeline_id); | |||||||||||||||||||||||||||||||||||||||||
| 347 | 1856 | ▶ | if($ack_state eq 'acked') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 348 | 369 | $cond = sql_and([$cond, sql_ne(utc_acked_at => $UNDEF_TIMESTAMP)]); | |||||||||||||||||||||||||||||||||||||||||
| 349 | }elsif($ack_state eq 'unacked') { | ||||||||||||||||||||||||||||||||||||||||||
| 350 | 970 | $cond = sql_and([$cond, sql_eq(utc_acked_at => $UNDEF_TIMESTAMP)]); | |||||||||||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||||||||||
| 352 | 1856 | return $cond; | |||||||||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||
| 355 | 244 | ▶ | sub _get_timestamps_of { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_timestamps_of: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 356 | 244 | my ($self, $dbh, $timeline_id, $status_id, $ack_state) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 357 | 244 | my $cond = $self->_create_base_condition($timeline_id, $ack_state); | |||||||||||||||||||||||||||||||||||||||||
| 358 | 244 | $cond = sql_and([$cond, sql_eq(status_id => $status_id)]); | |||||||||||||||||||||||||||||||||||||||||
| 359 | 244 | my ($sql, @bind) = $self->{maker}->select("statuses", ['utc_acked_at', 'utc_created_at'], $cond, { | |||||||||||||||||||||||||||||||||||||||||
| 360 | limit => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 361 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 362 | 244 | my $record = $dbh->selectrow_arrayref($sql, undef, @bind); | |||||||||||||||||||||||||||||||||||||||||
| 363 | 244 | ▶ | if(!$record) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 364 | 96 | return (); | |||||||||||||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||||||||||||
| 366 | 148 | return ($record->[0], $record->[1]); | |||||||||||||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | 244 | ▶ | sub _create_max_id_condition { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_create_max_id_condition: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 370 | 244 | my ($self, $dbh, $timeline_id, $max_id, $ack_state) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 371 | 244 | my ($max_acked_at, $max_created_at) = $self->_get_timestamps_of($dbh, $timeline_id, $max_id, $ack_state); | |||||||||||||||||||||||||||||||||||||||||
| 372 | 244 | ▶ | if(!defined($max_acked_at) || !defined($max_created_at)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $max_acked_at and defined $max_created_at
MC/DC50%1/2
defined $max_acked_at and defined $max_created_at
defined $max_acked_at
defined $max_created_at
Truth table66%2/3
A and B
A= defined $max_acked_atB= defined $max_created_at
| |||||||||||||||||||||||||||||||||||||||||||
| 373 | 96 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||||||||||||||||
| 375 | 148 | return $self->_create_max_time_condition($max_acked_at, $max_created_at, $max_id); | |||||||||||||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | 156 | ▶ | sub _create_max_time_condition { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_create_max_time_condition: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 379 | 156 | my ($self, $max_acked_at, $max_created_at, $max_id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 380 | 156 | my $cond = sql_or([ | |||||||||||||||||||||||||||||||||||||||||
| 381 | sql_lt(utc_acked_at => $max_acked_at), | ||||||||||||||||||||||||||||||||||||||||||
| 382 | sql_and([ | ||||||||||||||||||||||||||||||||||||||||||
| 383 | sql_eq(utc_acked_at => $max_acked_at), | ||||||||||||||||||||||||||||||||||||||||||
| 384 | sql_or([ | ||||||||||||||||||||||||||||||||||||||||||
| 385 | sql_lt(utc_created_at => $max_created_at), | ||||||||||||||||||||||||||||||||||||||||||
| 386 | sql_and([ | ||||||||||||||||||||||||||||||||||||||||||
| 387 | sql_eq(utc_created_at => $max_created_at), | ||||||||||||||||||||||||||||||||||||||||||
| 388 | sql_le(status_id => $max_id), | ||||||||||||||||||||||||||||||||||||||||||
| 389 | ]) | ||||||||||||||||||||||||||||||||||||||||||
| 390 | ]) | ||||||||||||||||||||||||||||||||||||||||||
| 391 | ]) | ||||||||||||||||||||||||||||||||||||||||||
| 392 | ]); | ||||||||||||||||||||||||||||||||||||||||||
| 393 | 156 | return $cond; | |||||||||||||||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||
| 396 | 145 | ▶ | sub ack_statuses { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ack_statuses: called CC=11 SCAR=24.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 397 | 145 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 398 | 145 | my $timeline = $args{timeline}; | |||||||||||||||||||||||||||||||||||||||||
| 399 | 145 | ▶ | croak "timeline parameter is mandatory" if not defined $timeline; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 400 | 143 | ▶ | my $callback = defined($args{callback}) ? $args{callback} : sub {}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 401 | 143 | ▶ | croak "callback parameter must be a CODEREF" if ref($callback) ne 'CODE'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 402 | 143 | my $ids = $args{ids}; | |||||||||||||||||||||||||||||||||||||||||
| 403 | 143 | ▶ | if(defined($ids) && ref($ids) && ref($ids) ne 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $ids and ref $ids and ref $ids ne "ARRAY"
defined $ids and ref $ids
MC/DC0%0/3
defined $ids and ref $ids and ref $ids ne "ARRAY"
defined $ids
ref $ids
ref $ids ne "ARRAY"
Truth table75%3/4
A and B and C
A= defined $idsB= ref $idsC= ref $ids ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 404 | 0 | croak "ids parameter must be either undef, a status ID or an array-ref of status IDs"; | |||||||||||||||||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||||||||||||||||
| 406 | 143 | ▶ | if(defined($ids) && !ref($ids)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $ids and not ref $ids
MC/DC100%2/2
defined $ids and not ref $ids
defined $ids
not ref $ids
Truth table100%3/3
A and not(B)
A= defined $idsB= not ref $ids
| |||||||||||||||||||||||||||||||||||||||||||
| 407 | 19 | $ids = [$ids]; | |||||||||||||||||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||||||||||||||||
| 409 | 143 | ▶ | if(defined($ids) && grep { !defined($_) } @$ids) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $ids and grep {not defined $_;} @$ids
MC/DC100%2/2
defined $ids and grep {not defined $_;} @$ids
defined $ids
grep {not defined $_;} @$ids
Truth table100%3/3
A and B
A= defined $idsB= grep {not defined $_;} @$ids
| |||||||||||||||||||||||||||||||||||||||||||
| 410 | 2 | croak "ids parameter array must not contain undef."; | |||||||||||||||||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||||||||||||||||
| 412 | 141 | my $max_id = $args{max_id}; | |||||||||||||||||||||||||||||||||||||||||
| 413 | 141 | my $dbh; | |||||||||||||||||||||||||||||||||||||||||
| 414 | 141 | ▶ | my @results = try { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||
| 415 | 141 | my $ack_utc_timestamp = _format_datetime(DateTime->now(time_zone => 'UTC')); | |||||||||||||||||||||||||||||||||||||||||
| 416 | 141 | $dbh = $self->_get_my_dbh(); | |||||||||||||||||||||||||||||||||||||||||
| 417 | 141 | my $timeline_id = $self->_get_timeline_id($dbh, $timeline); | |||||||||||||||||||||||||||||||||||||||||
| 418 | 141 | ▶ | return (undef, 0) if not defined $timeline_id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 419 | 141 | $dbh->begin_work(); | |||||||||||||||||||||||||||||||||||||||||
| 420 | 141 | my $total_count = 0; | |||||||||||||||||||||||||||||||||||||||||
| 421 | 141 | ▶ | if(!defined($ids) && !defined($max_id)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $ids || defined $max_id
MC/DC100%2/2
defined $ids || defined $max_id
defined $ids
defined $max_id
Truth table100%3/3
A || B
A= defined $idsB= defined $max_id
| |||||||||||||||||||||||||||||||||||||||||||
| 422 | 36 | $total_count = $self->_ack_all($dbh, $timeline_id, $ack_utc_timestamp); | |||||||||||||||||||||||||||||||||||||||||
| 423 | }else { | ||||||||||||||||||||||||||||||||||||||||||
| 424 | 105 | ▶ | if(defined($max_id)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 425 | 51 | my $max_id_count = $self->_ack_max_id($dbh, $timeline_id, $ack_utc_timestamp, $max_id); | |||||||||||||||||||||||||||||||||||||||||
| 426 | 51 | ▶ | $total_count += $max_id_count if $max_id_count > 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||||||||||
| 428 | 105 | ▶ | if(defined($ids)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 429 | 71 | my $ids_count = $self->_ack_ids($dbh, $timeline_id, $ack_utc_timestamp, $ids); | |||||||||||||||||||||||||||||||||||||||||
| 430 | 71 | ▶ | $total_count += $ids_count if $ids_count > 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||||||||||||
| 433 | 141 | $dbh->commit(); | |||||||||||||||||||||||||||||||||||||||||
| 434 | 141 | ▶ | $total_count = 0 if $total_count < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 435 | 141 | return (undef, $total_count); | |||||||||||||||||||||||||||||||||||||||||
| 436 | 0 | ▶ | }catch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 437 | 0 | my $e = shift; | |||||||||||||||||||||||||||||||||||||||||
| 438 | 0 | ▶ | if($dbh) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 439 | 0 | $dbh->rollback(); | |||||||||||||||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||||||||||||||
| 441 | 0 | return ($e); | |||||||||||||||||||||||||||||||||||||||||
| 442 | 141 | }; | |||||||||||||||||||||||||||||||||||||||||
| 443 | 141 | @_ = @results; | |||||||||||||||||||||||||||||||||||||||||
| 444 | 141 | goto $callback; | |||||||||||||||||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | 36 | ▶ | sub _ack_all { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ack_all: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 448 | 36 | my ($self, $dbh, $timeline_id, $ack_utc_timestamp) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 449 | my ($sql, @bind) = $self->{maker}->update( | ||||||||||||||||||||||||||||||||||||||||||
| 450 | 36 | 'statuses', [utc_acked_at => $ack_utc_timestamp], | |||||||||||||||||||||||||||||||||||||||||
| 451 | sql_and([sql_eq(timeline_id => $timeline_id), sql_eq(utc_acked_at => $UNDEF_TIMESTAMP)]), | ||||||||||||||||||||||||||||||||||||||||||
| 452 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 453 | 36 | return $dbh->do($sql, undef, @bind); | |||||||||||||||||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | 51 | ▶ | sub _ack_max_id { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ack_max_id: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 457 | 51 | my ($self, $dbh, $timeline_id, $ack_utc_timestamp, $max_id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 458 | 51 | my $max_id_cond = $self->_create_max_id_condition($dbh, $timeline_id, $max_id, 'unacked'); | |||||||||||||||||||||||||||||||||||||||||
| 459 | 51 | ▶ | if(!defined($max_id_cond)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 460 | 18 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||||||||||||||||
| 462 | 33 | my $cond = $self->_create_base_condition($timeline_id, 'unacked'); | |||||||||||||||||||||||||||||||||||||||||
| 463 | my ($sql, @bind) = $self->{maker}->update( | ||||||||||||||||||||||||||||||||||||||||||
| 464 | 33 | 'statuses', [utc_acked_at => $ack_utc_timestamp], sql_and([$cond, $max_id_cond]) | |||||||||||||||||||||||||||||||||||||||||
| 465 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 466 | 33 | return $dbh->do($sql, undef, @bind); | |||||||||||||||||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | 71 | ▶ | sub _ack_ids { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ack_ids: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 470 | 71 | my ($self, $dbh, $timeline_id, $ack_utc_timestamp, $ids) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 471 | 71 | ▶ | if(@$ids == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 472 | 14 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||||||||||||
| 474 | 57 | my $total_count = 0; | |||||||||||||||||||||||||||||||||||||||||
| 475 | 57 | my $sth; | |||||||||||||||||||||||||||||||||||||||||
| 476 | 57 | foreach my $id (@$ids) { | |||||||||||||||||||||||||||||||||||||||||
| 477 | 184 | my $cond = $self->_create_base_condition($timeline_id, 'unacked'); | |||||||||||||||||||||||||||||||||||||||||
| 478 | 184 | $cond = sql_and([$cond, sql_eq(status_id => $id)]); | |||||||||||||||||||||||||||||||||||||||||
| 479 | my ($sql, @bind) = $self->{maker}->update( | ||||||||||||||||||||||||||||||||||||||||||
| 480 | 184 | 'statuses', [utc_acked_at => $ack_utc_timestamp], $cond | |||||||||||||||||||||||||||||||||||||||||
| 481 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 482 | 184 | ▶ | if(!$sth) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 483 | 57 | $sth = $dbh->prepare($sql); | |||||||||||||||||||||||||||||||||||||||||
| 484 | } | ||||||||||||||||||||||||||||||||||||||||||
| 485 | 184 | my $count = $sth->execute(@bind); | |||||||||||||||||||||||||||||||||||||||||
| 486 | 184 | ▶ | if($count > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 487 | 94 | $total_count += $count; | |||||||||||||||||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||||||||||||||||
| 489 | } | ||||||||||||||||||||||||||||||||||||||||||
| 490 | 57 | return $total_count; | |||||||||||||||||||||||||||||||||||||||||
| 491 | } | ||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||
| 493 | 151 | ▶ | sub delete_statuses { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete_statuses: called CC=12 SCAR=25.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 494 | 151 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 495 | 151 | my $timeline = $args{timeline}; | |||||||||||||||||||||||||||||||||||||||||
| 496 | 151 | ▶ | croak 'timeline parameter is mandatory' if not defined $timeline; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 497 | 149 | ▶ | croak 'ids parameter is mandatory' if not exists $args{ids}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 498 | 147 | my $ids = $args{ids}; | |||||||||||||||||||||||||||||||||||||||||
| 499 | 147 | ▶ | if(defined($ids) && ref($ids) && ref($ids) ne 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $ids and ref $ids and ref $ids ne "ARRAY"
defined $ids and ref $ids
MC/DC0%0/3
defined $ids and ref $ids and ref $ids ne "ARRAY"
defined $ids
ref $ids
ref $ids ne "ARRAY"
Truth table75%3/4
A and B and C
A= defined $idsB= ref $idsC= ref $ids ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 500 | 0 | croak 'ids parameter must be either undef, a status ID or array-ref of status IDs.'; | |||||||||||||||||||||||||||||||||||||||||
| 501 | } | ||||||||||||||||||||||||||||||||||||||||||
| 502 | 147 | ▶ | if(defined($ids) && !ref($ids)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $ids and not ref $ids
MC/DC100%2/2
defined $ids and not ref $ids
defined $ids
not ref $ids
Truth table100%3/3
A and not(B)
A= defined $idsB= not ref $ids
| |||||||||||||||||||||||||||||||||||||||||||
| 503 | 15 | $ids = [$ids]; | |||||||||||||||||||||||||||||||||||||||||
| 504 | } | ||||||||||||||||||||||||||||||||||||||||||
| 505 | 147 | ▶ | if(defined($ids) && grep { !defined($_) } @$ids) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $ids and grep {not defined $_;} @$ids
MC/DC100%2/2
defined $ids and grep {not defined $_;} @$ids
defined $ids
grep {not defined $_;} @$ids
Truth table100%3/3
A and B
A= defined $idsB= grep {not defined $_;} @$ids
| |||||||||||||||||||||||||||||||||||||||||||
| 506 | 2 | croak "ids parameter array must not contain undef."; | |||||||||||||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||||||||||||
| 508 | 145 | ▶ | my $callback = defined($args{callback}) ? $args{callback} : sub {}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 509 | 145 | ▶ | croak 'callback parameter must be a CODEREF' if ref($callback) ne 'CODE'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 510 | 145 | my $dbh; | |||||||||||||||||||||||||||||||||||||||||
| 511 | 145 | ▶ | my @results = try { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 512 | 145 | my $dbh = $self->_get_my_dbh(); | |||||||||||||||||||||||||||||||||||||||||
| 513 | 145 | my $timeline_id = $self->_get_timeline_id($dbh, $timeline); | |||||||||||||||||||||||||||||||||||||||||
| 514 | 145 | ▶ | if(!defined($timeline_id)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 515 | 27 | return (undef, 0); | |||||||||||||||||||||||||||||||||||||||||
| 516 | } | ||||||||||||||||||||||||||||||||||||||||||
| 517 | 118 | $dbh->begin_work(); | |||||||||||||||||||||||||||||||||||||||||
| 518 | 118 | my $total_count; | |||||||||||||||||||||||||||||||||||||||||
| 519 | 118 | ▶ | if(defined($ids)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 520 | 31 | $total_count = $self->_delete_ids($dbh, $timeline_id, $ids); | |||||||||||||||||||||||||||||||||||||||||
| 521 | }else { | ||||||||||||||||||||||||||||||||||||||||||
| 522 | 87 | $total_count = $self->_delete_timeline($dbh, $timeline_id); | |||||||||||||||||||||||||||||||||||||||||
| 523 | } | ||||||||||||||||||||||||||||||||||||||||||
| 524 | 118 | ▶ | $total_count = 0 if $total_count < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 525 | 118 | $dbh->commit(); | |||||||||||||||||||||||||||||||||||||||||
| 526 | 118 | $self->_add_to_delete_count($dbh, $total_count); | |||||||||||||||||||||||||||||||||||||||||
| 527 | 118 | return (undef, $total_count); | |||||||||||||||||||||||||||||||||||||||||
| 528 | 0 | ▶ | }catch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 529 | 0 | my $e = shift; | |||||||||||||||||||||||||||||||||||||||||
| 530 | 0 | ▶ | if($dbh) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 531 | 0 | $dbh->rollback(); | |||||||||||||||||||||||||||||||||||||||||
| 532 | } | ||||||||||||||||||||||||||||||||||||||||||
| 533 | 0 | return ($e); | |||||||||||||||||||||||||||||||||||||||||
| 534 | 145 | }; | |||||||||||||||||||||||||||||||||||||||||
| 535 | 145 | @_ = @results; | |||||||||||||||||||||||||||||||||||||||||
| 536 | 145 | goto $callback; | |||||||||||||||||||||||||||||||||||||||||
| 537 | } | ||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||
| 539 | 87 | ▶ | sub _delete_timeline { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_delete_timeline: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 540 | 87 | my ($self, $dbh, $timeline_id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 541 | 87 | my ($sql, @bind) = $self->{maker}->delete('statuses', sql_eq(timeline_id => $timeline_id)); | |||||||||||||||||||||||||||||||||||||||||
| 542 | 87 | my $status_count = $dbh->do($sql, undef, @bind); | |||||||||||||||||||||||||||||||||||||||||
| 543 | 87 | ($sql, @bind) = $self->{maker}->delete('timelines', sql_eq(timeline_id => $timeline_id)); | |||||||||||||||||||||||||||||||||||||||||
| 544 | 87 | $dbh->do($sql, undef, @bind); | |||||||||||||||||||||||||||||||||||||||||
| 545 | 87 | return $status_count; | |||||||||||||||||||||||||||||||||||||||||
| 546 | } | ||||||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||||||
| 548 | 31 | ▶ | sub _delete_ids { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_delete_ids: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 549 | 31 | my ($self, $dbh, $timeline_id, $ids) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 550 | 31 | ▶ | return 0 if @$ids == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 551 | 31 | my $sth; | |||||||||||||||||||||||||||||||||||||||||
| 552 | 31 | my $total_count = 0; | |||||||||||||||||||||||||||||||||||||||||
| 553 | 31 | foreach my $id (@$ids) { | |||||||||||||||||||||||||||||||||||||||||
| 554 | 84 | my ($sql, @bind) = $self->{maker}->delete('statuses', sql_and([ | |||||||||||||||||||||||||||||||||||||||||
| 555 | sql_eq(timeline_id => $timeline_id), sql_eq(status_id => $id) | ||||||||||||||||||||||||||||||||||||||||||
| 556 | ])); | ||||||||||||||||||||||||||||||||||||||||||
| 557 | 84 | ▶ | if(!$sth) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 558 | 31 | $sth = $dbh->prepare($sql); | |||||||||||||||||||||||||||||||||||||||||
| 559 | } | ||||||||||||||||||||||||||||||||||||||||||
| 560 | 84 | my $count = $sth->execute(@bind); | |||||||||||||||||||||||||||||||||||||||||
| 561 | 84 | ▶ | if($count > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 562 | 70 | $total_count += $count; | |||||||||||||||||||||||||||||||||||||||||
| 563 | } | ||||||||||||||||||||||||||||||||||||||||||
| 564 | } | ||||||||||||||||||||||||||||||||||||||||||
| 565 | 31 | return $total_count; | |||||||||||||||||||||||||||||||||||||||||
| 566 | } | ||||||||||||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||||||||||||
| 568 | 325 | ▶ | sub _delete_exceeding_statuses { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_delete_exceeding_statuses: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||||||||||||||||
| 569 | 325 | my ($self, $dbh, $timeline_id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 570 | ## get total count in the timeline | ||||||||||||||||||||||||||||||||||||||||||
| 571 | 325 | my ($sql, @bind) = $self->{maker}->select('statuses', [\'count(*)'], sql_eq(timeline_id => $timeline_id)); | |||||||||||||||||||||||||||||||||||||||||
| 572 | 325 | my $row = $dbh->selectrow_arrayref($sql, undef, @bind); | |||||||||||||||||||||||||||||||||||||||||
| 573 | 325 | ▶ | if(!defined($row)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 574 | 0 | die "count query for timeline $timeline_id returns undef. something is wrong."; | |||||||||||||||||||||||||||||||||||||||||
| 575 | } | ||||||||||||||||||||||||||||||||||||||||||
| 576 | 325 | my $total_count = $row->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||||||||
| 578 | 325 | ▶ | if($total_count <= $self->{hard_max_status_num}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 579 | 317 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 580 | } | ||||||||||||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||||||||||||
| 582 | ## get the top of the exceeding statuses | ||||||||||||||||||||||||||||||||||||||||||
| 583 | ($sql, @bind) = $self->{maker}->select('statuses', [qw(utc_acked_at utc_created_at status_id)], sql_eq(timeline_id => $timeline_id), { | ||||||||||||||||||||||||||||||||||||||||||
| 584 | order_by => \@STATUSES_ORDER_BY, | ||||||||||||||||||||||||||||||||||||||||||
| 585 | offset => $self->{max_status_num}, | ||||||||||||||||||||||||||||||||||||||||||
| 586 | 8 | limit => 1, | |||||||||||||||||||||||||||||||||||||||||
| 587 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 588 | 8 | $row = $dbh->selectrow_arrayref($sql, undef, @bind); | |||||||||||||||||||||||||||||||||||||||||
| 589 | 8 | ▶ | if(!defined($row)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 590 | 0 | die "selecting the top of exceeding status returns undef. something is wrong."; | |||||||||||||||||||||||||||||||||||||||||
| 591 | } | ||||||||||||||||||||||||||||||||||||||||||
| 592 | 8 | my $time_cond = $self->_create_max_time_condition(@$row); | |||||||||||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||||||||||||
| 594 | ## execute deletion | ||||||||||||||||||||||||||||||||||||||||||
| 595 | 8 | my $timeline_cond = sql_eq(timeline_id => $timeline_id); | |||||||||||||||||||||||||||||||||||||||||
| 596 | 8 | ($sql, @bind) = $self->{maker}->delete('statuses', sql_and([$timeline_cond, $time_cond])); | |||||||||||||||||||||||||||||||||||||||||
| 597 | 8 | return $dbh->do($sql, undef, @bind); | |||||||||||||||||||||||||||||||||||||||||
| 598 | } | ||||||||||||||||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||||||||||||||||
| 600 | 607 | ▶ | sub get_unacked_counts { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_unacked_counts: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 601 | 607 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 602 | 607 | my $timeline = $args{timeline}; | |||||||||||||||||||||||||||||||||||||||||
| 603 | 607 | ▶ | croak 'timeline parameter is mandatory' if not defined $timeline; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 604 | 605 | my $callback = $args{callback}; | |||||||||||||||||||||||||||||||||||||||||
| 605 | 605 | ▶ | croak 'callback parameter is mandatory' if not defined $callback; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 606 | 603 | ▶ | croak 'callback parameter must be a CODEREF' if ref($callback) ne 'CODE'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 607 | 603 | ▶ | my @results = try { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 608 | 603 | my $dbh = $self->_get_my_dbh(); | |||||||||||||||||||||||||||||||||||||||||
| 609 | 603 | my $timeline_id = $self->_get_timeline_id($dbh, $timeline); | |||||||||||||||||||||||||||||||||||||||||
| 610 | 603 | my %result_obj = (total => 0); | |||||||||||||||||||||||||||||||||||||||||
| 611 | 603 | ▶ | if(!defined($timeline_id)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 612 | 260 | return (undef, \%result_obj); | |||||||||||||||||||||||||||||||||||||||||
| 613 | } | ||||||||||||||||||||||||||||||||||||||||||
| 614 | 343 | my $cond = $self->_create_base_condition($timeline_id, 'unacked'); | |||||||||||||||||||||||||||||||||||||||||
| 615 | 343 | my ($sql, @bind) = $self->{maker}->select('statuses', ['level', \'count(status_id)'], $cond, { | |||||||||||||||||||||||||||||||||||||||||
| 616 | group_by => 'level' | ||||||||||||||||||||||||||||||||||||||||||
| 617 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 618 | 343 | my $sth = $dbh->prepare($sql); | |||||||||||||||||||||||||||||||||||||||||
| 619 | 343 | $sth->execute(@bind); | |||||||||||||||||||||||||||||||||||||||||
| 620 | 343 | ▶ | while(my $record = $sth->fetchrow_arrayref()) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 621 | 395 | $result_obj{total} += $record->[1]; | |||||||||||||||||||||||||||||||||||||||||
| 622 | 395 | $result_obj{$record->[0]} = $record->[1]; | |||||||||||||||||||||||||||||||||||||||||
| 623 | } | ||||||||||||||||||||||||||||||||||||||||||
| 624 | 343 | return (undef, \%result_obj); | |||||||||||||||||||||||||||||||||||||||||
| 625 | 0 | ▶ | }catch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 626 | 0 | my $e = shift; | |||||||||||||||||||||||||||||||||||||||||
| 627 | 0 | return ($e); | |||||||||||||||||||||||||||||||||||||||||
| 628 | 603 | }; | |||||||||||||||||||||||||||||||||||||||||
| 629 | 603 | @_ = @results; | |||||||||||||||||||||||||||||||||||||||||
| 630 | 603 | goto $callback; | |||||||||||||||||||||||||||||||||||||||||
| 631 | } | ||||||||||||||||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||||||||||||||||
| 633 | 126 | ▶ | sub _add_to_delete_count { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_add_to_delete_count: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||||||||||||||||
| 634 | 126 | my ($self, $dbh, $add_count) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 635 | 126 | ▶ | return if $self->{vacuum_on_delete} <= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 636 | 126 | ▶ | return if $add_count <= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 637 | my ($sql, @bind) = $self->{maker}->update( | ||||||||||||||||||||||||||||||||||||||||||
| 638 | 122 | 'delete_counts', | |||||||||||||||||||||||||||||||||||||||||
| 639 | [delete_count => sql_raw('delete_count + ?', $add_count)], | ||||||||||||||||||||||||||||||||||||||||||
| 640 | sql_eq(delete_count_id => $DELETE_COUNT_ID)); | ||||||||||||||||||||||||||||||||||||||||||
| 641 | 122 | $dbh->do($sql, undef, @bind); | |||||||||||||||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||||||||||||||||
| 643 | 122 | ($sql, @bind) = $self->{maker}->select('delete_counts', ["delete_count"], sql_eq(delete_count_id => $DELETE_COUNT_ID)); | |||||||||||||||||||||||||||||||||||||||||
| 644 | 122 | my $row = $dbh->selectrow_arrayref($sql, undef, @bind); | |||||||||||||||||||||||||||||||||||||||||
| 645 | 122 | ▶ | if(!defined($row)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 646 | 0 | die "no delete_counts row with delete_count_id = $DELETE_COUNT_ID. something is wrong."; | |||||||||||||||||||||||||||||||||||||||||
| 647 | } | ||||||||||||||||||||||||||||||||||||||||||
| 648 | 122 | my $current_delete_count = $row->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||||||||||||||||
| 650 | 122 | ▶ | if($current_delete_count >= $self->{vacuum_on_delete}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 651 | 17 | $self->_do_vacuum($dbh); | |||||||||||||||||||||||||||||||||||||||||
| 652 | } | ||||||||||||||||||||||||||||||||||||||||||
| 653 | } | ||||||||||||||||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||||||||||||||||
| 655 | 19 | ▶ | sub _do_vacuum { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_do_vacuum: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 656 | 19 | my ($self, $dbh) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 657 | 19 | my ($sql, @bind) = $self->{maker}->update('delete_counts', [delete_count => 0], sql_eq(delete_count_id => $DELETE_COUNT_ID)); | |||||||||||||||||||||||||||||||||||||||||
| 658 | 19 | $dbh->do($sql, undef, @bind); | |||||||||||||||||||||||||||||||||||||||||
| 659 | 19 | $dbh->do('VACUUM'); | |||||||||||||||||||||||||||||||||||||||||
| 660 | } | ||||||||||||||||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||||||||||||||||
| 662 | 2 | ▶ | sub vacuum { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
vacuum: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 663 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 664 | 2 | $self->_do_vacuum($self->_get_my_dbh()); | |||||||||||||||||||||||||||||||||||||||||
| 665 | } | ||||||||||||||||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||||||||||||||||
| 667 | 47 | ▶ | sub contains { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
contains: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 668 | 47 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 669 | 47 | my $timeline = $args{timeline}; | |||||||||||||||||||||||||||||||||||||||||
| 670 | 47 | ▶ | croak 'timeline parameter is mandatory' if not defined $timeline; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 671 | 45 | my $callback = $args{callback}; | |||||||||||||||||||||||||||||||||||||||||
| 672 | 45 | ▶ | croak 'callback parameter is mandatory' if not defined $callback; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 673 | 43 | my $query = $args{query}; | |||||||||||||||||||||||||||||||||||||||||
| 674 | 43 | ▶ | croak 'query parameter is mandatory' if not defined $query; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 675 | 41 | my $ref_query = ref($query); | |||||||||||||||||||||||||||||||||||||||||
| 676 | 41 | ▶ | if(!$ref_query || $ref_query eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
not $ref_query or $ref_query eq "HASH"
MC/DC100%2/2
not $ref_query or $ref_query eq "HASH"
not $ref_query
$ref_query eq "HASH"
Truth table100%3/3
not(A) or B
A= not $ref_queryB= $ref_query eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 677 | 16 | $query = [$query]; | |||||||||||||||||||||||||||||||||||||||||
| 678 | }elsif($ref_query eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||
| 679 | ; | ||||||||||||||||||||||||||||||||||||||||||
| 680 | }else { | ||||||||||||||||||||||||||||||||||||||||||
| 681 | 0 | croak 'query parameter must be either a status, status ID or array-ref'; | |||||||||||||||||||||||||||||||||||||||||
| 682 | } | ||||||||||||||||||||||||||||||||||||||||||
| 683 | 41 | ▶ | if(grep { !defined($_) } @$query) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 684 | 2 | croak "query element must be defined"; | |||||||||||||||||||||||||||||||||||||||||
| 685 | } | ||||||||||||||||||||||||||||||||||||||||||
| 686 | 39 | ▶ | my @method_result = try { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||
| 687 | 39 | my $dbh = $self->_get_my_dbh(); | |||||||||||||||||||||||||||||||||||||||||
| 688 | 39 | my $timeline_id = $self->_get_timeline_id($dbh, $timeline); | |||||||||||||||||||||||||||||||||||||||||
| 689 | 39 | my @ret_contained = (); | |||||||||||||||||||||||||||||||||||||||||
| 690 | 39 | my @ret_not_contained = (); | |||||||||||||||||||||||||||||||||||||||||
| 691 | 39 | ▶ | if(!defined($timeline_id)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 692 | 4 | @ret_not_contained = @$query; | |||||||||||||||||||||||||||||||||||||||||
| 693 | 4 | return (undef, \@ret_contained, \@ret_not_contained); | |||||||||||||||||||||||||||||||||||||||||
| 694 | } | ||||||||||||||||||||||||||||||||||||||||||
| 695 | 35 | my $sth; | |||||||||||||||||||||||||||||||||||||||||
| 696 | 35 | foreach my $query_elem (@$query) { | |||||||||||||||||||||||||||||||||||||||||
| 697 | 132 | ▶ | my $status_id = (ref($query_elem) eq 'HASH') ? $query_elem->{id} : $query_elem; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 698 | 132 | ▶ | if(!defined($status_id)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 699 | ## ID-less statuses are always 'not contained'. | ||||||||||||||||||||||||||||||||||||||||||
| 700 | 12 | push @ret_not_contained, $query_elem; | |||||||||||||||||||||||||||||||||||||||||
| 701 | 12 | next; | |||||||||||||||||||||||||||||||||||||||||
| 702 | } | ||||||||||||||||||||||||||||||||||||||||||
| 703 | my ($sql, @bind) = $self->{maker}->select( | ||||||||||||||||||||||||||||||||||||||||||
| 704 | 120 | 'statuses', ['timeline_id', 'status_id'], | |||||||||||||||||||||||||||||||||||||||||
| 705 | sql_and([sql_eq(timeline_id => $timeline_id), sql_eq(status_id => $status_id)]) | ||||||||||||||||||||||||||||||||||||||||||
| 706 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 707 | 120 | ▶ | if(!$sth) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 708 | 28 | $sth = $dbh->prepare($sql); | |||||||||||||||||||||||||||||||||||||||||
| 709 | } | ||||||||||||||||||||||||||||||||||||||||||
| 710 | 120 | $sth->execute(@bind); | |||||||||||||||||||||||||||||||||||||||||
| 711 | 120 | my $result = $sth->fetchall_arrayref(); | |||||||||||||||||||||||||||||||||||||||||
| 712 | 120 | ▶ | if(!defined($result)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 713 | 0 | confess "Statement handle is inactive. Something is wrong."; | |||||||||||||||||||||||||||||||||||||||||
| 714 | } | ||||||||||||||||||||||||||||||||||||||||||
| 715 | 120 | ▶ | if(@$result) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 716 | 67 | push @ret_contained, $query_elem; | |||||||||||||||||||||||||||||||||||||||||
| 717 | }else { | ||||||||||||||||||||||||||||||||||||||||||
| 718 | 53 | push @ret_not_contained, $query_elem; | |||||||||||||||||||||||||||||||||||||||||
| 719 | } | ||||||||||||||||||||||||||||||||||||||||||
| 720 | } | ||||||||||||||||||||||||||||||||||||||||||
| 721 | 35 | return (undef, \@ret_contained, \@ret_not_contained); | |||||||||||||||||||||||||||||||||||||||||
| 722 | 0 | ▶ | }catch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 723 | 0 | my $e = shift; | |||||||||||||||||||||||||||||||||||||||||
| 724 | 0 | return ($e); | |||||||||||||||||||||||||||||||||||||||||
| 725 | 39 | }; | |||||||||||||||||||||||||||||||||||||||||
| 726 | 39 | @_ = @method_result; | |||||||||||||||||||||||||||||||||||||||||
| 727 | 39 | goto $callback; | |||||||||||||||||||||||||||||||||||||||||
| 728 | } | ||||||||||||||||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||||||||||||||||
| 730 | 10 | ▶ | sub get_timeline_names { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_timeline_names: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 731 | 10 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 732 | 10 | my $dbh = $self->_get_my_dbh(); | |||||||||||||||||||||||||||||||||||||||||
| 733 | my ($sql, @bind) = $self->{maker}->select( | ||||||||||||||||||||||||||||||||||||||||||
| 734 | 10 | 'timelines', ['name'] | |||||||||||||||||||||||||||||||||||||||||
| 735 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 736 | 10 | my $result = $dbh->selectall_arrayref($sql, undef, @bind); | |||||||||||||||||||||||||||||||||||||||||
| 737 | 10 | my @return = map { $_->[0] } @$result; | |||||||||||||||||||||||||||||||||||||||||
| 738 | 10 | return @return; | |||||||||||||||||||||||||||||||||||||||||
| 739 | } | ||||||||||||||||||||||||||||||||||||||||||
| 740 | |||||||||||||||||||||||||||||||||||||||||||
| 741 | 12 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||||||||||||||||
| 743 | __END__ | ||||||||||||||||||||||||||||||||||||||||||