Audio-DB

 view release on metacpan or  search on metacpan

DB/Build.pm  view on Meta::CPAN

$VERSION = '';

# Initialize a new database
sub initialize {
  my $self = shift;
  my $adaptor = $self->adaptor;
  if (defined $self->{shout}) { print STDERR "Initializing database...\n"; };
  $adaptor->do_initialize(1) if @_ == 1 && $_[0];
  my ($erase,$meta) = rearrange(['ERASE'],@_);
  $meta ||= {};
  
  # initialize (possibly erasing)
  return unless $adaptor->do_initialize($erase);
  # return;
  1;
}


# Wrapper method for quickly building a database
# I think that much of this code belongs in the adaptor...
sub load_database {
  my ($self,@p) = @_;
  my ($dirs,$files,$library,$columns,$tmp,$shout,@others) =
    rearrange([
	       [qw(DIR DIRS TOP_DIRS)],
	       FILES,
	       LIBRARY,	
	       COLUMNS,
	       TMP,
	       [qw(VERBOSE YELL SCREAM)],
	      ],@p);
  
  # Set some system-level variables
  $tmp ||= $ENV_TMP;
  $self->{tmp}   = $tmp;
  $self->{shout} = $shout if ($shout);

  # User has supplied an iTunes library file. Let's parse that.
  if ($library) {
    Audio::DB::Parse::iTunes->parse_library($self,$library);
  } elsif (@$files > 0) {
    # Parsing information from a flat file of mp3 information
    Audio::DB::Parse::FlatFile->parse_files($self,$files,$columns);
  } else {
    # Reading data directly from MP3z...
    Audio::DB::Parse::ReadFiles->process_directories($self,$dirs);
  }

  my $adaptor = $self->adaptor;
  $self->_dump_data_structures();
  $self->_load_db();
  my $stats = $self->get_stats();
  return $stats;
}


# UPDATE DATABASE IS NOT COMPLETELY WORKING YET!!
# Wrapper method for quickly adding new songs to a database.
# This needs to check within the DB itself for artists
# and albums, as well as within the current set.
sub update_database {
  my ($self,@p) = @_;
  my ($dirs,$files,$columns,$tmp,$shout,@others) =
    rearrange([
	       [qw(DIR DIRS TOP_DIRS)],
	       FILES,
	       COLUMNS,
	       TMP,
	       [qw(VERBOSE YELL SCREAM)],
	      ],@p);
  
  # Establish the counters based on preexisitng values in database
  $self->_establish_counters();

  if ($files) {
    Audio::DB::Parse::FlatFile->parse_files($self,$files,$columns);
  } else {
    # Reading data directly from MP3z...
    # Save the top level directory
    
    @{$self->{top_dirs}} = @{$dirs};
    $tmp ||= $ENV_TMP;
    $self->{tmp}   = $tmp;
    $self->{shout} = $shout if ($shout);
    Audio::DB::Parse::ReadFiles->process_directories($self);
  }

  $self->_dump_data_structures();
  $self->_load_db();
  return;
}

sub get_stats {
  my $self = shift;
  my $stats = {};
  $stats->{artists} = $self->{counters}->{artists};
  $stats->{albums}  = $self->{counters}->{albums};
  $stats->{genres}  = $self->{counters}->{genres};
  $stats->{songs}   = $self->{counters}->{songs};
  return $stats;
}


# TODO WARNING
# This is ONLY used during updates and has not been brought up-to-date.
# Get the highest values for each before adding
# new info to the DB.
# This requires MySQL specific code and should probably be moved...
# not documented yet
sub _establish_counters {
  my $self = shift;
  if (defined $self->{shout}) { print STDERR "Establishing counters...\n"; };
  my $adaptor = $self->adaptor;
  my $dbh = $adaptor->dbh();
  my %fields = (
		song_id   => songs,
		artist_id => artists,
		album_id  => albums,
		genre_id  => genres);
  
  foreach (keys %fields) {



( run in 1.353 second using v1.01-cache-2.11-cpan-804bf51f3ce )