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 82 83 84 85 86 87 88
use crate::executor::Executor;
use crate::Error;
use async_trait::async_trait;
use rbdc::db::ExecResult;
use rbs::Value;
use std::fmt::Debug;
#[derive(Debug, Clone)]
pub enum ResultType<A, B> {
/// Exec type
Exec(/* #[pin] */ A),
/// Query type
Query(/* #[pin] */ B),
}
impl<A, B> ResultType<A, B> {
pub fn type_name<'a, 'b>(&'a self) -> &'b str {
match self {
ResultType::Exec(_) => "exec",
ResultType::Query(_) => "query",
}
}
}
/// sql intercept
/// example:
///
/// ```rust
/// use rbatis::Error;
/// use rbatis::executor::Executor;
/// use rbatis::intercept::{Intercept, ResultType};
/// use rbdc::db::ExecResult;
/// use rbs::Value;
///
/// #[derive(Debug)]
/// pub struct ReturningIdPlugin{}
///
/// #[rbatis::async_trait]
/// impl Intercept for ReturningIdPlugin {
/// async fn before(
/// &self,
/// _task_id: i64,
/// rb: &dyn Executor,
/// sql: &mut String,
/// args: &mut Vec<Value>,
/// result: ResultType<&mut Result<ExecResult, Error>, &mut Result<Vec<Value>, Error>>,
/// ) -> Result<Option<bool>, Error> {
/// Ok(Some(true))
/// }
/// }
/// ```
#[async_trait]
pub trait Intercept: Send + Sync + Debug {
fn name(&self) -> &str {
std::any::type_name::<Self>()
}
/// task_id maybe is conn_id or tx_id,
/// is_prepared_sql = !args.is_empty(),
///
/// if return None will be return result
/// if return Some(true) will be run next intercept
/// if return Some(false) will be break
async fn before(
&self,
_task_id: i64,
_rb: &dyn Executor,
_sql: &mut String,
_args: &mut Vec<Value>,
_result: ResultType<&mut Result<ExecResult, Error>, &mut Result<Vec<Value>, Error>>,
) -> Result<Option<bool>, Error> {
Ok(Some(true))
}
/// task_id maybe is conn_id or tx_id,
/// is_prepared_sql = !args.is_empty(),
/// if return Ok(false) will be return data. return Ok(true) will run next
async fn after(
&self,
_task_id: i64,
_rb: &dyn Executor,
_sql: &mut String,
_args: &mut Vec<Value>,
_result: ResultType<&mut Result<ExecResult, Error>, &mut Result<Vec<Value>, Error>>,
) -> Result<Option<bool>, Error> {
Ok(Some(true))
}
}