Object/Table.pm

filter: Statementstmt 23.5% 75 / 318 Branchbran 10.0% 6 / 60 Conditioncond 3.3% 1 / 30 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 44.0% 22 / 50 Podpod 92.8% 13 / 14 totaltotal 24.0% 117 / 486 SCAR 71.9
CC
50
Cov
20%
insert_many
110.0
_insert_for_data
42.0
_load_from_dbms
20.0
CRAP 1325.3
↑ Summary Meta/Role/FromSelect.pm »
1 package Fey::Object::Table;
2 {
3 9 $Fey::Object::Table::VERSION = '0.46';
4 }
5  
6 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 ▶use Fey::Literal::Function;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Fey::Placeholder;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Fey::SQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Fey::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use List::AllUtils qw( all );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Object::ID qw( object_id );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 9 ▶use Fey::Exceptions qw( param_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 9 ▶use Fey::ORM::Exceptions qw( no_such_row );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 override new => sub {
25 4 ▶ my $class = shift;
Subroutine __ANON__: called CC=6 SCAR=22.2
26  
27 4 ▶ if ( $class->meta()->_object_cache_is_enabled() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->meta->_object_cache_is_enabled) 0 4 4
28 0 ▶ my $instance
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
29 = $class->meta()->_search_cache( ref $_[0] ? $_[0] : {@_} );
30  
31 0 ▶ return $instance if $instance;
Branch: 1 branch
BranchTrueFalseTotal
if $instance 0 0 0
32 }
33  
34 4 my $instance;
35 4 my @args = @_;
36  
37 4 $class->_ClearConstructorError();
38  
39 try {
40 4 ▶ $instance = super();
Subroutine __ANON__: called CC=1 SCAR=0.0
41 }
42 catch {
43 1 ▶ die $_ unless blessed $_ && $_->isa('Fey::Exception::NoSuchRow');
Subroutine __ANON__: called CC=3 SCAR=15.4
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($_) and $_->isa("Fey::Exception::NoSuchRow") 1 0 1
Condition33%1/3
builtin::blessed($_) and $_->isa("Fey::Exception::NoSuchRow")
!ll&&!rl&&r
010
MC/DC0%0/2
builtin::blessed($_) and $_->isa("Fey::Exception::NoSuchRow")
builtin::blessed($_) $_->isa("Fey::Exception::NoSuchRow")
Truth table33%1/3
A and B
A= builtin::blessed($_)B= $_->isa("Fey::Exception::NoSuchRow")
ABresult
0X0
100
111
44 0 $class->_SetConstructorError($_);
45 4 };
46  
47 3 ▶ return unless $instance;
Branch: 1 branch
BranchTrueFalseTotal
unless $instance 0 3 3
48  
49 3 ▶ $class->meta()->_write_to_cache($instance)
Branch: 1 branch
BranchTrueFalseTotal
if $class->meta->_object_cache_is_enabled 0 3 3
50 if $class->meta()->_object_cache_is_enabled();
51  
52 3 return $instance;
53 9 };
54  
55 # I'd like to use MX::ClassAttribute but trying to apply this to each
56 # Fey::ORM::Table-using class causes all sorts of weird errors.
57 {
58 9 my %E;
59  
60 sub ConstructorError {
61 0 ▶ my $class = shift;
Subroutine ConstructorError: not called CC=1 SCAR=6.9
Pod documented
62  
63 0 return $E{$class};
64 }
65  
66 sub _SetConstructorError {
67 0 ▶ my $class = shift;
Subroutine _SetConstructorError: not called CC=1 SCAR=6.9
68  
69 0 $E{$class} = shift;
70 }
71  
72 sub _ClearConstructorError {
73 4 ▶ my $class = shift;
Subroutine _ClearConstructorError: called CC=1 SCAR=0.0
74  
75 4 delete $E{$class};
76 }
77 }
78  
79 9 sub BUILD {
80 4 ▶ my $self = shift;
Subroutine BUILD: called CC=2 SCAR=7.6
Pod undocumented
81 4 my $p = shift;
82  
83 4 ▶ if ( delete $p->{_from_query} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $p->{"_from_query"}) 4 0 4
84 4 $self->_require_pk($p);
85  
86 4 return;
87 }
88  
89 0 $self->_load_from_dbms($p);
90  
91 0 return;
92 }
93  
94 sub _require_pk {
95 4 ▶ my $self = shift;
Subroutine _require_pk: called CC=2 SCAR=7.5
96 4 my $p = shift;
97  
98 return
99 4 ▶ if all { defined $p->{$_} }
Subroutine __ANON__: called CC=1 SCAR=0.0
100 4 ▶ map { $_->name() } @{ $self->Table()->primary_key() };
Branch: 1 branch
BranchTrueFalseTotal
if &all(sub { defined $p->{$_}; } , map({$_->name;} @{$self->Table->primary_key;})) 4 0 4
101  
102 0 my $package = ref $self;
103 0 param_error
104 "$package->new() requires that you pass the primary key if you set _from_query to true.";
105 }
106  
107 sub EnableObjectCache {
108 0 ▶ my $class = shift;
Subroutine EnableObjectCache: not called CC=1 SCAR=6.9
Pod documented
109  
110 0 $class->meta()->_set_object_cache_is_enabled(1);
111 }
112  
113 sub DisableObjectCache {
114 0 ▶ my $class = shift;
Subroutine DisableObjectCache: not called CC=1 SCAR=6.9
Pod documented
115  
116 0 $class->meta()->_set_object_cache_is_enabled(0);
117 }
118  
119 sub ClearObjectCache {
120 0 ▶ my $class = shift;
Subroutine ClearObjectCache: not called CC=1 SCAR=6.9
Pod documented
121  
122 0 $class->meta()->_clear_object_cache();
123 }
124  
125 sub _load_from_dbms {
126 0 ▶ my $self = shift;
Subroutine _load_from_dbms: not called CC=4 SCAR=30.0
127 0 my $p = shift;
128  
129 0 for my $key ( @{ $self->Table()->candidate_keys() } ) {
130 0 my @names = map { $_->name() } @{$key};
131 0 ▶ next unless all { defined $p->{$_} } @names;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless &all(sub { defined $p->{$_}; } , @names) 0 0 0
132  
133 0 ▶ return if $self->_load_from_key( $key, [ @{$p}{@names} ] );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_load_from_key($key, [@{$p;}{@names}]) 0 0 0
134 }
135  
136 0 my $error = 'Could not find a row in ' . $self->Table()->name();
137 0 $error .= ' matching the values you provided to the constructor.';
138  
139 0 no_such_row $error;
140 }
141  
142 sub _load_from_key {
143 0 ▶ my $self = shift;
Subroutine _load_from_key: not called CC=3 SCAR=24.8
144 0 my $key = shift;
145 0 my $bind = shift;
146  
147 0 my $select = $self->_SelectSQLForKey($key);
148  
149 0 ▶ return 1 if $self->_get_column_values( $select, $bind );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_get_column_values($select, $bind) 0 0 0
150  
151 0 my $error = 'Could not find a row in ' . $self->Table()->name();
152 0 $error .= ' where ';
153  
154 0 my @where;
155  
156 0 ▶ for ( my $i = 0; $i < @{$key}; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$key;}) 0 0 0
157 0 push @where, $key->[$i]->name() . q{ = } . $bind->[$i];
158 }
159  
160 0 $error .= join ', ', @where;
161  
162 0 no_such_row $error;
163 }
164  
165 # Based on discussions on #moose, this could be done more elegantly
166 # with a custom instance metaclass that lazily initializes a batch of
167 # attributes at once.
168 sub _get_column_values {
169 0 ▶ my $self = shift;
Subroutine _get_column_values: not called CC=2 SCAR=17.9
170 0 my $select = shift;
171 0 my $bind = shift;
172  
173 0 my $dbh = $self->_dbh($select);
174  
175 0 my $sth = $dbh->prepare( $self->_sql_string( $select, $dbh ) );
176  
177 0 $sth->execute( @{$bind} );
178  
179 0 my %col_values;
180 0 $sth->bind_columns( \( @col_values{ @{ $sth->{NAME} } } ) );
181  
182 0 my $fetched = $sth->fetch();
183  
184 0 $sth->finish();
185  
186 0 ▶ return unless $fetched;
Branch: 1 branch
BranchTrueFalseTotal
unless $fetched 0 0 0
187  
188 0 $self->_set_column_values_from_hashref( \%col_values );
189  
190 0 return \%col_values;
191 }
192  
193 sub _set_column_values_from_hashref {
194 0 ▶ my $self = shift;
Subroutine _set_column_values_from_hashref: not called CC=2 SCAR=17.9
195 0 my $values = shift;
196  
197 0 for my $col ( keys %{$values} ) {
198 0 my $set = q{_set_} . $col;
199  
200 0 $self->$set( $values->{$col} );
201 }
202 }
203  
204 sub _get_column_value {
205 0 ▶ my $self = shift;
Subroutine _get_column_value: not called CC=1 SCAR=6.9
206  
207 0 my $col_values = $self->_get_column_values(
208 $self->meta()->_select_by_pk_sql(),
209 [ $self->pk_values_list() ],
210 );
211  
212 0 my $name = shift;
213  
214 0 return $col_values->{$name};
215 }
216  
217 sub pk_values_list {
218 0 ▶ my $self = shift;
Subroutine pk_values_list: not called CC=1 SCAR=6.9
Pod documented
219  
220 0 my @cols = ( map { $_->name() } @{ $self->Table()->primary_key() } );
221  
222 0 return map { $self->_deflated_value($_) } @cols;
223 }
224  
225 sub _MakeSelectByPKSQL {
226 0 ▶ my $class = shift;
Subroutine _MakeSelectByPKSQL: not called CC=1 SCAR=6.9
227  
228 0 return $class->_SelectSQLForKey( $class->Table->primary_key() );
229 }
230  
231 sub _SelectSQLForKey {
232 0 ▶ my $class = shift;
Subroutine _SelectSQLForKey: not called CC=3 SCAR=24.8
233 0 my $key = shift;
234  
235 0 my $cache = $class->meta()->_select_sql_cache();
236  
237 0 my $select = $cache->get($key);
238  
239 0 ▶ return $select if $select;
Branch: 1 branch
BranchTrueFalseTotal
if $select 0 0 0
240  
241 0 my $table = $class->Table();
242  
243 0 my @select = $table->columns();
244  
245 0 $select = $class->SchemaClass()->SQLFactoryClass()->new_select();
246 0 $select->select( sort { $a->name() cmp $b->name() } @select );
247 0 $select->from($table);
248 0 $select->where( $_, '=', Fey::Placeholder->new() ) for @{$key};
249  
250 0 $cache->store( $key => $select );
251  
252 0 return $select;
253 }
254  
255 sub insert {
256 0 ▶ my $class = shift;
Subroutine insert: not called CC=1 SCAR=6.9
Pod documented
257 0 my %p = @_;
258  
259 0 return $class->insert_many( \%p );
260 }
261  
262 sub insert_many {
263 0 ▶ my $class = shift;
Subroutine insert_many: not called CC=10 SCAR=47.0
Pod documented
264 0 my @rows = @_;
265  
266 0 my $insert = $class->_insert_for_data( $rows[0] );
267  
268 0 my $dbh = $class->_dbh($insert);
269  
270 0 my $sth = $dbh->prepare( $class->_sql_string( $insert, $dbh ) );
271  
272 my @auto_inc_columns = (
273 0 grep { !exists $rows[0]->{$_} }
274 0 map { $_->name() }
275 0 grep { $_->is_auto_increment() } $class->Table->columns()
276 );
277  
278 0 my $table_name = $class->Table()->name();
279  
280 0 my @non_literal_row_keys;
281 my @literal_row_keys;
282 0 my @ref_row_keys;
283  
284 0 for my $key ( sort keys %{ $rows[0] } ) {
285 0 my $val = $rows[0]{$key};
286  
287 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and builtin::blessed($val) and $val->can('does') and $val->does('Fey::Role::IsLiteral') || $val->does('Fey::Role::SQL::ReturnsData')) { } 0 0 0
Condition0%0/12
defined $val and builtin::blessed($val) and $val->can("does") and $val->does("Fey::Role::IsLiteral") || $val->does("Fey::Role::SQL::ReturnsData")
!ll&&!rl&&r
000
defined $val and builtin::blessed($val) and $val->can("does")
!ll&&!rl&&r
000
defined $val and builtin::blessed($val)
!ll&&!rl&&r
000
$val->does("Fey::Role::IsLiteral") or $val->does("Fey::Role::SQL::ReturnsData")
l!l&&r!l&&!r
000
MC/DC0%0/5
defined $val and builtin::blessed($val) and $val->can("does") and $val->does("Fey::Role::IsLiteral") || $val->does("Fey::Role::SQL::ReturnsData")
defined $val builtin::blessed($val) $val->can("does") $val->does("Fey::Role::IsLiteral") $val->does("Fey::Role::SQL::ReturnsData")
Truth table0%0/6
A and B and C and D or E
A= defined $valB= builtin::blessed($val)C= $val->can("does")D= $val->does("Fey::Role::IsLiteral")E= $val->does("Fey::Role::SQL::ReturnsData")
ABCDEresult
0XXXX0
10XXX0
110XX0
111000
1111X1
111011
288 defined $val
289 && blessed $val
290 && $val->can('does')
291 && ( $val->does('Fey::Role::IsLiteral')
292 || $val->does('Fey::Role::SQL::ReturnsData') )
293 ) {
294 0 push @literal_row_keys, $key;
295 0 push @ref_row_keys, $key;
296 }
297 else {
298 0 push @non_literal_row_keys, $key;
299 0 ▶ push @ref_row_keys, $key
Branch: 1 branch
BranchTrueFalseTotal
if ref $val 0 0 0
300 if ref $val;
301 }
302 }
303  
304 my @bind_attributes
305 0 = $class->_bind_attributes_for( $dbh, @non_literal_row_keys );
306  
307 0 my $wantarray = wantarray;
308  
309 0 my @objects;
310 0 for my $row (@rows) {
311 0 push @objects,
312 $class->_insert_one_row(
313 $row,
314 $dbh,
315 $sth,
316 \@non_literal_row_keys,
317 \@ref_row_keys,
318 \@bind_attributes,
319 \@auto_inc_columns,
320 $table_name,
321 $wantarray,
322 );
323 }
324  
325 0 ▶ return $wantarray ? @objects : $objects[0];
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
326 }
327  
328 sub _bind_attributes_for {
329 0 ▶ my $self = shift;
Subroutine _bind_attributes_for: not called CC=4 SCAR=30.0
330 0 my $dbh = shift;
331 0 my @keys = @_;
332  
333 0 ▶ return unless $dbh->{Driver}{Name} eq 'Pg';
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->{"Driver"}{"Name"} eq "Pg" 0 0 0
334  
335 my @attr = map {
336 0 ▶ lc $self->Table()->column($_)->type() eq 'bytea'
Branch: 1 branch
BranchTrueFalseTotal
lc $self->Table->column($_)->type eq 'bytea' ? : 0 0 0
337 ? { pg_type => DBD::Pg::PG_BYTEA() }
338 : {}
339 } @keys;
340  
341 0 ▶ return unless grep { keys %{$_} } @attr;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {keys %{$_;};} @attr 0 0 0
342  
343 0 return @attr;
344 }
345  
346 sub _insert_one_row {
347 0 ▶ my $class = shift;
Subroutine _insert_one_row: not called CC=4 SCAR=30.0
348  
349 # This is really grotesque
350 0 my $row = shift;
351 0 my $dbh = shift;
352 0 my $sth = shift;
353 0 my $non_literal_row_keys = shift;
354 0 my $ref_row_keys = shift;
355 0 my $bind_attributes = shift;
356 0 my $auto_inc_columns = shift;
357 0 my $table_name = shift;
358 0 my $wantarray = shift;
359  
360 $class->_sth_execute(
361 $sth,
362 [
363 0 map { $class->_deflated_value( $_, $row->{$_} ) }
364 0 @{$non_literal_row_keys}
365 ],
366 $bind_attributes,
367 );
368  
369 0 ▶ return unless defined $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wantarray 0 0 0
370  
371 0 my %auto_inc;
372 0 for my $col ( @{$auto_inc_columns} ) {
373 0 $auto_inc{$col}
374 = $dbh->last_insert_id( undef, undef, $table_name, $col );
375 }
376  
377 0 delete @{$row}{ @{$ref_row_keys} }
378 0 ▶ if @{$ref_row_keys};
Branch: 1 branch
BranchTrueFalseTotal
if @{$ref_row_keys;} 0 0 0
379  
380 0 return $class->new( %{$row}, %auto_inc, _from_query => 1 );
381 }
382  
383 sub _sth_execute {
384 0 ▶ my $self = shift;
Subroutine _sth_execute: not called CC=3 SCAR=24.8
385 0 my $sth = shift;
386 0 my $vals = shift;
387 0 my $attr = shift;
388  
389 0 ▶ if ( @{$attr} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$attr;}) { } 0 0 0
390 0 ▶ for ( my $i = 0; $i < @{$vals}; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$vals;}) 0 0 0
391 0 $sth->bind_param( $i + 1, $vals->[$i], $attr->[$i] );
392 }
393  
394 0 return $sth->execute();
395 }
396 else {
397 0 return $sth->execute( @{$vals} );
398 }
399 }
400  
401 sub _deflated_value {
402 0 ▶ my $self = shift;
Subroutine _deflated_value: not called CC=3 SCAR=24.8
403 0 my $name = shift;
404 0 ▶ my $val = @_ ? shift : $self->$name();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
405  
406 0 my $meth = $self->meta()->deflator_for($name);
407  
408 0 ▶ return $meth ? $self->$meth($val) : $val;
Branch: 1 branch
BranchTrueFalseTotal
$meth ? : 0 0 0
409 }
410  
411 sub _insert_for_data {
412 0 ▶ my $class = shift;
Subroutine _insert_for_data: not called CC=6 SCAR=37.4
413 0 my $data = shift;
414  
415 0 my $insert = $class->SchemaClass()->SQLFactoryClass()->new_insert();
416  
417 0 my $table = $class->Table();
418  
419 0 $insert->into( $table->columns( sort keys %{$data} ) );
420  
421 0 my $ph = Fey::Placeholder->new();
422  
423 my @vals = (
424 map {
425 $_ => (
426 defined $data->{$_}
427 && blessed $data->{$_}
428 && $data->{$_}->can('does')
429 && ( $data->{$_}->does('Fey::Role::IsLiteral')
430 || $data->{$_}->does('Fey::Role::SQL::ReturnsData') )
431 0 ▶ ? $data->{$_}
Branch: 1 branch
BranchTrueFalseTotal
defined $data->{$_} && builtin::blessed($data->{$_}) && $data->{$_}->can('does') && ($data->{$_}->does('Fey::Role::IsLiteral') || $data->{$_}->does('Fey::Role::SQL::ReturnsData')) ? : 0 0 0
Condition0%0/12
defined $data->{$_} and builtin::blessed($data->{$_}) and $data->{$_}->can("does") and $data->{$_}->does("Fey::Role::IsLiteral") || $data->{$_}->does("Fey::Role::SQL::ReturnsData")
!ll&&!rl&&r
000
defined $data->{$_} and builtin::blessed($data->{$_}) and $data->{$_}->can("does")
!ll&&!rl&&r
000
defined $data->{$_} and builtin::blessed($data->{$_})
!ll&&!rl&&r
000
$data->{$_}->does("Fey::Role::IsLiteral") or $data->{$_}->does("Fey::Role::SQL::ReturnsData")
l!l&&r!l&&!r
000
MC/DC0%0/5
defined $data->{$_} and builtin::blessed($data->{$_}) and $data->{$_}->can("does") and $data->{$_}->does("Fey::Role::IsLiteral") || $data->{$_}->does("Fey::Role::SQL::ReturnsData")
defined $data->{$_} builtin::blessed($data->{$_}) $data->{$_}->can("does") $data->{$_}->does("Fey::Role::IsLiteral") $data->{$_}->does("Fey::Role::SQL::ReturnsData")
Truth table0%0/6
A and B and C and D or E
A= defined $data->{$_}B= builtin::blessed($data->{$_})C= $data->{$_}->can("does")D= $data->{$_}->does("Fey::Role::IsLiteral")E= $data->{$_}->does("Fey::Role::SQL::ReturnsData")
ABCDEresult
0XXXX0
10XXX0
110XX0
111000
1111X1
111011
432 : $ph
433 )
434 }
435 0 sort keys %{$data}
436 );
437  
438 0 $insert->values(@vals);
439  
440 0 return $insert;
441 }
442  
443 sub update {
444 0 ▶ my $self = shift;
Subroutine update: not called CC=4 SCAR=30.0
Pod documented
445 0 my %p = @_;
446  
447 0 my $update = $self->SchemaClass()->SQLFactoryClass()->new_update();
448  
449 0 my $table = $self->Table();
450  
451 0 $update->update($table);
452  
453 $update->set(
454 0 map { $table->column($_) => $self->_deflated_value( $_, $p{$_} ) }
455 sort keys %p );
456  
457 0 for my $col ( @{ $table->primary_key() } ) {
458 0 my $name = $col->name();
459  
460 0 $update->where( $col, '=', $self->_deflated_value($name) );
461 }
462  
463 0 my $dbh = $self->_dbh($update);
464  
465 0 my $sth = $dbh->prepare( $self->_sql_string( $update, $dbh ) );
466  
467 my @attr = $self->_bind_attributes_for(
468 $dbh,
469 (
470 sort keys %p,
471 0 map { $_->name() } @{ $table->primary_key() }
472 ),
473 );
474  
475 0 $self->_sth_execute( $sth, [ $update->bind_params() ], \@attr );
476  
477 0 for my $k ( sort keys %p ) {
478 0 ▶ if ( ref $p{$k} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p{$k}) { } 0 0 0
479 0 my $clear = q{_clear_} . $k;
480 0 $self->$clear();
481 }
482 else {
483 0 my $set = q{_set_} . $k;
484 0 $self->$set( $p{$k} );
485 }
486 }
487  
488 0 return;
489 }
490  
491 sub delete {
492 0 ▶ my $self = shift;
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
493  
494 0 my $delete = $self->SchemaClass()->SQLFactoryClass()->new_delete();
495  
496 0 my $table = $self->Table();
497  
498 0 $delete->from($table);
499  
500 0 for my $col ( @{ $table->primary_key() } ) {
501 0 my $name = $col->name();
502  
503 0 $delete->where( $col, '=', $self->_deflated_value($name) );
504 }
505  
506 0 my $dbh = $self->_dbh($delete);
507  
508 0 $dbh->do(
509 $self->_sql_string( $delete, $dbh ),
510 {},
511 $delete->bind_params()
512 );
513  
514 0 return;
515 }
516  
517 sub _dbh {
518 0 ▶ my $self = shift;
Subroutine _dbh: not called CC=2 SCAR=17.9
519 0 my $sql = shift;
520  
521 0 my $source = $self->SchemaClass()->DBIManager()->source_for_sql($sql);
522  
523 0 ▶ die "Could not get a source for this sql ($sql)"
Branch: 1 branch
BranchTrueFalseTotal
unless $source 0 0 0
524 unless $source;
525  
526 0 return $source->dbh();
527 }
528  
529 sub pk_values_hash {
530 0 ▶ my $self = shift;
Subroutine pk_values_hash: not called CC=2 SCAR=17.9
Pod documented
531  
532 0 ▶ my @vals = $self->pk_values_list()
Branch: 1 branch
BranchTrueFalseTotal
my(@vals) = $self->pk_values_list or return 0 0 0
533 or return;
534  
535 0 my @cols = ( map { $_->name() } @{ $self->Table()->primary_key() } );
536  
537 0 return map { $cols[$_] => $vals[$_] } 0 .. $#vals;
538 }
539  
540 sub Count {
541 0 ▶ my $class = shift;
Subroutine Count: not called CC=1 SCAR=6.9
Pod documented
542  
543 0 my $select = $class->meta()->_count_sql();
544  
545 0 my $dbh = $class->_dbh($select);
546  
547 0 my $row = $dbh->selectcol_arrayref( $class->_sql_string( $select, $dbh ) );
548  
549 0 return $row->[0];
550 }
551  
552 sub Table {
553 7 ▶ my $class = shift;
Subroutine Table: called CC=1 SCAR=0.0
Pod documented
554  
555 7 return $class->meta()->table();
556 }
557  
558 sub SchemaClass {
559 0 ▶ my $class = shift;
Subroutine SchemaClass: not called CC=1 SCAR=6.9
Pod documented
560  
561 0 return $class->meta()->schema_class();
562 }
563  
564 sub _sql_string {
565 0 ▶ my $self = shift;
Subroutine _sql_string: not called CC=2 SCAR=17.9
566 0 my $sql = shift;
567 0 my $dbh = shift;
568  
569 0 my $cache = $self->meta()->_sql_string_cache();
570  
571 0 ▶ return $cache->{ object_id($sql) . object_id($dbh) } ||= $sql->sql($dbh);
Condition0%0/3
$$cache{object_id($sql) . object_id($dbh)} ||= $sql->sql($dbh)
l!l&&r!l&&!r
000
MC/DC0%0/2
$$cache{object_id($sql) . object_id($dbh)} ||= $sql->sql($dbh)
$$cache{object_id($sql) . object_id($dbh)} $sql->sql($dbh)
Truth table0%0/3
A ||= B
A= $$cache{object_id($sql) . object_id($dbh)}B= $sql->sql($dbh)
ABresult
1X1
011
000
572 }
573  
574 9 __PACKAGE__->meta()->make_immutable( inline_constructor => 0 );
575  
576 9 1;
577  
578 # ABSTRACT: Base class for table-based objects
579  
580 __END__
581  
582 =pod
583  
584 =head1 NAME
585  
586 Fey::Object::Table - Base class for table-based objects
587  
588 =head1 VERSION
589  
590 version 0.46
591  
592 =head1 SYNOPSIS
593  
594 package MyApp::User;
595  
596 use Fey::ORM::Table;
597  
598 has_table(...);
599  
600 =head1 DESCRIPTION
601  
602 This class is a the base class for all table-based objects. It
603 implements a large amount of the core L<Fey::ORM> functionality,
604 including CRUD (create, update, delete) and loading of data from the
605 DBMS.
606  
607 =head1 METHODS
608  
609 This class provides the following methods:
610  
611 =head2 $class->new(...)
612  
613 This method overrides the default C<Moose::Object> constructor in
614 order to implement cache management.
615  
616 By default, object caching is disabled. In that case, this method lets
617 its parent class do most of the work. However, unlike the standard
618 Moose constructor, this method may sometimes not return an object. If
619 it attempts to load object data from the DBMS and cannot find anything
620 matching the parameters given to the constructor, it will return
621 false.
622  
623 If the constructor fails, you can check the value of C<<
624 $class->ConstructorError >> for the error message. This is done so that
625 calling the constructor does not overwrite any value already in C<$@>.
626  
627 If caching is enabled, then this method will attempt to find a
628 matching object in the cache. A match is determined by looking for an
629 object which has a candidate key with the same values as are passed to
630 the constructor.
631  
632 If no match is found, it attempts to create a new object. If this
633 succeeds, it stores it in the cache before returning it.
634  
635 =head3 Constructor Parameters
636  
637 The constructor accepts any attribute of the class as a
638 parameter. This includes any column-based attributes, as well as any
639 additional attributes defined by C<has_one()> or C<has_many()>. Of
640 course, if you disabled caching for C<has_one()> or C<has_many()>
641 relationships, then they are implemented as simple methods, not
642 attributes.
643  
644 If you define additional methods via Moose's C<has()> function, and
645 these will be accepted by the constructor as well.
646  
647 Finally, the constructor accepts a parameter C<_from_query>. This
648 tells the constructor that the parameters passed to the constructor
649 are the result of a C<SELECT>. This stops the C<BUILD()> method from
650 attempting to load the object from the DBMS. However, you still must
651 pass values for the primary key, so that the object is identifiable in
652 the DBMS.
653  
654 =head2 $class->ConstructorError()
655  
656 If the constructor does not return an object, this will always contain the
657 error message from the constructor. This should always be something like
658 "Could not a find a row in SomeTable matching the values you provided to the
659 constructor" or "Could not find a row in SomeTable where table_id = 42".
660  
661 This error is cleared each time the class's constructor is called.
662  
663 =head2 $class->EnableObjectCache()
664  
665 =head2 $class->DisableObjectCache()
666  
667 These methods enable or disable the object cache for the calling
668 class.
669  
670 =head2 $class->Count()
671  
672 Returns the number of rows in the class's associated table.
673  
674 =head2 $class->ClearObjectCache()
675  
676 Clears the object cache for the calling class.
677  
678 =head2 $class->Table()
679  
680 Returns the L<Fey::Table> object passed to C<has_table()>.
681  
682 =head2 $class->SchemaClass()
683  
684 Returns the name of the class associated with the class's table's
685 schema.
686  
687 =head2 $class->insert(%values)
688  
689 Given a hash of column names and values, this method inserts a new row
690 for the class's table, and returns a new object for that row.
691  
692 The values for the columns can be plain scalars or object. Values will
693 be passed through the appropriate deflators. You can also pass
694 L<Fey::Literal> objects of any type.
695  
696 As an optimization, no object will be created in void context.
697  
698 =head2 $class->insert_many( \%values, \%values, ... )
699  
700 This method allows you to insert multiple rows efficiently. It expects
701 an array of hash references. Each hash reference should contain the
702 same set of column names as its keys. The advantage of using this
703 method is that under the hood it uses the same C<DBI> statement handle
704 repeatedly. If you were to call C<< $class->insert() >> repeatedly it
705 would have to recreate the same SQL and DBI statement handle
706 repeatedly.
707  
708 In scalar context, it returns the first object created. In list
709 context, it returns all the objects created.
710  
711 As an optimization, no objects will be created in void context.
712  
713 =head2 $object->update(%values)
714  
715 This method accepts a hash of column keys and values, just like C<<
716 $class->insert() >>. However, it instead updates the values for an
717 existing object's row. It will also make sure that the object's
718 attributes are updated properly. In some cases, it will just clear the
719 attribute, forcing it to be reloaded the next time it is
720 accessed. This is necessary when the update value was a
721 L<Fey::Literal>, since that could be a function that gets interpreted
722 by the DBMS, such as C<NOW()>.
723  
724 =head2 $object->delete()
725  
726 This method deletes the object's associated row from the DBMS.
727  
728 The object is still usable after this method is called, but if you
729 attempt to call any method that tries to access the DBMS it will
730 probably blow up.
731  
732 =head2 $object->pk_values_hash()
733  
734 Returns a hash representing the names and values for the object's
735 primary key. The values are returned in their raw form, regardless of
736 any transforms specific for a primary key column.
737  
738 This may return an empty hash if the primary key for the object has
739 not yet been determined. This can happen if you try to call this
740 method on an object before its attributes have been fetched from the
741 dbms.
742  
743 =head2 $object->pk_values_list()
744  
745 Returns a list of values for the object's primary key. The values are
746 returned in the same order as C<< $self->primary_key() >> returns the
747 columns. The values are returned in their raw form, regardless of any
748 transforms specific for a primary key column.
749  
750 This may return an empty list if the primary key for the object
751 has not yet been determined.
752  
753 =head1 METHODS FOR SUBCLASSES
754  
755 Since your table-based class will be a subclass of this object, there
756 are several methods you may want to use that are not intended for use
757 outside of your subclasses. You may also want to subclass some of
758 these methods in this class.
759  
760 =head2 $class->_dbh($sql)
761  
762 Given a L<Fey::SQL> object, this method returns an appropriate C<DBI>
763 object for that SQL. Internally, it calls C<source_for_sql()> on the
764 schema class's L<Fey::DBIManager> object and then calls C<<
765 $source->dbh() >> on the source.
766  
767 If there is no source for the given SQL, it will die.
768  
769 =head2 $object->_load_from_dbms($params)
770  
771 This method will be called as part of object construction (unless
772 C<_from_query> was true).
773  
774 By default, this method attempts to find a row in the associated table
775 by looking at each of the table's candidate keys in turn. If the
776 parameters passed to the constructor include values for all parts of a
777 key, it does a select to find a matching row.
778  
779 You can override this method in order to attempt to load an object
780 based on some other method. For example, if your user table stores a
781 username and a hashed password, you could accept an I<unhashed>
782 password, and then do a lookup based on the hashed value.
783  
784 This method is expected to create a C<SELECT> statement and then pass
785 the statement and bind parameters to C<< $object->_get_column_values()
786 >>.
787  
788 On success, this method should simply return. If it fails, it should throw a
789 Fey::Exception::NoSuchRow exception. See L<Fey::ORM::Exceptions> for details.
790  
791 =head2 $object->_get_column_values( $select, $bind_params )
792  
793 This method takes a C<SELECT> statement and an array reference of bind
794 parameters. The C<SELECT> is expected to find a single row, which
795 should correspond to the current object. If it finds a row, it sets
796 the corresponding attributes in the object based on the values returns
797 by the C<SELECT>.
798  
799 =head1 AUTHOR
800  
801 Dave Rolsky <autarch@urth.org>
802  
803 =head1 COPYRIGHT AND LICENSE
804  
805 This software is copyright (c) 2011 by Dave Rolsky.
806  
807 This is free software; you can redistribute it and/or modify it under
808 the same terms as the Perl 5 programming language system itself.
809  
810 =cut
↑ Summary Meta/Role/FromSelect.pm »