This repository was archived by the owner on Jun 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmod.rs
More file actions
117 lines (109 loc) · 3.84 KB
/
Copy pathmod.rs
File metadata and controls
117 lines (109 loc) · 3.84 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
mod nodes;
mod state;
mod types;
pub use {
nodes::Node,
state::{FinalParserState, ParserState},
};
#[cfg(test)]
mod tests {
use crate::parser::{nodes::CompleteCombinedResultNode, ParserState};
fn get_combined_result_nodes(query: &str)
-> Vec<CompleteCombinedResultNode> {
let owned = query.to_string();
let mut parser = ParserState::initialize(&owned);
parser.parse().unwrap();
let finalized = parser.finalize().unwrap();
finalized.combined_result_nodes.clone()
}
#[test]
fn no_nodes_from_empty_string() {
let nodes = get_combined_result_nodes("");
assert_eq!(0, nodes.len());
}
#[test]
fn no_nodes_from_sql_query() {
let nodes = get_combined_result_nodes("SELECT * FROM somewhere;");
assert_eq!(0, nodes.len());
}
#[test]
fn node_found() {
let query = "
SELECT * FROM
(
combined_result (SELECT col_a1 FROM table_a) AS $id_a {
SELECT a.col_a1, a.col_a2, b.col_b1, b.col_b2 FROM table_a a
INNER JOIN table_b b
ON b.col_a1 = a.col_a1 AND b.cond1 = %s AND b.cond2 = %s
WHERE a.col_a1 = $id_a
}
)
";
let nodes = get_combined_result_nodes(query);
assert_eq!(
vec![
CompleteCombinedResultNode::new(
57,
371,
"SELECT col_a1 FROM table_a".to_string(),
"$id_a".to_string(),
111,
"SELECT a.col_a1, a.col_a2, b.col_b1, b.col_b2 FROM table_a a
INNER JOIN table_b b
ON b.col_a1 = a.col_a1 AND b.cond1 = %s AND b.cond2 = %s
WHERE a.col_a1 = $id_a".to_string(),
),
],
nodes,
);
}
#[test]
fn nodes_found() {
let query = "
SELECT * FROM
(
combined_result (SELECT col_a1 FROM table_a) AS $id_a {
SELECT a.col_a1, a.col_a2, b.col_b1, b.col_b2 FROM table_a a
INNER JOIN table_b b
ON b.col_a1 = a.col_a1 AND b.cond1 = %s AND b.cond2 = %s
WHERE a.col_a1 = $id_a
}
UNION ALL
combined_result (SELECT col_z1 FROM table_z) AS $id_z {
SELECT z.col_z1, z.col_z2, b.col_b1, b.col_b2 FROM table_z z
INNER JOIN table_b b
ON b.col_z1 = z.col_z1 AND b.cond3 = ? AND b.cond4 = ?
WHERE z.col_z1 = $id_z
}
)
";
let nodes = get_combined_result_nodes(query);
assert_eq!(
vec![
CompleteCombinedResultNode::new(
57,
371,
"SELECT col_a1 FROM table_a".to_string(),
"$id_a".to_string(),
111,
"SELECT a.col_a1, a.col_a2, b.col_b1, b.col_b2 FROM table_a a
INNER JOIN table_b b
ON b.col_a1 = a.col_a1 AND b.cond1 = %s AND b.cond2 = %s
WHERE a.col_a1 = $id_a".to_string()
),
CompleteCombinedResultNode::new(
415,
727,
"SELECT col_z1 FROM table_z".to_string(),
"$id_z".to_string(),
469,
"SELECT z.col_z1, z.col_z2, b.col_b1, b.col_b2 FROM table_z z
INNER JOIN table_b b
ON b.col_z1 = z.col_z1 AND b.cond3 = ? AND b.cond4 = ?
WHERE z.col_z1 = $id_z".to_string()
),
],
nodes,
);
}
}