Changeset 32 in mp3-find for trunk/lib/MP3
- Timestamp:
- 05/22/06 04:14:18 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/MP3/Find/DB.pm
r31 r32 15 15 16 16 my @COLUMNS = ( 17 [ mtime => 'INTEGER' ], # thefilesystem mtime, so we can do incremental updates17 [ mtime => 'INTEGER' ], # filesystem mtime, so we can do incremental updates 18 18 [ FILENAME => 'TEXT' ], 19 19 [ TITLE => 'TEXT' ], … … 397 397 } 398 398 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 403 sub 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 414 409 my $status_callback = $self->{status_callback} || $DEFAULT_STATUS_CALLBACK; 415 410 416 my $dbh = DBI->connect("dbi:SQLite:dbname=$db_file", '', '', {RaiseError => 1});417 411 my $select_sth = $dbh->prepare('SELECT FILENAME FROM mp3'); 418 412 my $delete_sth = $dbh->prepare('DELETE FROM mp3 WHERE FILENAME = ?'); … … 433 427 } 434 428 429 =head2 sync_db 430 431 my $count = $finder->sync_db($db_filename); 432 433 Removes entries from the database that refer to files that no longer 434 exist in the filesystem. Returns the count of how many records were 435 removed. 436 437 =cut 438 439 # TODO: use DSNs instead of SQLite db names 440 sub 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 435 465 =head2 destroy_db 436 466
Note: See TracChangeset
for help on using the changeset viewer.