« blib/lib/AutoSQL/DBSQL/ObjectAdaptor/Fetch.pm
↑ Summary
blib/lib/AutoSQL/DBSQL/ObjectAdaptor/Store.pm »
| 1 | package AutoSQL::LazyLoader; | ||
| 2 | use strict; | ||
| 3 | use AutoCode::AccessorMaker; | ||
| 4 | our @ISA=qw(AutoCode::AccessorMaker); | ||
| 5 | |||
| 6 | use AutoCode::SymbolTableUtils; | ||
| 7 | |||
| 8 | sub import { | ||
| 9 | my ($class, @args)=@_; | ||
| 10 | my $self=$class->new; | ||
| 11 | my $caller=ref(caller)||caller; | ||
| 12 | my %args=@args; | ||
| 13 | if (exists $args{'$'}){ | ||
| 14 | my $scalar_accessors=$args{'$'}; | ||
| 15 | foreach(@$scalar_accessors){ | ||
| 16 | $self->make_scalar_loader($_, $caller); | ||
| 17 | } | ||
| 18 | } | ||
| 19 | } | ||
| 20 | |||
| 21 | sub make_scalar_loader { | ||
| 22 | my $self=shift; | ||
| 23 | my ($accessor, $pkg, $typeglob, $slot) = $self->__accessor_to_glob(@_); | ||
| 24 | no strict 'refs'; | ||
| 25 | *$typeglob=sub{ | ||
| 26 | my $self=shift; | ||
| 27 | return $self->{$slot}=shift if @_; | ||
| 28 | |||
| 29 | $self->{"_load_$accessor"} unless(defined $self->{$slot}); | ||
| 30 | return $self->{$slot}; | ||
| 31 | }; | ||
| 32 | *{"$pkg\::_load_$accessor"}=sub{ | ||
| 33 | my $self=shift; | ||
| 34 | $self->throw("Must be of AutoSQL::DBObject") | ||
| 35 | unless $self->isa('AutoSQL::DBObject'); | ||
| 36 | |||
| 37 | my $dbid = $self->dbid; | ||
| 38 | my $adaptor = $self->adaptor; | ||
| 39 | my $method="only_fetch_$accessor\_by_dbid"; | ||
| 40 | my $val = $adaptor->$method ($dbid); | ||
| 41 | $self->{$slot}= $val; | ||
| 42 | return $val; | ||
| 43 | }; | ||
| 44 | |||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | 1; | ||
| 49 | |||
| 50 | __END__ |