Audio-DB

 view release on metacpan or  search on metacpan

DB/Adaptor/dbi/mysql.pm  view on Meta::CPAN

#  $dbh->do('UNLOCK TABLES') if $self->lock_on_load;
#
#  foreach (keys %{$self->{load_stuff}{sth}}) {
#    $self->{load_stuff}{sth}{$_}->finish;
#  }
#
#  my $counter = $self->{load_stuff}{counter};
#  delete $self->{load_stuff};
#  return $counter;
#}


=head2 dbh

 Title   : dbh
 Usage   : $dbh->dbh
 Function: get database handle
 Returns : a DBI handle
 Args    : none
 Status  : Public

=cut


sub dbh      { shift->{dbh} }


=head2 DESTROY

 Title   : DESTROY
 Usage   : $dbh->DESTROY
 Function: disconnect database at destruct time
 Returns : void
 Args    : none
 Status  : protected

This is the destructor for the class.

=cut

sub DESTROY {
  my $self = shift;
  $self->dbh->disconnect if defined $self->dbh;
}


=head2 debug

 Title   : debug
 Usage   : $dbh = $dbh->debug
 Function: prints out debugging information
 Returns : debugging information
 Args    : none
 Status  : Private

=cut

sub debug {
  my $self = shift;
  $self->dbh->debug(@_);
  $self->SUPER::debug(@_);
}






##################
# QUERIES
##################
# Used to find out the id of the last value inserted
# for building databases...
sub lookup_counter {
  my ($self,$field,$table) = @_;
  my $dbh = $self->dbh;
  my $sth = $dbh->prepare(qq{select $field from $table ORDER BY $field DESC LIMIT 1});
}


#### NEW SUB
# This might come in handy later
# SOME OF THIS COULD BE MIGRATED OUT
# Generically fetch an item for a single table by its ID
# MIGRATE TO QUERY
# DEPRECATING
sub fetch_by_id {
  my ($self,$class,$id) = @_;
  my $dbh = $self->dbh;
  my $table = $class;
  $table =~ s/s$//;
  my $sth = $dbh->prepare(qq{select * from $class where $class.$table\_id=?});
  $sth->execute($id) or warn $sth->errstr;
  my $h = $sth->fetchrow_hashref;
  return $h;
}
## END NEW



# RETAIN
# Generic queries that should be part of each adaptor
# queries are grouped by the predominant class of object they are intended to retrieve
# This approach is not as clever as that which used generic fetch_class type subroutines.
# On the other hand, it allows for more tailored queries that let me build up more informative data structures

sub artist_queries {
  my ($self,$query,$table) = @_;
  my $dbh = $self->dbh;
  # BY IDs
  # Easiest case: Fetch an artist by its ID (returns a single result)
  if ($query eq 'by_artist_id') {
    return ($dbh->prepare(qq{select * from artists where artist_id=?}));
  }

  # Fetch an artist by an album ID (may possibly return more than one result)
  if ($query eq 'by_album_id') {
    return ($dbh->prepare(qq{select * from album_artists,artists where album_artists.album_id=? and album_artists.artist_id=artists.artist_id}));
  }

  # Fetch artist by a song ID (should return one and only one result)



( run in 1.819 second using v1.01-cache-2.11-cpan-5b529ec07f3 )