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

Function register_array_prepend

src/functions/array_functions.rs:203–239  ·  view source on GitHub ↗

array_prepend(element, array) - Prepend element to array

(conn: &Connection)

Source from the content-addressed store, hash-verified

201
202/// array_prepend(element, array) - Prepend element to array
203fn register_array_prepend(conn: &Connection) -> Result<()> {
204 conn.create_scalar_function(
205 "array_prepend",
206 2,
207 FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
208 |ctx| {
209 // Handle element parameter of different types
210 let elem_value = match ctx.get_raw(0) {
211 rusqlite::types::ValueRef::Text(s) => {
212 let text = std::str::from_utf8(s).unwrap_or("");
213 serde_json::from_str::<JsonValue>(text)
214 .unwrap_or_else(|_| JsonValue::String(text.to_string()))
215 }
216 rusqlite::types::ValueRef::Integer(i) => JsonValue::Number(serde_json::Number::from(i)),
217 rusqlite::types::ValueRef::Real(f) => {
218 JsonValue::Number(serde_json::Number::from_f64(f).unwrap_or_else(|| serde_json::Number::from(0)))
219 }
220 rusqlite::types::ValueRef::Null => JsonValue::Null,
221 rusqlite::types::ValueRef::Blob(b) => {
222 JsonValue::String(format!("\\x{}", hex::encode(b)))
223 }
224 };
225
226 let array_json: String = ctx.get(1)?;
227
228 match serde_json::from_str::<JsonValue>(&array_json) {
229 Ok(JsonValue::Array(mut arr)) => {
230 arr.insert(0, elem_value);
231 Ok(serde_json::to_string(&arr).ok())
232 }
233 _ => Ok(None),
234 }
235 },
236 )?;
237
238 Ok(())
239}
240
241/// array_cat(array1, array2) - Concatenate two arrays
242fn register_array_cat(conn: &Connection) -> Result<()> {

Callers 1

register_array_functionsFunction · 0.85

Calls 2

getMethod · 0.45
insertMethod · 0.45

Tested by

no test coverage detected