12
votes

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!

1

1 Answers

9
votes

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"] []