[CDBI] find_or_create() usage or feature request (Code proposal)

Carlos Ramirez carlos at quantumfx.com
Fri May 25 04:23:24 BST 2007


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 at lists.digitalcraftsmen.net
> http://lists.digitalcraftsmen.net/mailman/listinfo/classdbi
>
>
>   




More information about the ClassDBI mailing list