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

Add ILIKE for Postgres (case-insensitive LIKE) #3

Merged
merged 3 commits into from
Aug 14, 2024

Conversation

mcamara
Copy link
Contributor

@mcamara mcamara commented Jun 7, 2024

Hello @jeremychone,

First of all, thank you for your excellent work on maintaining the modql crate!

I needed to perform a case-insensitive search using modql, but I couldn't find a way to do this directly. I noticed that sea-query includes support for the ILIKE operator, so I decided to add this functionality to modql.

Summary

This PR introduces support for the ILIKE operator, which is particularly useful for case-insensitive pattern matching in PostgreSQL. This feature is optional and can be enabled by using the with-ilike feature flag in Cargo.toml. Additionally, I have added several other case-insensitive operators for more comprehensive support.

Changes

  1. Added Case-Insensitive Operators to OpValString:
    • Extended the OpValString enum to include ContainsCi(String), NotContainsCi(String), StartsWithCi(String), NotStartsWithCi(String), EndsWithCi(String), NotEndsWithCi(String), and Ilike(String) variants.
  2. Implemented Conditional Handling for Case-Insensitive Operators in Sea Query:
    • Updated the into_sea_cond_expr method to handle the new operators.
    • Added conditional compilation to ensure PgBinOper is only used when the with-ilike feature is enabled.
    • Included logic to perform case-insensitive comparisons by transforming both the column and value to lowercase.

Usage

To use the ILIKE operator and other case-insensitive operators, ensure the with-ilike feature is enabled in your Cargo.toml:

[dependencies]
modql = { path = "../rust-modql", features = ["with-sea-query", "with-ilike"] }

Example of using the $containsCi operator in a filter:

let list_filter: TaskFilter = serde_json::from_value(json! ({
    "project_id": 123,
    "title": {"$containsCi": "example"} ,
}))?;

Feedback

I believe this feature could be beneficial to other users as well. Please let me know if there are any changes or additions you would like me to make to this PR.

Thanks again for your hard work and for considering this addition!

Best regards,
Marc

@mcamara mcamara changed the title Add ILIKE for ostgres (case-insensitive LIKE) Add ILIKE for Postgres (case-insensitive LIKE) Jun 7, 2024
@mcamara mcamara force-pushed the main branch 2 times, most recently from db2e9d4 to d67b99e Compare June 10, 2024 10:48
@jeremychone
Copy link
Owner

jeremychone commented Aug 14, 2024

@mcamara How did you know that I wanted to add the ...Ci to the string operators? This is awesome!

I was hoping the ...Ci would be enough and that we wouldn't need a PostgreSQL-specific ILIKE, but the fact that it’s added as a feature might be okay.

By the way, the Sea-Query update should have been in a separate PR, not part of this one, since I wanted to do it ASAP.

Can you remove it from the PR? (I think you just need to remove the commit and do a force push).

@jeremychone
Copy link
Owner

ha, that was part of our previous conversation. Somehow, I did not find the thread.

Ok, did a first scan, and it looks good. I need to double check more.

@jeremychone jeremychone merged commit d58e26e into jeremychone:main Aug 14, 2024
@jeremychone
Copy link
Owner

@mcamara Ok, I merge it as is. This is really good work.

Note: I will add my rustfmt.toml to make sure there is a common whitespace (my bad), but this PR was great.

Big thanks!

@mcamara
Copy link
Contributor Author

mcamara commented Aug 15, 2024

@jeremychone thanks to you for your awesome job with these crates and your videos! It was a pleasure to help you!

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 this pull request may close these issues.

2 participants