Re: [CDBI] find_or_create() usage or feature request (Code proposal)
Michael Reece <[email protected]> Fri, 25 May 2007 09:10:05 -0700
| Newsgroups | gmane.comp.lang.perl.modules.class-dbi |
|---|---|
| Message-ID | <[email protected]> |
that looks buggy. if ref $_[0] ne "HASH" then you are passing my
$record_hash = ''; to $class->insert() ..
this looks more correct, but good luck getting it added to CDBI
unless you write some tests and contact tony..
sub find_or_create {
my $class = shift;
my $search = ref $_[0] eq "HASH" ? shift : {@_};
my $insert = ref $_[0] eq "HASH" ? shift : $search;
my ($exists) = $class->search($search);
return defined($exists) ? $exists : $class->insert($insert);
}
On May 24, 2007, at 8:23 PM, Carlos Ramirez wrote:
> 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
>>
>>
>>
>
>
> _______________________________________________
> ClassDBI mailing list
> ClassDBI-Ra3b/[email protected]
> http://lists.digitalcraftsmen.net/mailman/listinfo/classdbi
---
michael reece :: software engineer :: [email protected]