Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of persistent with backend-specific operator

Tags:

haskell

yesod

I would like to do a LIKE query in persistent, I'm using sqlite. The yesod book gives an example of using raw SQL to do it, but says:

you can express a LIKE operator directly in the normal syntax due to a feature added in Persistent 0.6, which allows backend-specific operators

I couldn't find an example of that, though. Would somebody have an example of what it would mean to use a specific operator like LIKE with selectList or something equivalent?

Thanks!

like image 987
JP Moresmau Avatar asked Jun 15 '12 09:06

JP Moresmau


1 Answers

I know I've used it before, but I can't remember where. Anyway, a simple example (not GHC-checked, apologies) would be:

selectList [Filter PersonName (Left $ PersistText "%Michael%") (BackendSpecificFilter "ILIKE")] []

Obviously you can create some helper function, e.g.:

icontains field val = Filter field (Left $ PersistText $ T.concat ["%", val, "%"]) (BackendSpecificFilter "ILIKE")
selectList [Personname `icontains` "Michael"] []
like image 131
Michael Snoyman Avatar answered Sep 17 '22 07:09

Michael Snoyman