SPOPS/Iterator/DBI.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 180 CC 13 SCAR 45.0
CC
13
Cov
0%
fetch_object
156.0
finish
6.0
initialize
2.0
CRAP 90.4
1 package SPOPS::Iterator::DBI;
2  
3 # $Id: DBI.pm,v 3.4 2004/06/02 00:48:23 lachoy Exp $
4  
5 use strict;
6 use base qw( SPOPS::Iterator );
7 use Log::Log4perl qw( get_logger );
8  
9 use SPOPS;
10 use SPOPS::Iterator qw( ITER_IS_DONE ITER_FINISHED );
11 use SPOPS::Secure qw( :level );
12  
13 my $log = get_logger();
14  
15 $SPOPS::Iterator::DBI::VERSION = sprintf("%d.%02d", q$Revision: 3.4 $ =~ /(\d+)\.(\d+)/);
16  
17 # Keys with _DBI at the beginning are specific to this implementation;
18 # keys without _DBI at the begining are used in all iterators.
19  
20 sub initialize {
21 my ( $self, $p ) = @_;
22 $self->{_DBI_STH} = $p->{sth};
23  
24 $self->{_DBI_OFFSET} = $p->{offset};
25 $self->{_DBI_MAX} = $p->{max};
26  
27 $self->{_DBI_DB} = $p->{db};
28 $self->{_DBI_ID_LIST} = $p->{id_list};
29  
30 $self->{_DBI_COUNT} = 1;
31 $self->{_DBI_RAW_COUNT} = 0;
32 }
33  
34  
35 # TODO [fetch_object]: Put the pre_fetch_action in here for when we
36 # don't have _DBI_ID_LIST
37  
38 sub fetch_object {
39 my ( $self ) = @_;
40  
41 # First either grab the row with the ID if we've already got it or
42 # kick the DBI statement handle for the next row
43  
44 my ( $object );
45 my $object_class = $self->{_CLASS};
46 if ( $self->{_DBI_ID_LIST} ) {
47 my $id = $self->{_DBI_ID_LIST}->[ $self->{_DBI_RAW_COUNT} ];
48 unless ( defined $id ) {
49 $log->error( "Got undefined ID value from iterator ID list; ",
50 "returning undef as a result" );
51 return undef;
52 }
53 $log->is_info &&
54 $log->info( "Trying to retrieve idx ($self->{_DBI_RAW_COUNT}) with ",
55 "ID ($id) from class ($self->{_CLASS}" );
56 $object = eval { $object_class->fetch( $id,
57 { skip_security => $self->{_SKIP_SECURITY},
58 db => $self->{_DBI_DB} } ) };
59  
60 # If the object doesn't exist then it's likely a security issue,
61 # in which case we bump up our internal count (but not the
62 # position!) and try again.
63  
64 if ( $@ and $@->isa( 'SPOPS::Exception::Security' ) ) {
65 $log->is_info &&
66 $log->info( "Skip to next item, caught security exception: $@" );
67 $self->{_DBI_RAW_COUNT}++;
68 return $self->fetch_object;
69 }
70 elsif ( $@ ) {
71 $log->is_info &&
72 $log->info( "Caught other type of exception: $@" );
73 }
74  
75 unless( $object ) {
76 $log->is_info &&
77 $log->info( "Iterator is depleted (no object fetched), notify parent" );
78 return ITER_IS_DONE;
79 }
80 }
81 else {
82 my $row = $self->{_DBI_STH}->fetchrow_arrayref;
83 unless ( $row ) {
84 $log->is_info &&
85 $log->info( "Iterator is depleted (no row returned), notify parent" );
86 return ITER_IS_DONE;
87 }
88  
89 # It's ok to create the object now
90  
91 $object = $object_class->new({ skip_default_values => 1 });
92 $object->_fetch_assign_row( $self->{_FIELDS}, $row );
93  
94 # Check security on the row unless overridden. If the security
95 # check fails that's ok, just skip the row and move on -- DO
96 # increase our internal index but DON'T increase the position.
97  
98 $object->{tmp_security_level} = SEC_LEVEL_WRITE;
99 unless ( $self->{_SKIP_SECURITY} ) {
100 $object->{tmp_security_level} =
101 eval { $object->check_action_security({
102 required => SEC_LEVEL_READ }) };
103 if ( $@ ) {
104 $log->is_info &&
105 $log->info( "Security check for ($self->{_CLASS}) failed: $@" );
106 $self->{_DBI_RAW_COUNT}++;
107 return $self->fetch_object;
108 }
109 }
110  
111 # Now call the post_fetch callback; if it fails, fetch another row
112  
113 my $post_ok = $object->_fetch_post_process(
114 {}, $object->{tmp_security_level} );
115 unless ( $post_ok ) {
116 $log->is_info &&
117 $log->info( "Post process for object failed; get next" );
118 $self->{_DBI_RAW_COUNT}++;
119 return $self->fetch_object;
120 }
121 }
122  
123 # Not to the offset yet, so call ourselves again to go to the next
124 # row but first bump up the count.
125  
126 # Note: this *does* need to be beneath the fetch and security stuff
127 # above b/c the count refers to the records this user *could*
128 # see. So if 25 records matched the criteria but the user couldn't
129 # see 5 of them, we only want to the user to know and iterate
130 # through 1-20, not know that we actually picked the records
131 # 1-10,12,14,16-22,24-25 and discarded the others due to security.
132  
133 if ( $self->{_DBI_OFFSET} and
134 ( $self->{_DBI_COUNT} < $self->{_DBI_OFFSET} ) ) {
135 $log->is_info &&
136 $log->info( "Not reached the offset yet, get next object" );
137 $self->{_DBI_COUNT}++;
138 $self->{_DBI_RAW_COUNT}++;
139 return $self->fetch_object;
140 }
141  
142 # Oops, we've gone past the max. Finish up.
143  
144 if ( $self->{_DBI_MAX} and
145 ( $self->{_DBI_COUNT} > $self->{_DBI_MAX} ) ) {
146 $log->is_info &&
147 $log->info( "Fetched past the MAX number of objects, done" );
148 return ITER_IS_DONE;
149 }
150  
151 # Ok, we've navigated everything we need to, which means we can
152 # actually return this record. So bump up both counts. Note that
153 # we also use this count for the ID list.
154  
155 $self->{_DBI_COUNT}++;
156 $self->{_DBI_RAW_COUNT}++;
157  
158 return ( $object, $self->{_DBI_COUNT} );
159 }
160  
161  
162 # Might need to wrap this in an eval
163  
164 sub finish {
165 my ( $self ) = @_;
166 $self->{_DBI_STH}->finish() if ( $self->{_DBI_STH} );
167 return $self->{ ITER_FINISHED() } = 1;
168 }
169  
170 1;
171  
172 __END__