[CDBI] Using scalar functions with AbstractSearch

Rhesa Rozendaal perl at rhesa.com
Thu Feb 2 01:41:57 GMT 2006


Bill Moseley wrote:
> On Thu, Feb 02, 2006 at 01:07:13AM +0100, Rhesa Rozendaal wrote:
> 
>>>Why not just use { -between => [ $start, $end ] } ?
>>>
>>
>>Because in this case "start_date" and "end_date" are columns in the table.
>>I tried to get a search with several other criteria, and the fixed string 
>>"start_date < now() and end_date > now()" or something to that effect.
>>The above way was the first thing I got working in Sweet.
> 
> 
> Do you mean this?
> 
> 
> $ perl -MData::Dumper -MSQL::Abstract -le "print Dumper [SQL::Abstract->new->where( { foo => 'bar', start => {'<', 'now()'}, end => { '>=', 'now()'} } )]"
> $VAR1 = [
>           ' WHERE ( end >= ? AND foo = ? AND start < ? )',
>           'now()',
>           'bar',
>           'now()'
>         ];

Close, but no sigar ;)

That way makes the "now()" just a plain string, whereas I was looking for the 
database function now() [might be mysql specific]. The resulting SQL would be:

  WHERE ( end >= 'now()' AND foo = 'bar' AND start < 'now()' )

which is different from

  WHERE ( end >= now() AND foo = 'bar' AND start < now() )

I suppose

   SQL::Abstract->new->where(
     {
        foo   => 'bar',
        start => \ '< now()',
        end   => \ '>= now()'
     } )

would have worked just as well. Now you made me wonder what the hell(*) I was 
thinking at the time... :-P

Thanks, Bill!

Rhesa

(*) Hell is a little village in Norway:
http://commons.wikimedia.org/wiki/Image:Hell_norway_sign.jpg




More information about the ClassDBI mailing list