Changeset 31 in mp3-find for trunk/lib/MP3
- Timestamp:
- 05/21/06 18:10:35 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/MP3/Find/DB.pm
r30 r31 158 158 status_callback => sub {} 159 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 160 184 =head2 create_db 161 185 … … 172 196 my $db_file = shift or croak "Need a name for the database I'm about to create"; 173 197 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); 175 200 } 176 201 … … 190 215 =cut 191 216 217 sub _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 192 242 # this is update_db and update_files (from Matt Dietrich) rolled into one 193 243 sub update { … … 195 245 my $args = shift; 196 246 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 198 249 my @dirs = $args->{dirs} 199 250 ? ref $args->{dirs} eq 'ARRAY' … … 210 261 my $status_callback = $self->{status_callback} || $DEFAULT_STATUS_CALLBACK; 211 262 212 my $dbh = DBI->connect($dsn, '', '', {RaiseError => 1});213 263 my $mtime_sth = $dbh->prepare('SELECT mtime FROM mp3 WHERE FILENAME = ?'); 214 264 my $insert_sth = $dbh->prepare(
Note: See TracChangeset
for help on using the changeset viewer.