1use crate::db::db_adapter::DataDecoder;
2use crate::db::DriverType;
3use crate::Result;
4use rbson::Bson;
5
6pub trait StmtConvert {
8 fn stmt_convert(&self, index: usize, item: &mut String);
9}
10
11#[macro_export]
12macro_rules! push_index {
13 ($n:expr,$new_sql:ident,$index:expr) => {{
14 let num = $index / $n;
15 $new_sql.push((num + 48) as u8 as char);
16 $index % $n
17 }};
18 ($index:ident,$new_sql:ident) => {
19 if $index >= 0 && $index < 10 {
20 $new_sql.push(($index + 48) as u8 as char);
21 } else if $index >= 10 && $index < 100 {
22 let $index = push_index!(10, $new_sql, $index);
23 let $index = push_index!(1, $new_sql, $index);
24 } else if $index >= 100 && $index < 1000 {
25 let $index = push_index!(100, $new_sql, $index);
26 let $index = push_index!(10, $new_sql, $index);
27 let $index = push_index!(1, $new_sql, $index);
28 } else if $index >= 1000 && $index < 10000 {
29 let $index = push_index!(1000, $new_sql, $index);
30 let $index = push_index!(100, $new_sql, $index);
31 let $index = push_index!(10, $new_sql, $index);
32 let $index = push_index!(1, $new_sql, $index);
33 } else {
34 use std::fmt::Write;
35 $new_sql
36 .write_fmt(format_args!("{}", $index))
37 .expect("a Display implementation returned an error unexpectedly");
38 }
39 };
40}
41
42impl StmtConvert for DriverType {
43 fn stmt_convert(&self, index: usize, item: &mut String) {
44 match &self {
45 DriverType::Postgres => {
46 item.push('$');
47 let index = index + 1;
48 push_index!(index, item);
49 }
50 DriverType::Mysql => {
51 item.push('?');
52 }
53 DriverType::Sqlite => {
54 item.push('?');
55 }
56 DriverType::Mssql => {
57 item.push('@');
58 item.push('p');
59 let index = index + 1;
60 push_index!(index, item);
61 }
62 DriverType::None => {
63 panic!("[mybatis] un support none for driver type!")
64 }
65 }
66 }
67}
68
69pub trait JsonCodec {
71 fn try_to_bson(self) -> Result<Bson>;
73}
74
75pub trait RefJsonCodec {
77 fn try_to_bson(&self, decoder: &dyn DataDecoder) -> Result<Bson>;
79}
80
81pub trait ResultCodec<T> {
83 fn into_result(self) -> Result<T>;
84}
85
86#[macro_export]
87macro_rules! to_bson_macro {
88 ($r:ident) => {{
89 if $r.is_some() {
90 rbson::bson!($r.unwrap())
91 } else {
92 rbson::Bson::Null
93 }
94 }};
95}