Skip to content

Commit

Permalink
Merge pull request #35 from tbro/functions/unicode
Browse files Browse the repository at this point in the history
add UNICODE funciton #13
  • Loading branch information
AmrDeveloper authored Sep 15, 2023
2 parents e9861ec + 783b3f2 commit 1d0e851
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
15 changes: 15 additions & 0 deletions crates/gitql-ast/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ lazy_static! {
map.insert("translate", text_translate);
map.insert("soundex", text_soundex);
map.insert("concat", text_concat);
map.insert("unicode", text_unicode);

// Date functions
map.insert("current_date", date_current_date);
Expand Down Expand Up @@ -189,6 +190,13 @@ lazy_static! {
result: DataType::Text
},
);
map.insert(
"unicode",
Prototype {
parameters: vec![DataType::Text],
result: DataType::Integer
},
);

// Date functions
map.insert(
Expand Down Expand Up @@ -388,6 +396,13 @@ fn text_translate(inputs: Vec<Value>) -> Value {
return Value::Text(text);
}

fn text_unicode(inputs: Vec<Value>) -> Value {
if let Some(c) = inputs[0].as_text().chars().next() {
return Value::Integer((c as u32).into());
}
return Value::Integer(0);
}

fn text_soundex(inputs: Vec<Value>) -> Value {
let text = inputs[0].as_text();
if text.is_empty() {
Expand Down
3 changes: 3 additions & 0 deletions docs/function/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ note that all functions names are case-insensitive.
| TRANSLATE | Text, Text, Text, | Text | Returns the string from the first argument after the characters specified in the second argument are translated into the characters specified in the third argument. |
| SOUNDEX | Text | Text | Returns a four-character code to evaluate the similarity of two expressions. |
| CONCAT | Text, Text | Text | Adds two or more strings together. |
| UNICODE | Text | Number | Return an integer value (the Unicode value), for the first character of the input expression. |

### String functions samples

```sql
Expand All @@ -45,6 +47,7 @@ SELECT RIGHT("AmrDeveloper", 3) AS extract
SELECT TRANSLATE("Amr[Dev]{eloper}", "[]{}", "()()")
SELECT SOUNDEX("AmrDeveloper") as code
SELECT CONCAT("amrdeveloper", ".github.io")
SELECT UNICODE("AmrDeveloper")
```

### Date functions
Expand Down

0 comments on commit 1d0e851

Please sign in to comment.