source: mp3-find/trunk/lib/MP3/Find/DB.pm @ 1

Last change on this file since 1 was 1, checked in by peter, 18 years ago

Initial import

File size: 1.0 KB
Line 
1package MP3::Find::DB;
2
3use strict;
4use warnings;
5
6use base qw(MP3::Find::Base);
7
8use DBI;
9use SQL::Abstract;
10
11my $sql = SQL::Abstract->new;
12
13sub 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
481;
Note: See TracBrowser for help on using the repository browser.