| 1 | package DBomb::Tie::PrimaryKeyList; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | DBomb::Tie::PrimaryKeyList - A list of primary keys that auto creates objects when fetched. | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | tie @ids, 'DBomb::Tie::PrimaryKeyList', 'MyPackage::Customer'; | ||
| 10 | |||
| 11 | ## store plain ids, or PrimaryKey objects in the list | ||
| 12 | for (@{$dbh->selectrow_arrayref("SELECT id FROM Customer")){ | ||
| 13 | push @ids, $_; ## Stores the [id] key. | ||
| 14 | } | ||
| 15 | |||
| 16 | ## Later, fetch the ids as objects. | ||
| 17 | $customer = $ids[0]; | ||
| 18 | print $customer-name; | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | use strict; | ||
| 23 | use warnings; | ||
| 24 | our $VERSION = '$Revision: 1.4 $'; | ||
| 25 | |||
| 26 | use Tie::Array; | ||
| 27 | use base qw(Tie::StdArray); | ||
| 28 | |||
| 29 | ## We need this because Tie::Array doesn't give us anywhere to store extra data. | ||
| 30 | my %extra; ## $arr => +{ obj_class => $class } | ||
| 31 | |||
| 32 | sub TIEARRAY | ||
| 33 | { | ||
| 34 | my ($class, $obj_class, $values) = @_; | ||
| 35 | my $arr = $class->SUPER::TIEARRAY; | ||
| 36 | $extra{$arr} = +{}; | ||
| 37 | $extra{$arr}->{'obj_class'} = $obj_class; | ||
| 38 | |||
| 39 | push @$arr, @$values if $values; | ||
| 40 | $arr | ||
| 41 | } | ||
| 42 | |||
| 43 | sub DESTROY | ||
| 44 | { | ||
| 45 | delete $extra{shift}; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub FETCH | ||
| 49 | { | ||
| 50 | my ($arr, $ix) = @_; | ||
| 51 | my $self = $extra{$arr}; | ||
| 52 | |||
| 53 | my $v = $arr->[$ix]; | ||
| 54 | return undef unless defined $v; | ||
| 55 | |||
| 56 | $v = [$v] unless ref $v; | ||
| 57 | if (UNIVERSAL::isa($v, 'ARRAY') || UNIVERSAL::isa($v, 'DBomb::Value::Key')){ | ||
| 58 | my $obj = $self->{'obj_class'}->new($v); | ||
| 59 | return $arr->[$ix] = $obj; | ||
| 60 | } | ||
| 61 | $v | ||
| 62 | } | ||
| 63 | |||
| 64 | # | ||
| 65 | #sub STORE | ||
| 66 | #{ | ||
| 67 | # my ($self, $ix, $value) = @_; | ||
| 68 | #} | ||
| 69 | # | ||
| 70 | #sub FETCHSIZE | ||
| 71 | #{ | ||
| 72 | # my $self = shift; | ||
| 73 | #} | ||
| 74 | # | ||
| 75 | #sub STORESIZE | ||
| 76 | #{ | ||
| 77 | # my ($self, $count) = @_; | ||
| 78 | #} | ||
| 79 | # | ||
| 80 | #sub EXTEND | ||
| 81 | #{ | ||
| 82 | # my ($self, $count) = @_; | ||
| 83 | #} | ||
| 84 | # | ||
| 85 | #sub EXISTS | ||
| 86 | #{ | ||
| 87 | # my ($self, $key) = @_; | ||
| 88 | #} | ||
| 89 | # | ||
| 90 | #sub DELETE | ||
| 91 | #{ | ||
| 92 | # my ($self, $key) = @_; | ||
| 93 | #} | ||
| 94 | # | ||
| 95 | #sub CLEAR | ||
| 96 | #{ | ||
| 97 | # my $self = shift; | ||
| 98 | #} | ||
| 99 | # | ||
| 100 | #sub PUSH | ||
| 101 | #{ | ||
| 102 | # my $self = shift; | ||
| 103 | ## LIST | ||
| 104 | #} | ||
| 105 | # | ||
| 106 | #sub POP | ||
| 107 | #{ | ||
| 108 | # my $self = shift; | ||
| 109 | #} | ||
| 110 | # | ||
| 111 | #sub SHIFT | ||
| 112 | #{ | ||
| 113 | # my $self = shift; | ||
| 114 | #} | ||
| 115 | # | ||
| 116 | #sub UNSHIFT | ||
| 117 | #{ | ||
| 118 | # my $self = shift; | ||
| 119 | ## LIST | ||
| 120 | #} | ||
| 121 | # | ||
| 122 | #sub SPLICE | ||
| 123 | #{ | ||
| 124 | # my ($self, $offsef, $length, @list) = @_; | ||
| 125 | #} | ||
| 126 | # | ||
| 127 | #sub UNTIE | ||
| 128 | #{ | ||
| 129 | # my $self = shift; | ||
| 130 | #} | ||
| 131 | # | ||
| 132 | 1; | ||
| 133 | __END__ |