gitql_core/
environment.rsuse std::collections::HashMap;
use gitql_ast::types::base::DataType;
use crate::schema::Schema;
use crate::signature::AggregationFunction;
use crate::signature::Signature;
use crate::signature::StandardFunction;
use crate::signature::WindowFunction;
use crate::types_table::TypesTable;
use crate::values::base::Value;
pub struct Environment {
pub schema: Schema,
pub std_signatures: HashMap<&'static str, Signature>,
pub std_functions: HashMap<&'static str, StandardFunction>,
pub aggregation_signatures: HashMap<&'static str, Signature>,
pub aggregation_functions: HashMap<&'static str, AggregationFunction>,
pub window_signatures: HashMap<&'static str, Signature>,
pub window_functions: HashMap<&'static str, WindowFunction>,
pub globals: HashMap<String, Box<dyn Value>>,
pub globals_types: HashMap<String, Box<dyn DataType>>,
pub scopes: HashMap<String, Box<dyn DataType>>,
pub types_table: TypesTable,
}
impl Environment {
pub fn new(schema: Schema) -> Self {
Self {
schema,
std_signatures: HashMap::default(),
std_functions: HashMap::default(),
aggregation_signatures: HashMap::default(),
aggregation_functions: HashMap::default(),
window_signatures: HashMap::default(),
window_functions: HashMap::default(),
globals: HashMap::default(),
globals_types: HashMap::default(),
scopes: HashMap::default(),
types_table: TypesTable::new(),
}
}
pub fn with_standard_functions(
&mut self,
signatures: &HashMap<&'static str, Signature>,
functions: &HashMap<&'static str, StandardFunction>,
) {
self.std_signatures.extend(signatures.to_owned());
self.std_functions.extend(functions.to_owned());
}
pub fn with_aggregation_functions(
&mut self,
signatures: &HashMap<&'static str, Signature>,
aggregation: &HashMap<&'static str, AggregationFunction>,
) {
self.aggregation_signatures.extend(signatures.to_owned());
self.aggregation_functions.extend(aggregation.to_owned());
}
pub fn with_window_functions(
&mut self,
signatures: &HashMap<&'static str, Signature>,
aggregation: &HashMap<&'static str, WindowFunction>,
) {
self.window_signatures.extend(signatures.to_owned());
self.window_functions.extend(aggregation.to_owned());
}
pub fn with_types_table(&mut self, types_table: TypesTable) {
self.types_table = types_table
}
pub fn is_std_function(&self, str: &str) -> bool {
self.std_functions.contains_key(str)
}
pub fn std_signature(&self, str: &str) -> Option<&Signature> {
self.std_signatures.get(str)
}
pub fn std_function(&self, str: &str) -> Option<&StandardFunction> {
self.std_functions.get(str)
}
pub fn is_aggregation_function(&self, str: &str) -> bool {
self.aggregation_signatures.contains_key(str)
}
pub fn aggregation_signature(&self, str: &str) -> Option<&Signature> {
self.aggregation_signatures.get(str)
}
pub fn aggregation_function(&self, str: &str) -> Option<&AggregationFunction> {
self.aggregation_functions.get(str)
}
pub fn is_window_function(&self, str: &str) -> bool {
self.window_functions.contains_key(str)
}
pub fn window_function_signature(&self, str: &str) -> Option<&Signature> {
self.window_signatures.get(str)
}
pub fn window_function(&self, str: &str) -> Option<&WindowFunction> {
self.window_functions.get(str)
}
pub fn define(&mut self, str: String, data_type: Box<dyn DataType>) {
self.scopes.insert(str, data_type);
}
pub fn define_global(&mut self, str: String, data_type: Box<dyn DataType>) {
self.globals_types.insert(str, data_type);
}
pub fn contains(&self, str: &String) -> bool {
self.scopes.contains_key(str) || self.globals_types.contains_key(str)
}
#[allow(clippy::borrowed_box)]
pub fn resolve_type(&self, str: &String) -> Option<&Box<dyn DataType>> {
if str.starts_with('@') {
return self.globals_types.get(str);
}
self.scopes.get(str)
}
pub fn clear_session(&mut self) {
self.scopes.clear()
}
}