MCPcopy Create free account
hub / github.com/erans/pgsqlite / register_regex_functions

Function register_regex_functions

src/functions/regex_functions.rs:6–76  ·  view source on GitHub ↗

Register PostgreSQL-compatible regular expression functions

(conn: &Connection)

Source from the content-addressed store, hash-verified

4
5/// Register PostgreSQL-compatible regular expression functions
6pub fn register_regex_functions(conn: &Connection) -> Result<()> {
7 debug!("Registering regex functions");
8
9 // Register case-sensitive REGEXP function
10 conn.create_scalar_function(
11 "regexp",
12 2,
13 FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
14 |ctx| {
15 let pattern: String = ctx.get(0)?;
16 let text: String = ctx.get(1)?;
17
18 trace!("regexp('{}', '{}')", pattern, text);
19
20 match Regex::new(&pattern) {
21 Ok(re) => Ok(re.is_match(&text)),
22 Err(e) => {
23 debug!("Invalid regex pattern '{}': {}", pattern, e);
24 // PostgreSQL returns NULL for invalid patterns
25 Ok(false)
26 }
27 }
28 },
29 )?;
30
31 // Register case-insensitive REGEXPI function
32 conn.create_scalar_function(
33 "regexpi",
34 2,
35 FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
36 |ctx| {
37 let pattern: String = ctx.get(0)?;
38 let text: String = ctx.get(1)?;
39
40 trace!("regexpi('{}', '{}')", pattern, text);
41
42 // Add (?i) flag for case-insensitive matching
43 let case_insensitive_pattern = format!("(?i){pattern}");
44
45 match Regex::new(&case_insensitive_pattern) {
46 Ok(re) => Ok(re.is_match(&text)),
47 Err(e) => {
48 debug!("Invalid regex pattern '{}': {}", case_insensitive_pattern, e);
49 // PostgreSQL returns NULL for invalid patterns
50 Ok(false)
51 }
52 }
53 },
54 )?;
55
56 // Also register the standard SQLite REGEXP operator handler
57 // This enables "text REGEXP pattern" syntax in SQLite
58 conn.create_scalar_function(
59 "regexp",
60 2,
61 FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
62 |ctx| {
63 // Note: SQLite calls this with (pattern, text) order

Callers 4

register_all_functionsFunction · 0.85
test_regexp_functionFunction · 0.85
test_regexpi_functionFunction · 0.85
test_invalid_regexFunction · 0.85

Calls 1

getMethod · 0.45

Tested by 3

test_regexp_functionFunction · 0.68
test_regexpi_functionFunction · 0.68
test_invalid_regexFunction · 0.68