Case-sensitive contains, starts_with and ends_with that match % and _ literally on every database

Open
#874 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
58/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
mariadb, mysql, postgresql, rust, sqlite
Domain
database

Research direction

Start with crates/database/src/db/builder/expr.rs around Expr::like_text and crates/database/src/db/builder/compile.rs around glob_pattern and SQLite like_case_sensitive compilation. Trace how the existing literal matchers escape text and how case-sensitive patterns compile for each database, then add equivalent coverage for the proposed Expr and text model column methods. Done means %, _ and backslash match literally with case respected across the supported engines.

Written by the indexing model from the issue text.

Description

The problem

starts_with, ends_with and contains match text literally (%, _ and \ in the text match only themselves), but they always ignore case where the engine does. like_case_sensitive tells case apart on every engine, but it takes a pattern, and there's no portable way to put a literal % or _ in that pattern:

  • On PostgreSQL, MySQL and MariaDB, \% is a literal %, because \ is like's default escape there.
  • On SQLite, like_case_sensitive becomes a glob with Escape::Driver (compile.rs L556, L1524–L1545). % always becomes * and _ becomes ?, and a \ is a literal backslash. So like_case_sensitive("100\\%%") finds "100% off" on PostgreSQL and MySQL, and any text that starts with 100\ on SQLite.

The escaping that contains does is Expr::like_text, which is pub(crate) (expr.rs L874–L891). glob_pattern already handles Escape::Backslash, so a case-sensitive like_text would compile correctly on SQLite; nothing public builds one.

Laravel

Laravel Classic's whereLike($column, $value, caseSensitive: true) has the same gap on SQLite: SQLiteGrammar::whereLike turns the pattern into a glob by replacing % and _, so it can't hold them literally either. Laravel has no contains helper, so it has nothing to escape.

Proposal

Case-sensitive versions of the literal matchers, on Expr and on text model columns:

db::table("coupons").filter(col("code").contains_case_sensitive("50%"));
Coupon::query().filter(Coupon::code.starts_with_case_sensitive("VIP_"));

They escape text as contains does and compile as like_case_sensitive does, with Escape::Backslash, so SQLite's glob keeps % and _ as literal characters and every engine matches the same rows. Alternatively, like_text could become public with a Case argument, but three named methods follow the existing like / like_case_sensitive pair.

Use case

An exact-match search box in a package's data table, or an operations dashboard that filters log lines or SKUs where case matters and the text can hold % or _ (50%_OFF, api_v2). Today that needs like_case_sensitive with hand-escaped input that's only correct off SQLite, or raw instr(column, ?) with a separate branch per engine.

Dominant language
Rust
Stars
847
Forks
174
Avg merge
5h 40m
Merged PRs (30d)
167

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from laravel/rust-framework

All issues in laravel/rust-framework

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.