Add SQL array::add() function for adding unique items

Related to #1690
This commit is contained in:
Tobie Morgan Hitchcock 2023-03-26 08:53:19 +01:00
parent 1102a2c6da
commit fb2356b17a
3 changed files with 22 additions and 0 deletions

View file

@ -9,6 +9,26 @@ use crate::sql::array::Union;
use crate::sql::array::Uniq; use crate::sql::array::Uniq;
use crate::sql::value::Value; use crate::sql::value::Value;
pub fn add((array, value): (Value, Value)) -> Result<Value, Error> {
match (array, value) {
(Value::Array(mut arr), Value::Array(other)) => {
for v in other.0 {
if !arr.0.iter().any(|x| *x == v) {
arr.0.push(v)
}
}
Ok(arr.into())
}
(Value::Array(mut arr), value) => {
if !arr.0.iter().any(|x| *x == value) {
arr.0.push(value)
}
Ok(arr.into())
}
_ => Ok(Value::None),
}
}
pub fn all((arg,): (Value,)) -> Result<Value, Error> { pub fn all((arg,): (Value,)) -> Result<Value, Error> {
match arg { match arg {
Value::Array(v) => Ok(v.iter().all(Value::is_truthy).into()), Value::Array(v) => Ok(v.iter().all(Value::is_truthy).into()),

View file

@ -66,6 +66,7 @@ pub fn synchronous(ctx: &Context<'_>, name: &str, args: Vec<Value>) -> Result<Va
dispatch!( dispatch!(
name, name,
args, args,
"array::add" => array::add,
"array::all" => array::all, "array::all" => array::all,
"array::any" => array::any, "array::any" => array::any,
"array::append" => array::append, "array::append" => array::append,

View file

@ -303,6 +303,7 @@ fn function_names(i: &str) -> IResult<&str, &str> {
fn function_array(i: &str) -> IResult<&str, &str> { fn function_array(i: &str) -> IResult<&str, &str> {
alt(( alt((
alt(( alt((
tag("array::add"),
tag("array::all"), tag("array::all"),
tag("array::any"), tag("array::any"),
tag("array::append"), tag("array::append"),