The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

MP3::Find::DB - SQLite database backend to MP3::Find

SYNOPSIS

    use MP3::Find::DB;
    my $finder = MP3::Find::DB->new;
    
    my @mp3s = $finder->find_mp3s(
        dir => '/home/peter/music',
        query => {
            artist => 'ilyaimy',
            album  => 'myxomatosis',
        },
        ignore_case => 1,
        db_file => 'mp3.db',
    );
    
    # you can do things besides just searching the database
    
    # create another database
    $finder->create({ db_file => 'my_mp3s.db' });
    
    # update the database by searching the filesystem
    $finder->update({
        db_file => 'my_mp3s.db',
        dirs => ['/home/peter/mp3', '/home/peter/cds'],
    });

    # or just update specific mp3s
    $finder->update({
        db_file => 'my_mp3s.db',
        files => \@filenames,
    });
    
    # and then blow it away
    $finder->destroy_db('my_mp3s.db');

REQUIRES

DBI, DBD::SQLite, SQL::Abstract

DESCRIPTION

This is the database backend for MP3::Find. The easiest way to use it is with a SQLite database, but you can also pass in your own DSN or database handle.

The database you use should have at least one table named mp3 with the following schema:

    CREATE TABLE mp3 (
        mtime         INTEGER,
        FILENAME      TEXT, 
        TITLE         TEXT, 
        ARTIST        TEXT, 
        ALBUM         TEXT,
        YEAR          INTEGER, 
        COMMENT       TEXT, 
        GENRE         TEXT, 
        TRACKNUM      INTEGER, 
        VERSION       NUMERIC,
        LAYER         INTEGER, 
        STEREO        TEXT,
        VBR           TEXT,
        BITRATE       INTEGER, 
        FREQUENCY     INTEGER, 
        SIZE          INTEGER, 
        OFFSET        INTEGER, 
        SECS          INTEGER, 
        MM            INTEGER,
        SS            INTEGER,
        MS            INTEGER, 
        TIME          TEXT,
        COPYRIGHT     TEXT, 
        PADDING       INTEGER, 
        MODE          INTEGER,
        FRAMES        INTEGER, 
        FRAME_LENGTH  INTEGER, 
        VBR_SCALE     INTEGER
    );

Note: I'm still working out some kinks in here, so this backend is currently not as stable as the Filesystem backend. Expect API fluctuations for now.

Deprecated Methods: create_db, update_db, sync_db, and destroy_db have been deprecated in this release, and will be removed in a future release. Please switch to the new methods create, update, sync, and destory.

Special Options

When using this backend, provide one of the following additional options to the search method:

dsn, username, password

A custom DSN and (optional) username and password. This gets passed to the connect method of DBI.

dbh

An already created DBI database handle object.

db_file

The name of the SQLite database file to use.

METHODS

new

    my $finder = MP3::Find::DB->new(
        status_callback => \&callback,
    );

The status_callback gets called each time an entry in the database is added, updated, or deleted by the update and sync methods. The arguments passed to the callback are a status code (A, U, or D) and the filename for that entry. The default callback just prints these to STDERR:

    sub default_callback {
        my ($status_code, $filename) = @_;
        print STDERR "$status_code $filename\n";
    }

To suppress any output, set status_callback to an empty sub:

    status_callback => sub {}

create

    $finder->create({
        dsn => 'dbi:SQLite:dbname=mp3.db',
        dbh => $dbh,
        db_file => 'mp3.db',
    });

Creates a new table for storing mp3 info in the database. You can provide either a DSN (plus username and password, if needed), an already created database handle, or just the name of an SQLite database file.

create_db (DEPRECATED)

    $finder->create_db($db_filename);

Creates a SQLite database in the file named $db_filename.

update

    my $count = $finder->update({
        dsn   => 'dbi:SQLite:dbname=mp3.db',
        files => \@filenames,
        dirs  => \@dirs,
    });

Compares the files in the files list plus any MP3s found by searching in dirs to their records in the database pointed to by dsn. If the files found have been updated since they have been recorded in the database (or if they are not in the database), they are updated (or added).

Instead of a dsn, you can also provide either an already created database handle as dbh or the filename of an SQLite database as db_file.

update_db (DEPRECATED)

    my $count = $finder->update_db($db_filename, \@dirs);

Searches for all mp3 files in the directories named by @dirs using MP3::Find::Filesystem, and adds or updates the ID3 info from those files to the database. If a file already has a record in the database, then it will only be updated if it has been modified since the last time update_db was run.

sync

    my $count = $finder->sync({ dsn => $DSN });

Removes entries from the database that refer to files that no longer exist in the filesystem. Returns the count of how many records were removed.

sync_db (DEPRECATED)

    my $count = $finder->sync_db($db_filename);

Removes entries from the database that refer to files that no longer exist in the filesystem. Returns the count of how many records were removed.

destroy

    $finder->destroy({ db_file => $filename });

Permanantly removes the database. Unlike the other utility methods, this one can only act on SQLite db_file filenames, and not DSNs or database handles.

destroy_db (DEPRECATED)

    $finder->destroy_db($db_filename);

Permanantly removes the database.

TODO

Store/search ID3v2 tags

Driver classes to handle database dependent tasks?

SEE ALSO

MP3::Find, MP3::Find::Filesystem, mp3db

AUTHOR

Peter Eichman <peichman@cpan.org>

THANKS

Thanks to Matt Dietrich <perl@rainboxx.de> for suggesting having an option to just update specific files instead of doing a (longer) full search.

COPYRIGHT AND LICENSE

Copyright (c) 2006 by Peter Eichman. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.