[CDBI] search() with no arguments

Michael G Schwern schwern at pobox.com
Fri Jan 25 02:02:32 GMT 2008


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




More information about the ClassDBI mailing list