MQdb/DBStream.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 122 CC 11 SCAR 22.4
CC
11
Cov
0%
next_in_stream
20.0
init
12.0
prepare
12.0
CRAP 9.4
1 # $Id: DBStream.pm,v 1.15 2009/06/03 06:47:32 severin Exp $
2 =head1 NAME
3  
4 MQdb::DBStream - DESCRIPTION of Object
5  
6 =head1 SYNOPSIS
7  
8 A simplified object to manage a collection of information related to streaming data from
9 a database. at least with MYSQL, the perl driver does odd caching so to stream one
10 needs to create a new database connection in order to stream
11  
12 =head1 DESCRIPTION
13  
14 =head1 CONTACT
15  
16 Jessica Severin <jessica.severin@gmail.com>
17  
18 =head1 LICENSE
19  
20 * Software License Agreement (BSD License)
21 * MappedQueryDB [MQdb] toolkit
22 * copyright (c) 2006-2009 Jessica Severin
23 * All rights reserved.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions are met:
26 * * Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * * Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * * Neither the name of Jessica Severin nor the
32 * names of its contributors may be used to endorse or promote products
33 * derived from this software without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
36 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
37 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS BE LIABLE FOR ANY
39 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
41 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
42 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  
46  
47 =head1 APPENDIX
48  
49 The rest of the documentation details each of the object methods. Internal methods are labeled.
50  
51 =cut
52  
53 my $__mqdb_dbstream_should_use_result = undef;
54  
55 $VERSION=0.954;
56  
57 package MQdb::DBStream;
58  
59 use strict;
60 use MQdb::DBObject;
61 our @ISA = qw(MQdb::DBObject);
62  
63  
64 #################################################
65 # Class methods
66 #################################################
67  
68 sub class { return "DBStream"; }
69  
70 =head2 set_stream_useresult_behaviour
71  
72 Description : sets a global behaviour for all DBStreams.
73 setting use_result to "on" will leave the results on the database and
74 will keep the database connection open durring streaming.
75 Both methods have similar speed performance, but keeping the results
76 on the database server means the client uses essentially no memory.
77 The risk of turning this on is that the the database connection remains open
78 and there is risk of it timing out if processing takes a long time to stream all data.
79 When turned off, the entire result set is transfered in bulk to the driver (DBD::mysql)
80 and streaming happens from the underlying driver code and the perl code layer.
81 Default is "off" since this is safer but one risks needing lots of memory on the client.
82 Parameter[1] : 1 or "y" or "on" turns the use_result on and keeps the database connection open
83 Returntype : none
84 Exceptions : none
85 Example : MQdb::DBStream->set_stream_useresult_behaviour(1);
86  
87 =cut
88  
89 sub set_stream_useresult_behaviour {
90 my $class = shift;
91 my $mode = shift;
92
93 if(defined($mode) and (($mode == 1) or ($mode eq "y") or ($mode eq "on"))) {
94 $__mqdb_dbstream_should_use_result = 1;
95 } else {
96 $__mqdb_dbstream_should_use_result = undef;
97 }
98 return $__mqdb_dbstream_should_use_result;
99 }
100  
101 #################################################
102 # user API stream access methods
103 #################################################
104  
105 =head2 next_in_stream
106  
107 Description: gets the next object in the stream. If the stream is empty it return undef.
108 Returntype : instance of the defined DBStream::stream_class, or undef
109 Exceptions : none
110 Example :
111 my $stream = WorldKit::Person->stream_all_by_country_region($db, "USA", "wisconsin");
112 while(my $person = $stream->next_in_stream) {
113 #do something
114 }
115  
116 =cut
117  
118 sub next_in_stream {
119 my $self = shift;
120
121 return undef unless(defined($self->sth));
122
123 my $class = $self->stream_class;
124 my $sth = $self->sth;
125  
126 if(my $row_hash = $sth->fetchrow_hashref) {
127  
128 my $obj = $class->new();
129 $obj = $obj->mapRow($row_hash); #required by subclass
130 $obj->database($self->database) if($obj);
131  
132 return $obj;
133 }
134 $sth->finish;
135 $self->{'_stream_sth'} = undef;
136 return undef;
137 }
138  
139 =head2 as_array
140  
141 Description: instantiates all remaining instances in the stream and returns them as an array
142 Returntype : reference to array of instances of the defined class of this stream
143 Exceptions : none
144 Example :
145 my $stream = WorldKit::Person->stream_all_by_country_region($db, "USA", "wisconsin");
146 my $all_people = $stream->as_array; #because I have a large memory machine and need them all in memory
147 foreach my $person (@$all_people) {
148 #do something
149 }
150  
151 =cut
152  
153 sub as_array {
154 my $self = shift;
155 my @array;
156 while(my $obj = $self->next_in_stream) {
157 push @array, $obj;
158 }
159 return \@array;
160 }
161  
162  
163 #################################################
164 # attribute methods
165 #################################################
166  
167 sub init {
168 my $self = shift;
169 my %args = @_;
170 $self->SUPER::init(@_);
171
172 if($args{'db'}) { $self->database($args{'db'}); }
173 if($args{'class'}) { $self->stream_class($args{'class'}); }
174 }
175  
176 =head2 stream_database
177  
178 Description: this is an internal system method.
179 Needs to have two database connections open, one for the active
180 stream handle, and one for lazy-loading additional data on the returned
181 objects. This is used to set the database which is the one streaming objects
182 Arg (1) : $database (MQdb::Database) for setting
183 Returntype : MQdb::Database
184 Exceptions : none
185 Callers : MQdb::MappedQuery
186  
187 =cut
188  
189  
190 sub stream_database {
191 my $self = shift;
192 if($self->database and !defined($self->{'_stream_database'})) {
193 $self->{'_stream_database'} = MQdb::Database->new_from_url($self->database->full_url);
194 }
195 return $self->{'_stream_database'};
196 }
197  
198 =head2 stream_class
199  
200 Description: this is an internal system method.
201 Set/get the class used for creation of objects on this stream.
202 The class must be a subclass of MQdb::MappedQuery
203 Arg (1) : $class (must be subclass of MQdb::MappedQuery) for setting
204 Returntype : a class which is a subclass of MQdb::MappedQuery
205 Exceptions : none
206 Callers : MQdb::MappedQuery
207  
208 =cut
209  
210 sub stream_class {
211 my $self = shift;
212 return $self->{'_stream_class'} = shift if(@_);
213 return $self->{'_stream_class'};
214 }
215  
216 sub sth {
217 my $self = shift;
218 return $self->{'_stream_sth'};
219 }
220  
221 #################################################
222 # stream prepare and access methods
223 #################################################
224  
225 =head2 prepare
226  
227 Description: this is an internal system method. It is used to set the SQL query used
228 to stream objects out of a database. Must be have stream_class() set
229 to a subclass of MappedQuery which implements the mapRow() method.
230 Returntype : $self
231 Exceptions : none
232 Callers : MQdb::MappedQuery
233  
234 =cut
235  
236 sub prepare {
237 my $self = shift;
238 my $sql = shift;
239 my @params = @_;
240  
241 throw("no database defined\n") unless($self->stream_database);
242 my $dbc = $self->stream_database->get_connection;
243 if($__mqdb_dbstream_should_use_result) {
244 #keeps sth open and streams results from server
245 $self->{'_stream_sth'} = $dbc->prepare($sql, { "mysql_use_result" => 1 });
246 } else {
247 #bulk transfers result to mysql driver and streams from driver cache
248 $self->{'_stream_sth'} = $dbc->prepare($sql);
249 }
250 $self->{'_stream_sth'}->execute(@params);
251 return $self;
252 }
253  
254  
255 1;
256  
257  
258  
259  
260