The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

DBIx::Cookbook::Recipe::Searching::fetch_all - recipe to fetch all records

DESCRIPTION

Select all records from the actor table, with the option to order by a column.

Sample Usage:

${orm}_cmd fetch_all --order_by last_name     # orm = dbic, rdbo, skinny, etc

RECIPES

DBIx::Class

package DBIx::Cookbook::DBIC::Command::fetch_all;
use Moose;
extends qw(MooseX::App::Cmd::Command);

has order_by => (
		 traits => [qw(Getopt)],
		 isa => "Str",
		 is  => "rw",
		);


sub execute {
  my ($self, $opt, $args) = @_;

  my $where = {};
  my $attr = {};

  if (my $val = $opt->{order_by}) {
    $attr->{order_by} = $val;
  }

  my $rs = $self->app->schema->resultset('Actor')->search($where, $attr);

  while (my $row = $rs->next) {
    use Data::Dumper;
    my %data = $row->get_columns;
    warn Dumper(\%data);
    
  }
}

1;

Rose::DB::Object

package DBIx::Cookbook::RDBO::Command::fetch_all;
use Moose;
extends qw(MooseX::App::Cmd::Command);

has order_by => (
		 traits => [qw(Getopt)],
		 isa => "Str",
		 is  => "rw",
		);


sub execute {
  my ($self, $opt, $args) = @_;

  my @attr = $opt->{order_by} ? (sort_by => $opt->{order_by} ) : () ;

  use Sakila::Actor::Manager;


  my $result = Sakila::Actor::Manager->get_actor_iterator(@attr);

  while (my $row = $result->next) {
    use Data::Dumper;
    warn Dumper($row->as_tree);
  }
}

1;

# Another way to get all rows:
# my $result = Sakila::Actor::Manager->get_actor;

DBIx::Skinny

package DBIx::Cookbook::Skinny::Command::fetch_all;
use Moose;
extends qw(MooseX::App::Cmd::Command);

has order_by => (
		 traits => [qw(Getopt)],
		 isa => "Str",
		 is  => "rw",
		);


sub execute {
  my ($self, $opt, $args) = @_;

  my @attr = $opt->{order_by} ? (sort_by => $opt->{order_by} ) : () ;

  use Sakila;
  my $result = Sakila->search(actor => $opt);

  while (my $row = $result->next) {
    use Data::Dumper;
    warn Dumper($row->{row_data});
  }
}

1;

# Another way to get all rows:
# my @rows = Sakila->search...