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

Function register_string_functions

src/functions/string_functions.rs:5–230  ·  view source on GitHub ↗

Register all PostgreSQL string functions

(conn: &Connection)

Source from the content-addressed store, hash-verified

3
4/// Register all PostgreSQL string functions
5pub fn register_string_functions(conn: &Connection) -> Result<()> {
6 debug!("Registering string functions");
7
8 // Register split_part function
9 conn.create_scalar_function(
10 "split_part",
11 3,
12 FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
13 |ctx| {
14 let string = ctx.get::<String>(0)?;
15 let delimiter = ctx.get::<String>(1)?;
16 let field_num = ctx.get::<i64>(2)?;
17
18 if field_num < 1 {
19 return Ok("".to_string());
20 }
21
22 let parts: Vec<&str> = string.split(&delimiter).collect();
23 let index = (field_num - 1) as usize; // Convert to 0-based index
24
25 if index < parts.len() {
26 Ok(parts[index].to_string())
27 } else {
28 Ok("".to_string())
29 }
30 },
31 )?;
32
33 // Register string_agg function - this is an aggregate function
34 conn.create_aggregate_function(
35 "string_agg",
36 2,
37 FunctionFlags::SQLITE_UTF8,
38 StringAggregator,
39 )?;
40
41 // Register translate function
42 conn.create_scalar_function(
43 "translate",
44 3,
45 FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
46 |ctx| {
47 let string = ctx.get::<String>(0)?;
48 let from_chars = ctx.get::<String>(1)?;
49 let to_chars = ctx.get::<String>(2)?;
50
51 let from_vec: Vec<char> = from_chars.chars().collect();
52 let to_vec: Vec<char> = to_chars.chars().collect();
53
54 let mut result = String::new();
55 for ch in string.chars() {
56 if let Some(pos) = from_vec.iter().position(|&c| c == ch) {
57 if pos < to_vec.len() {
58 result.push(to_vec[pos]);
59 }
60 // If to_chars is shorter than from_chars, characters are removed
61 } else {
62 result.push(ch);

Callers 8

test_split_partFunction · 0.85
test_translateFunction · 0.85
test_ascii_chrFunction · 0.85
test_repeatFunction · 0.85
test_reverseFunction · 0.85
test_left_rightFunction · 0.85
test_lpad_rpadFunction · 0.85
register_all_functionsFunction · 0.85

Calls 2

takeMethod · 0.80
lenMethod · 0.45

Tested by 7

test_split_partFunction · 0.68
test_translateFunction · 0.68
test_ascii_chrFunction · 0.68
test_repeatFunction · 0.68
test_reverseFunction · 0.68
test_left_rightFunction · 0.68
test_lpad_rpadFunction · 0.68