Skip to content

Commit

Permalink
Merge pull request #85 from frectonz/master
Browse files Browse the repository at this point in the history
add `quotename` string function #13
  • Loading branch information
AmrDeveloper authored Feb 26, 2024
2 parents c8fc4fb + 09f3c9e commit 7531749
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
23 changes: 23 additions & 0 deletions crates/gitql-ast/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ lazy_static! {
map.insert("concat_ws", text_concat_ws);
map.insert("unicode", text_unicode);
map.insert("strcmp", text_strcmp);
map.insert("quotename", text_quotename);

// Date functions
map.insert("current_date", date_current_date);
Expand Down Expand Up @@ -260,6 +261,13 @@ lazy_static! {
},
);
map.insert("strcmp", Prototype { parameters: vec![DataType::Text, DataType::Text], result: DataType::Integer });
map.insert(
"quotename",
Prototype {
parameters: vec![DataType::Text, DataType::Optional(Box::new(DataType::Text))],
result: DataType::Text
}
);

// Date functions
map.insert(
Expand Down Expand Up @@ -752,6 +760,21 @@ fn text_strcmp(inputs: &[Value]) -> Value {
})
}

fn text_quotename(inputs: &[Value]) -> Value {
let str = inputs[0].as_text();
let quote = inputs
.get(1)
.map(Value::as_text)
.map(|str| str.chars().collect())
.unwrap_or_else(|| vec!['[', ']']);

match quote.as_slice() {
[single] => Value::Text(format!("{single}{str}{single}")),
[start, end] => Value::Text(format!("{start}{str}{end}")),
_ => Value::Null,
}
}

// Date functions

fn date_current_date(_inputs: &[Value]) -> Value {
Expand Down
6 changes: 5 additions & 1 deletion docs/function/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ note that all functions names are case-insensitive.
| CONCAT_WS | Text, Any, Any, ...Any | Text | Add several string representations of values together together with separate. |
| UNICODE | Text | Integer | Return an integer value (the Unicode value), for the first character of the input expression. |
| STRCMP | Text , Text | Integer | Return 0 If string1 = string2, -1 if string1 < string2, this function returns -1, and 1 if string1 > string2 |
| QUOTENAME | Text , Text | Text | Returns the string (first argument) with specified delimiters (second argument), defaulting to [] |

### String functions samples

Expand All @@ -54,6 +55,9 @@ SELECT SOUNDEX("AmrDeveloper") as code
SELECT CONCAT("amrdeveloper", ".github.io")
SELECT CONCAT_WS("_", "Git", "Query", "Language");
SELECT UNICODE("AmrDeveloper")
SELECT QUOTENAME("AmrDeveloper")
SELECT QUOTENAME("AmrDeveloper", ".")
SELECT QUOTENAME("AmrDeveloper", "{}")
```

### Date functions
Expand Down Expand Up @@ -139,4 +143,4 @@ SELECT ISNUMERIC(null), ISNUMERIC(1), ISNUMERIC(1.1), ISNUMERIC(false)
SELECT TYPEOF(""), TYPEOF(1), TYPEOF(null)
SELECT GREATEST(1, 2, 3, 4)
SELECT LEAST(1, 2, 3, 4)
```
```

0 comments on commit 7531749

Please sign in to comment.