views:

77

answers:

2

In DBIx::Class, when I generate a query using this syntax:

...
'Time(submitted_at)' => { '>' => 'Time(Now()-Interval ' . $wait_period . ' minute)' }
...

The generated query is perfect except for the fact that the function on the right hand side is in quotes.

... AND ( Time(submitted_at) > 'Time(Now()-Interval 5 minute)' ) ...

If it was not quoted then it would be correct. How would I do that?

Thanks, Rob

+4  A: 

Pass the string as a scalar reference instead:

...
'Time(submitted_at)' => \"> Time(Now()-Interval $wait_period minute)" 
...
Adam Bellaire
Thanks for the replies. I can't seem to figure out though why it is not interpreting references in that way though. With the above (not changed at all) I get this SQL generated:... ( Time(submitted_at) > 'SCALAR(0x108b8cd8)' ) ...
rplevy
Hmm, I guess it only likes scalar refs at the top level of the hash. I've modified my answer accordingly, does it work now?
Adam Bellaire
Yup I figured that out just now and was about the post it, but you beat me to it. Thanks for the help!
rplevy
+4  A: 

It seems that the way to do expressions is to pass a scalar ref or array ref if you want to use literal SQL.

Here's an example showing using a query parameter for the $wait_period variable into the expression:

...
'Time(submitted_at)' => { '>' => \['Time(Now()-Interval ? Minute)', $wait_period] }
...
Bill Karwin
+1 for using a query parameter rather than plain string concatenation
Adam Bellaire