Skip to content

[QUESTION] how can I run regex search in my querry #110

@Rignchen

Description

@Rignchen

In one of my projects I have a search option which currently always uses the LIKE '%...%' search type to get the entries where the user's search is contained in the searched row, and I wanted to add a --regex option to replace the contains with some regex matching

After looking online I saw that sqlite seem to have a REGEXP key which can be used instead of the LIKE to use regex search

I tried in my sqlite3 cli (version 3.48.0) and it totally works no problem

sqlite> select alias, path from aliases where path like '%2%' order by alias;
alias12|path12
alias21|path21
alias22|path22
sqlite> select alias, path from aliases where path regexp '2\d' order by alias;
alias21|path21
alias22|path22

However when I tried using the sqlite crate for my rust code, I get an error no such function: regexp

let mut statement = database.connection.prepare("select alias, path from aliases where path like '%2%' order by alias").unwrap();
let mut expected = vec![
	("alias12", "path12"),
	("alias21", "path21"),
	("alias22", "path22"),
].iter()
	.map(|(a, p)| (a.to_string(), p.to_string()))
	.collect::<Vec<(String, String)>>();
while let Ok(State::Row) = statement.next() {
	assert_eq!(expected.remove(0), (
		statement.read::<String, _>("alias").unwrap(),
		statement.read::<String, _>("path").unwrap(),
	));
}

let mut statement = database.connection.prepare("select alias, path from aliases where path regexp '2\\d' order by alias").unwrap();
let mut expected = vec![
	("alias21", "path21"),
	("alias22", "path22"),
].iter()
	.map(|(a, p)| (a.to_string(), p.to_string()))
	.collect::<Vec<(String, String)>>();
while let Ok(State::Row) = statement.next() {
	assert_eq!(expected.remove(0), (
		statement.read::<String, _>("alias").unwrap(),
		statement.read::<String, _>("path").unwrap(),
	));
}

I tried to look online if there was a trait to enable or a command to run to enable the feature but didn't saw anything about it


name version
crate version 0.36.1
rust edition 2021
cargo version 1.86.0
rustc version 1.86.0
sqlite3 cli 3.48.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions