File Coverage

blib/lib/DBIx/Squirrel/rc.pm
Criterion Covered Total %
statement 35 81 43.2
branch 4 34 11.7
condition n/a
subroutine 14 20 70.0
pod 0 4 0.0
total 53 139 38.1


line stmt bran cond sub pod time code
1 9     9   65 use strict;
  9         24  
  9         403  
2 9     9   52 use warnings;
  9         19  
  9         539  
3 9     9   168 use 5.010_001;
  9         34  
4              
5             package # hide from PAUSE
6             DBIx::Squirrel::rc;
7              
8 9     9   60 use Sub::Name 'subname';
  9         73  
  9         707  
9 9     9   62 use DBIx::Squirrel::util 'confessf';
  9         18  
  9         555  
10 9     9   97 use namespace::clean;
  9         19  
  9         111  
11              
12 9         740 use constant E_BAD_OBJECT =>
13 9     9   2751 'A reference to either an array or hash was expected';
  9         23  
14 9     9   56 use constant E_STH_EXPIRED => 'Result is no longer associated with a statement';
  9         22  
  9         574  
15 9     9   53 use constant E_UNKNOWN_COLUMN => 'Unrecognised column (%s)';
  9         37  
  9         849  
16              
17             BEGIN {
18             require DBIx::Squirrel
19 9 50   9   67 unless keys %DBIx::Squirrel::;
20 9         1345 *DBIx::Squirrel::rc::VERSION = *DBIx::Squirrel::VERSION;
21             }
22              
23             sub new {
24 1651 50   1651 0 3375 my $class = ref $_[0] ? ref shift : shift;
25 1651 50       5892 return ref $_[0] ? bless shift, $class : shift;
26             }
27              
28             sub result_class {
29 0     0 0 0 return shift->results->result_class;
30             }
31              
32             BEGIN {
33 9     9   4309 *row_base_class = *result_class;
34             }
35              
36             sub row_class {
37 0     0 0 0 return shift->results->row_class;
38             }
39              
40             sub get_column {
41 0     0 0 0 my( $self, $name ) = @_;
42 0 0       0 return unless defined $name;
43 0 0       0 if ( UNIVERSAL::isa( $self, 'ARRAY' ) ) {
44 0 0       0 confessf E_STH_EXPIRED unless my $sth = $self->rs->sth;
45 0         0 my $n = $sth->{NAME_lc_hash}{ lc $name };
46 0 0       0 confessf E_UNKNOWN_COLUMN, $name unless defined $n;
47 0         0 return $self->[$n];
48             }
49             else {
50 0 0       0 confessf E_BAD_OBJECT unless UNIVERSAL::isa( $self, 'HASH' );
51 0 0       0 return $self->{$name} if exists $self->{$name};
52 0         0 my($n) = do {
53 0         0 local($_);
54 0         0 grep { lc eq $_[1] } keys %{$self};
  0         0  
  0         0  
55             };
56 0 0       0 confessf E_UNKNOWN_COLUMN, $name unless defined $n;
57 0         0 return $self->{$n};
58             }
59             }
60              
61             # AUTOLOAD is called whenever a row object attempts invoke an unknown
62             # method. We assume that the missing method is the name of a column, so
63             # we try to create an accessor asscoiated with that column. There is some
64             # initial overhead involved in the accessor's validation and creation.
65             #
66             # During accessor creation, AUTOLOAD will decide the best strategy for
67             # geting the column data depending on the underlying row implementation,
68             # which is determined by the slice type.
69              
70             our $AUTOLOAD;
71              
72             sub AUTOLOAD {
73 825 50   825   3850 return if substr( $AUTOLOAD, -7 ) eq 'DESTROY';
74 0           my($self) = @_;
75 0           ( my $name = $AUTOLOAD ) =~ s/.*:://;
76 0           my $symbol = $self->row_class . '::' . $name;
77 9     9   81 no strict 'refs'; ## no critic
  9         19  
  9         4200  
78 0           my $accessor = do {
79 0           push @{ $self->row_class . '::AUTOLOAD_ACCESSORS' }, $symbol;
  0            
80             # I'm not needlessly copying code from the `get_column` method, but
81             # doing the same checks once, before setting up the accessor, just
82             # to have the resulting accessor be as fast as it can be!
83 0 0         if ( UNIVERSAL::isa( $self, 'ARRAY' ) ) {
    0          
84 0 0         confessf E_STH_EXPIRED unless my $sth = $self->rs->sth;
85 0           my $n = $sth->{NAME_lc_hash}{ lc $name };
86 0 0         confessf E_UNKNOWN_COLUMN, $name unless defined $n;
87 0     0     sub { $_[0][$n] };
  0            
88             }
89             elsif ( UNIVERSAL::isa( $self, 'HASH' ) ) {
90 0 0         if ( exists $self->{$name} ) {
91 0     0     sub { $_[0]{$name} };
  0            
92             }
93             else {
94 0           my($n) = do {
95 0           local($_);
96 0           grep { lc eq $name } keys %{$self};
  0            
  0            
97             };
98 0 0         confessf E_UNKNOWN_COLUMN, $name unless defined $n;
99 0     0     sub { $_[0]{$n} };
  0            
100             }
101             }
102             else {
103 0           confessf E_BAD_OBJECT;
104             }
105             };
106 0           *{$symbol} = subname( $symbol => $accessor );
  0            
107 0           goto &{$symbol};
  0            
108             }
109              
110             1;