| 1 | package DBIx::Librarian::Statement::SelectOne; | ||
| 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::SelectOne - non-strict single-row SELECT statement | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | SELECT statement that expects to retrieve exactly one record from | ||
| 17 | the database, but might find none. An exception is raised if | ||
| 18 | more than one row is found. | ||
| 19 | |||
| 20 | By default, all values fetched will be stored in top-level scalars | ||
| 21 | in the data hash. If ALLARRAYS is set, results will be stored as | ||
| 22 | element zero in a list for each field. | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | sub fetch { | ||
| 27 | my ($self, $data) = @_; | ||
| 28 | |||
| 29 | my $hash_ref = $self->{STH}->fetchrow_hashref; | ||
| 30 | if (!$hash_ref) { | ||
| 31 | # print STDERR "...no matching rows\n"; | ||
| 32 | return 0; | ||
| 33 | } | ||
| 34 | |||
| 35 | while (my ($key, $val) = each %$hash_ref) { | ||
| 36 | my $node = $data; | ||
| 37 | |||
| 38 | if ($key =~ /\./) { | ||
| 39 | my ($base, $subkey) = split /\./, $key; | ||
| 40 | $node->{$base} = {} unless defined $node->{$base}; | ||
| 41 | $node = $node->{$base}; | ||
| 42 | $key = $subkey; | ||
| 43 | } | ||
| 44 | |||
| 45 | if ($self->{ALLARRAYS}) { | ||
| 46 | $node->{$key}[0] = $val; | ||
| 47 | } else { | ||
| 48 | $node->{$key} = $val; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | if ($self->{STH}->fetchrow_hashref) { | ||
| 53 | croak "Expected exactly one row; received more than one for\n" . $self->{STH}->{Statement} . "\n"; | ||
| 54 | } | ||
| 55 | |||
| 56 | return 1; | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | 1; | ||
| 61 | |||
| 62 | =head1 AUTHOR | ||
| 63 | |||
| 64 | Jason W. May <jmay@pobox.com> | ||
| 65 | |||
| 66 | =head1 COPYRIGHT | ||
| 67 | |||
| 68 | Copyright (C) 2001 Jason W. May. All rights reserved. | ||
| 69 | This module is free software; you can redistribute it and/or | ||
| 70 | modify it under the same terms as Perl itself. |