File Coverage

blib/lib/DBIx/Squirrel/rs.pm
Criterion Covered Total %
statement 62 94 65.9
branch 4 20 20.0
condition n/a
subroutine 16 22 72.7
pod 0 3 0.0
total 82 139 58.9


line stmt bran cond sub pod time code
1 9     9   67 use strict;
  9         21  
  9         414  
2 9     9   108 use warnings;
  9         21  
  9         561  
3 9     9   158 use 5.010_001;
  9         34  
4              
5             package # hide from PAUSE
6             DBIx::Squirrel::rs;
7              
8             =head1 NAME
9              
10             DBIx::Squirrel::rs - Statement results iterator class
11              
12             =head1 SYNOPSIS
13              
14              
15             =head1 DESCRIPTION
16              
17             This module subclasses L to provides another type of
18             statement results iterator.
19              
20             While it may be used in exactly the same way as L,
21             it also abstracts away the implementation details of the underlying
22             statement results. Results are returned as objects of a class that is
23             dynamically created for each statement results iterator, and column
24             data is accessed via accessor methods that are also dynamically
25             created. Thus, the user need not be concerned with whether the
26             underlying statement results are arrayrefs or hashrefs, or even
27             what case is used for the column names.
28              
29             =cut
30              
31 9     9   315 use Scalar::Util 'weaken';
  9         161  
  9         756  
32 9     9   61 use Sub::Name 'subname';
  9         104  
  9         504  
33 9     9   200 use namespace::clean;
  9         74  
  9         195  
34              
35             BEGIN {
36             require DBIx::Squirrel
37 9 50   9   3108 unless keys %DBIx::Squirrel::;
38 9         58 *DBIx::Squirrel::rs::VERSION = *DBIx::Squirrel::VERSION;
39 9         1140 @DBIx::Squirrel::rs::ISA = 'DBIx::Squirrel::it';
40             }
41              
42             sub DESTROY {
43 0 0   0   0 return if DBIx::Squirrel::util::global_destruct_phase();
44 0         0 my $self = shift;
45 0         0 local( $., $@, $!, $^E, $?, $_ );
46 0         0 my $row_class = $self->row_class;
47 9     9   71 no strict 'refs'; ## no critic
  9         25  
  9         1652  
48 0 0       0 $self->_autoloaded_accessors_unload if %{ $row_class . '::' };
  0         0  
49 0         0 undef &{ $row_class . '::rs' };
  0         0  
50 0         0 undef &{ $row_class . '::rset' };
  0         0  
51 0         0 undef &{ $row_class . '::results' };
  0         0  
52 0         0 undef &{ $row_class . '::resultset' };
  0         0  
53 0         0 undef *{$row_class};
  0         0  
54 0         0 return $self->SUPER::DESTROY;
55             }
56              
57             sub _autoloaded_accessors_unload {
58 0     0   0 my $self = shift;
59 9     9   80 no strict 'refs'; ## no critic
  9         31  
  9         1696  
60 0         0 undef &{$_} for @{ $self->row_class . '::AUTOLOAD_ACCESSORS' };
  0         0  
  0         0  
61 0         0 return $self;
62             }
63              
64             sub _result_preprocess {
65 1651     1651   2682 my $self = shift;
66 1651 50       4356 return ref $_[0] ? $self->_rebless(shift) : shift;
67             }
68              
69             sub _rebless {
70 1651     1651   2538 my $self = shift;
71 1651         3361 my $row_class = $self->row_class;
72 1651         2913 my $results_fn = $row_class . '::results';
73 9     9   66 no strict 'refs'; ## no critic
  9         19  
  9         2726  
74 1651 100       2645 unless ( defined &{$results_fn} ) {
  1651         6201  
75 2         6 my $resultset_fn = $row_class . '::resultset';
76 2         3 my $rset_fn = $row_class . '::rset';
77 2         3 my $rs_fn = $row_class . '::rs';
78 2         4 undef &{$resultset_fn};
  2         16  
79 2         4 undef &{$rset_fn};
  2         8  
80 2         3 undef &{$rs_fn};
  2         7  
81 2         3 *{$resultset_fn} = *{$results_fn} = *{$rset_fn} = *{$rs_fn} = do {
  2         6  
  2         7  
  2         7  
  2         8  
82 2         5 weaken( my $results = $self );
83 2     0   20 subname( $results_fn => sub { $results } );
  0     0   0  
        0      
84             };
85 2         8 @{ $row_class . '::ISA' } = ( $self->result_class );
  2         32  
86             }
87 1651         4696 return $row_class->new(shift);
88             }
89              
90             sub result_class {
91 2     2 0 3 return 'DBIx::Squirrel::rc';
92             }
93              
94             BEGIN {
95 9     9   877 *row_base_class = *result_class;
96             }
97              
98             sub row_class {
99 1651     1651 0 2566 my $self = shift;
100 1651         5912 return sprintf( '%s::Ox%x', ref $self, 0+ $self );
101             }
102              
103             sub slice {
104 9     9   89 no strict 'refs'; ## no critic
  9         37  
  9         1881  
105 0     0 0   my( $attr, $self ) = shift->_private_state;
106 0           my $slice = shift;
107 0 0         my $old = defined $attr->{slice} ? $attr->{slice} : '';
108 0           $self->SUPER::slice($slice);
109 0 0         if ( my $new = defined $attr->{slice} ? $attr->{slice} : '' ) {
    0          
110 0 0         if ( ref $new ne ref $old ) {
111 0 0         $self->_autoloaded_accessors_unload if %{ $self->row_class . '::' };
  0            
112             }
113             }
114 0           return $self;
115             }
116              
117             =head1 AUTHORS
118              
119             Iain Campbell Ecpanic@cpan.orgE
120              
121             =head1 COPYRIGHT AND LICENSE
122              
123             The DBIx::Squirrel module is Copyright (c) 2020-2025 Iain Campbell.
124             All rights reserved.
125              
126             You may distribute under the terms of either the GNU General Public
127             License or the Artistic License, as specified in the Perl 5.10.0 README file.
128              
129             =head1 SUPPORT / WARRANTY
130              
131             DBIx::Squirrel is free Open Source software. IT COMES WITHOUT WARRANTY OF ANY
132             KIND.
133              
134             =cut
135              
136             1;