Skip to content

Query add pattern matching on Field Reference types#605

Open
ElijahAhianyo wants to merge 13 commits into
masterfrom
elijah/like-query
Open

Query add pattern matching on Field Reference types#605
ElijahAhianyo wants to merge 13 commits into
masterfrom
elijah/like-query

Conversation

@ElijahAhianyo

@ElijahAhianyo ElijahAhianyo commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Description

This PR adds basic string pattern matching to queries. This is equivalent to LIKE, ILIKE(postgres specific) and GLOB(sqlite specific) variants on database engines
The expressions supported are:

  • contains
  • starts_with
  • ends_with
  • raw_like

and with an i-prefixed case-insensitive counterpart of each:

  • icontains
  • istarts_with
  • iends_with
  • iraw_like

Usage:

use cot::db::{Database, model, query};

#[model]
#[derive(Debug, Clone)]
struct Customer {
    #[model(primary_key)]
    id: i32,
    full_name: String,
}

let _ = query!(Customer, $full_name.contains("Doe"));
let _ = query!(Customer, $full_name.icontains("doe"));
let _ = query!(Customer, $full_name.starts_with("Jon"));
let _ = query!(Customer, $full_name.istarts_with("jon"));
let _ = query!(Customer, $full_name.ends_with("Doe"));
let _ = query!(Customer, $full_name.iends_with("doe"));

raw_like/iraw_like are escape hatches that accept a raw glob pattern (* for zero-or-more characters, ? for exactly one, \ to escape a wildcard) rather than a plain literal, for shapes the other four methods can't express, such as a fixed-width or middle-of-string match:

use cot::db::{model, query};

let _ = query!(Customer, $full_name.raw_like("J?n Do*"));
let _ = query!(Customer, $full_name.iraw_like("j*n ??e"));

These methods can be combined with Boolean and comparison operators like any other expression:

use cot::db::{model, query};

let _ = query!(Customer, $full_name.contains("Doe") && $is_active == true);

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Refactor / cleanup
  • Performance improvement
  • Other (describe above)

@github-actions github-actions Bot added C-lib Crate: cot (main library crate) C-macros Crate: cot-macros labels Jul 2, 2026
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

🐰 Bencher Report

Branchelijah/like-query
Testbedgithub-ubuntu-latest
Click to view all benchmark results
BenchmarkLatencyBenchmark Result
microseconds (µs)
(Result Δ%)
Upper Boundary
microseconds (µs)
(Limit %)
empty_router/empty_router📈 view plot
🚷 view threshold
6,531.80 µs
(+3.57%)Baseline: 6,306.45 µs
7,725.01 µs
(84.55%)
json_api/json_api📈 view plot
🚷 view threshold
1,097.70 µs
(+0.30%)Baseline: 1,094.42 µs
1,316.16 µs
(83.40%)
nested_routers/nested_routers📈 view plot
🚷 view threshold
1,033.00 µs
(+1.69%)Baseline: 1,015.86 µs
1,192.25 µs
(86.64%)
single_root_route/single_root_route📈 view plot
🚷 view threshold
997.20 µs
(+1.85%)Baseline: 979.07 µs
1,157.32 µs
(86.16%)
single_root_route_burst/single_root_route_burst📈 view plot
🚷 view threshold
19,923.00 µs
(+12.87%)Baseline: 17,651.05 µs
21,133.86 µs
(94.27%)
🐰 View full continuous benchmarking report in Bencher

@codecov

codecov Bot commented Jul 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 79.26267% with 90 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cot/src/db/query/expr.rs 73.98% 37 Missing and 8 partials ⚠️
cot-macros/src/query.rs 69.07% 30 Missing ⚠️
cot/src/db.rs 68.42% 0 Missing and 6 partials ⚠️
cot/src/db/query.rs 69.23% 2 Missing and 2 partials ⚠️
cot/src/db/query/expr/like.rs 94.44% 2 Missing and 2 partials ⚠️
cot/src/db/impl_sqlite.rs 97.14% 0 Missing and 1 partial ⚠️
Flag Coverage Δ
rust 90.16% <79.26%> (-0.10%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
cot-macros/src/model.rs 96.36% <ø> (ø)
cot/src/db/impl_mysql.rs 100.00% <100.00%> (ø)
cot/src/db/impl_postgres.rs 100.00% <100.00%> (ø)
cot/src/db/impl_sqlite.rs 96.29% <97.14%> (+1.55%) ⬆️
cot/src/db/query.rs 85.00% <69.23%> (+1.41%) ⬆️
cot/src/db/query/expr/like.rs 94.44% <94.44%> (ø)
cot/src/db.rs 87.50% <68.42%> (+0.66%) ⬆️
cot-macros/src/query.rs 74.09% <69.07%> (+0.60%) ⬆️
cot/src/db/query/expr.rs 73.98% <73.98%> (ø)

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@seqre seqre changed the title Query add .contains, .starts_with, .ends_with` proof of concept Query add .contains, .starts_with, .ends_with proof of concept Jul 2, 2026
- improve code quality
- Add docs
- refactor/move expr into a module since db/query file has grown bigger now
@github-actions github-actions Bot added the A-docs Area: Documentation label Jul 3, 2026
@ElijahAhianyo ElijahAhianyo changed the title Query add .contains, .starts_with, .ends_with proof of concept Query add .contains, .starts_with, .ends_with, .raw_like Jul 3, 2026
@ElijahAhianyo ElijahAhianyo marked this pull request as ready for review July 5, 2026 23:05
@ElijahAhianyo ElijahAhianyo changed the title Query add .contains, .starts_with, .ends_with, .raw_like Query add pattern matching on Field Reference types Jul 5, 2026
@ElijahAhianyo ElijahAhianyo requested review from m4tx and seqre July 5, 2026 23:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-docs Area: Documentation C-lib Crate: cot (main library crate) C-macros Crate: cot-macros

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant