Skip to content

Commit

Permalink
Merge pull request #62 from mobley-trent/charidx
Browse files Browse the repository at this point in the history
feat(string functions): CHARINDEX function
  • Loading branch information
AmrDeveloper authored Dec 20, 2023
2 parents d2b6ced + 6db894c commit b446caa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
19 changes: 19 additions & 0 deletions crates/gitql-ast/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ lazy_static! {
map.insert("datalength", text_datalength);
map.insert("char", text_char);
map.insert("nchar", text_char);
map.insert("charindex", text_charindex);
map.insert("replace", text_replace);
map.insert("substring", text_substring);
map.insert("stuff", text_stuff);
Expand Down Expand Up @@ -173,6 +174,13 @@ lazy_static! {
result: DataType::Text,
},
);
map.insert(
"charindex",
Prototype {
parameters: vec![DataType::Text, DataType::Text],
result: DataType::Integer,
}
);
map.insert(
"replace",
Prototype {
Expand Down Expand Up @@ -478,6 +486,17 @@ fn text_char(inputs: Vec<Value>) -> Value {
Value::Text("".to_string())
}

fn text_charindex(inputs: Vec<Value>) -> Value {
let substr = inputs[0].as_text();
let input = inputs[1].as_text();

if let Some(index) = input.to_lowercase().find(&substr.to_lowercase()) {
Value::Integer(index as i64 + 1)
} else {
Value::Integer(0)
}
}

fn text_replace(inputs: Vec<Value>) -> Value {
let text = inputs[0].as_text();
let old_string = inputs[1].as_text();
Expand Down
2 changes: 2 additions & 0 deletions docs/function/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ note that all functions names are case-insensitive.
| LEFT | Text, Integer | Text | Extracts a number of characters from a string (starting from left). |
| DATALENGTH | Text | Integer | Returns the number of bytes used to represent an expression. |
| CHAR | Integer | Text | Returns the character based on the ASCII code. |
| CHARINDEX | Text, Text | Integer | Returns the starting position of the first occurrence of a string in another string. |
| NCHAR | Integer | Text | Returns the character based on the ASCII code. |
| REPLACE | Text, Text, Text | Text | Replaces all occurrences of a substring within a string, with a new substring. |
| SUBSTRING | Text, Integer, Integer | Text | Extracts some characters from a string. |
Expand All @@ -41,6 +42,7 @@ SELECT name, ASCII(name) AS firstCharAscii FROM commits
SELECT LEFT("AmrDeveloper", 3) AS extract
SELECT DATALENGTH("AmrDeveloper") as bytelength
SELECT CHAR(345) AS code
SELECT CHARINDEX("DEV", "AmrDeveloper") AS position
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!")
Expand Down

0 comments on commit b446caa

Please sign in to comment.