-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathmodel.rs
More file actions
333 lines (311 loc) · 14.7 KB
/
Copy pathmodel.rs
File metadata and controls
333 lines (311 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
use std::cell::RefCell;
use std::collections::HashMap;
use std::collections::VecDeque;
use std::rc::Rc;
use std::rc::Weak;
use lsp_types::MessageType;
use weak_table::PtrWeakHashSet;
use std::collections::HashSet;
use crate::constants::BuildStatus;
use crate::constants::BuildSteps;
use crate::constants::OYarn;
use crate::constants::SymType;
use crate::threads::SessionInfo;
use super::symbols::module_symbol::ModuleSymbol;
use super::symbols::symbol::Symbol;
#[derive(Debug)]
pub struct ModelData {
pub name: OYarn,
pub inherit: Vec<OYarn>,
pub inherits: Vec<(OYarn, OYarn)>,
pub description: String,
pub auto: bool,
pub log_access: bool,
pub table: String,
pub sequence: String,
pub sql_constraints: Vec<String>,
pub is_abstract: bool,
pub transient: bool,
pub rec_name: Option<String>,
pub order: String,
pub check_company_auto: bool,
pub parent_name: String,
pub active_name: Option<String>,
pub parent_store: bool,
pub data_name: String,
pub fold_name: String,
/// Key: compute function name, Value: field names that are computed by this function
pub computes: HashMap<OYarn, HashSet<OYarn>>,
}
impl ModelData {
pub fn new() -> Self {
Self {
name: OYarn::from(""),
inherit: Vec::new(),
inherits: Vec::new(),
description: String::new(),
auto: false,
log_access: false,
table: String::new(),
sequence: String::new(),
sql_constraints: Vec::new(),
is_abstract: false,
transient: false,
rec_name: None,
order: String::from("id"),
check_company_auto: false,
parent_name: String::from("parent_id"),
active_name: None,
parent_store: false,
data_name: String::from("date"),
fold_name: String::from("fold"),
computes: HashMap::new(),
}
}
}
#[derive(Debug)]
pub struct Model {
name: OYarn,
symbols: PtrWeakHashSet<Weak<RefCell<Symbol>>>,
pub dependents: PtrWeakHashSet<Weak<RefCell<Symbol>>>,
}
impl Model {
pub fn new(name: OYarn, symbol: Rc<RefCell<Symbol>>) -> Self {
let mut res = Self {
name,
symbols: PtrWeakHashSet::new(),
dependents: PtrWeakHashSet::new(),
};
res.symbols.insert(symbol);
res
}
pub fn add_symbol(&mut self, session: &mut SessionInfo, symbol: Rc<RefCell<Symbol>>) {
if self.symbols.contains(&symbol) {
return;
}
self.symbols.insert(symbol.clone());
let from_module = symbol.borrow().find_module();
self.add_dependents_to_validation(session, from_module);
}
pub fn remove_symbol(&mut self, session: &mut SessionInfo, symbol: &Rc<RefCell<Symbol>>, from_module: Option<Rc<RefCell<Symbol>>>) {
self.symbols.remove(symbol);
self.add_dependents_to_validation(session, from_module);
}
pub fn get_symbols(&self, session: &mut SessionInfo, from_module: Option<Rc<RefCell<Symbol>>>) -> Vec<Rc<RefCell<Symbol>>> {
let mut symbol = Vec::new();
for s in self.symbols.iter() {
let module = s.borrow().find_module().expect("Unreachable: Model should be declared in a module");
if from_module.is_none() || ModuleSymbol::is_in_deps(session, from_module.as_ref().unwrap(), &module.borrow().as_module_package().dir_name) {
symbol.push(s);
}
}
symbol
}
pub fn get_main_symbols(&self, session: &mut SessionInfo, from_module: Option<Rc<RefCell<Symbol>>>) -> Vec<Rc<RefCell<Symbol>>> {
let mut res: Vec<Rc<RefCell<Symbol>>> = vec![];
for sym in self.symbols.iter() {
if !sym.borrow().as_class_sym()._model.as_ref().unwrap().inherit.contains(&sym.borrow().as_class_sym()._model.as_ref().unwrap().name) {
if from_module.is_none() || sym.as_ref().borrow().find_module().is_none() {
res.push(sym);
} else {
let dir_name = sym.borrow().find_module().unwrap().borrow().as_module_package().dir_name.clone();
if ModuleSymbol::is_in_deps(session, from_module.as_ref().unwrap(), &dir_name) {
res.push(sym);
}
}
}
}
res
}
pub fn model_in_deps(&self, session: &mut SessionInfo, from_module: &Rc<RefCell<Symbol>>) -> bool {
for sym in self.symbols.iter() {
if !sym.borrow().as_class_sym()._model.as_ref().unwrap().inherit.contains(&sym.borrow().as_class_sym()._model.as_ref().unwrap().name) {
let dir_name = sym.borrow().find_module().unwrap().borrow().as_module_package().dir_name.clone();
if ModuleSymbol::is_in_deps(session, from_module, &dir_name) {
return true;
}
}
}
false
}
pub fn get_full_model_symbols(model_rc: Rc<RefCell<Model>>, session: &mut SessionInfo, from_module: Rc<RefCell<Symbol>>) -> PtrWeakHashSet<Weak<RefCell<Symbol>>> {
let mut symbol_set = PtrWeakHashSet::new();
let mut already_in = HashSet::new();
let mut queue = VecDeque::from([model_rc]);
while let Some(current_model_rc) = queue.pop_front(){
let current_model = current_model_rc.borrow();
let symbols = current_model.get_symbols(session, Some(from_module.clone()));
for symbol in symbols.iter() {
let sym_ref = symbol.borrow();
let Some(model_data) = &sym_ref.as_class_sym()._model else {continue};
for inherit in model_data.inherit.iter() {
if let Some(model) = session.sync_odoo.models.get(inherit).cloned() {
if !already_in.contains(&model.borrow().name) {
already_in.insert(model.borrow().name.clone());
queue.push_back(model.clone());
}
}
}
}
symbol_set.extend(symbols.into_iter());
}
symbol_set
}
pub fn get_inherits_models(&self, session: &mut SessionInfo, from_module: Option<Rc<RefCell<Symbol>>>) -> Vec<Rc<RefCell<Model>>> {
let mut res = vec![];
let mut already_in = HashSet::new();
if let Some(from_module) = from_module {
let symbols = self.get_symbols(session, Some(from_module));
for symbol in symbols {
if let Some(model_data) = &symbol.borrow().as_class_sym()._model {
for (model_name, _field) in model_data.inherits.iter() {
if let Some(model) = session.sync_odoo.models.get(model_name).cloned() {
if !already_in.contains(&model.borrow().name) {
res.push(model.clone());
already_in.insert(model.borrow().name.clone());
}
}
}
}
}
}
res
}
pub fn has_symbols(&mut self) -> bool {
self.symbols.remove_expired();
!self.symbols.is_empty()
}
/* Return all symbols that build this model.
It returns the symbol and an optional string that represents the module name that should be added to dependencies to be used.
if with_inheritance is true, it will also return symbols from inherited models (NOT Base classes).
*/
pub fn all_symbols(&self, session: &mut SessionInfo, from_module: Option<Rc<RefCell<Symbol>>>, with_inheritance: bool) -> Vec<(Rc<RefCell<Symbol>>, Option<OYarn>)> {
self.all_symbols_helper(session, from_module, with_inheritance, &mut HashSet::new())
}
fn all_symbols_helper(&self, session: &mut SessionInfo, from_module: Option<Rc<RefCell<Symbol>>>, with_inheritance: bool, seen_inherited_models: &mut HashSet<OYarn>) -> Vec<(Rc<RefCell<Symbol>>, Option<OYarn>)> {
let mut symbols = Vec::new();
for s in self.symbols.iter() {
if let Some(from_module) = from_module.as_ref() {
let module = s.borrow().find_module();
if let Some(module) = module {
if ModuleSymbol::is_in_deps(session, &from_module, &module.borrow().as_module_package().dir_name) {
symbols.push((s.clone(), None));
} else {
symbols.push((s.clone(), Some(module.borrow().as_module_package().dir_name.clone())));
}
} else {
session.log_message(MessageType::WARNING, "A model should be declared in a module.".to_string());
}
} else {
symbols.push((s.clone(), None));
}
if !with_inheritance {
continue;
}
let inherited_models = s.borrow().as_class_sym()._model.as_ref().unwrap().inherit.clone();
for inherited_model in inherited_models.iter() {
if !seen_inherited_models.contains(inherited_model) {
seen_inherited_models.insert(inherited_model.clone());
if let Some(model) = session.sync_odoo.models.get(inherited_model).cloned() {
symbols.extend(model.borrow().all_symbols_helper(session, from_module.clone(), true, seen_inherited_models));
}
}
}
}
symbols
}
pub fn all_symbols_inherits(&self, session: &mut SessionInfo, from_module: Option<Rc<RefCell<Symbol>>>) -> (Vec<(Rc<RefCell<Symbol>>, Option<OYarn>)>, Vec<(Rc<RefCell<Symbol>>, Option<OYarn>)>) {
let mut visited_models = HashSet::new();
self.all_inherits_helper(session, from_module, &mut visited_models)
}
fn all_inherits_helper(&self, session: &mut SessionInfo, from_module: Option<Rc<RefCell<Symbol>>>, visited_models: &mut HashSet<String>) -> (Vec<(Rc<RefCell<Symbol>>, Option<OYarn>)>, Vec<(Rc<RefCell<Symbol>>, Option<OYarn>)>) {
if visited_models.contains(&self.name.to_string()) {
return (Vec::new(), Vec::new());
}
visited_models.insert(self.name.to_string());
let mut symbols = Vec::new();
let mut inherits_symbols = Vec::new();
for s in self.symbols.iter() {
if let Some(from_module) = from_module.as_ref() {
let module = s.borrow().find_module();
if let Some(module) = module {
if ModuleSymbol::is_in_deps(session, &from_module, &module.borrow().as_module_package().dir_name) {
symbols.push((s.clone(), None));
} else {
symbols.push((s.clone(), Some(module.borrow().as_module_package().dir_name.clone())));
}
} else {
session.log_message(MessageType::WARNING, "A model should be declared in a module.".to_string());
}
} else {
symbols.push((s.clone(), None));
}
// First get results from normal inherit
// To make sure we visit all of inherit before inherits, since it is DFS
// Only inherits in the tree that are not already visited will be processed in the next iteration
let inherited_models = s.borrow().as_class_sym()._model.as_ref().unwrap().inherit.clone();
for inherited_model in inherited_models.iter() {
if let Some(model) = session.sync_odoo.models.get(inherited_model).cloned() {
let (main_result, inherits_result) = model.borrow().all_inherits_helper(session, from_module.clone(), visited_models);
symbols.extend(main_result);
inherits_symbols.extend(inherits_result);
}
}
for (inherits_model, _) in s.borrow().as_class_sym()._model.as_ref().unwrap().inherits.clone() {
if let Some(model) = session.sync_odoo.models.get(&inherits_model).cloned() {
let (main_result, inherits_result) = model.borrow().all_inherits_helper(session, from_module.clone(), visited_models);
// Everything that is in inherits should be added to inherits_symbols, regardless of whether
// it was in inherit or inherits. Since we need that distinction to later only get fields
inherits_symbols.extend(main_result);
inherits_symbols.extend(inherits_result);
}
}
}
(symbols, inherits_symbols)
}
pub fn add_dependent(&mut self, symbol: &Rc<RefCell<Symbol>>) {
self.dependents.insert(symbol.clone());
}
pub fn add_dependents_to_validation(&self, session: &mut SessionInfo, module_change: Option<Rc<RefCell<Symbol>>>) {
for dep in self.dependents.iter() {
dep.borrow_mut().invalidate_sub_functions(session);
let module = dep.borrow().find_module();
if module_change.is_none() || module.is_none() || ModuleSymbol::is_in_deps(session, &module.as_ref().unwrap(), &module_change.as_ref().unwrap().borrow().as_module_package().dir_name) {
let typ = dep.borrow().typ().clone();
match typ {
SymType::FUNCTION => {
dep.borrow_mut().set_build_status(BuildSteps::ARCH_EVAL, BuildStatus::PENDING);
session.sync_odoo.add_to_validations(dep.clone());
},
_ => {
session.sync_odoo.add_to_validations(dep.clone());
}
}
}
}
}
pub fn inherits_from(&self, session: &mut SessionInfo, base: &Rc<RefCell<Model>>) -> bool {
fn inner(this: &Model, session: &mut SessionInfo, base: &Rc<RefCell<Model>>, checked: &mut HashSet<OYarn>) -> bool {
if checked.contains(&this.name) {
return false;
}
checked.insert(this.name.clone());
for symbol in this.symbols.iter() {
let sym_ref = symbol.borrow();
let Some(model_data) = &sym_ref.as_class_sym()._model else {continue};
for inherit in model_data.inherit.iter() {
if inherit == &base.borrow().name {
return true;
}
if let Some(model) = session.sync_odoo.models.get(inherit).cloned() {
if inner(&model.borrow(), session, base, checked) {
return true;
}
}
}
}
false
}
inner(self, session, base, &mut HashSet::new())
}
}