DBIx-Wrap

 view release on metacpan or  search on metacpan

Wrap.pm  view on Meta::CPAN

    my $hashref = $sth->fetchrow_hashref;
    $sth->finish;
    return $hashref;
  }
}

sub selectall {
  my $self   = shift;
  my %params = @_;

  my $sql = $self->_prepare_sql ('select', \%params)
    || return undef;

  my $sth = $self->{_dbh}->prepare ($sql);
  $sth->execute
    || return $self->error ("Could not execute '$sql': "
                            . $self->{_dbh}->errstr);
  $self->{_error} = undef;

  if (wantarray) {
    my @rows;
    while (my @row = $sth->fetchrow) {
      if (scalar (@row) == 1) {
        push (@rows, $row[0]);
      } else {
        push (@rows, \@row);
      }
    }
    $sth->finish;
    return @rows;
  } else {
    my @rows;
    while (my $row = $sth->fetchrow_hashref) {
      push (@rows, $row);
    }
    $sth->finish;
    return \@rows;
  }
}

=pod

=head2 each

  my $id;
  while (my ($username, $gcos) = $db->each (\$id,
                                   Table        => 'passwd',
                                   Fields       => 'username,gcos',
                                 )) {
    # Note, could have done 
    #   while (my $user = $db->each (...
    # to get a hash.
  }

Note that this method is deprecated.  Use the C<iterator> method.

This method is used for iterating through multiple database entries.  See
_prepare_sql for the named parameters used.

You must pass as the first arguement a reference to a scalar to store an
id for the iteration.  This allows iterations to be nested without 
conflict.

=cut

sub each {
  my $self = shift;
  my ($id, %params) = @_;

  my $sth;
  if (defined $$id) {
    $sth = $self->{_sth}->{$$id};
  } else {
    my $sql = $self->_prepare_sql ('select', \%params)
      || return undef;

    $sth = $self->{_dbh}->prepare ($sql);
    $sth->execute
      || return $self->error ("each: Could not execute: '$sql': "
                              . $self->{_dbh}->errstr);
    $self->{_error} = undef;

    $$id = scalar (keys %{$self->{_sth}});
    $self->{_sth}->{$$id} = $sth;
  }

  if (wantarray) {
    my @row = $sth->fetchrow;
    if (! @row) {
      $sth->finish;
      delete $self->{_sth}->{$$id};
    }
    return @row;
  } else {
    my $hashref = $sth->fetchrow_hashref;
    if (! $hashref) {
      $sth->finish;
      delete $self->{_sth}->{$$id};
    }
    return $hashref;
  }
}

=pod

=head2 iterator

  my $iterator = $db->iterator (Table	=> 'passwd',
                                Fields	=> 'username,gcos,homedir',
                                Where	=> "homedir like '/home/j/%");
  while (my ($username, $gcos, $homedir) = $iterator->next ()) {
    ...
  }

This method returns an iterator object used to iterate over multiple rows
returned by an SQL query.  See _prepare_sql for the named parameters used.

The iterator method C<next> is used to return the first or next row.  If
C<next> is called in an array context, an array of column values for the
specified fields is returned.  If C<next> is called in a scalar context, a
reference to a hash containing the name/values of the requested columns is



( run in 0.563 second using v1.01-cache-2.11-cpan-71847e10f99 )