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
/**
.
# Examples
```
use caisin::sqls::get_select_fields;
assert_eq!(get_select_fields("select `a`, b,`c` from "),vec!["a","b","c"] );
```
*/
pub fn get_select_fields(select_sql: &str) -> Vec<&str> {
let mut star_idx = 0;
let mut idx = 0;
let mut ret = vec![];
let s = select_sql.find("select ").unwrap();
let e = select_sql.find("from").unwrap();
let mut in_field = true;
let mut field = "";
for c in select_sql[s..e].chars() {
if !in_field && !field.is_empty() {
ret.push(field);
field = "";
}
match c {
' ' | ',' | '`' | '\r' | '\t' | '\n' => {
if star_idx == 0 {
star_idx = idx;
} else {
//两个空格
if idx - star_idx == 1 {
star_idx = idx;
} else {
field = &select_sql[(star_idx + 1)..idx];
star_idx = idx;
}
}
}
_ => {}
}
match c {
',' => {
in_field = false;
}
_ => in_field = true,
}
idx += 1;
}
if !field.is_empty() {
ret.push(field);
}
ret
}
#[test]
fn test_f() {
}