Skip to content

Commit a969a78

Browse files
committedJun 13, 2024·
feat: Implement FileQL DSL files_count function
1 parent 8c4a71c commit a969a78

File tree

4 files changed

+63
-6
lines changed

4 files changed

+63
-6
lines changed
 

‎CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Change Log
22
==========
33

4+
Version 0.5.0 *(2024-06-13)*
5+
-----------------------------
6+
7+
* Upgrade to GitQL SDk 0.23.0
8+
* Implement `files_count` function.
9+
410
Version 0.4.0 *(2024-06-04)*
511
-----------------------------
612

‎README.md

+9
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ SELECT "File Query Language" LIKE "%Query%"
3838
SELECT * FROM files
3939
SELECT COUNT(path) from files
4040
SELECT DISTINCT parent AS folders FROM files
41+
SELECT CHILDREN_COUNT(parent) FROM files
4142
```
4243

4344
---
@@ -55,6 +56,14 @@ SELECT DISTINCT parent AS folders FROM files
5556

5657
---
5758

59+
### Files QL Functions
60+
61+
| Name | Parameters | Return | Description |
62+
| ----------- | ---------- | ------- | ------------------------------------------------------------ |
63+
| FILES_COUNT | Text | Integer | Number of children for directory or 0 if it's not valid path |
64+
65+
---
66+
5867
### Download or Install
5968

6069
- Install from Cargo.io

‎src/functions.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use gitql_core::signature::Function;
2+
use gitql_core::signature::Signature;
3+
use gitql_core::types::DataType;
4+
use gitql_core::value::Value;
5+
use gitql_std::function::standard_function_signatures;
6+
use gitql_std::function::standard_functions;
7+
use std::collections::HashMap;
8+
use std::sync::OnceLock;
9+
10+
pub fn fileql_std_functions() -> &'static HashMap<&'static str, Function> {
11+
static HASHMAP: OnceLock<HashMap<&'static str, Function>> = OnceLock::new();
12+
HASHMAP.get_or_init(|| {
13+
let mut map: HashMap<&'static str, Function> =
14+
HashMap::from(standard_functions().to_owned());
15+
map.insert("files_count", files_count);
16+
map
17+
})
18+
}
19+
20+
pub fn fileql_std_signatures() -> &'static HashMap<&'static str, Signature> {
21+
static HASHMAP: OnceLock<HashMap<&'static str, Signature>> = OnceLock::new();
22+
HASHMAP.get_or_init(|| {
23+
let mut map: HashMap<&'static str, Signature> =
24+
HashMap::from(standard_function_signatures().to_owned());
25+
map.insert(
26+
"files_count",
27+
Signature {
28+
parameters: vec![DataType::Text],
29+
return_type: DataType::Integer,
30+
},
31+
);
32+
map
33+
})
34+
}
35+
36+
fn files_count(values: &[Value]) -> Value {
37+
let path = values[0].as_text();
38+
if let Ok(entries) = std::fs::read_dir(path) {
39+
let count = entries.flatten().count();
40+
return Value::Integer(count as i64);
41+
}
42+
Value::Integer(0)
43+
}

‎src/main.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@ use gitql_parser::parser;
1818
use gitql_parser::tokenizer;
1919
use gitql_std::aggregation::aggregation_function_signatures;
2020
use gitql_std::aggregation::aggregation_functions;
21-
use gitql_std::function::standard_function_signatures;
22-
use gitql_std::function::standard_functions;
2321
use schema::tables_fields_names;
2422
use schema::tables_fields_types;
2523

2624
mod arguments;
2725
mod data_provider;
26+
mod functions;
2827
mod schema;
2928

3029
fn main() {
@@ -49,8 +48,8 @@ fn main() {
4948
tables_fields_types: tables_fields_types().to_owned(),
5049
};
5150

52-
let std_signatures = standard_function_signatures();
53-
let std_functions = standard_functions();
51+
let std_signatures = functions::fileql_std_signatures();
52+
let std_functions = functions::fileql_std_functions();
5453

5554
let aggregation_signatures = aggregation_function_signatures();
5655
let aggregation_functions = aggregation_functions();
@@ -86,8 +85,8 @@ fn launch_fileql_repl(arguments: Arguments) {
8685
tables_fields_types: tables_fields_types().to_owned(),
8786
};
8887

89-
let std_signatures = standard_function_signatures();
90-
let std_functions = standard_functions();
88+
let std_signatures = functions::fileql_std_signatures();
89+
let std_functions = functions::fileql_std_functions();
9190

9291
let aggregation_signatures = aggregation_function_signatures();
9392
let aggregation_functions = aggregation_functions();

0 commit comments

Comments
 (0)
Please sign in to comment.