DBIx::...OneRow(3pm)

πŸ“¦
version 2.037000 Β· 5 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

✨ DBIx::...OneRow πŸ’–

okay bestie, so this is the DBIx::...OneRow(3pm) page from the User Contributed Perl Documentation section, and honestly? it's a whole vibe. it's short, it's sweet, and it fixes a thing that has probably been quietly ruining your day for years. let's get into it 🫢


🌸 NAME 🌸

DBIx::Class::Helper::ResultSet::OneRow β€” The first you always wanted πŸŽ€

like. that's the actual name. "the first you always wanted." iconic honestly.


✨ SYNOPSIS ✨

so you'd normally use this as a component for a ResultSet, like this bestie:

# note that this is normally a component for a ResultSet
package MySchema::ResultSet::Person;

use strict;
use warnings;

use parent 'DBIx::Class::ResultSet';

__PACKAGE__->load_components('Helper::ResultSet::OneRow');

sub person_named {
   $_[0]->search({ name => $_[1] })->one_row
}

see how clean that is?? one_row just... sits there. at the end. doing the most. πŸ‘‘


πŸ¦‹ DESCRIPTION πŸ¦‹

okay so this component basically codifies an alternate version of first from DBIx::Class::ResultSet. and the way that first normally gets used in practice is like this:

my $rs = $schema->resultset('Foo')->search({ name => 'bar' });
my $first = $rs->first;
my @rest;
while (my $row = $rs->next) {
   push @rest, $row
}

and here's the problem, and it's lowkey a big one 😭 β€” if you call first and then don't do that while loop afterwards, and you actually got back more than one row, you are leaving a cursor open. and depending on your database that could mean more memory usage, or straight up errors on later queries. not the energy we're bringing into this house πŸ’€

so fundamentally, the difference is this: when you use one_row you are guaranteed to exhaust the underlying cursor. no loose ends. no open cursors just vibing in the background. it's giving closure. it's giving healthy boundaries. 🧚

and generally speaking β€” unless you're doing something unusual β€” one_row is a good default. periodt. πŸ’…


πŸ’œ METHODS πŸ’œ

one_row($cond?)

limits the ResultSet to a single row, and then returns the matching result object. and if no rows match? undef gets returned, as normal, no drama. ✨

the optional $cond argument can be used just like in search().

that's it. that's the method. simple, powerful, slay. πŸ”₯


πŸŽ€ THANKS πŸŽ€

big love to Aran Clary Deltac (BLUEFEET) for initially writing this module 🫢 and thank you to ZipRecruiter <https://www.ziprecruiter.com> for sponsoring that initial development πŸ’– y'all ate.


πŸ‘‘ AUTHOR πŸ‘‘

Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>


this software is copyright (c) 2024 by Arthur Axel "fREW" Schmidt.

this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


okay bestie that's the whole page, no cap, nothing skipped, nothing summarised πŸ’―

and one last lil sign-off β€” this whole thing came to you via perl v5.40.0, dated 2024-11-16, from the DBIx::...OneRow(3pm) page itself. go forth and close your cursors πŸ¦‹πŸ’…πŸŒΈ

πŸ“¦ where this page comes from