Changeset 32 in mp3-find for trunk


Ignore:
Timestamp:
05/22/06 04:14:18 (18 years ago)
Author:
peter
Message:
  • added 'sync' method to DB.pm that uses '_get_dbh' metthod of getting database handles
File:
1 edited

Legend:

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

    r31 r32  
    1515 
    1616my @COLUMNS = ( 
    17     [ mtime        => 'INTEGER' ],  # the filesystem mtime, so we can do incremental updates 
     17    [ mtime        => 'INTEGER' ],  # filesystem mtime, so we can do incremental updates 
    1818    [ FILENAME     => 'TEXT' ],  
    1919    [ TITLE        => 'TEXT' ],  
     
    397397} 
    398398 
    399 =head2 sync_db 
    400  
    401     my $count = $finder->sync_db($db_filename); 
    402  
    403 Removes entries from the database that refer to files that no longer 
    404 exist in the filesystem. Returns the count of how many records were 
    405 removed. 
    406  
    407 =cut 
    408  
    409 # TODO: use DSNs instead of SQLite db names 
    410 sub sync_db { 
    411     my $self = shift; 
    412     my $db_file = shift or croak "Need the name of the databse to sync"; 
    413  
     399=head2 sync 
     400 
     401=cut 
     402 
     403sub sync { 
     404    my $self = shift; 
     405    my $args = shift; 
     406 
     407    my $dbh = _get_dbh($args) or croak "Please provide a DBI database handle, DSN, or SQLite database filename"; 
     408     
    414409    my $status_callback = $self->{status_callback} || $DEFAULT_STATUS_CALLBACK; 
    415410 
    416     my $dbh = DBI->connect("dbi:SQLite:dbname=$db_file", '', '', {RaiseError => 1}); 
    417411    my $select_sth = $dbh->prepare('SELECT FILENAME FROM mp3'); 
    418412    my $delete_sth = $dbh->prepare('DELETE FROM mp3 WHERE FILENAME = ?'); 
     
    433427} 
    434428 
     429=head2 sync_db 
     430 
     431    my $count = $finder->sync_db($db_filename); 
     432 
     433Removes entries from the database that refer to files that no longer 
     434exist in the filesystem. Returns the count of how many records were 
     435removed. 
     436 
     437=cut 
     438 
     439# TODO: use DSNs instead of SQLite db names 
     440sub sync_db { 
     441    my $self = shift; 
     442    my $db_file = shift or croak "Need the name of the databse to sync"; 
     443 
     444    my $status_callback = $self->{status_callback} || $DEFAULT_STATUS_CALLBACK; 
     445 
     446    my $dbh = DBI->connect("dbi:SQLite:dbname=$db_file", '', '', {RaiseError => 1}); 
     447    my $select_sth = $dbh->prepare('SELECT FILENAME FROM mp3'); 
     448    my $delete_sth = $dbh->prepare('DELETE FROM mp3 WHERE FILENAME = ?'); 
     449     
     450    # the number of records removed 
     451    my $count = 0; 
     452     
     453    $select_sth->execute; 
     454    while (my ($filename) = $select_sth->fetchrow_array) { 
     455        unless (-e $filename) { 
     456            $delete_sth->execute($filename); 
     457            $status_callback->(D => $filename); 
     458            $count++; 
     459        } 
     460    } 
     461     
     462    return $count;     
     463} 
     464 
    435465=head2 destroy_db 
    436466 
Note: See TracChangeset for help on using the changeset viewer.