-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathlib.rs
More file actions
218 lines (190 loc) · 5.47 KB
/
lib.rs
File metadata and controls
218 lines (190 loc) · 5.47 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
extern crate nom_sql;
use std::fs::File;
use std::io::Read;
use std::path::Path;
fn parse_queryset(queries: Vec<String>) -> (i32, i32) {
let mut parsed_ok = Vec::new();
let mut parsed_err = 0;
for query in queries.iter() {
println!("Trying to parse '{}': ", &query);
match nom_sql::parser::parse_query(&query) {
Ok(_) => {
println!("ok");
parsed_ok.push(query);
}
Err(_) => {
println!("failed");
parsed_err += 1;
}
}
}
println!("\nParsing failed: {} queries", parsed_err);
println!("Parsed successfully: {} queries", parsed_ok.len());
println!("\nSuccessfully parsed queries:");
for q in parsed_ok.iter() {
println!("{:?}", q);
}
(parsed_ok.len() as i32, parsed_err)
}
fn test_queries_from_file(f: &Path, name: &str) -> Result<i32, i32> {
let mut f = File::open(f).unwrap();
let mut s = String::new();
// Load queries
f.read_to_string(&mut s).unwrap();
let lines: Vec<String> = s
.lines()
.filter(|l| {
!l.is_empty()
&& !l.starts_with("#")
&& !l.starts_with("--")
&& !(l.starts_with("/*") && l.ends_with("*/;"))
})
.map(|l| {
if !(l.ends_with("\n") || l.ends_with(";")) {
String::from(l) + "\n"
} else {
String::from(l)
}
})
.collect();
println!("\nLoaded {} {} queries", lines.len(), name);
// Try parsing them all
let (ok, err) = parse_queryset(lines);
if err > 0 {
return Err(err);
}
Ok(ok)
}
fn parse_file(path: &str) -> (i32, i32) {
let mut f = File::open(Path::new(path)).unwrap();
let mut s = String::new();
// Load queries
f.read_to_string(&mut s).unwrap();
let lines: Vec<&str> = s
.lines()
.map(str::trim)
.filter(|l| {
!l.is_empty()
&& !l.starts_with("#")
&& !l.starts_with("--")
&& !l.starts_with("DROP")
&& !(l.starts_with("/*") && l.ends_with("*/;"))
})
.collect();
let mut q = String::new();
let mut queries = Vec::new();
for l in lines {
if !l.ends_with(";") {
q.push_str(l);
} else {
// end of query
q.push_str(l);
queries.push(q.clone());
q = String::new();
}
}
println!("Loaded {} table definitions", queries.len());
// Try parsing them all
parse_queryset(queries)
}
#[test]
#[ignore]
fn hotcrp_queries() {
assert!(test_queries_from_file(Path::new("tests/hotcrp-queries.txt"), "HotCRP").is_ok());
}
#[test]
fn hyrise_test_queries() {
assert!(test_queries_from_file(Path::new("tests/hyrise-test-queries.txt"), "HyRise").is_ok());
}
#[test]
fn tpcw_test_queries() {
assert!(test_queries_from_file(Path::new("tests/tpc-w-queries.txt"), "TPC-W").is_ok());
}
#[test]
fn tpcw_test_tables() {
let res = test_queries_from_file(Path::new("tests/tpc-w-tables.txt"), "TPC-W tables");
assert!(res.is_ok());
// There are 10 tables
assert_eq!(res.unwrap(), 10);
}
#[test]
fn exists_test_queries() {
let res = test_queries_from_file(
Path::new("tests/exists-queries.txt"),
"exists/not-exists queries",
);
assert!(res.is_ok());
// There are 4 queries
assert_eq!(res.unwrap(), 4);
}
#[test]
fn finkelstein82_test_queries() {
let res = test_queries_from_file(Path::new("tests/finkelstein82.txt"), "Finkelstein 1982");
assert!(res.is_ok());
// There are 3 tables and 6 queries
assert_eq!(res.unwrap(), 9);
}
#[test]
fn hotcrp_schema() {
let mut f = File::open(Path::new("tests/hotcrp-schema.txt")).unwrap();
let mut s = String::new();
// Load queries
f.read_to_string(&mut s).unwrap();
let lines: Vec<&str> = s
.lines()
.map(str::trim)
.filter(|l| {
!l.is_empty() && !l.starts_with("#") && !l.starts_with("--") && !l.starts_with("DROP")
})
.collect();
let mut q = String::new();
let mut queries = Vec::new();
for l in lines {
// remove inline comments, bah
let l = match l.find("#") {
None => l,
Some(pos) => &l[0..pos - 1],
};
if !l.ends_with(";") {
q.push_str(l);
} else {
// end of query
q.push_str(l);
queries.push(q.clone());
q = String::new();
}
}
println!("Loaded {} table definitions", queries.len());
// Try parsing them all
let (ok, fail) = parse_queryset(queries);
// There are 24 CREATE TABLE queries in the schema
assert_eq!(ok, 24);
assert_eq!(fail, 0);
}
#[test]
fn mediawiki_schema() {
let (ok, fail) = parse_file("tests/mediawiki-schema.txt");
// There are 17 CREATE TABLE queries in the schema
assert_eq!(ok, 17);
assert_eq!(fail, 0);
}
#[test]
fn parse_comments() {
let (ok, fail) = parse_file("tests/comments.txt");
// There are 2 CREATE TABLE queries in the schema
assert_eq!(ok, 2);
assert_eq!(fail, 0);
}
#[test]
fn parse_autoincrement() {
let (ok, fail) = parse_file("tests/autoincrement.txt");
// There is 1 CREATE TABLE queries in the schema
assert_eq!(ok, 1);
assert_eq!(fail, 0);
}
#[test]
fn parse_select() {
let (ok, fail) = parse_file("tests/select.txt");
assert_eq!(fail, 1);
assert_eq!(ok, 27);
}