DB/Adaptor/dbi/sqlite.pm untested

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