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
pub mod sqlite_table_sync;
pub use sqlite_table_sync::*;
use crate::executor::RBatisConnExecutor;
use crate::utils::string_util::to_snake_name;
use crate::Error;
use rbdc::db::Connection;
use rbs::{to_value, Value};
use serde::Serialize;
use std::any::Any;
use std::collections::HashMap;
use std::ops::{Deref, DerefMut};
use futures_core::future::BoxFuture;
pub trait TableSync {
fn sync(&self, rb: RBatisConnExecutor, table: Value, name: &str) -> BoxFuture<Result<(), Error>>;
}
pub struct RbatisTableSync {
pub plugins: HashMap<String, Box<dyn TableSync>>,
}
impl Deref for RbatisTableSync {
type Target = HashMap<String, Box<dyn TableSync>>;
fn deref(&self) -> &Self::Target {
&self.plugins
}
}
impl DerefMut for RbatisTableSync {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.plugins
}
}
impl RbatisTableSync {
pub fn new() -> Self {
Self {
plugins: Default::default(),
}
}
pub async fn sync<Table: Serialize + Any>(
&self,
driver_type: &str,
rb: RBatisConnExecutor,
table: Table,
) -> Result<(), Error> {
let plugin = self.plugins.get(driver_type);
match plugin {
None => Err(Error::from("not support or load plugin!")),
Some(plugin) => {
let name = std::any::type_name::<Table>();
let struct_name = name.split("::").last().unwrap_or_default();
let struct_name = to_snake_name(struct_name);
log::info!("sync table_name:{}", struct_name);
plugin.sync(rb, to_value!(table), &struct_name).await
}
}
}
pub async fn sync_with_table_name<Table: Serialize + Any>(
&self,
driver_type: &str,
rb: RBatisConnExecutor,
table: Table,
table_name: &str,
) -> Result<(), Error> {
let plugin = self.plugins.get(driver_type);
match plugin {
None => Err(Error::from("not support or load plugin!")),
Some(plugin) => {
log::info!("sync table_name:{}", table_name);
plugin.sync(rb, to_value!(table), table_name).await
}
}
}
}