From e47eddc4eaa0ada1ba157e34f157862cca06a8f1 Mon Sep 17 00:00:00 2001 From: Lilit0x Date: Thu, 7 Sep 2023 23:43:26 +0100 Subject: [PATCH] chore: datalength string function --- crates/gitql-ast/src/function.rs | 13 +++++++++++++ docs/function/functions.md | 2 ++ 2 files changed, 15 insertions(+) diff --git a/crates/gitql-ast/src/function.rs b/crates/gitql-ast/src/function.rs index 48b2454d..da123b20 100644 --- a/crates/gitql-ast/src/function.rs +++ b/crates/gitql-ast/src/function.rs @@ -25,6 +25,7 @@ lazy_static! { map.insert("rtrim", text_right_trim); map.insert("len", text_len); map.insert("ascii", text_ascii); + map.insert("datalength", text_datalength); map }; } @@ -102,6 +103,13 @@ lazy_static! { result: DataType::Number, }, ); + map.insert( + "datalength", + Prototype { + parameters: vec![DataType::Text], + result: DataType::Number, + }, + ); map }; } @@ -152,3 +160,8 @@ fn text_ascii(inputs: Vec) -> Value { } return Value::Number(text.chars().nth(0).unwrap() as i64); } + +fn text_datalength(inputs: Vec) -> Value { + let text = inputs[0].as_text(); + return Value::Number(text.as_bytes().len() as i64); +} diff --git a/docs/function/functions.md b/docs/function/functions.md index 3ebced97..eec07023 100644 --- a/docs/function/functions.md +++ b/docs/function/functions.md @@ -14,6 +14,7 @@ note that all functions names are case-insensitive. | REPLICATE | Text, Number | Text | Return repeated a string a specified number of times. | | SPACE | Number | Text | Returns a string of the specified number of space characters. | | ASCII | Text | Number | Returns the ASCII value for the specific character. | +| DATALENGTH| Text | Number | Returns the number of bytes used to represent an expression. | ### String functions samples @@ -25,4 +26,5 @@ SELECT * FROM commits where TRIM(name) = "" SELECT * FROM commits where LEN(name) > 0 SELECT * FROM commits where name = SPACE(5) SELECT name, ASCII(name) AS firstCharAscii FROM commits +SELECT DATALENGTH("AmrDeveloper") as bytelength ``` \ No newline at end of file