Changeset 33 in mp3-find for trunk


Ignore:
Timestamp:
05/22/06 18:02:46 (19 years ago)
Author:
peter
Message:
  • marked (create|update|sync)_db as DEPRECATED methods
  • abstracted SQLite active statement handle workaround code into its own function
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/MP3/Find/DB.pm

    r32 r33  
    135135=back 
    136136 
    137 =head1 METHODS 
    138  
    139 =head2 new 
    140  
    141     my $finder = MP3::Find::DB->new( 
    142         status_callback => \&callback, 
    143     ); 
    144  
    145 The C<status_callback> gets called each time an entry in the 
    146 database is added, updated, or deleted by the C<update_db> and 
    147 C<sync_db> methods. The arguments passed to the callback are 
    148 a status code (A, U, or D) and the filename for that entry. 
    149 The default callback just prints these to C<STDERR>: 
    150  
    151     sub default_callback { 
    152         my ($status_code, $filename) = @_; 
    153         print STDERR "$status_code $filename\n"; 
    154     } 
    155  
    156 To suppress any output, set C<status_callback> to an empty sub: 
    157  
    158     status_callback => sub {} 
    159  
    160 =head2 create 
    161  
    162     $finder->create({ 
    163         dsn => 'dbi:SQLite:dbname=mp3.db', 
    164         dbh => $dbh, 
    165         db_file => 'mp3.db', 
    166     }); 
    167  
    168 Creates a new table for storing mp3 info in the database. You can provide 
    169 either a DSN (plus username and password, if needed), an already created 
    170 database handle, or just the name of an SQLite database file. 
    171  
    172 =cut 
    173  
    174 sub create { 
    175     my $self = shift; 
    176     my $args = shift; 
    177  
    178     my $dbh = _get_dbh($args) or croak "Please provide a DBI database handle, DSN, or SQLite database filename"; 
    179      
    180     my $create = 'CREATE TABLE mp3 (' . join(',', map { "$$_[0] $$_[1]" } @COLUMNS) . ')'; 
    181     $dbh->do($create); 
    182 } 
    183  
    184 =head2 create_db 
    185  
    186     $finder->create_db($db_filename); 
    187  
    188 Creates a SQLite database in the file named c<$db_filename>. 
    189  
    190 =cut 
    191  
    192 # TODO: convert to using DSNs instead of hardcoded SQLite connections 
    193 # TODO: extended table for ID3v2 data 
    194 sub create_db { 
    195     my $self = shift; 
    196     my $db_file = shift or croak "Need a name for the database I'm about to create"; 
    197     my $dbh = DBI->connect("dbi:SQLite:dbname=$db_file", '', '', {RaiseError => 1}); 
    198     my $create = 'CREATE TABLE mp3 (' . join(',', map { "$$_[0] $$_[1]" } @COLUMNS) . ')'; 
    199     $dbh->do($create); 
    200 } 
    201  
    202 =head2 update 
    203  
    204     my $count = $finder->update({ 
    205         dsn   => 'dbi:SQLite:dbname=mp3.db', 
    206         files => \@filenames, 
    207         dirs  => \@dirs, 
    208     }); 
    209  
    210 Compares the files in the C<files> list plus any MP3s found by searching 
    211 in C<dirs> to their records in the database pointed to by C<dsn>. If the 
    212 files found have been updated since they have been recorded in the database 
    213 (or if they are not in the database), they are updated (or added). 
    214  
    215137=cut 
    216138 
     
    239161    return; 
    240162} 
    241          
     163 
     164sub _sqlite_workaround { 
     165    # as a workaround for the 'closing dbh with active staement handles warning 
     166    # (see http://rt.cpan.org/Ticket/Display.html?id=9643#txn-120724) 
     167    foreach (@_) { 
     168        $_->{RaiseError} = 0;  # don't die on error 
     169        $_->{PrintError} = 0;  # ...and don't even say anything 
     170        $_->{Active} = 1; 
     171        $_->finish; 
     172    } 
     173} 
     174  
     175=head1 METHODS 
     176 
     177=head2 new 
     178 
     179    my $finder = MP3::Find::DB->new( 
     180        status_callback => \&callback, 
     181    ); 
     182 
     183The C<status_callback> gets called each time an entry in the 
     184database is added, updated, or deleted by the C<update_db> and 
     185C<sync_db> methods. The arguments passed to the callback are 
     186a status code (A, U, or D) and the filename for that entry. 
     187The default callback just prints these to C<STDERR>: 
     188 
     189    sub default_callback { 
     190        my ($status_code, $filename) = @_; 
     191        print STDERR "$status_code $filename\n"; 
     192    } 
     193 
     194To suppress any output, set C<status_callback> to an empty sub: 
     195 
     196    status_callback => sub {} 
     197 
     198=head2 create 
     199 
     200    $finder->create({ 
     201        dsn => 'dbi:SQLite:dbname=mp3.db', 
     202        dbh => $dbh, 
     203        db_file => 'mp3.db', 
     204    }); 
     205 
     206Creates a new table for storing mp3 info in the database. You can provide 
     207either a DSN (plus username and password, if needed), an already created 
     208database handle, or just the name of an SQLite database file. 
     209 
     210=cut 
     211 
     212sub create { 
     213    my $self = shift; 
     214    my $args = shift; 
     215 
     216    my $dbh = _get_dbh($args) or croak "Please provide a DBI database handle, DSN, or SQLite database filename"; 
     217     
     218    my $create = 'CREATE TABLE mp3 (' . join(',', map { "$$_[0] $$_[1]" } @COLUMNS) . ')'; 
     219    $dbh->do($create); 
     220} 
     221 
     222=head2 create_db (DEPRECATED) 
     223 
     224    $finder->create_db($db_filename); 
     225 
     226Creates a SQLite database in the file named c<$db_filename>. 
     227 
     228=cut 
     229 
     230# TODO: extended table for ID3v2 data 
     231sub create_db { 
     232    my $self = shift; 
     233    my $db_file = shift or croak "Need a name for the database I'm about to create"; 
     234    my $dbh = DBI->connect("dbi:SQLite:dbname=$db_file", '', '', {RaiseError => 1}); 
     235    my $create = 'CREATE TABLE mp3 (' . join(',', map { "$$_[0] $$_[1]" } @COLUMNS) . ')'; 
     236    $dbh->do($create); 
     237} 
     238 
     239=head2 update 
     240 
     241    my $count = $finder->update({ 
     242        dsn   => 'dbi:SQLite:dbname=mp3.db', 
     243        files => \@filenames, 
     244        dirs  => \@dirs, 
     245    }); 
     246 
     247Compares the files in the C<files> list plus any MP3s found by searching 
     248in C<dirs> to their records in the database pointed to by C<dsn>. If the 
     249files found have been updated since they have been recorded in the database 
     250(or if they are not in the database), they are updated (or added). 
     251 
     252Instead of a C<dsn>, you can also provide either an already created 
     253database handle as C<dbh> or the filename of an SQLite database as C<db_file>. 
     254 
     255=cut 
     256 
    242257# this is update_db and update_files (from Matt Dietrich) rolled into one 
    243258sub update { 
     
    310325    } 
    311326     
    312     # SQLite specific code: 
    313     # as a workaround for the 'closing dbh with active staement handles warning 
    314     # (see http://rt.cpan.org/Ticket/Display.html?id=9643#txn-120724) 
    315     foreach ($mtime_sth, $insert_sth, $update_sth) { 
    316         $_->{RaiseError} = 0;  # don't die on error 
    317         $_->{PrintError} = 0;  # ...and don't even say anything 
    318         $_->{Active} = 1; 
    319         $_->finish; 
    320     } 
    321      
     327    # SQLite buggy driver 
     328    _sqlite_workaround($mtime_sth, $insert_sth, $update_sth); 
     329      
    322330    return $count; 
    323331} 
    324332 
    325 =head2 update_db 
     333=head2 update_db (DEPRECATED) 
    326334 
    327335    my $count = $finder->update_db($db_filename, \@dirs); 
     
    385393    } 
    386394     
    387     # as a workaround for the 'closing dbh with active staement handles warning 
    388     # (see http://rt.cpan.org/Ticket/Display.html?id=9643#txn-120724) 
    389     foreach ($mtime_sth, $insert_sth, $update_sth) { 
    390         $_->{RaiseError} = 0;  # don't die on error 
    391         $_->{PrintError} = 0;  # ...and don't even say anything 
    392         $_->{Active} = 1; 
    393         $_->finish; 
    394     } 
     395    # SQLite buggy driver 
     396    _sqlite_workaround($mtime_sth, $insert_sth, $update_sth); 
    395397     
    396398    return $count; 
     
    398400 
    399401=head2 sync 
     402 
     403    my $count = $finder->sync({ dsn => $DSN }); 
     404 
     405Removes entries from the database that refer to files that no longer 
     406exist in the filesystem. Returns the count of how many records were 
     407removed. 
    400408 
    401409=cut 
     
    424432    } 
    425433     
     434    # SQLite buggy driver 
     435    _sqlite_workaround($select_sth, $delete_sth); 
     436     
    426437    return $count;     
    427438} 
    428439 
    429 =head2 sync_db 
     440=head2 sync_db (DEPRECATED) 
    430441 
    431442    my $count = $finder->sync_db($db_filename); 
     
    437448=cut 
    438449 
    439 # TODO: use DSNs instead of SQLite db names 
    440450sub sync_db { 
    441451    my $self = shift; 
     
    474484sub destroy_db { 
    475485    my $self = shift; 
    476     my $db_file = shift or croak "Need the name of a database to destory"; 
     486    my $db_file = shift or croak "Need the name of a database to destroy"; 
    477487    unlink $db_file; 
    478488} 
    479489 
    480490 
    481 # TODO: use DSNs instead of SQLite db names 
    482491sub search { 
    483492    my $self = shift; 
     
    486495    croak 'Need a database name to search (set "db_file" in the call to find_mp3s)' unless $$options{db_file}; 
    487496     
    488     my $dbh = DBI->connect("dbi:SQLite:dbname=$$options{db_file}", '', '', {RaiseError => 1}); 
     497    my $dbh = _get_dbh($options); 
    489498     
    490499    # use the 'LIKE' operator to ignore case 
Note: See TracChangeset for help on using the changeset viewer.