Skip to content

Commit

Permalink
Merge pull request #32 from Lilit0x/functions/translate
Browse files Browse the repository at this point in the history
TRANSLATE String Function: #13
  • Loading branch information
AmrDeveloper authored Sep 13, 2023
2 parents 9207364 + af17dad commit a295ebc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
25 changes: 25 additions & 0 deletions crates/gitql-ast/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ lazy_static! {
map.insert("substring", text_substring);
map.insert("stuff", text_stuff);
map.insert("right", text_right);
map.insert("translate", text_translate);

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

// Date functions
map.insert(
Expand Down Expand Up @@ -347,6 +355,23 @@ fn text_right(inputs: Vec<Value>) -> Value {
return Value::Text(text[text.len() - number_of_chars..text.len()].to_string());
}

fn text_translate(inputs: Vec<Value>) -> Value {
let mut text = inputs[0].as_text();
let characters = inputs[1].as_text();
let translations = inputs[2].as_text();

if translations.len() != characters.len() {
return Value::Text("".to_string());
}

let translations = translations.chars().collect::<Vec<_>>();
for (idx, letter) in characters.char_indices() {
text = text.replace(letter, &char::to_string(&translations[idx]));
}

return Value::Text(text);
}

// Date functions

fn date_current_date(_inputs: Vec<Value>) -> Value {
Expand Down
2 changes: 1 addition & 1 deletion crates/gitql-ast/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub mod aggregation;
pub mod date_utils;
pub mod expression;
pub mod function;
pub mod object;
pub mod scope;
pub mod statement;
pub mod types;
pub mod value;
pub mod date_utils;
2 changes: 2 additions & 0 deletions docs/function/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ note that all functions names are case-insensitive.
| SUBSTRING | Text, Number, Number | Text | Extracts some characters from a string. |
| STUFF | Text, Number, Number, Text | Deletes a part of a string and then inserts another part into the string, starting at a specified position. |
| RIGHT | Text, Number | Text | Extracts a number of characters from a string (starting from right).|
| 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. |
### String functions samples

```sql
Expand All @@ -39,6 +40,7 @@ SELECT REPLACE("ABC ABC ABC", "a", "c") as replacedText
SELECT name, SUBSTRING(name, 1, 5) AS extract FROM commits
SELECT STUFF("GQL tutorial!", 13, 1, " is fun!")
SELECT RIGHT("AmrDeveloper", 3) AS extract
SELECT TRANSLATE("Amr[Dev]{eloper}", "[]{}", "()()")
```

### Date functions
Expand Down

0 comments on commit a295ebc

Please sign in to comment.