DBIx::Librarian::Statement::SelectOne - non-strict single-row SELECT statement


DBIx-Librarian documentation Contained in the DBIx-Librarian distribution.

Index


Code Index:

NAME

Top

DBIx::Librarian::Statement::SelectOne - non-strict single-row SELECT statement

DESCRIPTION

Top

SELECT statement that expects to retrieve exactly one record from the database, but might find none. An exception is raised if more than one row is found.

By default, all values fetched will be stored in top-level scalars in the data hash. If ALLARRAYS is set, results will be stored as element zero in a list for each field.

AUTHOR

Top

Jason W. May <jmay@pobox.com>

COPYRIGHT

Top


DBIx-Librarian documentation Contained in the DBIx-Librarian distribution.

package DBIx::Librarian::Statement::SelectOne;

require 5.005;
use base qw(DBIx::Librarian::Statement);
use strict;
use Carp;
use vars qw($VERSION);
$VERSION = '0.4';

sub fetch {
    my ($self, $data) = @_;

    my $hash_ref = $self->{STH}->fetchrow_hashref;
    if (!$hash_ref) {
#	print STDERR "...no matching rows\n";
	return 0;
    }

    while (my ($key, $val) = each %$hash_ref) {
	my $node = $data;

	if ($key =~ /\./) {
	    my ($base, $subkey) = split /\./, $key;
	    $node->{$base} = {} unless defined $node->{$base};
	    $node = $node->{$base};
	    $key = $subkey;
	}

	if ($self->{ALLARRAYS}) {
	    $node->{$key}[0] = $val;
	} else {
	    $node->{$key} = $val;
	}
    }

    if ($self->{STH}->fetchrow_hashref) {
	croak "Expected exactly one row; received more than one for\n" . $self->{STH}->{Statement} . "\n";
    }

    return 1;
}


1;