Skip to content

Commit 38e9b6f

Browse files
committed
Add support for interval - operator.
1 parent 5c4445f commit 38e9b6f

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

crates/gitql-ast/src/interval.rs

+11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ impl Interval {
2121
result.seconds += interval.seconds;
2222
result
2323
}
24+
25+
pub fn sub(&self, interval: &Interval) -> Interval {
26+
let mut result = self.clone();
27+
result.years -= interval.years;
28+
result.months -= interval.months;
29+
result.days -= interval.days;
30+
result.hours -= interval.hours;
31+
result.minutes -= interval.minutes;
32+
result.seconds -= interval.seconds;
33+
result
34+
}
2435
}
2536

2637
impl fmt::Display for Interval {

crates/gitql-ast/src/types/interval.rs

+8
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,12 @@ impl DataType for IntervalType {
2525
fn add_op_result_type(&self, _other: &Box<dyn DataType>) -> Box<dyn DataType> {
2626
Box::new(IntervalType)
2727
}
28+
29+
fn can_perform_sub_op_with(&self) -> Vec<Box<dyn DataType>> {
30+
vec![Box::new(IntervalType)]
31+
}
32+
33+
fn sub_op_result_type(&self, _other: &Box<dyn DataType>) -> Box<dyn DataType> {
34+
Box::new(IntervalType)
35+
}
2836
}

crates/gitql-core/src/values/interval.rs

+8
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,12 @@ impl Value for IntervalValue {
4949
}
5050
Err("Unexpected type to perform `+` with".to_string())
5151
}
52+
53+
fn sub_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
54+
if let Some(other_interval) = other.as_any().downcast_ref::<IntervalValue>() {
55+
let result = self.interval.sub(&other_interval.interval);
56+
return Ok(Box::new(IntervalValue::new(result)));
57+
}
58+
Err("Unexpected type to perform `-` with".to_string())
59+
}
5260
}

docs/index.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ SELECT 1 + 2
2424
SELECT LEN("Git Query Language")
2525
SELECT "One" IN ("One", "Two", "Three")
2626
SELECT "Git Query Language" LIKE "%Query%"
27+
SELECT INTERVAL '1 year 2 mons 3 days 04:05:06.789'
2728

2829
SET @arr = [1, 2, 3];
2930
SELECT [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
@@ -32,25 +33,27 @@ SELECT @arr[1:2], @arr[2:], @arr[:2];
3233

3334
SELECT DISTINCT title AS tt FROM commits
3435
SELECT author_name, COUNT(author_name) AS commit_num FROM commits GROUP BY author_name, author_email ORDER BY commit_num DESC LIMIT 10
35-
SELECT commit_count FROM branches WHERE commit_count BETWEEN 0 .. 10
36+
SELECT commit_count FROM branches WHERE commit_count BETWEEN 0 AND 10
3637

3738
SELECT * FROM refs WHERE type = "branch"
3839
SELECT * FROM refs ORDER BY type
3940

4041
SELECT * FROM commits
4142
SELECT author_name, author_email FROM commits
4243
SELECT author_name, author_email FROM commits ORDER BY author_name DESC, author_email ASC
43-
SELECT author_name, author_email FROM commits WHERE name LIKE "%gmail%" ORDER BY author_name
44-
SELECT * FROM commits WHERE LOWER(name) = "amrdeveloper"
44+
SELECT author_name, author_email FROM commits WHERE author_email LIKE "%gmail%" ORDER BY author_name
45+
SELECT * FROM commits WHERE LOWER(author_name) = "amrdeveloper"
4546
SELECT author_name FROM commits GROUP By author_name
46-
SELECT author_name FROM commits GROUP By author_name having author_name = "AmrDeveloper"
47+
SELECT author_name FROM commits GROUP By author_name HAVING author_name = "AmrDeveloper"
4748

4849
SELECT * FROM branches
4950
SELECT * FROM branches WHERE is_head = true
5051
SELECT name, LEN(name) FROM branches
5152

5253
SELECT * FROM tags
5354
SELECT * FROM tags OFFSET 1 LIMIT 1
55+
56+
SELECT path, count() AS changes_count, SUM(insertions) AS additions, SUM(removals) AS removes FROM diffs_changes GROUP BY path ORDER BY changes_count DESC
5457
```
5558

5659
---

0 commit comments

Comments
 (0)