Example of persistent with backend-specific operator

笑着哭i 提交于 2019-12-08 16:31:16

问题


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:


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


来源:https://stackoverflow.com/questions/11048143/example-of-persistent-with-backend-specific-operator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!