| 1 | package MP3::Find::DB; |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | |
|---|
| 6 | use base qw(MP3::Find::Base); |
|---|
| 7 | |
|---|
| 8 | use DBI; |
|---|
| 9 | use SQL::Abstract; |
|---|
| 10 | |
|---|
| 11 | my $sql = SQL::Abstract->new; |
|---|
| 12 | |
|---|
| 13 | sub search { |
|---|
| 14 | my $self = shift; |
|---|
| 15 | my ($query, $dirs, $sort, $options) = @_; |
|---|
| 16 | |
|---|
| 17 | my $dbh = DBI->connect("dbi:SQLite:dbname=$$options{db_file}", '', ''); |
|---|
| 18 | |
|---|
| 19 | # use the 'LIKE' operator to ignore case |
|---|
| 20 | my $op = $$options{ignore_case} ? 'LIKE' : '='; |
|---|
| 21 | |
|---|
| 22 | # add the SQL '%' wildcard to match substrings |
|---|
| 23 | unless ($$options{exact_match}) { |
|---|
| 24 | for my $value (values %$query) { |
|---|
| 25 | $value = [ map { "%$_%" } @$value ]; |
|---|
| 26 | } |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | my ($where, @bind) = $sql->where( |
|---|
| 30 | { map { $_ => { $op => $query->{$_} } } keys %$query }, |
|---|
| 31 | ( @$sort ? [ map { uc } @$sort ] : () ), |
|---|
| 32 | ); |
|---|
| 33 | |
|---|
| 34 | my $select = "SELECT * FROM mp3 $where"; |
|---|
| 35 | |
|---|
| 36 | my $sth = $dbh->prepare($select); |
|---|
| 37 | $sth->execute(@bind); |
|---|
| 38 | |
|---|
| 39 | my @results; |
|---|
| 40 | while (my $row = $sth->fetchrow_hashref) { |
|---|
| 41 | push @results, $row; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | return @results; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | # module return |
|---|
| 48 | 1; |
|---|
| 49 | |
|---|
| 50 | =head1 NAME |
|---|
| 51 | |
|---|
| 52 | MP3::Find::DB - SQLite database backend to MP3::Find |
|---|
| 53 | |
|---|
| 54 | =head1 SYNOPSIS |
|---|
| 55 | |
|---|
| 56 | use MP3::Find::DB; |
|---|
| 57 | my $finder = MP3::Find::DB->new; |
|---|
| 58 | |
|---|
| 59 | my @mp3s = $finder->find_mp3s( |
|---|
| 60 | dir => '/home/peter/music', |
|---|
| 61 | query => { |
|---|
| 62 | artist => 'ilyaimy', |
|---|
| 63 | album => 'myxomatosis', |
|---|
| 64 | }, |
|---|
| 65 | ignore_case => 1, |
|---|
| 66 | ); |
|---|
| 67 | |
|---|
| 68 | =head1 REQUIRES |
|---|
| 69 | |
|---|
| 70 | L<DBI>, L<DBD::SQLite>, L<SQL::Abstract> |
|---|
| 71 | |
|---|
| 72 | =head1 DESCRIPTION |
|---|
| 73 | |
|---|
| 74 | This is the SQLite database backend for L<MP3::Find>. |
|---|
| 75 | |
|---|
| 76 | B<Note:> I'm still working out some kinks in here, so this backend |
|---|
| 77 | is currently not as stable as the Filesystem backend. |
|---|
| 78 | |
|---|
| 79 | =head2 Special Options |
|---|
| 80 | |
|---|
| 81 | =over |
|---|
| 82 | |
|---|
| 83 | =item C<db_file> |
|---|
| 84 | |
|---|
| 85 | The name of the SQLite database file to use. Defaults to F<~/mp3.db>. |
|---|
| 86 | |
|---|
| 87 | The database should have at least one table named C<mp3> with the |
|---|
| 88 | following schema: |
|---|
| 89 | |
|---|
| 90 | CREATE TABLE mp3 ( |
|---|
| 91 | mtime INTEGER, |
|---|
| 92 | FILENAME TEXT, |
|---|
| 93 | TITLE TEXT, |
|---|
| 94 | ARTIST TEXT, |
|---|
| 95 | ALBUM TEXT, |
|---|
| 96 | YEAR INTEGER, |
|---|
| 97 | COMMENT TEXT, |
|---|
| 98 | GENRE TEXT, |
|---|
| 99 | TRACKNUM INTEGER, |
|---|
| 100 | VERSION NUMERIC, |
|---|
| 101 | LAYER INTEGER, |
|---|
| 102 | STEREO TEXT, |
|---|
| 103 | VBR TEXT, |
|---|
| 104 | BITRATE INTEGER, |
|---|
| 105 | FREQUENCY INTEGER, |
|---|
| 106 | SIZE INTEGER, |
|---|
| 107 | OFFSET INTEGER, |
|---|
| 108 | SECS INTEGER, |
|---|
| 109 | MM INTEGER, |
|---|
| 110 | SS INTEGER, |
|---|
| 111 | MS INTEGER, |
|---|
| 112 | TIME TEXT, |
|---|
| 113 | COPYRIGHT TEXT, |
|---|
| 114 | PADDING INTEGER, |
|---|
| 115 | MODE INTEGER, |
|---|
| 116 | FRAMES INTEGER, |
|---|
| 117 | FRAME_LENGTH INTEGER, |
|---|
| 118 | VBR_SCALE INTEGER |
|---|
| 119 | ); |
|---|
| 120 | |
|---|
| 121 | =back |
|---|
| 122 | |
|---|
| 123 | =head1 TODO |
|---|
| 124 | |
|---|
| 125 | Move the database/table creation code from F<mp3db> into this |
|---|
| 126 | module. |
|---|
| 127 | |
|---|
| 128 | Database maintanence routines (e.g. clear out old entries) |
|---|
| 129 | |
|---|
| 130 | =head1 SEE ALSO |
|---|
| 131 | |
|---|
| 132 | L<MP3::Find>, L<MP3::Find::DB> |
|---|
| 133 | |
|---|
| 134 | =head1 AUTHOR |
|---|
| 135 | |
|---|
| 136 | Peter Eichman <peichman@cpan.org> |
|---|
| 137 | |
|---|
| 138 | =head1 COPYRIGHT AND LICENSE |
|---|
| 139 | |
|---|
| 140 | Copyright (c) 2006 by Peter Eichman. All rights reserved. |
|---|
| 141 | |
|---|
| 142 | This program is free software; you can redistribute it and/or |
|---|
| 143 | modify it under the same terms as Perl itself. |
|---|
| 144 | |
|---|
| 145 | =cut |
|---|