array_append(array, element) - Append element to array
(conn: &Connection)
| 162 | |
| 163 | /// array_append(array, element) - Append element to array |
| 164 | fn register_array_append(conn: &Connection) -> Result<()> { |
| 165 | conn.create_scalar_function( |
| 166 | "array_append", |
| 167 | 2, |
| 168 | FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC, |
| 169 | |ctx| { |
| 170 | let array_json: String = ctx.get(0)?; |
| 171 | |
| 172 | // Handle element parameter of different types |
| 173 | let elem_value = match ctx.get_raw(1) { |
| 174 | rusqlite::types::ValueRef::Text(s) => { |
| 175 | let text = std::str::from_utf8(s).unwrap_or(""); |
| 176 | serde_json::from_str::<JsonValue>(text) |
| 177 | .unwrap_or_else(|_| JsonValue::String(text.to_string())) |
| 178 | } |
| 179 | rusqlite::types::ValueRef::Integer(i) => JsonValue::Number(serde_json::Number::from(i)), |
| 180 | rusqlite::types::ValueRef::Real(f) => { |
| 181 | JsonValue::Number(serde_json::Number::from_f64(f).unwrap_or_else(|| serde_json::Number::from(0))) |
| 182 | } |
| 183 | rusqlite::types::ValueRef::Null => JsonValue::Null, |
| 184 | rusqlite::types::ValueRef::Blob(b) => { |
| 185 | JsonValue::String(format!("\\x{}", hex::encode(b))) |
| 186 | } |
| 187 | }; |
| 188 | |
| 189 | match serde_json::from_str::<JsonValue>(&array_json) { |
| 190 | Ok(JsonValue::Array(mut arr)) => { |
| 191 | arr.push(elem_value); |
| 192 | Ok(serde_json::to_string(&arr).ok()) |
| 193 | } |
| 194 | _ => Ok(None), |
| 195 | } |
| 196 | }, |
| 197 | )?; |
| 198 | |
| 199 | Ok(()) |
| 200 | } |
| 201 | |
| 202 | /// array_prepend(element, array) - Prepend element to array |
| 203 | fn register_array_prepend(conn: &Connection) -> Result<()> { |
no test coverage detected