Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LIKE and ILIKE #144

Closed
fabfianda opened this issue Nov 25, 2021 · 4 comments · Fixed by #146
Closed

LIKE and ILIKE #144

fabfianda opened this issue Nov 25, 2021 · 4 comments · Fixed by #146

Comments

@fabfianda
Copy link

Thanks for the lib, I'm enjoying using it a lot.

One question, though.
I don't seem to find a way to express a LIKE or ILIKE query.

If not supported directly, as it seems, what would be the best workaround?

Thanks

@ocharles
Copy link
Contributor

Hi @fabfianda, thanks for the kind words! LIKE and ILIKE are not currently provided by Rel8, but probably should be. In the meantime, you can add:

like, ilike :: Expr Text -> Expr Text -> Expr Bool
like = dbFunction "LIKE"
ilike = dbFunction "ILIKE"

@fabfianda
Copy link
Author

oh wow, excellent lib with excellent support XD

@fabfianda
Copy link
Author

fabfianda commented Nov 25, 2021

Follow up:
dbFunction (actually named 'function' at least in 1.1.0) was giving me an SQL "function ilike(character varying, text) does not exist" error.

I solved by doing this instead:

like, ilike :: Expr Text -> Expr Text -> Expr Bool
like = binaryOperator "LIKE"
ilike = binaryOperator "ILIKE"

So now I can happily have functions like this:

type Pattern = Text
articlesNamedLike :: Pattern -> Query (Article Expr)
articlesNamedLike pattern = do
        article <- allArticles
        where_ $ (articleName article) `like` (lit pattern)
        return article

@ocharles
Copy link
Contributor

ocharles commented Dec 6, 2021

Oh sorry, you're exactly right - you want binaryOperator, not dbFunction

ocharles added a commit that referenced this issue Jan 31, 2022
I couldn't quite decide the right home for these operators, but have gone for `Rel8.Expr.Text` in the end. As they are standard SQL operators, I've also re-exported `like` and `ilike` from `Rel8`.

Fixes #144.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants