DB/Query.pm untested

filter: Statementstmt 0.0% 0 / 96 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 122 CC 3 SCAR 10.6
CC
3
Cov
0%
fetch_artist_with_albums
6.0
fetch
6.0
fetch_class
2.0
CRAP 2.9
1 package Audio::DB::Query;
2  
3 # $Id: Query.pm,v 1.2 2005/02/27 16:56:25 todd Exp $
4  
5 use strict 'vars';
6 use vars qw(@ISA $VERSION);
7 #use Apache::Constants qw(:common REDIRECT HTTP_NO_CONTENT);
8 #use CGI qw/:standard *table *div *TR/;
9 #use CGI::Cookie;
10 use DBI;
11  
12 use Audio::DB::Factory;
13 use Audio::DB::Util::Rearrange;
14 use Audio::DB::Util::SystemConfig;
15 use Audio::DB::Util::Playlists;
16  
17 use Audio::DB::DataTypes::Artist;
18 use Audio::DB::DataTypes::Album;
19 use Audio::DB::DataTypes::Genre;
20 #use Audio::DB::DataTypes::ArtistList;
21 #use Audio::DB::DataTypes::AlbumList;
22 #use Audio::DB::DataTypes::SongList;
23  
24 @ISA = qw/Audio::DB::Util::DataAccess Audio::DB::Factory/;
25  
26 =pod
27  
28 =head1 NAME
29  
30 Audio::DB::Query - query methods of a Audio::DB database
31  
32 =head1 SYNOPSIS
33  
34 Audio::DB::Query objects are used internally by Audio::DB::Reports and
35 Audio::DB::Web. You will not normally interact with Audio::DB::Query
36 objects. The documentation contained here is listed for completeness
37 as well as for those looking to extend the functionality of Audio::DB.
38  
39 =head1 METHODS
40  
41 =over 4
42  
43 =cut
44  
45 =pod
46  
47 =item $report->fetch_class(@options);
48  
49 Provided with a table name, fetch_class returns an array reference of
50 appropriate objects. For example, passing the -class=>'artists' option
51 results in an array reference of Audio::DB::DataTypes::Artist objects.
52  
53 Options:
54  
55 Some classes accept additional parameters.
56  
57 Class: albums
58  
59 TODO: COMPLETE
60 By default, album objects will not be populated with songs. To retreive populated Album objects, try using ...
61  
62 If provided, Album objects will be populated with Song
63 objects. These can be accessed by calling the songs() method on
64 each album object.
65  
66 @albums = $music->fetch_class(-class=>'artist');
67 foreach my $album_obj (@albums) {
68 print $album_obj->album,"\n";
69 print join("\n",map {$_->title} $album_obj->songs);
70 }
71  
72 If not provided, summaries that require touching each song (ie to
73 see if it is a compilation album) will still be created, but the
74 individual songs will not be returned. This can be useful ina
75 situation requiring greater speed and less memory consumption.
76  
77 =cut
78  
79 sub fetch_class {
80 my ($self,@p) = @_;
81 my ($class) = rearrange([qw/CLASS/],@p);
82 my $adaptor = $self->adaptor;
83 my $sth = $adaptor->generic_queries('fetch_class',$class);
84 $sth->execute();
85 my @temp;
86 while (my $h = $sth->fetchrow_hashref) {
87 # Create objects of the appropriate type
88 my $obj = Audio::DB::Factory->new(-data =>$h,-class=>$class);
89 push (@temp,$obj);
90 }
91 return \@temp;
92 }
93  
94  
95  
96 ####################################
97 # Simple counts
98 # eg: no of artists, albums, songs
99 #####################################
100  
101 =pod
102  
103 =head1 count()
104  
105 Usage: count('type');
106  
107 Where type is one of artists, albums, songs, genres
108 Status: internal
109  
110 Generate a simple count of the number of $type entries in
111 the database
112  
113 =cut
114  
115 sub count {
116 my ($self,$table) = @_;
117 my $adaptor = $self->adaptor;
118 my $sth = $adaptor->generic_queries('simple_count',$table);
119 $sth->execute();
120 return ($sth->fetchrow_array)
121 }
122  
123  
124 sub dbh { shift->adaptor->{dbh}; }
125 sub adaptor { shift->{adaptor}; }
126  
127  
128 ####################################
129 # ARTIST QUERIES
130 ####################################
131 # Fetch all of the songs for all artists
132 # If the fill option is provided, songs and genres will also be populated
133 # THIS COULD PROBABLY BE OPTIMIZED WITH A SINGLE QUERY INSTEAD OF 3
134 # ON THE OTHER HAND, THIS GIVES ME SEPERATE OBJECTS FOR EACH
135  
136 # THIS IS A PAN METHOD THAT INVOKES fetch()
137  
138 sub fetch_artist_with_albums {
139 my ($self,@p) = @_;
140 my ($query,$perspective,$fill,@others) = rearrange([qw/QUERY PERSPECTIVE FILL/],@p);
141 my $adaptor = $self->adaptor;
142 my $artists = $self->fetch_class(-query=>$query,-perspective=>$perspective);
143  
144 my $sth = $adaptor->album_queries('by_artist_id');
145 foreach my $artist (@$artists) {
146 $sth->execute($artist->artist_id);
147 while (my $h = $sth->fetchrow_hashref) {
148 my $obj = Audio::DB::Factory->new(-data=>$h,-class=>'album');
149 $artist->add_album($obj);
150 }
151
152 # Filling the object. Group songs with albums
153 if ($fill) {
154 my $sth = $adaptor->song_queries('by_album_id');
155 foreach my $album ($artist->albums) {
156 $sth->execute($album->album_id);
157 while (my $h = $sth->fetchrow_hashref) {
158 my $obj = Audio::DB::Factory->new(-data=>$h,-class=>'song');
159 $album->add_song($obj);
160 }
161 }
162
163 # Now populate with genres
164 my $sth = $adaptor->genre_queres('by_artist_id');
165 $sth->execute($artist->artist_id);
166 while (my $h = $sth->fetchrow_hashref) {
167 my $obj = Audio::DB::Factory->new(-data=>$h,-class=>'genre');
168 $artist->add_genre($obj);
169 }
170 }
171 }
172 return $artists;
173 }
174  
175  
176  
177  
178  
179 ####################################
180 # ALBUM QUERIES
181 ####################################
182 # WRITE SOME PAN METHODS LIKE
183 # fetch_album_with_songs
184  
185  
186  
187  
188 ####################################
189 # SONG QUERIES
190 ####################################
191  
192  
193 ####################################
194 # GENRE QUERIES
195 ####################################
196  
197 # This is a generic method for fetching artists, albums, songs, or genres
198 # Options
199 # -class the primary class of the object to return
200 # one of artist, album, song, or genre
201  
202 # -perspective How to execute the fetch
203 # This can be one of by_album_id
204 # by_album
205 # by_artist_id
206 # by_artist
207 # -query an internal ID or text-based query to retrieve
208 #
209 # For example
210 # fetch(-class=>'album',-query=>'Rolling Stones',-perspective=>'artist');
211 # Will fetch all albums for the Rolling Stones
212  
213 # I SHOULD ALSO BE ABLE TO CALL THIS WITH AN OBJECT
214 # $artist->fetch(-class=>'album');
215 # (This would fetch and store all albums for the given artist
216 # That would be cool
217  
218 sub fetch {
219 my ($self,@p) = @_;
220 my ($class,$query,$perspective,@others) = rearrange([qw/CLASS QUERY PERSPECTIVE/],@p);
221 my $adaptor = $self->adaptor;
222  
223 my $coderef = $class . '_queries';
224 my $sth;
225  
226 # (One of by_artist, by_album, by_song, by_genre);
227 # (One of by_artist_id, by_album_id, by_song_id, by_genre_id);
228 $sth = $adaptor->$coderef($perspective);
229  
230 if ($query) {
231 $sth->execute($query);
232 } else {
233 $sth->execute();
234 }
235 my @retain;
236 while (my $h = $sth->fetchrow_hashref) {
237 my $obj = Audio::DB::Factory->new(-data=>$h,-class=>$class);
238 push @retain,$obj;
239 }
240  
241 # TO DO:
242  
243 # Return either a list object containing all items fetched
244 # (list item should have a class key so I can easily find out how to
245 # display it)
246  
247 # return the primary object if only one is fetched and user has
248 # requested scalar
249  
250 # If we recovered more than a single object,
251 # create a new container object. This lets me contextually
252 # display items
253 #if (@retain > 1) {
254 # my $list_obj = Audio::DB::Factory->new(-class=>$class . 'List');
255 # @{$list_obj->{(lc $class) . 's'}} = @retain;
256 # return $list_obj;
257 #}
258 #return (wantarray ? @retain : \@retain);
259 return \@retain;
260 }
261  
262 1;
263