DB/Adaptor/dbi/mysql.pm untested

filter: Statementstmt 0.0% 0 / 264 Branchbran 0.0% 0 / 100 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 430 CC 51 SCAR 43.4
CC
51
Cov
0%
genre_queries
156.0
song_queries
132.0
artist_queries
110.0
CRAP 76.7
1 package Audio::DB::Adaptor::dbi::mysql;
2  
3 # VERSION: $Id: mysql.pm,v 1.2 2005/02/27 16:56:25 todd Exp $
4  
5 use strict;
6 use DBI;
7  
8 use Audio::DB::Util::Rearrange;
9 use Audio::DB::DataTypes::Song;
10  
11 use vars qw($VERSION);
12  
13 $VERSION = '1.11';
14  
15 use constant MYSQL => 'mysql';
16  
17 my %queries = (
18 # Get a count of the distinct albums so
19 # information can easily be presented on
20 # a preliminary page without additional queries
21  
22 # THESE PAN QUERIES COULD BE USEFUL LATER ON....
23 # Find all uniqe albums that an artist contributes to
24 artist => qq{select *,COUNT(DISTINCT album)
25 from artists,album_artists,albums
26 where artist REGEXP ?
27 and artists.artist_id=album_artists.artist_id
28 and album_artists.album_id=albums.album_id
29 GROUP by artist ORDER BY artist},
30
31 # Group on album_id to keep from clobbering
32 # albums of the same name
33 album => qq{select * from albums,album_artists,artists
34 where album REGEXP ?
35 and albums.album_id=album_artists.album_id
36 and album_artists.artist_id=artists.artist_id
37 GROUP BY albums.album_id ORDER BY album},
38
39 song => qq{select * from songs,artists,albums
40 where song REGEXP ?
41 and songs.artist_id=artists.artist_id
42 and songs.album_id=albums.album_id
43 GROUP by title ORDER by title}
44 );
45  
46  
47 =head2 new
48  
49 Title : new
50 Usage : $db = Audio::DB->new(@args)
51 Function: create a new adaptor
52 Returns : an Audio::DB object
53 Args : see below
54 Status : Public
55
56 Argument Description
57 -------- -----------
58 -dsn the DBI data source, e.g. 'dbi:mysql:music_db' or "music_db"
59 Also accepts DB or database as synonyms.
60 -user username for authentication
61 -pass the password for authentication
62  
63 =cut
64  
65 sub new {
66 my ($class,@args) = @_;
67 my ($db,$host,$user,$auth,$create)
68 = rearrange([
69 [qw(DB DSN DATABASE)],
70 [qw(HOST)],
71 [qw(USERNAME USER)],
72 [qw(PASSWORD PASS)],
73 'CREATE'
74 ],@args);
75
76 my $host ||= 'localhost';
77 _create_database($user,$auth,$host,$db) if ($create);
78 my $music_db;
79  
80 if ($auth && $user) {
81 $music_db = DBI->connect("dbi:mysql:$db" . ';host=' . $host,$user,$auth)
82 } elsif ($user) {
83 $music_db = DBI->connect("dbi:mysql:$db" . ';host=' . $host,$user);
84 } else {
85 $music_db = DBI->connect("dbi:mysql:$db" . ';host=' . $host);
86 }
87  
88 $music_db or die "Couldn't connect to the database $db\n";
89 # fill in object, bless it, and send us on our way
90 return bless { dbh => $music_db },$class;
91 }
92  
93  
94 ################################ loading, initialization, schema ##################################
95  
96 =head2 _create_database
97  
98 Title : _create_database
99 Usage : _create_database(user,pass,host,db)
100 Function: create the database
101 Returns : a boolean indicating the success of the operation
102 Args : username, password, host and database
103 Status : protected
104  
105 Called internally by new to create the database
106 if it does not already exist.
107  
108 =cut
109  
110 # Create the schema from scratch.
111 # You will need create privileges for this.
112 sub _create_database {
113 my ($user,$auth,$host,$db) = @_;
114 my $success = 1;
115 my $command =<<END;
116 ${\MYSQL} -u $user -p$auth -h $host -e "create database $db"
117 END
118 ;
119 $success && system($command) == 0;
120 die "Couldn't create the database $db" if !$success;
121 }
122  
123  
124 =head2 do_initialize
125  
126 Title : do_initialize
127 Usage : $success = $db->do_initialize($drop_all)
128 Function: initialize the database
129 Returns : a boolean indicating the success of the operation
130 Args : a boolean indicating whether to delete existing data
131 Status : protected
132  
133 This method will load the schema into the database. If $drop_all is
134 true, then any existing data in the tables known to the schema will be
135 deleted.
136  
137 Internally, this method calls schema() to get the schema data.
138  
139 =cut
140  
141 sub do_initialize {
142 my $self = shift;
143 my $erase = shift;
144 $self->drop_all if $erase;
145  
146 my $dbh = $self->dbh;
147 my ($schema,$raw_schema) = $self->schema;
148 foreach (values %$schema) {
149 $dbh->do($_) || warn $dbh->errstr;
150 }
151 1;
152 }
153  
154  
155 =head2 drop_all
156  
157 Title : drop_all
158 Usage : $dbh->drop_all
159 Function: empty the database
160 Returns : void
161 Args : none
162 Status : protected
163  
164 This method drops the tables known to this module. Internally it
165 calls the abstract tables() method to get a list of all tables to
166 drop.
167  
168 =cut
169  
170 # Drop all the tables -- dangerous!
171 sub drop_all {
172 my $self = shift;
173 my $dbh = $self->dbh;
174 local $dbh->{PrintError} = 0;
175 foreach ($self->tables) {
176 $dbh->do("drop table $_");
177 }
178 }
179  
180  
181 # return list of table names.
182  
183 =head2 tables
184  
185 Title : tables
186 Usage : @tables = $db->tables
187 Function: return list of tables that belong to this module
188 Returns : list of tables
189 Args : none
190 Status : protected
191  
192 This method returns a list of all the tables in the database.
193  
194 =cut
195  
196 sub tables {
197 my ($schema,$raw_schema) = shift->schema;
198 return keys %$schema;
199 }
200  
201  
202 =head2 schema
203  
204 Title : schema
205 Usage : ($schema,$raw_schema) = $mp3->schema
206 Function: return the CREATE script for the schema and
207 the raw_schema as a hashref
208 for easily accessing columns in proper order.
209 Returns : a hash of CREATE statements; hash of tables and parameters
210 Args : none
211 Status : protected
212  
213 This method returns a list containing the various CREATE statements
214 needed to initialize the database tables. Each create statement
215 is built programatically so I can maintain all fields in a central
216 location . This raw schema is returned for building temporary tables
217 for loading.
218  
219 =cut
220  
221 sub schema {
222 my $tables = {};
223
224 push (@{$tables->{artists}},
225 { artist_id => 'int not null auto_increment' },
226 { artist => 'text' },
227 { 'primary key(artist_id)' => 1 },
228 { 'INDEX(artist(12))' => 1 }
229 );
230  
231 push (@{$tables->{albums}},
232 { album_id => 'int not null auto_increment' },
233 { album => 'text' },
234 { type => "ENUM('compilation','standard')" },
235 { total_tracks => 'int' },
236 { year => 'text' },
237 { 'primary key(album_id)' => 1 },
238 { 'INDEX(album(12))' => 1 },
239 { "INDEX(year(8))" => 1 }
240 );
241
242 push (@{$tables->{songs}},
243 { song_id => 'int not null auto_increment' },
244 { title => 'text' },
245 { artist_id => 'int' },
246 { album_id => 'int' },
247 { genre_id => 'int' },
248 { track => 'int' },
249 { duration => 'text' },
250 { seconds => 'int' },
251 { lyrics => 'text' },
252 { comment => 'text' },
253 { bitrate => 'int' },
254 { samplerate => 'real' },
255 { fileformat => 'text' },
256 { channels => 'text' },
257 { tagtypes => 'text' },
258 { filename => 'text' },
259 { filesize => 'real' },
260 { filepath => 'text' },
261 { year => 'text' },
262 { uber_rating => 'int' },
263 { uber_playcount => 'int' },
264 { 'primary key(song_id)' => 1 },
265 { 'INDEX(title(10))' => 1 },
266 { 'INDEX(year(8))' => 1 },
267 { 'INDEX(uber_playcount)' => 1 },
268 { 'INDEX(uber_rating)' => 1 },
269 { 'INDEX(album_id)' => 1 },
270 { 'INDEX(artist_id)' => 1 },
271 { 'INDEX(genre_id)' => 1 },
272 );
273
274 push (@{$tables->{album_artists}},
275 { artist_id => 'int not null' },
276 { album_id => 'int not null' },
277 { 'primary key(artist_id,album_id)' => 1 });
278
279 push (@{$tables->{genres}},
280 { genre_id => 'int not null auto_increment' },
281 { genre => 'text' },
282 { 'primary key(genre_id)' => 1 },
283 { 'INDEX(genre(10))' => 1 },
284 );
285  
286 push (@{$tables->{artist_genres}},
287 { artist_id => 'int not null' },
288 { genre_id => 'int not null' },
289 {'primary key(artist_id,genre_id)' => 1});
290  
291 push (@{$tables->{song_genres}},
292 { song_id => 'int not null' },
293 { genre_id => 'int not null' },
294 {'primary key(song_id,genre_id)' => 1 });
295
296 push (@{$tables->{song_types}},
297 { song_id => 'int not null' },
298 { type => "ENUM('live','cover','bootleg','single') not null" },
299 {'primary key(song_id)' => 1 });
300  
301 # The following tables are for maintaining users and playlists
302 push (@{$tables->{users}},
303 { user_id => 'int not null auto_increment' },
304 { first => 'text' },
305 { last => 'text' },
306 { email => 'text' },
307 { username => 'text' },
308 { password => 'text' },
309 { privs => 'text' },
310 { joined => 'timestamp' },
311 { last_access => 'date' },
312 { songs_played => 'int' },
313 {'primary key(user_id)' => 1 },
314 {'UNIQUE(username(8))' => 1 });
315  
316 push (@{$tables->{user_ratings}},
317 { user_id => 'int not null' },
318 { song_id => 'int not null' },
319 { rating => 'int' },
320 { playcount => 'int' },
321 {'primary key(user_id,song_id)' => 1 },
322 { 'INDEX(rating)' => 1 },
323 { 'INDEX(playcount)' => 1 },
324 );
325
326 push (@{$tables->{playlists}},
327 { playlist_id => 'int not null auto_increment' },
328 { playlist => 'text' },
329 { description => 'text' },
330 { user_id => 'int not null' }, # each playlist has to belong to someone
331 { is_shared => "ENUM('yes','no')" },
332 { created => 'date' },
333 { viewed => 'int' },
334 {'primary key(playlist_id)' => 1 },
335 { 'INDEX(playlist(10))' => 1 },
336 );
337  
338 push (@{$tables->{playlist_songs}},
339 { playlist_id => 'int not null' },
340 { song_id => 'int not null' },
341 {'primary key(playlist_id,song_id)' => 1 });
342  
343 my %schema;
344 foreach my $table (keys %$tables) {
345 my $create = "create table $table (";
346 my $count;
347 foreach my $param (@{$tables->{$table}}) {
348 $count++;
349 # Append a comma to the previous entry, but only if this
350 # isn't the first...
351 $create .= ',' if ($count > 1);
352
353 my ($key) = keys %$param;
354 my ($val) = values %$param;
355 if ($val == 1) {
356 $create .= $key;
357 } else {
358 $create .= $key . ' ' . $val;
359 # $create .= $key . ' ' . $val . ',';
360 }
361 }
362 $create .= ')';
363 $schema{$table} = $create;
364 }
365 return (\%schema,$tables);
366 }
367  
368  
369 ### NOT YET IMPLEMENTED
370 ###=head2 finish_load
371 #
372 # Title : finish_load
373 # Usage : $db->finish_load
374 # Function: called after load_gff_line()
375 # Returns : number of records loaded
376 # Args : none
377 # Status : protected
378  
379 #This method performs schema-specific cleanup after loading a set of
380 #MP3 records. It finishes each of the statement handlers prepared by
381 #setup_load().
382 #
383 #=cut
384 #
385 #### NOT USING
386 #sub finish_load {
387 # my $self = shift;
388 #
389 # my $dbh = $self->features_db or return;
390 # $dbh->do('UNLOCK TABLES') if $self->lock_on_load;
391 #
392 # foreach (keys %{$self->{load_stuff}{sth}}) {
393 # $self->{load_stuff}{sth}{$_}->finish;
394 # }
395 #
396 # my $counter = $self->{load_stuff}{counter};
397 # delete $self->{load_stuff};
398 # return $counter;
399 #}
400  
401  
402 =head2 dbh
403  
404 Title : dbh
405 Usage : $dbh->dbh
406 Function: get database handle
407 Returns : a DBI handle
408 Args : none
409 Status : Public
410  
411 =cut
412  
413  
414 sub dbh { shift->{dbh} }
415  
416  
417 =head2 DESTROY
418  
419 Title : DESTROY
420 Usage : $dbh->DESTROY
421 Function: disconnect database at destruct time
422 Returns : void
423 Args : none
424 Status : protected
425  
426 This is the destructor for the class.
427  
428 =cut
429  
430 sub DESTROY {
431 my $self = shift;
432 $self->dbh->disconnect if defined $self->dbh;
433 }
434  
435  
436 =head2 debug
437  
438 Title : debug
439 Usage : $dbh = $dbh->debug
440 Function: prints out debugging information
441 Returns : debugging information
442 Args : none
443 Status : Private
444  
445 =cut
446  
447 sub debug {
448 my $self = shift;
449 $self->dbh->debug(@_);
450 $self->SUPER::debug(@_);
451 }
452  
453  
454  
455  
456  
457  
458 ##################
459 # QUERIES
460 ##################
461 # Used to find out the id of the last value inserted
462 # for building databases...
463 sub lookup_counter {
464 my ($self,$field,$table) = @_;
465 my $dbh = $self->dbh;
466 my $sth = $dbh->prepare(qq{select $field from $table ORDER BY $field DESC LIMIT 1});
467 }
468  
469  
470 #### NEW SUB
471 # This might come in handy later
472 # SOME OF THIS COULD BE MIGRATED OUT
473 # Generically fetch an item for a single table by its ID
474 # MIGRATE TO QUERY
475 # DEPRECATING
476 sub fetch_by_id {
477 my ($self,$class,$id) = @_;
478 my $dbh = $self->dbh;
479 my $table = $class;
480 $table =~ s/s$//;
481 my $sth = $dbh->prepare(qq{select * from $class where $class.$table\_id=?});
482 $sth->execute($id) or warn $sth->errstr;
483 my $h = $sth->fetchrow_hashref;
484 return $h;
485 }
486 ## END NEW
487  
488  
489  
490 # RETAIN
491 # Generic queries that should be part of each adaptor
492 # queries are grouped by the predominant class of object they are intended to retrieve
493 # This approach is not as clever as that which used generic fetch_class type subroutines.
494 # On the other hand, it allows for more tailored queries that let me build up more informative data structures
495  
496 sub artist_queries {
497 my ($self,$query,$table) = @_;
498 my $dbh = $self->dbh;
499 # BY IDs
500 # Easiest case: Fetch an artist by its ID (returns a single result)
501 if ($query eq 'by_artist_id') {
502 return ($dbh->prepare(qq{select * from artists where artist_id=?}));
503 }
504  
505 # Fetch an artist by an album ID (may possibly return more than one result)
506 if ($query eq 'by_album_id') {
507 return ($dbh->prepare(qq{select * from album_artists,artists where album_artists.album_id=? and album_artists.artist_id=artists.artist_id}));
508 }
509  
510 # Fetch artist by a song ID (should return one and only one result)
511 if ($query eq 'by_song_id') {
512 return ($dbh->prepare(qq{select * from songs,artists where songs.song_id=? and songs.artist_id=artists.artist_id}));
513 }
514  
515 # Fetch all artists associated with a given genre (may return multiple results)
516 if ($query eq 'by_genre_id') {
517 return ($dbh->prepare(qq{select * from artist_genres,artists where artist_genres.genre_id=? and artist_genres.artist_id=artists.artist_id}));
518 }
519
520 # BY TEXT-BASED QUERIES
521 # Fetch an artist by name (possibly returns multiple entries)
522 if ($query eq 'by_artist') {
523 return ($dbh->prepare(qq{select * from artists where artist REGEXP ?}));
524 }
525  
526 # Fetch all artists associated with an album title (possibly returns multiple entries)
527 if ($query eq 'by_album') {
528 return ($dbh->prepare(qq{select *
529 from albums,album_artists,artists
530 where albums.album REGEXP ?
531 and albums.album_id=album_artists.album_id
532 and album_artists.artist_id=artists.artist_id}));
533 }
534  
535 # Fetch an artist by a song title (possibly returns multiple entries)
536 if ($query eq 'by_song') {
537 return ($dbh->prepare(qq{select * songs,artists
538 where songs.title REGEXP ?
539 and songs.artist_id=artists.artist_id}));
540 }
541  
542 # Fetch an artist by a genre (possibly returns multiple entries
543 if ($query eq 'by_genre') {
544 return ($dbh->prepare(qq{select * genres,artist_genres,artists
545 where genres.genre REGEXP ?
546 and genres.genre_id=artist_genres.genre_id
547 and artist_genres.artist_id=artists.artist_id}));
548 }
549  
550 # Find all artists with multiple genres assigned
551 if ($query eq 'artists_multiple_genres') {
552 return ($dbh->prepare(qq{select artist_genres.artist_id,count(artist_genres.artist_id) as total_genres,artist from artist_genres,artists where artist_genres.artist_id=artists.artist_id GROUP BY artists.artist_id HAVING total_genres > 1}));
553 }
554 }
555  
556 sub song_queries {
557 my ($self,$query,$table) = @_;
558 my $dbh = $self->dbh;
559  
560 # BY IDs
561 # Easiest case: Fetch a song by its ID (returns a single result)
562 if ($query eq 'by_song_id') {
563 return ($dbh->prepare(qq{select * from songs where song_id=?}));
564 }
565  
566 # Find all songs associated with a given album id
567 # Do a join to artists for convenience
568 if ($query eq 'by_album_id') {
569 return ($dbh->prepare(qq{select * from songs,artists where songs.album_id=? and songs.artist_id=artists.artist_id}));
570 }
571  
572 # Fetch all songs associated with a given artist ID (not typically used)
573 if ($query eq 'by_artist_id') {
574 return ($dbh->prepare(qq{select * from songs where artist_id=?}));
575 }
576  
577 # Fetch all songs associated with a given genre (may return multiple results)
578 # Not typically used as this data needs to be aggregated first
579 if ($query eq 'by_genre_id') {
580 return ($dbh->prepare(qq{select * from genres,artist_genres,songs where genres.genre_id=? and genres.genre_id=artist_genres.genre_id and artist_genres.artist_id=songs.artist_id}));
581 }
582
583 # BY TEXT-BASED QUERIES
584 # Fetch a song by name (possibly returns multiple entries)
585 if ($query eq 'by_song') {
586 return ($dbh->prepare(qq{select * from songs where title REGEXP ?}));
587 }
588 # Fetch all songs associated with an album title (possibly returns multiple entries)
589 # Not typically used
590 if ($query eq 'by_album') {
591 return ($dbh->prepare(qq{select *
592 from albums,songs
593 where albums.album REGEXP ?
594 and albums.album_id=songs.album_id}));
595 }
596  
597 # Fetch all songs associated with a given artist
598 # NOT TYPICALLY USED
599 if ($query eq 'by_artist') {
600 return ($dbh->prepare(qq{select * artists,songs
601 where artists.artist REGEXP ?
602 and artists.artist_id=songs.artist_id}));
603 }
604
605 # Fetch all songs associated with a given genre (possibly returns multiple entries
606 # Not typically used
607 if ($query eq 'by_genre') {
608 return ($dbh->prepare(qq{select * genres,artist_genres,songs
609 where genres.genre REGEXP ?
610 and genres.genre_id=artist_genres.genre_id
611 and artist_genres.artist_id=songs.artist_id}));
612 }
613  
614 # PAN QUERY TO SPEED UP REPORT
615 # Find all artists with multiple genres assigned
616 if ($query eq 'artists_multiple_genres') {
617 return ($dbh->prepare(qq{select artist_id,count(artist_id) as total_genres from artist_genres GROUP BY artist_id HAVING total_genres > 1}));
618 }
619  
620 # Used for Reports::distribution()
621 # Find all the songs released in a given year
622 if ($query eq 'songs_per_year') {
623 return ($dbh->prepare(qq{select count(*) from songs where year=?}));
624 }
625 }
626  
627 sub album_queries {
628 my ($self,$query,$table) = @_;
629 my $dbh = $self->dbh;
630 # BY IDs
631 # Easiest case: Fetch an album by its ID (returns a single result)
632 if ($query eq 'by_album_id') {
633 return ($dbh->prepare(qq{select * from albums where album_id=?}));
634 }
635  
636 # Fetch an album by an artist ID (may possibly return more than one result)
637 if ($query eq 'by_artist_id') {
638 return ($dbh->prepare(qq{select * from album_artists,albums where album_artists.artist_id=? and album_artists.album_id=albums.album_id}));
639 }
640  
641 # Fetch album by a song ID (should return one and only one result)
642 if ($query eq 'by_song_id') {
643 return ($dbh->prepare(qq{select * from songs,albums where songs.song_id=? and songs.album_id=albums.album_id}));
644 }
645  
646 # # Fetch all albums associated with a given genre (may return multiple results)
647 if ($query eq 'by_genre_id') {
648 return ($dbh->prepare(qq{select *,count(*) as total_albums from songs,albums where songs.genre_id=? and songs.album_id=albums.album_id GROUP BY albums.album}));
649 }
650
651 # BY TEXT-BASED QUERIES
652 # Fetch an album by name (possibly returns multiple entries)
653 if ($query eq 'by_album') {
654 return ($dbh->prepare(qq{select * from albums where album REGEXP ?}));
655 }
656  
657 # Fetch all albums associated with an artist (possibly returns multiple entries)
658 if ($query eq 'by_artist') {
659 return ($dbh->prepare(qq{select *
660 from artists,album_artists,albums
661 where artists.artist REGEXP ?
662 and artists.artist_id=album_artists.artist_id
663 and album_artists.album_id=albums.album_id}));
664 }
665  
666 # Fetch an artist by a song title (possibly returns multiple entries)
667 if ($query eq 'by_song') {
668 return ($dbh->prepare(qq{select * songs,albums
669 where songs.title REGEXP ?
670 and songs.album_id=albums.album_id}));
671 }
672  
673 # THIS IS NOT PARTICULARLY APPLICABLE
674 # # Fetch an artist by a genre (possibly returns multiple entries
675 # if ($query eq 'by_genre') {
676 # return ($dbh->prepare(qq{select * genres,artist_genres,artists
677 # where genres.genre REGEXP ?
678 # and genres.genre_id=artist_genres.genre_id
679 # and artist_genres.artist_id=artists.artist_id}));
680 # }
681  
682 # Find all albums that have *any* songs below a given bitrate threshold
683 if ($query eq 'albums_below_bitrate_threshold') {
684 return ($dbh->prepare(qq{select * from songs,albums where bitrate < ? and songs.album_id=albums.album_id GROUP BY albums.album_id}));
685 }
686  
687 # Find all the albums released in a given year
688 if ($query eq 'albums_per_year') {
689 return ($dbh->prepare(qq{select count(*) from albums where year=?}));
690 }
691 }
692  
693  
694 sub genre_queries {
695 my ($self,$query,$table) = @_;
696 my $dbh = $self->dbh;
697  
698 # BY IDs
699 # Easiest case: Fetch a genre by its ID (returns a single result)
700 if ($query eq 'by_genre_id') {
701 return ($dbh->prepare(qq{select * from genre where genre_id=?}));
702 }
703  
704 # Find all genres associated with a given album id
705 # Do a join to artists for convenience
706 if ($query eq 'by_album_id') {
707 return ($dbh->prepare(qq{select genre,genres.genre_id from
708 album_artists,artist_genres,genres
709 where album_artists.album_id=?
710 and album_artists.artist_id=artist_genres.artist_id
711 and artist_genres.genre_id=genres.genre_id
712 GROUP BY genre}));
713 }
714  
715 # Fetch all genres associated with a given artist ID
716 if ($query eq 'by_artist_id') {
717 return ($dbh->prepare(qq{select genre,genres.genre_id
718 from genres,artist_genres
719 where artist_genres.artist_id=? and
720 artist_genres.genre_id=genres.genre_id}));
721  
722  
723 return ($dbh->prepare(qq{select * from songs where artist_id=?}));
724 }
725  
726 # Fetch the genre for a given song
727 if ($query eq 'by_song_id') {
728 return ($dbh->prepare(qq{select * from songs where genre_id=?}));
729 }
730
731 # BY TEXT-BASED QUERIES
732 # Fetch a genre by name (possibly returns multiple entries)
733 if ($query eq 'by_genre') {
734 return ($dbh->prepare(qq{select * from genres where genre REGEXP ?}));
735 }
736 # Fetch all genres associated with an album title (possibly returns multiple entries)
737 # Not typically used
738 if ($query eq 'by_album') {
739 return ($dbh->prepare(qq{select *
740 from genres,songs,albums
741 where albums.album REGEXP ?
742 and albums.album_id=songs.album_id and songs.genre_id=genres.genre_id GROUP BY genres.genre}));
743 }
744  
745 # Fetch all genres associated with a given artist
746 if ($query eq 'by_artist') {
747 return ($dbh->prepare(qq{select * from artists,artist_genres
748 where artists.artist REGEXP ?
749 and artists.artist_id=artist_genres.artist_id
750 and artist_genres.genre_id=genres.genre_id}));
751 }
752
753 # Fetch the genre for a specific song
754 if ($query eq 'by_song') {
755 return ($dbh->prepare(qq{select * songs,genres
756 where songs.title REGEXP ?
757 and songs.genre_id=genres.genre_id}));
758 }
759
760 # PAN QUERY TO SPEED UP REPORT
761 # Find all artists with multiple genres assigned
762 if ($query eq 'artists_multiple_genres') {
763 return ($dbh->prepare(qq{select artist_id,count(artist_id) as total_genres from artist_genres GROUP BY artist_id HAVING total_genres > 1}));
764 }
765
766 if ($query eq 'all_genres') {
767 return ($dbh->prepare(qq{select * from genres}));
768 }
769  
770 # Used for Reports::distribution()
771 # Find all the songs released in a given year
772 if ($query eq 'songs_per_year') {
773 return ($dbh->prepare(qq{select count(*) from songs where year=?}));
774 }
775 }
776  
777  
778 sub generic_queries {
779 my ($self,$query,$table) = @_;
780 my $dbh = $self->dbh;
781 # USED BY Query::fetch_class
782 return ($dbh->prepare(qq{select * from $table})) if ($query eq 'fetch_class');
783  
784 # Return a count of all items within a provided table
785 if ($query eq 'simple_count') {
786 return ($dbh->prepare(qq{select count(*) from $table}));
787 }
788  
789 }
790  
791  
792  
793  
794 # Fetch the sum of seconds or total file size
795 # UPDATED
796 sub query_for_total {
797 my ($self,$field) = @_;
798 my $dbh = $self->dbh;
799 my $sth = $dbh->prepare(qq{select sum($field) from songs});
800 $sth->execute();
801 my ($sum) = $sth->fetchrow_array;
802 return $sum;
803 }
804  
805  
806 # DEPRECATING
807 sub fetch_by_letter {
808 my ($self,$letter,$class) = @_;
809 my $dbh = $self->dbh;
810 my $sth = $dbh->prepare($queries{$class});
811 $sth->execute($letter);
812 return $sth;
813 }
814  
815 # DEPRECATING
816 sub generic_search {
817 my ($self,@p) = @_;
818 my ($class,$query,$container) = rearrange([qw/CLASS QUERY CONTAINER/],@p);
819
820 my $dbh = $self->dbh;
821 my $sth = $dbh->prepare($queries{$class});
822 $sth->execute($query);
823
824 my $temp = 'Audio::DB::DataTypes::' . ucfirst $class;
825 while (my $h = $sth->fetchrow_hashref) {
826 my $obj = $temp->new(-summary=>$h);
827
828 # pluralize whatever is returned. Stupid complication
829 # Why do I do this?
830 push (@{$container->{$class . 's'}},$obj);
831 }
832
833 # What if only one item was returned?
834 # Should I directly shunt into the single item object method?
835 # I should probably be able to get this information from rows or something...
836
837 if (scalar (@{$container->{$class . 's'}} == 1)) {
838 # Fetch out the single object returned so that I can pass it in...
839 my $original = $container->{$class . 's'}[0];
840
841 # Quick fetch of the class and ID that I am trying to fetch...
842 my $coderef = $class . '_id';
843 my $obj = $temp->new(-adaptor => $self->adaptor,
844 -id => $original->$coderef);
845 return $obj;
846 }
847 return $container;
848 }
849  
850 =pod
851  
852 =head1 NAME
853  
854 Audio::DB::Adaptor::dbi::mysql -- Database adaptor for a specific mysql schema
855  
856 =head1 SYNOPSIS
857  
858 See L<Audio::DB>
859  
860 =head1 DESCRIPTION
861  
862 This adaptor implements a specific mysql database schema that is
863 compatible with Audio::DB. It inherits from Audio::DB. In addition
864 to implementing the abstract SQL-generating methods of
865 Audio::DB::Adaptor::dbi, this module also implements the data
866 loading functionality of Audio::DB.
867  
868 The schema uses several tables:
869  
870 B<artists>
871 This the artists data table. Its columns are:
872  
873 artist_id artist ID (integer); primary key
874 artist artist name (string); may be null; indexed
875  
876 B<albums>
877 This is the albums table. Its columns are:
878  
879 album_id album ID (integer); primary key
880 album album name (string); may be null; indexed
881 album_type one of compilation or standard; may be null
882 total_tracks total songs on album (integer)
883 year self explanatory, no? (integer)
884  
885 B<songs>
886 This is the primary songs table. Its columns are:
887  
888 song_id song ID (integer); primary key
889 title song title (string)
890 artist_id artist ID (integer); indexed
891 album_id album ID (integer)
892 genre_id genre ID (integer) # may be superceded...see note
893 track track number (integer)
894 duration formatted song length (string)
895 seconds length in seconds (integer)
896 lyrics song lyrics (long text)
897 comment ID3 tag comment (text)
898 bitrate encoded bitrate (integer)
899 samplerate sample rate (real)
900 format format of the file (ie MPEG) (string)
901 channels channels (string)
902 tag_types type of ID3 tags present (ie ID3v2.3.0) (text)
903 filename file name (text)
904 filesize file size in bytes (real)
905 filepath absolute path (text)
906 year the year tag for single tracks
907 (since singles or songs on compilations
908 each may be different) (integer)
909 uber_playcount total times the song has been played
910 uber_rating overall song rating (see "users" below)
911
912 Currently, ID3 tags support but a single genre.
913 The genre_id is now stored with the song table. Multiple
914 genres may be assigned via the song_genres join table
915 below. The 'year' is a database denormalization that
916 allows the assignment of years to single tracks not
917 belonging to an album.
918  
919 B<genres>
920 This is the genres table. Its columns are:
921  
922 genre_id genre ID (integer); primary key
923 genre genre (string)
924  
925 B<album_artists>
926 This is the album_artists join table. Its columns are:
927  
928 artist_id artist ID. May not be null.
929 album_id album ID. May not be null.
930  
931 B<artist_genres>
932 This is the artists_genres join table. It enables
933 multiple genres to be assigned to a single artist.
934 Its columns are:
935  
936 artist_id artist ID. May not be null
937 genre_id genre ID. May not be null
938  
939 B<song_genres>
940 This is the song_genres join table. It enables
941 multiple genres to be assigned to a single song.
942 Its columns are:
943  
944 song_id artist ID. May not be null
945 genre_id genre ID. May not be null
946  
947 B<song_types>
948 This is the song_types join table. It enables
949 multiple general descriptive types to be
950 assigned to a single song. Its columns are:
951  
952 song_id artist ID. May not be null
953 type one of: live cover bootleg single
954  
955 =head2 Supplementary tables used by Web.pm
956  
957 Audio::DB::Web provides a web interface to databases
958 created with Audio::DB. It requires a few extra
959 tables that are not directly related to the MP3
960 tag data.
961  
962 B<users>
963 The users table provides support for multiple users
964 of the database. Its columns are:
965  
966 user_id user UD. May not be null; primary key
967 first users first name (text)
968 last last name (text_
969 email email address (text)
970 username username in the system (text)
971 password password (text)
972 privs privileges (text)
973 joined date user joined (date)
974 last_access date of last access (timestamp)
975 songs_played number of songs played (integer)
976  
977 B<user_ratings>
978 The user_ratings table allows users to maintain individual
979 ratings and playcounts for every song (as opposed to the
980 uber playcounts and ratings above). I'll probably pitch
981 the uber columns above, instead determining these values in
982 middleware.
983  
984 user_id may not be null
985 song_id may not be null
986 rating user rating from 1-100 (integer)
987 playcount user playcount (integer)
988  
989 B<playlists>
990 Playlist names and descriptions. Columns are:
991  
992 playlist_id may not be null; primary key
993 playlist the playlist name (text)
994 description brief description of the playlist (text)
995 user_id the owner of the playlist (integer)
996 is_shared yes/no. Controls the public-accessiblity of the playlist
997 created date playlist created. (date)
998 viewed number of times playlist viewed (integer)
999  
1000 B<playlist_songs>
1001 A small join table that associates songs with playlists:
1002  
1003 playlist_id may not be null
1004 song_id may not be null
1005  
1006  
1007 =head2 Available Methods
1008  
1009  
1010  
1011  
1012  
1013  
1014 =head1 BUGS
1015  
1016 This module implements a fairly complex internal data structure,
1017 which in itself rests upon lots of things going right, like reading ID3 tags,
1018 tag naming conventions, etc. On top of
1019 that, I wrote this in a Starbucks full of screaming children.
1020  
1021 =head1 TODO
1022  
1023 Lots of error checking needs to be added. Support for custom data schemas,
1024 including new data types like more extensive artist info, paths to images,
1025 lyrics, etc.
1026  
1027 Robusticize new for different adaptor types.
1028  
1029 =head1 AUTHOR
1030  
1031 Copyright 2002, Todd W. Harris <harris@cshl.org>.
1032  
1033 This module is distributed under the same terms as Perl itself. Feel
1034 free to use, modify and redistribute it as long as you retain the
1035 correct attribution.
1036  
1037 =head1 ACKNOWLEDGEMENTS
1038  
1039 Much of this module was derived from B<Bio::DB::GFF>, written by
1040 Lincoln Stein <lstein@cshl.org>.
1041  
1042 =head1 SEE ALSO
1043  
1044 L<Audio::DB>,L<Audio::DB::Web>, L<Apache::MP3>
1045  
1046 =cut
1047  
1048  
1049  
1050 1;
1051  
1052