Changeset 33 in mp3-find
- Timestamp:
- 05/22/06 18:02:46 (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/MP3/Find/DB.pm
r32 r33 135 135 =back 136 136 137 =head1 METHODS138 139 =head2 new140 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 the146 database is added, updated, or deleted by the C<update_db> and147 C<sync_db> methods. The arguments passed to the callback are148 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 create161 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 provide169 either a DSN (plus username and password, if needed), an already created170 database handle, or just the name of an SQLite database file.171 172 =cut173 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_db185 186 $finder->create_db($db_filename);187 188 Creates a SQLite database in the file named c<$db_filename>.189 190 =cut191 192 # TODO: convert to using DSNs instead of hardcoded SQLite connections193 # TODO: extended table for ID3v2 data194 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 update203 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 searching211 in C<dirs> to their records in the database pointed to by C<dsn>. If the212 files found have been updated since they have been recorded in the database213 (or if they are not in the database), they are updated (or added).214 215 137 =cut 216 138 … … 239 161 return; 240 162 } 241 163 164 sub _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 183 The C<status_callback> gets called each time an entry in the 184 database is added, updated, or deleted by the C<update_db> and 185 C<sync_db> methods. The arguments passed to the callback are 186 a status code (A, U, or D) and the filename for that entry. 187 The 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 194 To 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 206 Creates a new table for storing mp3 info in the database. You can provide 207 either a DSN (plus username and password, if needed), an already created 208 database handle, or just the name of an SQLite database file. 209 210 =cut 211 212 sub 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 226 Creates a SQLite database in the file named c<$db_filename>. 227 228 =cut 229 230 # TODO: extended table for ID3v2 data 231 sub 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 247 Compares the files in the C<files> list plus any MP3s found by searching 248 in C<dirs> to their records in the database pointed to by C<dsn>. If the 249 files 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 252 Instead of a C<dsn>, you can also provide either an already created 253 database handle as C<dbh> or the filename of an SQLite database as C<db_file>. 254 255 =cut 256 242 257 # this is update_db and update_files (from Matt Dietrich) rolled into one 243 258 sub update { … … 310 325 } 311 326 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 322 330 return $count; 323 331 } 324 332 325 =head2 update_db 333 =head2 update_db (DEPRECATED) 326 334 327 335 my $count = $finder->update_db($db_filename, \@dirs); … … 385 393 } 386 394 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); 395 397 396 398 return $count; … … 398 400 399 401 =head2 sync 402 403 my $count = $finder->sync({ dsn => $DSN }); 404 405 Removes entries from the database that refer to files that no longer 406 exist in the filesystem. Returns the count of how many records were 407 removed. 400 408 401 409 =cut … … 424 432 } 425 433 434 # SQLite buggy driver 435 _sqlite_workaround($select_sth, $delete_sth); 436 426 437 return $count; 427 438 } 428 439 429 =head2 sync_db 440 =head2 sync_db (DEPRECATED) 430 441 431 442 my $count = $finder->sync_db($db_filename); … … 437 448 =cut 438 449 439 # TODO: use DSNs instead of SQLite db names440 450 sub sync_db { 441 451 my $self = shift; … … 474 484 sub destroy_db { 475 485 my $self = shift; 476 my $db_file = shift or croak "Need the name of a database to dest ory";486 my $db_file = shift or croak "Need the name of a database to destroy"; 477 487 unlink $db_file; 478 488 } 479 489 480 490 481 # TODO: use DSNs instead of SQLite db names482 491 sub search { 483 492 my $self = shift; … … 486 495 croak 'Need a database name to search (set "db_file" in the call to find_mp3s)' unless $$options{db_file}; 487 496 488 my $dbh = DBI->connect("dbi:SQLite:dbname=$$options{db_file}", '', '', {RaiseError => 1});497 my $dbh = _get_dbh($options); 489 498 490 499 # use the 'LIKE' operator to ignore case
Note: See TracChangeset
for help on using the changeset viewer.