Rev | Line | |
---|
[1] | 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; |
---|
Note: See
TracBrowser
for help on using the repository browser.