Skip to content

Commit

Permalink
Merge pull request #18 from Lilit0x/functions/ascii
Browse files Browse the repository at this point in the history
ASCII String Function: #13
  • Loading branch information
AmrDeveloper authored Sep 7, 2023
2 parents f7178d3 + 95ed671 commit 1637c7e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
16 changes: 16 additions & 0 deletions crates/gitql-ast/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ lazy_static! {
map.insert("ltrim", text_left_trim);
map.insert("rtrim", text_right_trim);
map.insert("len", text_len);
map.insert("ascii", text_ascii);
map
};
}
Expand Down Expand Up @@ -94,6 +95,13 @@ lazy_static! {
result: DataType::Number,
},
);
map.insert(
"ascii",
Prototype {
parameters: vec![DataType::Text],
result: DataType::Number,
},
);
map
};
}
Expand Down Expand Up @@ -136,3 +144,11 @@ fn text_right_trim(inputs: Vec<Value>) -> Value {
fn text_len(inputs: Vec<Value>) -> Value {
return Value::Number(inputs[0].as_text().len() as i64);
}

fn text_ascii(inputs: Vec<Value>) -> Value {
let text = inputs[0].as_text();
if text.is_empty() {
return Value::Number(0);
}
return Value::Number(text.chars().nth(0).unwrap() as i64);
}
2 changes: 2 additions & 0 deletions docs/function/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ note that all functions names are case-insensitive.
| LEN | Text | Number | Return the length of this string. |
| 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. |

### String functions samples

Expand All @@ -23,4 +24,5 @@ SELECT * FROM commits where REVERSE(name) = "repolevedrma"
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
```

0 comments on commit 1637c7e

Please sign in to comment.