| 1 | package DBIx::Class::Helper::ResultSet::OneRow; | ||
| 2 | 54 | $DBIx::Class::Helper::ResultSet::OneRow::VERSION = '2.032002'; | |
| 3 | # ABSTRACT: The first you always wanted | ||
| 4 | |||
| 5 | 54 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 54 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 54 | ▶ | use parent 'DBIx::Class::ResultSet'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 2 | ▶ | sub one_row { shift->search(undef, { rows => 1})->next } |
|
Subroutine
one_row: called CC=1 SCAR=0.0
Pod
documented
| |||
| 11 | |||
| 12 | 54 | 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) 2016 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 | ||