mybatis_sql/
sql_replace.rs1#[macro_export]
2macro_rules! push_index {
3 ($n:expr,$new_sql:ident,$index:expr) => {{
4 let num = $index / $n;
5 $new_sql.push((num + 48) as u8 as char);
6 $index % $n
7 }};
8 ($index:ident,$new_sql:ident) => {
9 if $index >= 0 && $index < 10 {
10 $new_sql.push(($index + 48) as u8 as char);
11 } else if $index >= 10 && $index < 100 {
12 let $index = push_index!(10, $new_sql, $index);
13 let $index = push_index!(1, $new_sql, $index);
14 } else if $index >= 100 && $index < 1000 {
15 let $index = push_index!(100, $new_sql, $index);
16 let $index = push_index!(10, $new_sql, $index);
17 let $index = push_index!(1, $new_sql, $index);
18 } else if $index >= 1000 && $index < 10000 {
19 let $index = push_index!(1000, $new_sql, $index);
20 let $index = push_index!(100, $new_sql, $index);
21 let $index = push_index!(10, $new_sql, $index);
22 let $index = push_index!(1, $new_sql, $index);
23 } else {
24 use std::fmt::Write;
25 $new_sql
26 .write_fmt(format_args!("{}", $index))
27 .expect("a Display implementation returned an error unexpectedly");
28 }
29 };
30}
31
32#[macro_export]
33macro_rules! sql_index {
34 ($sql:ident,$format_char:expr) => {
35 let mut new_sql = String::with_capacity($sql.len() + 20);
36 let mut string_start = false;
37 let mut index: i32 = 0;
38 for x in $sql.chars() {
39 if x == '\'' || x == '"' {
40 if string_start == true {
41 string_start = false;
42 new_sql.push(x);
43 continue;
44 }
45 string_start = true;
46 new_sql.push(x);
47 continue;
48 }
49 if string_start {
50 new_sql.push(x);
51 } else {
52 if x == '?' && $format_char != '?' {
53 index += 1;
54 new_sql.push($format_char);
55 push_index!(index, new_sql);
56 } else {
57 new_sql.push(x);
58 }
59 }
60 }
61 $sql = new_sql
62 };
63}