diff --git a/lib/src/fnc/cast.rs b/lib/src/fnc/cast.rs index 7146c3cb..40260980 100644 --- a/lib/src/fnc/cast.rs +++ b/lib/src/fnc/cast.rs @@ -6,13 +6,13 @@ use crate::sql::value::Value; pub fn run(_: &Context, name: &str, val: Value) -> Result { match name { "bool" => bool(val), - "int" => int(val), - "float" => float(val), - "string" => string(val), - "number" => number(val), - "decimal" => decimal(val), "datetime" => datetime(val), + "decimal" => decimal(val), "duration" => duration(val), + "float" => float(val), + "int" => int(val), + "number" => number(val), + "string" => string(val), _ => Ok(val), } } @@ -21,10 +21,24 @@ pub fn bool(val: Value) -> Result { Ok(val.is_truthy().into()) } -pub fn int(val: Value) -> Result { +pub fn datetime(val: Value) -> Result { Ok(match val { - Value::Number(Number::Int(_)) => val, - _ => Value::Number(Number::Int(val.as_int())), + Value::Datetime(_) => val, + _ => Value::Datetime(val.as_datetime()), + }) +} + +pub fn decimal(val: Value) -> Result { + Ok(match val { + Value::Number(Number::Decimal(_)) => val, + _ => Value::Number(Number::Decimal(val.as_decimal())), + }) +} + +pub fn duration(val: Value) -> Result { + Ok(match val { + Value::Duration(_) => val, + _ => Value::Duration(val.as_duration()), }) } @@ -35,14 +49,14 @@ pub fn float(val: Value) -> Result { }) } -pub fn number(val: Value) -> Result { +pub fn int(val: Value) -> Result { Ok(match val { - Value::Number(Number::Decimal(_)) => val, - _ => Value::Number(Number::Decimal(val.as_decimal())), + Value::Number(Number::Int(_)) => val, + _ => Value::Number(Number::Int(val.as_int())), }) } -pub fn decimal(val: Value) -> Result { +pub fn number(val: Value) -> Result { Ok(match val { Value::Number(Number::Decimal(_)) => val, _ => Value::Number(Number::Decimal(val.as_decimal())), @@ -55,17 +69,3 @@ pub fn string(val: Value) -> Result { _ => Value::Strand(val.as_strand()), }) } - -pub fn datetime(val: Value) -> Result { - Ok(match val { - Value::Datetime(_) => val, - _ => Value::Datetime(val.as_datetime()), - }) -} - -pub fn duration(val: Value) -> Result { - Ok(match val { - Value::Duration(_) => val, - _ => Value::Duration(val.as_duration()), - }) -} diff --git a/lib/src/sql/function.rs b/lib/src/sql/function.rs index 9c73de9e..567a25d7 100644 --- a/lib/src/sql/function.rs +++ b/lib/src/sql/function.rs @@ -202,13 +202,13 @@ fn cast(i: &str) -> IResult<&str, Function> { fn function_casts(i: &str) -> IResult<&str, &str> { alt(( tag("bool"), - tag("int"), - tag("float"), - tag("string"), - tag("number"), - tag("decimal"), tag("datetime"), + tag("decimal"), tag("duration"), + tag("float"), + tag("int"), + tag("number"), + tag("string"), ))(i) }