Function openvet_storage::database::functions::regex_functions

source ·
pub fn regex_functions(db: &Connection) -> Result<()>
Expand description

Register regular expression functions.

This will register the regexp function, which has a signature similar to this:

fn regexp(regex: &str, value: &str) -> bool;

You can use it to enforce regular expressions for column values

CREATE TABLE my_table(
    id INTEGER NOT NULL PRIMARY KEY,
    username TEXT NOT NULL UNIQUE CHECK(regexp("[a-z][a-zA-Z0-9-]*", username))
);

You can also use it in a query. Keep in mind that using a regex here means indices won’t be used, and a full table scan likely needs to be performed.

-- usernames consisting only of vovels
SELECT username WHERE regexp("[aeiou]+", username)

SQLite has an operator for using regular expressions. For this reason, writing value REGEXP regex is equivalent to calling regexp(regex, value).