jeremychone / rust-modql

Rust implementation for Model Query Language support
Apache License 2.0
30 stars 6 forks source link

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

Closed mcamara closed 3 months ago

mcamara commented 5 months ago

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

jeremychone commented 3 months ago

@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 commented 3 months ago

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 commented 3 months ago

@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 commented 3 months ago

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