uorm 0.9.5

Rust 下的轻量级 ORM 框架,借鉴了 Java MyBatis 的设计理念,强调 SQL 与业务逻辑分离。它结合 Rust 的类型系统与宏机制,支持编写原生 SQL 并自动映射结果,兼容 async/await,兼顾性能与可控性。
Documentation
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
use crate::tpl::ast::{AstNode, Expr, Op};
use crate::udbc::value::Value;
use std::collections::HashMap;

/// 模板解析时用于处理嵌套标签的栈帧表示。
///
/// 遇到起始标签(如 `<if>`)时,会将新的 `TagFrame` 入栈,
/// 以便解析器跟踪当前标签的属性与嵌套层级。
enum TagFrame {
    /// `<if>` 标签栈帧,保存测试表达式。
    If { test: Expr },
    /// `<foreach>` 标签栈帧,保存迭代细节。
    Foreach {
        item: String,
        collection: String,
        open: String,
        separator: String,
        close: String,
    },
}

/// SQL 模板语言的手写递归下降解析器。
///
/// 支持:
/// - 纯文本(SQL)
/// - 变量插值:`#{var}`
/// - 条件逻辑:`<if test="...">...</if>`
/// - 迭代:`<foreach item="..." collection="..." ...>...</foreach>`
/// - 模板包含:`<include refid="..." />`
///
/// 解析器使用基于栈的方法正确处理嵌套标签。
struct Parser<'a> {
    /// 正在解析的原始模板字符串。
    template: &'a str,
    /// 当前在模板中的字符位置。
    pos: usize,
    /// 节点集合栈。每一层对应一个嵌套标签的子节点集合。
    /// 第一层始终是根节点集合。
    nodes_stack: Vec<Vec<AstNode>>,
    /// 正在解析的活动标签栈。
    tag_stack: Vec<TagFrame>,
}

impl<'a> Parser<'a> {
    /// 基于给定模板字符串创建解析器实例。
    fn new(template: &'a str) -> Self {
        Self {
            template,
            pos: 0,
            nodes_stack: vec![Vec::new()], // 使用根层初始化
            tag_stack: Vec::new(),
        }
    }

    /// 解析完整模板并返回根级 `AstNode` 列表。
    fn parse(mut self) -> Vec<AstNode> {
        while self.pos < self.template.len() {
            // 先尝试解析结构化元素(标签或变量)
            if self.try_parse_tag() || self.try_parse_var() {
                continue;
            }

            // 回退:若当前位置没有结构化元素则按纯文本解析
            self.parse_text();
        }

        // 关闭所有未闭合标签(如缺少 </if>)
        self.close_remaining_tags();

        // 返回根级节点
        self.nodes_stack.pop().unwrap_or_default()
    }

    /// 尝试解析标签:`<if>`、`</if>`、`<foreach>`、`</foreach>`、`<include>`。
    /// 若标签成功解析并消费则返回 true。
    fn try_parse_tag(&mut self) -> bool {
        let remaining = &self.template[self.pos..];

        if remaining.starts_with("</") {
            return self.handle_close_tag(remaining);
        }
        if remaining.starts_with("<if ") {
            return self.handle_if_tag(remaining);
        }
        if remaining.starts_with("<foreach ") {
            return self.handle_foreach_tag(remaining);
        }
        if remaining.starts_with("<include") {
            return self.handle_include_tag(remaining);
        }

        false
    }

    /// 处理 <if test="...">
    fn handle_if_tag(&mut self, remaining: &str) -> bool {
        if let Some(end_idx) = find_tag_end(remaining) {
            let tag_content = &remaining[4..end_idx]; // 跳过 "<if "
            let attrs = parse_attributes(tag_content);
            if let Some(test_str) = attrs.get("test") {
                let test = parse_expr(test_str);
                self.nodes_stack.push(Vec::new());
                self.tag_stack.push(TagFrame::If { test });
                self.pos += end_idx + 1;
                return true;
            }
        }
        false
    }

    /// 处理 <foreach item="..." collection="...">
    fn handle_foreach_tag(&mut self, remaining: &str) -> bool {
        if let Some(end_idx) = find_tag_end(remaining) {
            let tag_content = &remaining[9..end_idx]; // 跳过 "<foreach "
            let attrs = parse_attributes(tag_content);
            if let (Some(item), Some(collection)) = (attrs.get("item"), attrs.get("collection")) {
                let open = attrs.get("open").map(|s| s.as_str()).unwrap_or("");
                let separator = attrs.get("separator").map(|s| s.as_str()).unwrap_or(",");
                let close = attrs.get("close").map(|s| s.as_str()).unwrap_or("");

                self.nodes_stack.push(Vec::new());
                self.tag_stack.push(TagFrame::Foreach {
                    item: item.to_string(),
                    collection: collection.to_string(),
                    open: open.to_string(),
                    separator: separator.to_string(),
                    close: close.to_string(),
                });
                self.pos += end_idx + 1;
                return true;
            }
        }
        false
    }

