Changeset 31 in mp3-find for trunk


Ignore:
Timestamp:
05/21/06 18:10:35 (19 years ago)
Author:
peter
Message:
  • abstracted out a '_get_dbh' function that returns a $dbh from either a DSN, an exisiting $dbh, or an SQLite db_file name
  • added a 'create' function that works like 'create_db' but uses named arguments instead
File:
1 edited

Legend:

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

    r30 r31  
    158158    status_callback => sub {} 
    159159 
     160=head2 create 
     161 
     162    $finder->create({ 
     163        dsn => 'dbi:SQLite:dbname=mp3.db', 
     164        dbh => $dbh, 
     165        db_file => 'mp3.db', 
     166    }); 
     167 
     168Creates a new table for storing mp3 info in the database. You can provide 
     169either a DSN (plus username and password, if needed), an already created 
     170database handle, or just the name of an SQLite database file. 
     171 
     172=cut 
     173 
     174sub 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 
    160184=head2 create_db 
    161185 
     
    172196    my $db_file = shift or croak "Need a name for the database I'm about to create"; 
    173197    my $dbh = DBI->connect("dbi:SQLite:dbname=$db_file", '', '', {RaiseError => 1}); 
    174     $dbh->do('CREATE TABLE mp3 (' . join(',', map { "$$_[0] $$_[1]" } @COLUMNS) . ')'); 
     198    my $create = 'CREATE TABLE mp3 (' . join(',', map { "$$_[0] $$_[1]" } @COLUMNS) . ')'; 
     199    $dbh->do($create); 
    175200} 
    176201 
     
    190215=cut 
    191216 
     217sub _get_dbh { 
     218    my $args = shift; 
     219    return $args->{dbh} if defined $args->{dbh}; 
     220    if (defined $args->{dsn}) { 
     221        my $dbh = DBI->connect( 
     222            $args->{dsn},  
     223            $args->{username},  
     224            $args->{password},  
     225            { RaiseError => 1 }, 
     226        ); 
     227        return $dbh; 
     228    } 
     229    # default to a SQLite database 
     230    if (defined $args->{db_file}) { 
     231        my $dbh = DBI->connect( 
     232            "dbi:SQLite:dbname=$$args{db_file}", 
     233            '', 
     234            '', 
     235            { RaiseError => 1 }, 
     236        ); 
     237        return $dbh; 
     238    } 
     239    return; 
     240} 
     241         
    192242# this is update_db and update_files (from Matt Dietrich) rolled into one 
    193243sub update { 
     
    195245    my $args = shift; 
    196246 
    197     my $dsn   = $args->{dsn} or croak "Need a DSN to connect to"; 
     247    my $dbh = _get_dbh($args) or croak "Please provide a DBI database handle, DSN, or SQLite database filename"; 
     248 
    198249    my @dirs  = $args->{dirs} 
    199250                    ? ref $args->{dirs} eq 'ARRAY' 
     
    210261    my $status_callback = $self->{status_callback} || $DEFAULT_STATUS_CALLBACK; 
    211262 
    212     my $dbh = DBI->connect($dsn, '', '', {RaiseError => 1}); 
    213263    my $mtime_sth = $dbh->prepare('SELECT mtime FROM mp3 WHERE FILENAME = ?'); 
    214264    my $insert_sth = $dbh->prepare( 
Note: See TracChangeset for help on using the changeset viewer.