| 1 | package DBIx::Librarian::Statement::SelectExactlyOne; | ||
| 2 | |||
| 3 | require 5.005; | ||
| 4 | use base qw(DBIx::Librarian::Statement); | ||
| 5 | use strict; | ||
| 6 | use Carp; | ||
| 7 | use vars qw($VERSION); | ||
| 8 | $VERSION = '0.4'; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | DBIx::Librarian::Statement::SelectExactlyOne - strict one-row SELECT statement | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | SELECT statement that expects to retrieve exactly one record from | ||
| 17 | the database. An exception is raised if zero rows are found or | ||
| 18 | if more than one row is found. | ||
| 19 | |||
| 20 | All values fetched will be stored in top-level scalars in the data hash. | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | sub fetch { | ||
| 25 | my ($self, $data) = @_; | ||
| 26 | |||
| 27 | my $hash_ref = $self->{STH}->fetchrow_hashref; | ||
| 28 | |||
| 29 | if (!$hash_ref) { | ||
| 30 | croak "Expected exactly one row but received none for\n" . $self->{STH}->{Statement}; | ||
| 31 | } | ||
| 32 | |||
| 33 | while (my ($key, $val) = each %$hash_ref) { | ||
| 34 | my $node = $data; | ||
| 35 | |||
| 36 | if ($key =~ /\./) { | ||
| 37 | my ($base, $subkey) = split /\./, $key; | ||
| 38 | $node->{$base} = {} unless defined $node->{$base}; | ||
| 39 | $node = $node->{$base}; | ||
| 40 | $key = $subkey; | ||
| 41 | } | ||
| 42 | |||
| 43 | if ($self->{ALLARRAYS}) { | ||
| 44 | $node->{$key}[0] = $val; | ||
| 45 | } else { | ||
| 46 | $node->{$key} = $val; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | if ($self->{STH}->fetchrow_hashref) { | ||
| 51 | croak "Expected exactly one row; received more than one for\n" . $self->{STH}->{Statement}; | ||
| 52 | } | ||
| 53 | |||
| 54 | return 1; | ||
| 55 | } | ||
| 56 | |||
| 57 | |||
| 58 | 1; | ||
| 59 | |||
| 60 | =head1 AUTHOR | ||
| 61 | |||
| 62 | Jason W. May <jmay@pobox.com> | ||
| 63 | |||
| 64 | =head1 COPYRIGHT | ||
| 65 | |||
| 66 | Copyright (C) 2001-2003 Jason W. May. All rights reserved. | ||
| 67 | This module is free software; you can redistribute it and/or | ||
| 68 | modify it under the same terms as Perl itself. |