    /// 处理 <include refid="..." />
    fn handle_include_tag(&mut self, remaining: &str) -> bool {
        if let Some(end_idx) = find_tag_end(remaining) {
            let tag_content = &remaining[8..end_idx]; // 跳过 "<include"
            let attrs = parse_attributes(tag_content);
            if let Some(refid) = attrs.get("refid") {
                self.append_node(AstNode::Include {
                    refid: refid.to_string(),
                });
                self.pos += end_idx + 1;
                return true;
            }
        }
        false
    }

    /// 处理结束标签 `</if>` 与 `</foreach>`。
    fn handle_close_tag(&mut self, remaining: &str) -> bool {
        if remaining.starts_with("</if>")
            && let Some(TagFrame::If { .. }) = self.tag_stack.last()
            && let Some(TagFrame::If { test }) = self.tag_stack.pop()
        {
            let mut body = self.nodes_stack.pop().unwrap_or_default();
            self.trim_text_nodes(&mut body);

            self.append_node(AstNode::If { test, body });
            self.pos += 5;
            return true;
        } else if remaining.starts_with("</foreach>")
            && let Some(TagFrame::Foreach { .. }) = self.tag_stack.last()
            && let Some(TagFrame::Foreach {
                item,
                collection,
                open,
                separator,
                close,
            }) = self.tag_stack.pop()
        {
            let mut body = self.nodes_stack.pop().unwrap_or_default();
            self.trim_text_nodes(&mut body);

            self.append_node(AstNode::Foreach {
                item,
                collection,
                open,
                separator,
                close,
                body,
            });
            self.pos += 10;
            return true;
        }
        false
    }

    fn trim_text_nodes(&self, nodes: &mut Vec<AstNode>) {
        if let Some(AstNode::Text(text)) = nodes.first_mut() {
            let trimmed = text.trim_start();
            let whitespace_len = text.len() - trimmed.len();
            let whitespace = &text[..whitespace_len];

            // 仅在空白包含换行时裁剪(块级格式)
            // 若仅为空格(行内格式)则保留
            if whitespace.contains('\n') {
                if trimmed.is_empty() {
                    nodes.remove(0);
                } else {
                    *text = trimmed.to_string();
                }
            }
        }

        // 可能删除后再检查末尾(len=1 时可能是同一节点)
        if let Some(AstNode::Text(text)) = nodes.last_mut() {
            let trimmed = text.trim_end();
            let whitespace = &text[trimmed.len()..];

            if whitespace.contains('\n') {
                if trimmed.is_empty() {
                    nodes.pop();
                } else {
                    *text = trimmed.to_string();
                }
            }
        }
    }

    /// 尝试解析变量表达式:`#{var}`。
    fn try_parse_var(&mut self) -> bool {
        let remaining = &self.template[self.pos..];
        if remaining.starts_with("#{")
            && let Some(end) = remaining.find('}')
        {
            let var_name = remaining[2..end].trim();
            if !var_name.is_empty() {
                self.append_node(AstNode::Var(var_name.to_string()));
                self.pos += end + 1;
                return true;
            }
        }
        false
    }

    /// 消费文本直到下一个特殊序列(`'<'` 或 `"#{"`)。
    fn parse_text(&mut self) {
        let remaining = &self.template[self.pos..];
        let next_tag = remaining.find('<').unwrap_or(remaining.len());
        let next_var = remaining.find("#{").unwrap_or(remaining.len());
        let next_stop = std::cmp::min(next_tag, next_var);

        if next_stop > 0 {
            self.append_text(&remaining[..next_stop]);
            self.pos += next_stop;
        } else {
            // 要么未找到标签,要么处于未解析成功的标签/变量边界
            // 消费一个字符以推进并避免死循环
            self.append_text(&remaining[0..1]);
            self.pos += 1;
        }
    }

    /// 向当前活动作用域追加节点。
    fn append_node(&mut self, node: AstNode) {
        if let Some(nodes) = self.nodes_stack.last_mut() {
            nodes.push(node);
        }
    }

