gitql_core/
environment.rs1use std::collections::HashMap;
2
3use gitql_ast::types::DataType;
4
5use crate::schema::Schema;
6use crate::signature::AggregationFunction;
7use crate::signature::Signature;
8use crate::signature::StandardFunction;
9use crate::signature::WindowFunction;
10use crate::types_table::TypesTable;
11use crate::values::Value;
12
13pub struct Environment {
16 pub schema: Schema,
18
19 pub std_signatures: HashMap<&'static str, Signature>,
21
22 pub std_functions: HashMap<&'static str, StandardFunction>,
24
25 pub aggregation_signatures: HashMap<&'static str, Signature>,
27
28 pub aggregation_functions: HashMap<&'static str, AggregationFunction>,
30
31 pub window_signatures: HashMap<&'static str, Signature>,
33
34 pub window_functions: HashMap<&'static str, WindowFunction>,
36
37 pub globals: HashMap<String, Box<dyn Value>>,
39
40 pub globals_types: HashMap<String, Box<dyn DataType>>,
42
43 pub scopes: HashMap<String, Box<dyn DataType>>,
45
46 pub types_table: TypesTable,
48}
49
50impl Environment {
51 pub fn new(schema: Schema) -> Self {
53 Self {
54 schema,
55 std_signatures: HashMap::default(),
56 std_functions: HashMap::default(),
57 aggregation_signatures: HashMap::default(),
58 aggregation_functions: HashMap::default(),
59 window_signatures: HashMap::default(),
60 window_functions: HashMap::default(),
61 globals: HashMap::default(),
62 globals_types: HashMap::default(),
63 scopes: HashMap::default(),
64 types_table: TypesTable::new(),
65 }
66 }
67
68 pub fn with_standard_functions(
70 &mut self,
71 signatures: &HashMap<&'static str, Signature>,
72 functions: &HashMap<&'static str, StandardFunction>,
73 ) {
74 self.std_signatures.extend(signatures.to_owned());
75 self.std_functions.extend(functions.to_owned());
76 }
77
78 pub fn with_aggregation_functions(
80 &mut self,
81 signatures: &HashMap<&'static str, Signature>,
82 aggregation: &HashMap<&'static str, AggregationFunction>,
83 ) {
84 self.aggregation_signatures.extend(signatures.to_owned());
85 self.aggregation_functions.extend(aggregation.to_owned());
86 }
87
88 pub fn with_window_functions(
90 &mut self,
91 signatures: &HashMap<&'static str, Signature>,
92 window: &HashMap<&'static str, WindowFunction>,
93 ) {
94 self.window_signatures.extend(signatures.to_owned());
95 self.window_functions.extend(window.to_owned());
96 }
97
98 pub fn with_types_table(&mut self, types_table: TypesTable) {
100 self.types_table = types_table
101 }
102
103 pub fn is_std_function(&self, str: &str) -> bool {
105 self.std_functions.contains_key(str)
106 }
107
108 pub fn std_signature(&self, str: &str) -> Option<&Signature> {
110 self.std_signatures.get(str)
111 }
112
113 pub fn std_function(&self, str: &str) -> Option<&StandardFunction> {
115 self.std_functions.get(str)
116 }
117
118 pub fn is_aggregation_function(&self, str: &str) -> bool {
120 self.aggregation_signatures.contains_key(str)
121 }
122
123 pub fn aggregation_signature(&self, str: &str) -> Option<&Signature> {
125 self.aggregation_signatures.get(str)
126 }
127
128 pub fn aggregation_function(&self, str: &str) -> Option<&AggregationFunction> {
130 self.aggregation_functions.get(str)
131 }
132
133 pub fn is_window_function(&self, str: &str) -> bool {
135 self.window_functions.contains_key(str)
136 }
137
138 pub fn window_function_signature(&self, str: &str) -> Option<&Signature> {
140 self.window_signatures.get(str)
141 }
142
143 pub fn window_function(&self, str: &str) -> Option<&WindowFunction> {
145 self.window_functions.get(str)
146 }
147
148 pub fn define(&mut self, str: String, data_type: Box<dyn DataType>) {
150 self.scopes.insert(str, data_type);
151 }
152
153 pub fn define_global(&mut self, str: String, data_type: Box<dyn DataType>) {
155 self.globals_types.insert(str, data_type);
156 }
157
158 pub fn contains(&self, str: &String) -> bool {
160 self.scopes.contains_key(str) || self.globals_types.contains_key(str)
161 }
162
163 #[allow(clippy::borrowed_box)]
165 pub fn resolve_type(&self, str: &String) -> Option<&Box<dyn DataType>> {
166 if str.starts_with('@') {
167 return self.globals_types.get(str);
168 }
169 self.scopes.get(str)
170 }
171
172 pub fn clear_session(&mut self) {
174 self.scopes.clear()
175 }
176}