Re: [CDBI] find_or_create() usage or feature request (Code proposal)
Carlos Ramirez <[email protected]> Thu, 24 May 2007 20:23:24 -0700
| Newsgroups | gmane.comp.lang.perl.modules.class-dbi |
|---|---|
| Message-ID | <[email protected]> |
After looking at the code, it didn't look to complicated. Can this be
incorporated in Class/DBI.pm ? Here's the implemented code change for my
feature request. It's based on version 3.0.16.
UPDATED:
sub find_or_create {
my $class = shift;
my $search_hash = '';
my $record_hash = '';
if (ref $_[0] eq "HASH") {
$search_hash = shift;
$record_hash = shift;
}
else {
$search_hash = {@_};
}
my ($exists) = $class->search($search_hash);
return defined($exists) ? $exists : $class->insert($record_hash);
}
PREV:
sub find_or_create {
my $class = shift;
my $hash = ref $_[0] eq "HASH" ? shift: {@_};
my ($exists) = $class->search($hash);
return defined($exists) ? $exists : $class->insert($hash);
}
-Carlos
Carlos Ramirez wrote:
> I like the functionality of find_or_create() however, i think it could
> be better if it allowed you specify the search criteria and the actual
> record data as seperate parameters like:
>
> ## find_or_create(\%search_criteria [,\%new_record]);
> Module->find_or_create({user_name => 'einstein'},
> { user_name => 'einstein', field => 'physics', fname => 'Albert'});
>
> For instance, if I want to find or create a new record where I only want
> to check that the user_name field doesn't already exist, before I insert
> the record. The current implementation requires that you specify all the
> new fields of the new record which are used as conditions for a search
> ... right?
>
>
> _______________________________________________
> ClassDBI mailing list
> ClassDBI-Ra3b/[email protected]
> http://lists.digitalcraftsmen.net/mailman/listinfo/classdbi
>
>
>