blib/lib/Teng/Row.pm

filter: Statementstmt 28.7% 38 / 132 Branchbran 14.5% 7 / 48 Conditioncond 10.5% 2 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 42.8% 9 / 21 Podpod 92.8% 13 / 14 totaltotal 28.1% 69 / 245 CC 35 SCAR 34.2
CC
35
Cov
24%
set_column
72.0
update
72.0
_where_cond
56.0
CRAP 30.5
« blib/lib/Teng/Plugin/Lookup.pm ↑ Summary blib/lib/Teng.pm »
1 package Teng::Row;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 our $AUTOLOAD;
6  
7 # inside-out
8 5 our %obj;
9  
10 sub new {
11 7 ▶ my ($class, $args) = @_;
Subroutine new: called CC=3 SCAR=11.4
Pod documented
12  
13 7 my $self = bless {
14 # inflated values
15 _get_column_cached => {},
16 # values will be updated
17 _dirty_columns => {},
18 _autoload_column_cache => {},
19 %$args,
20 }, $class;
21  
22 7 ▶ $self->{select_columns} ||= [keys %{$args->{row_data}}];
Condition50%1/2
$self->{"select_columns"} ||= [keys %{$$args{"row_data"};}]
l!l
07
MC/DC0%0/1
$self->{"select_columns"} ||= [keys %{$$args{"row_data"};}]
$self->{"select_columns"}
Truth table50%1/2
A
A= $self->{"select_columns"}
Aresult
11
00
23 7 ▶ $self->{table} ||= $args->{teng}->schema->get_table($args->{table_name});
Condition33%1/3
$self->{"table"} ||= $args->{"teng"}->schema->get_table($args->{"table_name"})
l!l&&r!l&&!r
700
MC/DC0%0/2
$self->{"table"} ||= $args->{"teng"}->schema->get_table($args->{"table_name"})
$self->{"table"} $args->{"teng"}->schema->get_table($args->{"table_name"})
Truth table33%1/3
A ||= B
A= $self->{"table"}B= $args->{"teng"}->schema->get_table($args->{"table_name"})
ABresult
1X1
011
000
24  
25 7 $obj{$self+0} = delete $self->{teng};
26  
27 7 $self;
28 }
29  
30 sub generate_column_accessor {
31 26 ▶ my ($x, $col) = @_;
Subroutine generate_column_accessor: called CC=1 SCAR=0.0
Pod undocumented
32  
33 return sub {
34 4 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.1
35  
36 # setter is alias of set_column (not deflate column) for historical reason
37 4 ▶ return $self->set_column( $col => @_ ) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 4 4
38  
39 # getter is alias of get (inflate column)
40 4 $self->get($col);
41 26 };
42 }
43  
44 0 ▶sub handle { $obj{$_[0]+0} }
Subroutine handle: not called CC=1 SCAR=6.9
Pod documented
45  
46 sub get {
47 4 ▶ my ($self, $col) = @_;
Subroutine get: called CC=4 SCAR=14.8
Pod documented
48  
49 # "Untrusted" means the row is set_column by scalarref.
50 # e.g.
51 # $row->set_column("date" => \"DATE()");
52 4 ▶ if ($self->{_untrusted_row_data}->{$col}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_untrusted_row_data"}{$col}) 0 4 4
53 0 Carp::carp("${col}'s row data is untrusted. by your update query.");
54 }
55 4 my $cache = $self->{_get_column_cached};
56 4 my $data = $cache->{$col};
57 4 ▶ if (! $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 4 0 4
58 4 ▶ $data = $cache->{$col} = $self->{table} ? $self->{table}->call_inflate($col, $self->get_column($col)) : $self->get_column($col);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'table'} ? : 4 0 4
59 }
60 4 return $data;
61 }
62  
63 sub set {
64 0 ▶ my ($self, $col, $val) = @_;
Subroutine set: not called CC=1 SCAR=6.9
Pod documented
65 0 $self->set_column( $col => $val, deflate => 1);
66 0 delete $self->{_get_column_cached}->{$col};
67 0 return $self;
68 }
69  
70 sub get_column {
71 5 ▶ my ($self, $col) = @_;
Subroutine get_column: called CC=5 SCAR=20.9
Pod documented
72  
73 5 ▶ unless ( $col ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($col) 0 5 5
74 0 Carp::croak('please specify $col for first argument');
75 }
76  
77 5 ▶ if ( exists $self->{row_data}->{$col} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'row_data'}{$col}) { } 5 0 5
78 5 ▶ if (exists $self->{_dirty_columns}->{$col}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_dirty_columns'}{$col}) { } 0 5 5
79 0 return $self->{_dirty_columns}->{$col};
80 } else {
81 5 return $self->{row_data}->{$col};
82 }
83 } else {
84 0 ▶ Carp::croak("Specified column '$col' not found in row (query: " . ( $self->{sql} || 'unknown' ) . ")" );
Condition0%0/2
$self->{'sql'} || 'unknown'
l!l
00
MC/DC0%0/1
$self->{'sql'} || 'unknown'
$self->{'sql'}
Truth table0%0/2
A
A= $self->{'sql'}
Aresult
11
00
85 }
86 }
87  
88 sub get_columns {
89 0 ▶ my $self = shift;
Subroutine get_columns: not called CC=2 SCAR=17.9
Pod documented
90  
91 0 my %data;
92 0 for my $col ( @{$self->{select_columns}} ) {
93 0 $data{$col} = $self->get_column($col);
94 }
95 0 return \%data;
96 }
97  
98 sub set_column {
99 0 ▶ my ($self, $col, $val, %opts) = @_;
Subroutine set_column: not called CC=8 SCAR=42.8
Pod documented
100 0 ▶ if ($opts{deflate} || $self->handle->{force_deflate_set_column}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"deflate"} or $self->handle->{"force_deflate_set_column"}) 0 0 0
Condition0%0/3
$opts{"deflate"} or $self->handle->{"force_deflate_set_column"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"deflate"} or $self->handle->{"force_deflate_set_column"}
$opts{"deflate"} $self->handle->{"force_deflate_set_column"}
Truth table0%0/3
A or B
A= $opts{"deflate"}B= $self->handle->{"force_deflate_set_column"}
ABresult
1X1
011
000
101 0 $val = $self->{table}->call_deflate($col, $val);
102 }
103  
104 0 ▶ if ( defined $self->{row_data}->{$col}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"row_data"}{$col} and defined $val and $self->{"row_data"}{$col} eq $val) 0 0 0
Condition0%0/6
defined $self->{"row_data"}{$col} and defined $val and $self->{"row_data"}{$col} eq $val
!ll&&!rl&&r
000
defined $self->{"row_data"}{$col} and defined $val
!ll&&!rl&&r
000
MC/DC0%0/3
defined $self->{"row_data"}{$col} and defined $val and $self->{"row_data"}{$col} eq $val
defined $self->{"row_data"}{$col} defined $val $self->{"row_data"}{$col} eq $val
Truth table0%0/4
A and B and C
A= defined $self->{"row_data"}{$col}B= defined $valC= $self->{"row_data"}{$col} eq $val
ABCresult
0XX0
10X0
1100
1111
105 && defined $val
106 && $self->{row_data}->{$col} eq $val ) {
107 0 ▶ if (exists $self->{_dirty_columns}->{$col}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_dirty_columns"}{$col}) 0 0 0
108 0 delete $self->{_dirty_columns}->{$col};
109 0 delete $self->{_get_column_cached}->{$col};
110 0 delete $self->{_untrusted_row_data}->{$col};
111 }
112 0 return $val;
113 }
114  
115 0 ▶ if (ref($val) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "SCALAR") 0 0 0
116 0 $self->{_untrusted_row_data}->{$col} = 1;
117 }
118  
119 0 delete $self->{_get_column_cached}->{$col};
120 0 $self->{_dirty_columns}->{$col} = $val;
121  
122 0 $val;
123 }
124  
125 sub set_columns {
126 0 ▶ my ($self, $args) = @_;
Subroutine set_columns: not called CC=2 SCAR=17.9
Pod documented
127  
128 0 for my $col (keys %$args) {
129 0 $self->set_column($col, $args->{$col});
130 }
131 }
132  
133 sub get_dirty_columns {
134 0 ▶ my $self = shift;
Subroutine get_dirty_columns: not called CC=1 SCAR=6.9
Pod documented
135 0 +{ %{ $self->{_dirty_columns} } };
136 }
137  
138 sub is_changed {
139 0 ▶ my $self = shift;
Subroutine is_changed: not called CC=1 SCAR=6.9
Pod documented
140 0 keys %{$self->{_dirty_columns}} > 0
141 }
142  
143 sub update {
144 0 ▶ my ($self, $upd, $where) = @_;
Subroutine update: not called CC=8 SCAR=42.8
Pod documented
145  
146 0 ▶ if (ref($self) eq 'Teng::Row') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq "Teng::Row") 0 0 0
147 0 Carp::croak q{can't update from basic Teng::Row class.};
148 }
149  
150 0 my $table = $self->{table};
151 0 my $table_name = $self->{table_name};
152 0 ▶ if (! $table) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table) 0 0 0
153 0 Carp::croak( "Table definition for $table_name does not exist (Did you declare it in our schema?)" );
154 }
155  
156 0 ▶ if ($upd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upd) 0 0 0
157 0 for my $col (keys %$upd) {
158 0 $self->set($col => $upd->{$col});
159 }
160 }
161  
162 0 ▶ if ($where) {
Branch: 1 branch
BranchTrueFalseTotal
if ($where) { } 0 0 0
163 $where = {
164 %$where,
165 0 %{ $self->_where_cond },
166 };
167 }
168 else {
169 0 $where = $self->_where_cond;
170 }
171  
172 0 $upd = $self->get_dirty_columns;
173 0 ▶ return 0 unless %$upd;
Branch: 1 branch
BranchTrueFalseTotal
unless %$upd 0 0 0
174  
175 0 my $bind_args = $self->handle->_bind_sql_type_to_args($table, $upd);
176 0 my $result = $self->handle->do_update($table_name, $bind_args, $where, 1);
177 0 ▶ if ($result > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result > 0) 0 0 0
178 $self->{row_data} = {
179 0 %{ $self->{row_data} },
180 %$upd,
181 };
182 }
183 0 $self->{_dirty_columns} = {};
184  
185 0 $result;
186 }
187  
188 sub delete {
189 0 ▶ my $self = shift;
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
190  
191 0 ▶ if (ref($self) eq 'Teng::Row') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq "Teng::Row") 0 0 0
192 0 Carp::croak q{can't delete from basic Teng::Row class.};
193 }
194  
195 0 $self->handle->delete($self->{table_name}, $self->_where_cond);
196 }
197  
198 sub refetch {
199 0 ▶ my ($self, $opt) = @_;
Subroutine refetch: not called CC=1 SCAR=6.9
Pod documented
200 0 $self->handle->single($self->{table_name}, $self->_where_cond, $opt);
201 }
202  
203 # Generate a where clause to fetch this row itself.
204 sub _where_cond {
205 0 ▶ my $self = shift;
Subroutine _where_cond: not called CC=7 SCAR=40.3
206  
207 0 my $table = $self->{table};
208 0 my $table_name = $self->{table_name};
209 0 ▶ unless ($table) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table) 0 0 0
210 0 Carp::croak("Unknown table: $table_name");
211 }
212  
213 # get target table pk
214 0 my $pk = $table->primary_keys;
215 0 ▶ unless ($pk) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pk) 0 0 0
216 0 Carp::croak("$table_name has no primary key.");
217 }
218  
219 # multi primary keys
220 0 ▶ if ( ref $pk eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pk eq 'ARRAY') { } 0 0 0
221 0 ▶ unless (@$pk) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$pk) 0 0 0
222 0 Carp::croak("$table_name has no primary key.");
223 }
224  
225 0 my %pks = map { $_ => 1 } @$pk;
226  
227 0 ▶ unless ( ( grep { exists $pks{ $_ } } @{$self->{select_columns}} ) == @$pk ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep({exists $pks{$_};} @{$$self{"select_columns"};}) == @$pk) 0 0 0
228 0 Carp::croak "can't get primary columns in your query.";
229 }
230  
231 0 return +{ map { $_ => $self->{row_data}->{$_} } @$pk };
232 } else {
233 0 ▶ unless (grep { $pk eq $_ } @{$self->{select_columns}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$pk eq $_;} @{$$self{"select_columns"};}) 0 0 0
234 0 Carp::croak "can't get primary column in your query.";
235 }
236  
237 0 return +{ $pk => $self->{row_data}->{$pk} };
238 }
239 }
240  
241 # for +columns option by some search methods
242 sub AUTOLOAD {
243 0 ▶ my $self = shift;
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
244 0 my($method) = ($AUTOLOAD =~ /([^:']+$)/);
245 0 ▶ ($self->{_autoload_column_cache}{$method} ||= $self->generate_column_accessor($method))->($self);
Condition0%0/3
$self->{"_autoload_column_cache"}{$method} ||= $self->generate_column_accessor($method)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_autoload_column_cache"}{$method} ||= $self->generate_column_accessor($method)
$self->{"_autoload_column_cache"}{$method} $self->generate_column_accessor($method)
Truth table0%0/3
A ||= B
A= $self->{"_autoload_column_cache"}{$method}B= $self->generate_column_accessor($method)
ABresult
1X1
011
000
246 }
247  
248 ### don't autoload this
249 sub DESTROY {
250 7 ▶ my $self = shift;
Subroutine DESTROY: called CC=1 SCAR=0.0
251 7 delete $obj{$self+0};
252 };
253  
254 5 1;
255  
256 __END__
257 =head1 NAME
258  
259 Teng::Row - Teng's Row class
260  
261 =head1 METHODS
262  
263 =over
264  
265 =item $row = Teng::Row->new
266  
267 create new Teng::Row's instance
268  
269 =item $row->get($col)
270  
271 my $val = $row->get($column_name);
272  
273 # alias
274 my $val = $row->$column_name;
275  
276 get a column value from a row object.
277  
278 Note: This method inflates values.
279  
280 =item $row->set($col, $val)
281  
282 $row->set($col => $val);
283  
284 set column data.
285  
286 Note: This method deflates values.
287  
288 =item $row->get_column($column_name)
289  
290 my $val = $row->get_column($column_name);
291  
292 get a column value from a row object.
293  
294 Note: This method does not inflate values.
295  
296 =item $row->get_columns
297  
298 my $data = $row->get_columns;
299  
300 Does C<get_column>, for all column values.
301  
302 Note: This method does not inflate values.
303  
304 =item $row->set_columns(\%new_row_data)
305  
306 $row->set_columns({$col => $val});
307  
308 set columns data.
309  
310 Note: This method does not deflate values.
311  
312 =item $row->set_column($col => $val)
313  
314 $row->set_column($col => $val);
315  
316 # alias
317 $row->$col($val);
318  
319 set column data.
320  
321 Note: This method does not deflate values.
322  
323 =item $row->get_dirty_columns
324  
325 returns those that have been changed.
326  
327 =item $row->is_changed
328  
329 returns true, If the row object have a updated column.
330  
331 =item $row->update([$arg : HashRef, $where : HashRef])
332  
333 update is executed for instance record.
334  
335 It works by schema in which primary key exists.
336  
337 $row->update({name => 'tokuhirom'});
338 # or
339 $row->set({name => 'tokuhirom'});
340 $row->update;
341  
342 If C<$arg> is supplied, each pairs are passed to C<set()> method before update.
343  
344 If C<$where> is supplied, each pairs to be merged into default (primary keys) WHERE condition.
345 It is useful for optimistic lock.
346  
347 $row = $teng->single(table_name, {id => 1});
348 $result = $row->update({point => 2}, {point => 1});
349 # UPDATE table_name SET point = 2 WHERE id = 1 AND point = 1;
350  
351 =item $row->delete
352  
353 delete is executed for instance record.
354  
355 It works by schema in which primary key exists.
356  
357 =item my $refetched_row = $row->refetch([$opt:HashRef]);
358  
359 refetch record from database. get new row object.
360  
361 You can specify C<$opt> like C<< { for_update => 1} >> optionally, which is used to build query.
362  
363 =item $row->handle
364  
365 get Teng object.
366  
367 $row->handle->single('table', {id => 1});
368  
369 =back
370  
371 =head1 NOTE FOR COLUMN NAME METHOD
372  
373 Teng::Row has methods that have name from column name. For example, if a table has column named 'foo', Teng::Row instance of it has method 'foo'.
374  
375 This method has different behave for setter or getter as following:
376  
377 # (getter) is alias of $row->get('foo')
378 # so this method returns inflated value.
379 my $inflated_value = $row->foo;
380  
381 # (setter) is alias of $row->set_column('foo', $raw_value)
382 # so this method does not deflate the value. This only accepts raw value but inflated object.
383 $row->foo($raw_value);
384  
385 This behave is from historical reason. You should use column name methods with great caution, if you want to use this.
386  
387 =cut
388  
« blib/lib/Teng/Plugin/Lookup.pm ↑ Summary blib/lib/Teng.pm »