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