TaskPipe/Task_Record.pm

filter: Statementstmt 26.9% 28 / 104 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 76.9% 10 / 13 Podpod 0.0% 0 / 3 totaltotal 20.5% 38 / 185 CC 27 SCAR 53.3
CC
27
Cov
18%
record_row
373.9
action
72.0
set_dt
2.0
CRAP 206.9
↑ Summary TaskPipe/InterpParam/Parts.pm »
1 package TaskPipe::Task_Record;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use TryCatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 extends 'TaskPipe::Task';
10 1 with 'MooseX::ConfigCascade';
11  
12 1 has _test_table => (is => 'ro', isa => 'Str');
13 1 has dt => (is => 'rw', isa => 'DateTime');
14  
15 sub set_dt{
16 0 ▶ my ($self) = @_;
Subroutine set_dt: not called CC=1 SCAR=6.9
Pod undocumented
17 0 $self->dt( DateTime->now );
18 }
19  
20  
21 sub action{
22 0 ▶ my ($self) = @_;
Subroutine action: not called CC=8 SCAR=42.8
Pod undocumented
23
24 0 my $logger = Log::Log4perl->get_logger;
25  
26 0 ▶ if ( $self->pinterp->{'require'} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pinterp->{"require"}) 0 0 0
27 0 my $ok = 1;
28 0 foreach my $required ( @{$self->pinterp->{'require'}} ){
29 0 ▶ if ( ! $self->pinterp->{'values'}{$required} ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->pinterp->{"values"}{$required}) 0 0 0
30 0 $ok = 0;
31 0 last;
32 }
33 }
34 0 ▶ return [{}] unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
35 }
36  
37 0 my $table = $self->param->{table};
38 0 $self->run_info->task_details( $table );
39  
40 0 ▶ confess $self->run_info.": Could not record - no table specified" unless $self->param->{table};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->param->{"table"} 0 0 0
41  
42 0 $self->set_dt;
43  
44 0 my $record = $self->record_row( $self->pinterp->{'values'} );
45  
46 0 ▶ return [] unless $record;
Branch: 1 branch
BranchTrueFalseTotal
unless $record 0 0 0
47 0 ▶ return [{}] if ref $record eq ref {};
Branch: 1 branch
BranchTrueFalseTotal
if ref $record eq ref {} 0 0 0
48  
49 0 my $rec_hash = { $record->get_columns };
50  
51 0 delete $rec_hash->{modified_dt};
52 0 delete $rec_hash->{created_dt};
53  
54 0 return [ $rec_hash ];
55  
56 }
57  
58  
59 sub record_row{
60  
61 0 ▶ my ($self,$row,$table_name) = @_;
Subroutine record_row: not called CC=20 SCAR=59.2
Pod undocumented
62  
63 0 ▶ $table_name ||= $self->param->{table};
Condition0%0/3
$table_name ||= $self->param->{"table"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$table_name ||= $self->param->{"table"}
$table_name $self->param->{"table"}
Truth table0%0/3
A ||= B
A= $table_nameB= $self->param->{"table"}
ABresult
1X1
011
000
64 0 my $table = $self->sm->table( $table_name, 'plan' );
65  
66 0 my $logger = Log::Log4perl->get_logger;
67  
68 0 my $record={};
69 0 my $mode = $self->param->{mode};
70 0 my $options = {};
71 0 ▶ if ( $self->param->{key} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->param->{"key"}) 0 0 0
72 0 $options->{key} = $self->param->{key};
73 }
74  
75 0 my $row_serialized = $self->utils->serialize( $row );
76 0 $logger->debug( $row_serialized );
77  
78 0 ▶ if ( $self->_test_table ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_test_table) 0 0 0
79
80 0 $self->sm->table( $self->_test_table, 'plan' )->create({
81 thread_id => $self->run_info->thread_id,
82 target_table => $table_name,
83 result => $row_serialized
84 });
85  
86 0 sleep 1;
87 }
88  
89 0 ▶ if ( $mode && $mode eq 'insert' ){
Branch: 2 branches
BranchTrueFalseTotal
if ($mode and $mode eq 'insert') { } 0 0 0
elsif ($mode and $mode =~ /(find|new|skip)/) { } 0 0 0
Condition0%0/3
$mode and $mode eq "insert"
!ll&&!rl&&r
000
MC/DC0%0/2
$mode and $mode eq "insert"
$mode $mode eq "insert"
Truth table0%0/3
A and B
A= $modeB= $mode eq "insert"
ABresult
0X0
100
111
90  
91 0 $record = $table->create({
92 %$row,
93 created_dt => $self->dt,
94 modified_dt => $self->dt
95 });
96  
97 ▶ } elsif ( $mode && $mode =~ /(find|new|skip)/ ){
Condition0%0/3
$mode and $mode =~ /(find|new|skip)/
!ll&&!rl&&r
000
MC/DC0%0/2
$mode and $mode =~ /(find|new|skip)/
$mode $mode =~ /(find|new|skip)/
Truth table0%0/3
A and B
A= $modeB= $mode =~ /(find|new|skip)/
ABresult
0X0
100
111
98  
99 0 my $found = $table->find( $row );
100 0 ▶ $record = $found if $found and $mode eq 'find';
Branch: 1 branch
BranchTrueFalseTotal
if $found and $mode eq "find" 0 0 0
Condition0%0/3
$found and $mode eq "find"
!ll&&!rl&&r
000
MC/DC0%0/2
$found and $mode eq "find"
$found $mode eq "find"
Truth table0%0/3
A and B
A= $foundB= $mode eq "find"
ABresult
0X0
100
111
101 0 ▶ $record = undef if $found and $mode eq 'skip';
Branch: 1 branch
BranchTrueFalseTotal
if $found and $mode eq "skip" 0 0 0
Condition0%0/3
$found and $mode eq "skip"
!ll&&!rl&&r
000
MC/DC0%0/2
$found and $mode eq "skip"
$found $mode eq "skip"
Truth table0%0/3
A and B
A= $foundB= $mode eq "skip"
ABresult
0X0
100
111
102  
103 0 ▶ if ( ! $found ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 0 0
104  
105 1 ▶ try {
Subroutine BEGIN: called CC=1 SCAR=4.1
106  
107 0 $record = $table->create({
108 %$row,
109 created_dt => $self->dt,
110 modified_dt => $self->dt
111 }, $options);
112  
113 1 ▶ } catch ( DBIx::Error::IntegrityConstraintViolation $err ){
Subroutine BEGIN: called CC=1 SCAR=5.3
Branch: 2 branches
BranchTrueFalseTotal
if ($TryCatch::Error) 0 0 0
if ('TryCatch'->check_tc('DBIx::Error::IntegrityConstraintViolation')) { } 0 0 0
114  
115 0 ▶ $record = $table->find({
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "find" 0 0 0
116 %$row
117 }, $options) if $mode eq 'find';
118  
119 0 };
120  
121 }
122  
123 } else {
124  
125 0 $record = $table->update_or_new({
126 %$row,
127 }, $options);
128  
129  
130 0 ▶ if ( ! $record->in_storage ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($record->in_storage) 0 0 0
131
132 1 ▶ try {
Subroutine BEGIN: called CC=1 SCAR=4.1
133  
134 0 $record->insert;
135 0 $record->update({ created_dt => $self->dt });
136  
137 1 ▶ } catch ( DBIx::Error::IntegrityConstraintViolation $err ){
Subroutine BEGIN: called CC=1 SCAR=5.3
Branch: 2 branches
BranchTrueFalseTotal
if ($TryCatch::Error) 0 0 0
if ('TryCatch'->check_tc('DBIx::Error::IntegrityConstraintViolation')) { } 0 0 0
138  
139 0 $record = $table->find({
140 %$row,
141 },$options);
142  
143 0 ▶ confess "Unknown error when trying to update record: ".Dumper( $row ) unless $record;
Branch: 1 branch
BranchTrueFalseTotal
unless $record 0 0 0
144  
145 0 $record->update({
146 %$row
147 });
148  
149 0 };
150  
151 }
152  
153 $record->update({
154 0 modified_dt => $self->dt
155 });
156
157 }
158  
159 0 return $record;
160  
161 }
162  
163  
164 =head1 NAME
165  
166 TaskPipe::Task_Record - record a record to the database
167  
168 =head1 DESCRIPTION
169  
170 TaskPipe::Task_Record extends TaskPipe::Task. It is the standard task which records a record to the database for TaskPipe. You specify can this task in your plan in the following way:
171  
172 # (tree format):
173  
174 task:
175 _name: Record
176 mode: insert
177 values:
178 column1: value1
179 column2: value2
180  
181 # ...
182  
183 C<mode> is optional. If C<mode> is omitted, records where the primary key exists will be updated, and will be inserted new otherwise (ie "update or create"). If C<mode> is C<insert> an error will be produced in the duplicate record situation.
184  
185 =head1 AUTHOR
186  
187 Tom Gracey <tomgracey@gmail.com>
188  
189 =head1 COPYRIGHT AND LICENSE
190  
191 Copyright (c) Tom Gracey 2018
192  
193 TaskPipe is free software, licensed under
194  
195 The GNU Public License Version 3
196  
197 =cut
198
199  
200 1 __PACKAGE__->meta->make_immutable;
201 1 1;
↑ Summary TaskPipe/InterpParam/Parts.pm »