File Coverage

blib/lib/DBIx/Class/Helper/ResultSet/OneRow.pm
Criterion Covered Total %
statement 10 10 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 15 15 100.0


line stmt bran cond sub pod time code
1             package DBIx::Class::Helper::ResultSet::OneRow;
2             $DBIx::Class::Helper::ResultSet::OneRow::VERSION = '2.034002';
3             # ABSTRACT: The first you always wanted
4              
5 55     55   21410 use strict;
  55         106  
  55         1303  
6 55     55   243 use warnings;
  55         93  
  55         1194  
7              
8 55     55   233 use parent 'DBIx::Class::ResultSet';
  55         85  
  55         232  
9              
10 2     2 1 119 sub one_row { shift->search(undef, { rows => 1})->next }
11              
12             1;
13              
14             __END__
15              
16             =pod
17              
18             =head1 NAME
19              
20             DBIx::Class::Helper::ResultSet::OneRow - The first you always wanted
21              
22             =head1 SYNOPSIS
23              
24             # note that this is normally a component for a ResultSet
25             package MySchema::ResultSet::Person;
26              
27             use strict;
28             use warnings;
29              
30             use parent 'DBIx::Class::ResultSet';
31              
32             __PACKAGE__->load_components('Helper::ResultSet::OneRow');
33              
34             sub person_named {
35             $_[0]->search({ name => $_[1] })->one_row
36             }
37              
38             =head1 DESCRIPTION
39              
40             This component codifies an alternate version of L<DBIx::Class::ResultSet/first>.
41             In practical use, C<first> allows a user to do something like the following:
42              
43             my $rs = $schema->resultset('Foo')->search({ name => 'bar' });
44             my $first = $rs->first;
45             my @rest;
46             while (my $row = $rs->next) {
47             push @rest, $row
48             }
49              
50             Problematically, if you call C<first> without the while loop afterwards B<and>
51             you got back more than one row, you are leaving a cursor open. Depending on
52             your database this could increase memory usage or cause errors with later
53             queries.
54              
55             Fundamentally the difference is that when you use C<one_row> you are guaranteed
56             to exhaust the underlying cursor.
57              
58             Generally speaking, unless you are doing something unusual, C<one_row> is a good
59             default.
60              
61             =head1 METHODS
62              
63             =head2 one_row
64              
65             Limits the ResultSet to a single row, and then returns the matching result
66             object. In case no rows match, C<undef> is returned as normal.
67              
68             =head1 THANKS
69              
70             Thanks to Aran Clary Deltac (BLUEFEET) for initially writing this module, and
71             thanks to L<ZipRecruiter|https://www.ziprecruiter.com> for sponsoring that
72             initial developmentl
73              
74             =head1 AUTHOR
75              
76             Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
77              
78             =head1 COPYRIGHT AND LICENSE
79              
80             This software is copyright (c) 2019 by Arthur Axel "fREW" Schmidt.
81              
82             This is free software; you can redistribute it and/or modify it under
83             the same terms as the Perl 5 programming language system itself.
84              
85             =cut