Skip to content

Commit

Permalink
Merge pull request #21 from Lilit0x/functions/datalength
Browse files Browse the repository at this point in the history
DATALENGTH String Function: #13
  • Loading branch information
AmrDeveloper authored Sep 8, 2023
2 parents d96a918 + da98713 commit 73f94fb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 15 additions & 1 deletion crates/gitql-ast/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ lazy_static! {
map.insert("rtrim", text_right_trim);
map.insert("len", text_len);
map.insert("ascii", text_ascii);
map.insert("datalength", text_datalength);
map.insert("char", text_char);

map
};
}
Expand Down Expand Up @@ -103,12 +105,19 @@ lazy_static! {
result: DataType::Number,
},
);
map.insert(
"datalength",
Prototype {
parameters: vec![DataType::Text],
result: DataType::Number,
},
);
map.insert(
"char",
Prototype {
parameters: vec![DataType::Number],
result: DataType::Text,
},
},
);
map
};
Expand Down Expand Up @@ -161,6 +170,11 @@ fn text_ascii(inputs: Vec<Value>) -> Value {
return Value::Number(text.chars().nth(0).unwrap() as i64);
}

fn text_datalength(inputs: Vec<Value>) -> Value {
let text = inputs[0].as_text();
return Value::Number(text.as_bytes().len() as i64);
}

fn text_char(inputs: Vec<Value>) -> Value {
let code = inputs[0].as_number() as u32;
if let Some(character) = char::from_u32(code) {
Expand Down
2 changes: 2 additions & 0 deletions docs/function/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
| CHAR | Number | Text | Returns the character based on the ASCII code. |

### String functions samples
Expand All @@ -26,5 +27,6 @@ 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
SELECT CHAR(345) AS code
```

0 comments on commit 73f94fb

Please sign in to comment.