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

Function register_json_functions

src/functions/json_functions.rs:5–882  ·  view source on GitHub ↗

Register JSON/JSONB-related functions in SQLite

(conn: &Connection)

Source from the content-addressed store, hash-verified

3
4/// Register JSON/JSONB-related functions in SQLite
5pub fn register_json_functions(conn: &Connection) -> Result<()> {
6 // json_valid(text) - Validate JSON (SQLite built-in, but we override for consistency)
7 conn.create_scalar_function(
8 "json_valid",
9 1,
10 FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
11 |ctx| {
12 let value: String = ctx.get(0)?;
13 Ok(serde_json::from_str::<JsonValue>(&value).is_ok())
14 },
15 )?;
16
17 // jsonb_typeof(jsonb) - Get JSON value type
18 conn.create_scalar_function(
19 "jsonb_typeof",
20 1,
21 FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
22 json_typeof,
23 )?;
24
25 // json_typeof(json) - Alias for jsonb_typeof
26 conn.create_scalar_function(
27 "json_typeof",
28 1,
29 FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
30 json_typeof,
31 )?;
32
33 // jsonb_array_length(jsonb) - Get array length
34 conn.create_scalar_function(
35 "jsonb_array_length",
36 1,
37 FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
38 |ctx| {
39 let value: String = ctx.get(0)?;
40 match serde_json::from_str::<JsonValue>(&value) {
41 Ok(JsonValue::Array(arr)) => Ok(Some(arr.len() as i64)),
42 Ok(_) => Ok(None),
43 Err(_) => Ok(None),
44 }
45 },
46 )?;
47
48 // json_array_length(json) - Alias
49 conn.create_scalar_function(
50 "json_array_length",
51 1,
52 FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
53 |ctx| {
54 let value: String = ctx.get(0)?;
55 match serde_json::from_str::<JsonValue>(&value) {
56 Ok(JsonValue::Array(arr)) => Ok(Some(arr.len() as i64)),
57 Ok(_) => Ok(None),
58 Err(_) => Ok(None),
59 }
60 },
61 )?;
62

Calls 15

extract_json_pathFunction · 0.85
json_containsFunction · 0.85
strip_nullsFunction · 0.85
parse_json_pathFunction · 0.85
set_json_valueFunction · 0.85
insert_json_valueFunction · 0.85
delete_json_valueFunction · 0.85
register_json_aggFunction · 0.85
register_jsonb_aggFunction · 0.85
register_json_object_aggFunction · 0.85