    /// 追加文本,尽可能与上一个文本节点合并。
    fn append_text(&mut self, text: &str) {
        if let Some(nodes) = self.nodes_stack.last_mut() {
            if let Some(AstNode::Text(last_text)) = nodes.last_mut() {
                last_text.push_str(text);
            } else {
                nodes.push(AstNode::Text(text.to_string()));
            }
        }
    }

    /// 在模板末尾自动闭合所有未闭合标签。
    fn close_remaining_tags(&mut self) {
        while let Some(tag) = self.tag_stack.pop() {
            let mut body = self.nodes_stack.pop().unwrap_or_default();
            self.trim_text_nodes(&mut body);

            let node = match tag {
                TagFrame::If { test } => AstNode::If { test, body },
                TagFrame::Foreach {
                    item,
                    collection,
                    open,
                    separator,
                    close,
                } => AstNode::Foreach {
                    item,
                    collection,
                    open,
                    separator,
                    close,
                    body,
                },
            };
            self.append_node(node);
        }
    }
}

/// 主入口:将模板字符串解析为 AST。
pub fn parse_template(template: &str) -> Vec<AstNode> {
    Parser::new(template).parse()
}

/// 查找标签闭合 `>` 的索引,忽略引号内内容。
fn find_tag_end(s: &str) -> Option<usize> {
    let mut in_quote = false;
    for (i, c) in s.char_indices() {
        if c == '"' {
            in_quote = !in_quote;
        } else if c == '>' && !in_quote {
            return Some(i);
        }
    }
    None
}

/// 从标签内容解析属性为 HashMap
fn parse_attributes(content: &str) -> HashMap<String, String> {
    let mut attrs = HashMap::new();

    let mut rest = content;
    while !rest.is_empty() {
        rest = rest.trim_start();
        if rest.is_empty() {
            break;
        }

        // 查找 key 结束位置
        let key_end = rest
            .find(|c: char| !c.is_alphanumeric() && c != '_' && c != '-')
            .unwrap_or(rest.len());
        if key_end == 0 {
            // 若 trim_start 正常且字符有效,理论上不会发生
            // 若存在脏数据,消费一个字符以避免死循环
            rest = &rest[1..];
            continue;
        }
        let key = &rest[..key_end];
        rest = rest[key_end..].trim_start();

        // 期待 '='
        if !rest.starts_with('=') {
            continue;
        }
        rest = rest[1..].trim_start();

        // 期待引号
        if rest.is_empty() {
            break;
        }
        let quote = rest.chars().next().unwrap();
        if quote != '"' && quote != '\'' {
            continue;
        }
        rest = &rest[1..];

        // 查找匹配的引号
        if let Some(val_end) = rest.find(quote) {
            let val = &rest[..val_end];
            attrs.insert(key.to_string(), val.to_string());
            rest = &rest[val_end + 1..];
        } else {
            break; // 引号未闭合
        }
    }
    attrs
}

fn parse_expr(input: &str) -> Expr {
    // 1. 按 OR 拆分
    let parts: Vec<&str> = input.split(" or ").collect();
    if parts.len() > 1 {
        let mut expr = parse_and_expr(parts[0]);
        for part in &parts[1..] {
            expr = Expr::Binary(Op::Or, Box::new(expr), Box::new(parse_and_expr(part)));
        }
        return expr;
    }
    parse_and_expr(input)
}

fn parse_and_expr(input: &str) -> Expr {
    let parts: Vec<&str> = input.split(" and ").collect();
    if parts.len() > 1 {
        let mut expr = parse_atom(parts[0]);
        for part in &parts[1..] {
            expr = Expr::Binary(Op::And, Box::new(expr), Box::new(parse_atom(part)));
        }
        return expr;
    }
    parse_atom(input)
}

fn parse_atom(input: &str) -> Expr {
    let input = input.trim();
    // 检查运算符,顺序很重要(最长优先)
    let ops = [
        ("!=", Op::Ne),
        ("==", Op::Eq),
        (">=", Op::Ge),
        ("<=", Op::Le),
        (">", Op::Gt),
        ("<", Op::Lt),
    ];

    for (sym, op) in ops {
        if let Some((left, right)) = input.split_once(sym) {
            return Expr::Binary(op, Box::new(parse_val(left)), Box::new(parse_val(right)));
        }
    }

    // 隐式布尔判断
    parse_val(input)
}

