[CDBI] search() with no arguments
Michael G Schwern <[email protected]> Thu, 24 Jan 2008 18:02:32 -0800
| Newsgroups | gmane.comp.lang.perl.modules.class-dbi |
|---|---|
| Message-ID | <[email protected]> |
I was writing a small wrapper routine today which would return an object from
the database at random, but allowed the user to control the set of objects by
passing in key/value search fields. Something like:
sub rand_thing {
my @objs = $class->search(@_);
return $objs[rand @objs];
}
but I discovered that if you don't give search() any arguments it pukes
because the WHERE clause is hard coded and a WHERE clause with no conditions
is illegal SQL. So I had to do this.
sub rand_thing {
my @objs = @_ ? $class->search(@_) : $class->retrieve_all();
return $objs[rand @objs];
}
That's a bit annoying to have to remember to do, so I pushed it up a level.
sub search {
my $class = shift;
return @_ ? $class->search(@_) : $class->retrieve_all();
}
That would seem to make a nice CDBI feature.
Similar issues with search_like().
--
184. When operating a military vehicle I may *not* attempt something
“I saw in a cartoon”.
-- The 213 Things Skippy Is No Longer Allowed To Do In The U.S. Army
http://skippyslist.com/?page_id=3
_______________________________________________
ClassDBI mailing list
[email protected]
http://lists.digitalcraftsmen.net/mailman/listinfo/classdbi