Changeset 183

Show
Ignore:
Timestamp:
05/04/06 00:17:15 (4 years ago)
Author:
sky
Message:

r186@crucially-3 (orig r1220): plindner | 2006-04-18 17:55:20 -0700
Support composite keys, yeah\!

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/Data/ObjectDriver/BaseObject.pm

    r182 r183  
    5858            die "Please specify a valid column for $parentclass"  
    5959        } 
    60         # TBD Is column a composite key? 
    6160 
    6261        # create a method name based on the column 
    6362        if (! defined $method) { 
    64             $method = $column; 
    65             $method =~ s/_id$//; 
    66             $method .= "_obj"; 
    67         } 
    68  
    69         # die if we can't find a way to make a valid method 
    70         # TBD check current list of columns to avoid clash 
    71         if (! defined $method || ($method eq $column)) { 
     63            if (!ref($column)) { 
     64                $method = $column; 
     65                $method =~ s/_id$//; 
     66                $method .= "_obj"; 
     67            } elsif (ref($column) eq 'ARRAY') { 
     68                foreach my $col (@{$column}) { 
     69                    $col =~ s/_id$//; 
     70                    $method .= $col . '_'; 
     71                } 
     72                $method .= "obj"; 
     73            } 
     74        } 
     75      
     76        # die if we have clashing methods method 
     77        if (! defined $method || defined(*{"${class}::$method"})) { 
    7278            die "Please define a valid method for $class->$column"; 
    7379        } 
     
    7985            my $cachekey = "__cache_$method"; 
    8086 
    81             *{"${class}::$method"} = sub { 
    82                 my $obj = shift; 
    83                 unless (exists $obj->{$cachekey}) { 
    84                     $obj->{$cachekey} = $parentclass->lookup($obj->column($column)); 
    85                     weaken $obj->{$cachekey}; 
    86                 } 
    87                 return $obj->{$cachekey}; 
    88             }; 
     87            if (ref($column)) { 
     88                *{"${class}::$method"} = sub { 
     89                    my $obj = shift; 
     90                    unless (exists $obj->{$cachekey}) { 
     91                        if (ref($column) eq 'ARRAY') { 
     92                            $obj->{$cachekey} = $parentclass->lookup([ map{ $obj->{column_values}->{$_} } @{$column}]); 
     93                        } else { 
     94                            $obj->{$cachekey} = $parentclass->lookup($obj->{column_values}->{$column}); 
     95                        } 
     96                        weaken $obj->{$cachekey}; 
     97                    } 
     98                    return $obj->{$cachekey}; 
     99                }; 
     100            } else { 
     101                # array version 
     102            } 
    89103        } else { 
    90             *{"${class}::$method"} = sub { 
    91                 return $parentclass->lookup(shift()->column($column)); 
    92             }; 
     104            if (ref($column)) { 
     105                *{"${class}::$method"} = sub { 
     106                    my $obj = shift; 
     107                    return $parentclass->lookup([ map{ $obj->{column_values}->{$_} } @{$column}]); 
     108                }; 
     109            } else { 
     110                *{"${class}::$method"} = sub { 
     111                    return $parentclass->lookup(shift()->{column_values}->{$column}); 
     112                }; 
     113            } 
    93114        } 
    94115 
     
    100121            $parent_method .= '_objs'; 
    101122        } 
    102         *{"${parentclass}::$parent_method"} = sub { 
    103             my $obj = shift; 
    104             my $terms = shift; 
    105             my $args = shift; 
    106             # TBD - allow user defined extra terms here?... 
    107             # TBD - use primary_key_to_terms 
    108             return $class->search({$column => $obj->id}, $args); 
     123        if (ref($column)) { 
     124            *{"${parentclass}::$parent_method"} = sub { 
     125                my $obj = shift; 
     126                my $terms = shift || {}; 
     127                my $args = shift; 
     128 
     129                my $primary_key_tuple = $obj->primary_key_tuple; 
     130                my $primary_key = $obj->primary_key; 
     131 
     132                # inject pk search into given terms. 
     133                # composite key, ugh 
     134                foreach my $key (@{$primary_key_tuple}) { 
     135                    $terms->{$key} = shift(@{$primary_key}); 
     136                } 
     137 
     138                return $class->search($terms, $args); 
     139            } 
     140        } else { 
     141            *{"${parentclass}::$parent_method"} = sub { 
     142                my $obj = shift; 
     143                my $terms = shift || {}; 
     144                my $args = shift; 
     145                # TBD - use primary_key_to_terms 
     146                $terms->{$column} = $obj->primary_key; 
     147                return $class->search($terms, $args); 
     148            } 
    109149        }; 
    110150    } # end of loop over class names 
     
    265305    } 
    266306 
     307    # set some values 
    267308    if (@_) { 
    268309        $obj->{column_values}->{$col} = shift; 
     
    431472the column is a singular key, an array ref if this is a composite key. 
    432473 
     474   column => 'user_id' 
     475   column => ['user_id', 'photo_id'] 
     476 
    433477=item * method [OPTIONAL] 
    434478 
    435 Name of the method to create in this class.  Defaults to the column name without 
     479Name of the method to create in this class.  Defaults to the column name(s) without 
    436480the _id suffix and with the suffix _obj appended. 
    437481 
     
    443487=item * cached [OPTIONAL] 
    444488 
    445 If set to 1 then we will cache in-memory the resulting object inside this class. 
     489If set to 1 cache the result of the fetching the parent object in the current class.  Note 
     490that this is a private copy to this class only, and does not interact with other caches 
     491in the system. 
    446492 
    447493=back