fn parse_val(input: &str) -> Expr {
    let s = input.trim();
    if s == "null" {
        return Expr::Literal(Value::Null);
    }
    if s == "true" {
        return Expr::Literal(Value::Bool(true));
    }
    if s == "false" {
        return Expr::Literal(Value::Bool(false));
    }
    if (s.starts_with('\'') && s.ends_with('\'')) || (s.starts_with('"') && s.ends_with('"')) {
        return Expr::Literal(Value::Str(s[1..s.len() - 1].to_string()));
    }
    if let Ok(n) = s.parse::<i64>() {
        return Expr::Literal(Value::I64(n));
    }
    if let Ok(n) = s.parse::<f64>() {
        return Expr::Literal(Value::F64(n));
    }
    // 变量
    Expr::Var(s.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_simple_text() {
        let tpl = "hello world";
        let nodes = parse_template(tpl);
        assert_eq!(nodes.len(), 1);
        match &nodes[0] {
            AstNode::Text(t) => assert_eq!(t, "hello world"),
            _ => panic!("Expected Text"),
        }
    }

    #[test]
    fn test_parse_merged_text() {
        let tpl = "hello < world";
        let nodes = parse_template(tpl);
        assert_eq!(nodes.len(), 1);
        match &nodes[0] {
            AstNode::Text(t) => assert_eq!(t, "hello < world"),
            _ => panic!("Expected Text"),
        }
    }

    #[test]
    fn test_parse_var() {
        let tpl = "hello #{name}!";
        let nodes = parse_template(tpl);
        assert_eq!(nodes.len(), 3);
        match &nodes[0] {
            AstNode::Text(t) => assert_eq!(t, "hello "),
            _ => panic!(),
        }
        match &nodes[1] {
            AstNode::Var(v) => assert_eq!(v, "name"),
            _ => panic!(),
        }
        match &nodes[2] {
            AstNode::Text(t) => assert_eq!(t, "!"),
            _ => panic!(),
        }
    }

    #[test]
    fn test_parse_if() {
        let tpl = r#"<if test="a > 1">content</if>"#;
        let nodes = parse_template(tpl);
        assert_eq!(nodes.len(), 1);
        match &nodes[0] {
            AstNode::If { test, body } => {
                match test {
                    Expr::Binary(Op::Gt, left, right) => {
                        assert_eq!(**left, Expr::Var("a".to_string()));
                        assert_eq!(**right, Expr::Literal(Value::I64(1)));
                    }
                    _ => panic!("Expected Binary expression, got {:?}", test),
                }
                assert_eq!(body.len(), 1);
                match &body[0] {
                    AstNode::Text(t) => assert_eq!(t, "content"),
                    _ => panic!(),
                }
            }
            _ => panic!("Expected If"),
        }
    }

    #[test]
    fn test_parse_nested() {
        let tpl = r#"<if test="x"><foreach item="i" collection="list">#{i}</foreach></if>"#;
        let nodes = parse_template(tpl);
        assert_eq!(nodes.len(), 1);
        match &nodes[0] {
            AstNode::If { body, .. } => {
                assert_eq!(body.len(), 1);
                match &body[0] {
                    AstNode::Foreach { item, body, .. } => {
                        assert_eq!(item, "i");
                        assert_eq!(body.len(), 1);
                    }
                    _ => panic!("Expected Foreach"),
                }
            }
            _ => panic!("Expected If"),
        }
    }

    #[test]
    fn test_auto_close() {
        let tpl = r#"<if test="x">content"#;
        let nodes = parse_template(tpl);
        assert_eq!(nodes.len(), 1);
        match &nodes[0] {
            AstNode::If { test, body } => {
                match test {
                    Expr::Var(v) => assert_eq!(v, "x"),
                    _ => panic!("Expected Var"),
                }
                assert_eq!(body.len(), 1);
                match &body[0] {
                    AstNode::Text(t) => assert_eq!(t, "content"),
                    _ => panic!(),
                }
            }
            _ => panic!("Expected If"),
        }
    }

    #[test]
    fn test_malformed_tags() {
        let tpl = r#"<if test="x"> <unknown> #{ unclosed"#;
        let nodes = parse_template(tpl);
        assert_eq!(nodes.len(), 1);
        match &nodes[0] {
            AstNode::If { body, .. } => {
                assert_eq!(body.len(), 1);
                match &body[0] {
                    AstNode::Text(t) => assert_eq!(t, " <unknown> #{ unclosed"),
                    _ => panic!("Expected Text, got {:?}", body[0]),
                }
            }
            _ => panic!("Expected If"),
        }
    }
}