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
use std::collections::HashMap;
use std::collections::linked_list::LinkedList;
use std::sync::RwLock;
use serde_json::Value;
use crate::engine::node::Node;
use crate::engine::parser::parse;
lazy_static! {
static ref EXPR_CACHE: RwLock<HashMap<String, Node>> = RwLock::new(HashMap::new());
}
#[derive(Clone, Debug)]
pub struct RbatisEngine {
pub opt_map: OptMap<'static>,
}
impl RbatisEngine {
pub fn new() -> Self {
return Self {
opt_map: OptMap::new(),
};
}
pub fn eval(&self, expr: &str, arg: &Value) -> Result<Value, rbatis_core::Error> {
let mut lexer_arg = expr.to_string();
if expr.find(" and ").is_some() {
lexer_arg = lexer_arg.replace(" and ", " && ");
}
let cached = self.cache_read(lexer_arg.as_str());
if cached.is_none() {
let nodes = parse(lexer_arg.as_str(), &self.opt_map);
if nodes.is_err() {
return Result::Err(nodes.err().unwrap());
}
let node = nodes.unwrap();
self.cache_insert(lexer_arg.to_string(), node.clone());
return node.eval(arg);
} else {
let nodes = cached.unwrap();
return nodes.eval(arg);
}
}
fn cache_read(&self, arg: &str) -> Option<Node> {
let cache_read = EXPR_CACHE.try_read();
if cache_read.is_err() {
return Option::None;
}
let cache_read = cache_read.unwrap();
let r = cache_read.get(arg);
return if r.is_none() {
Option::None
} else {
r.cloned()
};
}
fn cache_insert(&self, key: String, node: Node) -> Result<(), rbatis_core::Error> {
let cache_write = EXPR_CACHE.try_write();
if cache_write.is_err() {
return Err(rbatis_core::Error::from(cache_write.err().unwrap().to_string()));
}
let mut cache_write = cache_write.unwrap();
cache_write.insert(key, node);
return Ok(());
}
pub fn eval_no_cache(&self, lexer_arg: &str, arg: &Value) -> Result<Value, rbatis_core::Error> {
let nodes = parse(lexer_arg, &self.opt_map);
if nodes.is_err() {
return Result::Err(nodes.err().unwrap());
}
let node = nodes.unwrap();
return node.eval(arg);
}
}
pub fn is_number(arg: &String) -> bool {
let chars = arg.chars();
for item in chars {
if item == '.' ||
item == '0' ||
item == '1' ||
item == '2' ||
item == '3' ||
item == '4' ||
item == '5' ||
item == '6' ||
item == '7' ||
item == '8' ||
item == '9'
{
} else {
return false;
}
}
return true;
}
pub fn parser_tokens(s: &String, opt_map: &OptMap) -> Vec<String> {
let chars = s.chars();
let chars_len = s.len() as i32;
let mut result = LinkedList::new();
let mut find_str = false;
let mut temp_str = String::new();
let mut temp_arg = String::new();
let mut index: i32 = -1;
for item in chars {
index = index + 1;
let is_opt = opt_map.is_opt(item.to_string().as_str());
if item == '\'' || item == '`' {
if find_str {
find_str = false;
temp_str.push(item);
trim_push_back(&temp_str, &mut result);
temp_str.clear();
continue;
}
find_str = true;
temp_str.push(item);
continue;
}
if find_str {
temp_str.push(item);
continue;
}
if item != '`' && item != '\'' && is_opt == false && !find_str {
temp_arg.push(item);
if (index + 1) == chars_len {
trim_push_back(&temp_arg, &mut result);
}
} else {
trim_push_back(&temp_arg, &mut result);
temp_arg.clear();
}
if is_opt {
if result.len() > 0 {
let def = String::new();
let back = result.back().unwrap_or(&def).clone();
if back != "" && opt_map.is_opt(back.as_str()) {
result.pop_back();
let mut new_item = back.clone().to_string();
new_item.push(item);
trim_push_back(&new_item, &mut result);
continue;
}
}
trim_push_back(&item.to_string(), &mut result);
continue;
}
}
let mut v = vec![];
for item in result {
v.push(item);
}
return v;
}
fn trim_push_back(arg: &String, list: &mut LinkedList<String>) {
let trim_str = arg.trim().to_string();
if trim_str.is_empty() {
return;
}
list.push_back(trim_str);
}
#[derive(Clone, Debug)]
pub struct OptMap<'a> {
pub list: Vec<&'a str>,
pub map: HashMap<&'a str, bool>,
pub mul_ops_map: HashMap<&'a str, bool>,
pub single_opt_map: HashMap<&'a str, bool>,
pub allow_priority_array: Vec<&'a str>,
}
impl<'a> OptMap<'a> {
pub fn new() -> Self {
let mut list = Vec::new();
let mut def_map = HashMap::new();
let mut mul_ops_map = HashMap::new();
let mut single_opt_map = HashMap::new();
list.push("*");
list.push("/");
list.push("%");
list.push("^");
list.push("+");
list.push("-");
list.push("(");
list.push(")");
list.push("@");
list.push("#");
list.push("$");
list.push("&");
list.push("|");
list.push("=");
list.push("!");
list.push(">");
list.push("<");
list.push("&&");
list.push("||");
list.push("==");
list.push("!=");
list.push(">=");
list.push("<=");
for item in &mut list {
def_map.insert(*item, true);
}
for item in &mut list {
if item.len() > 1 {
mul_ops_map.insert(item.clone(), true);
} else {
single_opt_map.insert(item.clone(), true);
}
}
let mut vecs = vec![];
vecs.push("*");
vecs.push("/");
vecs.push("+");
vecs.push("-");
vecs.push("<=");
vecs.push("<");
vecs.push(">=");
vecs.push(">");
vecs.push("!=");
vecs.push("==");
vecs.push("&&");
vecs.push("||");
Self {
list: list,
map: def_map,
mul_ops_map: mul_ops_map,
single_opt_map: single_opt_map,
allow_priority_array: vecs,
}
}
pub fn priority_array(&self) -> Vec<&str> {
return self.allow_priority_array.clone();
}
pub fn is_opt(&self, arg: &str) -> bool {
let opt = self.map.get(arg);
return opt.is_none() == false;
}
pub fn is_allow_opt(&self, arg: &str) -> bool {
for item in &self.allow_priority_array {
if arg == *item {
return true;
}
}
return false;
}
}