Teng/Row.pm

filter: Statementstmt 10.2% 11 / 107 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 16.6% 3 / 18 Podpod 100% 10 / 10 totaltotal 13.4% 24 / 178 CC 23 SCAR 29.5
CC
23
Cov
8%
update
42.0
_where_cond
42.0
__ANON__
20.0
CRAP 19.1
« Teng/Plugin/BulkInsert.pm ↑ Summary Teng/Plugin/Pager.pm »
1 package Teng::Row;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our $AUTOLOAD;
6  
7 sub new {
8 0 ▶ my ($class, $args) = @_;
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
9  
10 0 my $self = bless {
11 _get_column_cached => {},
12 _dirty_columns => {},
13 %$args,
14 }, $class;
15  
16 0 ▶ $self->{select_columns} ||= [keys %{$args->{row_data}}];
Condition0%0/2
$self->{"select_columns"} ||= [keys %{$$args{"row_data"};}]
l!l
00
MC/DC0%0/1
$self->{"select_columns"} ||= [keys %{$$args{"row_data"};}]
$self->{"select_columns"}
Truth table0%0/2
A
A= $self->{"select_columns"}
Aresult
11
00
17 0 ▶ $self->{table} ||= $args->{teng}->schema->get_table($args->{table_name});
Condition0%0/3
$self->{"table"} ||= $args->{"teng"}->schema->get_table($args->{"table_name"})
l!l&&r!l&&!r
000
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 table0%0/3
A ||= B
A= $self->{"table"}B= $args->{"teng"}->schema->get_table($args->{"table_name"})
ABresult
1X1
011
000
18  
19 0 $self;
20 }
21  
22 sub _lazy_get_data {
23 0 ▶ my ($x, $col) = @_;
Subroutine _lazy_get_data: not called CC=1 SCAR=6.9
24  
25 return sub {
26 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
27  
28 0 ▶ if ($self->{_untrusted_row_data}->{$col}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_untrusted_row_data"}{$col}) 0 0 0
29 0 Carp::carp("${col}'s row data is untrusted. by your update query.");
30 }
31 0 my $cache = $self->{_get_column_cached};
32 0 my $data = $cache->{$col};
33 0 ▶ if (! $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 0 0
34 0 ▶ $data = $cache->{$col} = $self->{table} ? $self->{table}->call_inflate($col, $self->get_column($col)) : $self->get_column($col);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'table'} ? : 0 0 0
35 }
36 0 return $data;
37 0 };
38 }
39  
40 0 ▶sub handle { $_[0]->{teng} }
Subroutine handle: not called CC=1 SCAR=6.9
Pod documented
41  
42 sub get_column {
43 0 ▶ my ($self, $col) = @_;
Subroutine get_column: not called CC=4 SCAR=30.0
Pod documented
44  
45 0 ▶ unless ( $col ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($col) 0 0 0
46 0 Carp::croak('please specify $col for first argument');
47 }
48  
49 0 ▶ if ( exists $self->{row_data}->{$col} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'row_data'}{$col}) { } 0 0 0
50 0 return $self->{row_data}->{$col};
51 } else {
52 0 ▶ Carp::croak("Specified colum '$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
53 }
54 }
55  
56 sub get_columns {
57 0 ▶ my $self = shift;
Subroutine get_columns: not called CC=2 SCAR=17.9
Pod documented
58  
59 0 my %data;
60 0 for my $col ( @{$self->{select_columns}} ) {
61 0 $data{$col} = $self->get_column($col);
62 }
63 0 return \%data;
64 }
65  
66 sub set_column {
67 0 ▶ my ($self, $col, $val) = @_;
Subroutine set_column: not called CC=2 SCAR=17.9
Pod documented
68  
69 0 ▶ if (ref($val) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "SCALAR") 0 0 0
70 0 $self->{_untrusted_row_data}->{$col} = 1;
71 }
72  
73 0 $self->{row_data}->{$col} = $val;
74 0 delete $self->{_get_column_cached}->{$col};
75 0 $self->{_dirty_columns}->{$col} = 1;
76 }
77  
78 sub set_columns {
79 0 ▶ my ($self, $args) = @_;
Subroutine set_columns: not called CC=2 SCAR=17.9
Pod documented
80  
81 0 for my $col (keys %$args) {
82 0 $self->set_column($col, $args->{$col});
83 }
84 }
85  
86 sub get_dirty_columns {
87 0 ▶ my $self = shift;
Subroutine get_dirty_columns: not called CC=1 SCAR=6.9
Pod documented
88  
89 0 my %rows = map {$_ => $self->get_column($_)}
90 0 keys %{$self->{_dirty_columns}};
91  
92 0 return \%rows;
93 }
94  
95 sub update {
96 0 ▶ my ($self, $upd) = @_;
Subroutine update: not called CC=6 SCAR=37.4
Pod documented
97  
98 0 ▶ if (ref($self) eq 'Teng::Row') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq "Teng::Row") 0 0 0
99 0 Carp::croak q{can't update from basic Teng::Row class.};
100 }
101  
102 0 my $table = $self->{table};
103 0 my $table_name = $self->{table_name};
104 0 ▶ if (! $table) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table) 0 0 0
105 0 Carp::croak( "Table definition for $table_name does not exist (Did you declare it in our schema?)" );
106 }
107  
108 0 ▶ %$upd = (%{$self->get_dirty_columns}, %{$upd||+{}});
Branch: 1 branch
BranchTrueFalseTotal
$upd or {} 0 0 0
109 0 for my $col (keys %{$upd}) {
110 0 $upd->{$col} = $table->call_deflate($col, $upd->{$col});
111 }
112  
113 0 my $where = $self->_where_cond;
114 0 $self->set_columns($upd);
115  
116 0 $upd = $self->get_dirty_columns;
117 0 ▶ return 0 unless %$upd;
Branch: 1 branch
BranchTrueFalseTotal
unless %$upd 0 0 0
118  
119 0 my $bind_args = $self->{teng}->_bind_sql_type_to_args($table, $upd);
120 0 my $result = $self->{teng}->_update($table_name, $bind_args, $where, 1);
121 0 $self->{_dirty_columns} = {};
122  
123 0 $result;
124 }
125  
126 sub delete {
127 0 ▶ my $self = shift;
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
128  
129 0 ▶ if (ref($self) eq 'Teng::Row') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq "Teng::Row") 0 0 0
130 0 Carp::croak q{can't delete from basic Teng::Row class.};
131 }
132  
133 0 $self->{teng}->delete($self->{table_name}, $self->_where_cond);
134 }
135  
136 sub refetch {
137 0 ▶ my $self = shift;
Subroutine refetch: not called CC=1 SCAR=6.9
Pod documented
138 0 $self->{teng}->single($self->{table_name}, $self->_where_cond);
139 }
140  
141 sub _where_cond {
142 0 ▶ my $self = shift;
Subroutine _where_cond: not called CC=6 SCAR=37.4
143  
144 0 my $table = $self->{table};
145 0 my $table_name = $self->{table_name};
146 0 ▶ unless ($table) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table) 0 0 0
147 0 Carp::croak("Unknown table: $table_name");
148 }
149  
150 # get target table pk
151 0 my $pk = $table->primary_keys;
152 0 ▶ unless ($pk) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pk) 0 0 0
153 0 Carp::croak("$table_name has no primary key.");
154 }
155  
156 # multi primary keys
157 0 ▶ if ( ref $pk eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pk eq 'ARRAY') { } 0 0 0
158 0 my %pks = map { $_ => 1 } @$pk;
159  
160 0 ▶ unless ( ( grep { exists $pks{ $_ } } @{$self->{select_columns}} ) == @$pk ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep({exists $pks{$_};} @{$$self{"select_columns"};}) == @$pk) 0 0 0
161 0 Carp::croak "can't get primary columns in your query.";
162 }
163  
164 0 return +{ map { $_ => $self->$_() } @$pk };
165 } else {
166 0 ▶ unless (grep { $pk eq $_ } @{$self->{select_columns}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$pk eq $_;} @{$$self{"select_columns"};}) 0 0 0
167 0 Carp::croak "can't get primary column in your query.";
168 }
169  
170 0 return +{ $pk => $self->$pk };
171 }
172 }
173  
174 sub AUTOLOAD{
175 0 ▶ my $self = shift;
Subroutine AUTOLOAD: not called CC=1 SCAR=6.9
176 0 my($method) = ($AUTOLOAD =~ /([^:']+$)/);
177 0 $self->_lazy_get_data($method)->($self);
178 }
179  
180 ### don't autoload this
181 0 ▶sub DESTROY { 1 };
Subroutine DESTROY: not called CC=1 SCAR=6.9
182  
183 1 1;
184  
185 __END__
186 =head1 NAME
187  
188 Teng::Row - Teng's Row class
189  
190 =head1 METHODS
191  
192 =over
193  
194 =item $row = Teng::Row->new
195  
196 create new Teng::Row's instance
197  
198 =item $row->get_column($column_name)
199  
200 my $val = $row->get_column($column_name);
201  
202 get a column value from a row object.
203  
204 =item $row->get_columns
205  
206 my %data = $row->get_columns;
207  
208 Does C<get_column>, for all column values.
209  
210 =item $row->set_columns(\%new_row_data)
211  
212 $row->set_columns({$col => $val});
213  
214 set columns data.
215  
216 =item $row->set_column($col => $val)
217  
218 $row->set_column($col => $val);
219  
220 set column data.
221  
222 =item $row->get_dirty_columns
223  
224 returns those that have been changed.
225  
226 =item $row->update([$arg])
227  
228 update is executed for instance record.
229  
230 It works by schema in which primary key exists.
231  
232 $row->update({name => 'tokuhirom'});
233 # or
234 $row->set({name => 'tokuhirom'});
235 $row->update;
236  
237 =item $row->delete
238  
239 delete is executed for instance record.
240  
241 It works by schema in which primary key exists.
242  
243 =item my $refetched_row = $row->refetch;
244  
245 refetch record from database. get new row object.
246  
247 =item $row->handle
248  
249 get teng object.
250  
251 $row->handle->single('table', {id => 1});
252  
253 =cut
254  
« Teng/Plugin/BulkInsert.pm ↑ Summary Teng/Plugin/Pager.pm »