pub struct RBatis {
pub pool: Arc<OnceLock<Box<dyn Pool>>>,
pub intercepts: Arc<SyncVec<Arc<dyn Intercept>>>,
}
Expand description
RBatis engine
Fields§
§pool: Arc<OnceLock<Box<dyn Pool>>>
§intercepts: Arc<SyncVec<Arc<dyn Intercept>>>
Implementations§
source§impl RBatis
impl RBatis
sourcepub async fn link<Driver: Driver + 'static>(
&self,
driver: Driver,
url: &str,
) -> Result<(), Error>
pub async fn link<Driver: Driver + 'static>( &self, driver: Driver, url: &str, ) -> Result<(), Error>
self.init(driver, url)? and self.try_acquire().await? a connection. DefaultPool use FastPool
sourcepub fn init<Driver: Driver + 'static>(
&self,
driver: Driver,
url: &str,
) -> Result<(), Error>
pub fn init<Driver: Driver + 'static>( &self, driver: Driver, url: &str, ) -> Result<(), Error>
init pool. The default connection pool only binds one type of database driver, please use separate RBatis for different database drivers DefaultPool is FastPool,if you want other pool please use init_option
sourcepub fn init_option<Driver: Driver + 'static, ConnectOptions: ConnectOptions, Pool: Pool + 'static>(
&self,
driver: Driver,
option: ConnectOptions,
) -> Result<(), Error>
pub fn init_option<Driver: Driver + 'static, ConnectOptions: ConnectOptions, Pool: Pool + 'static>( &self, driver: Driver, option: ConnectOptions, ) -> Result<(), Error>
init pool by DBPoolOptions and Pool for example:
use rbatis::{DefaultPool, RBatis};
use rbdc_sqlite::{SqliteConnectOptions, SqliteDriver};
let rb=RBatis::new();
let opts=SqliteConnectOptions::new();
let _ = rb.init_option::<SqliteDriver, SqliteConnectOptions, DefaultPool>(SqliteDriver{},opts);
pub fn init_pool<Pool: Pool + 'static>(&self, pool: Pool) -> Result<(), Error>
pub fn init_opt<Driver: Driver + 'static, ConnectOptions: ConnectOptions>( &self, driver: Driver, options: ConnectOptions, ) -> Result<(), Error>
sourcepub fn set_intercepts(&mut self, arg: Vec<Arc<dyn Intercept>>)
pub fn set_intercepts(&mut self, arg: Vec<Arc<dyn Intercept>>)
set_intercepts for many
sourcepub fn get_pool(&self) -> Result<&dyn Pool, Error>
pub fn get_pool(&self) -> Result<&dyn Pool, Error>
get conn pool
can set option for example:
use rbatis::RBatis;
#[tokio::main]
async fn main(){
let rb = RBatis::new();
rb.init(rbdc_sqlite::driver::SqliteDriver{},"sqlite://target/sqlite.db").unwrap();
rb.get_pool().unwrap().set_max_open_conns(10).await;
}
sourcepub fn driver_type(&self) -> Result<&str, Error>
pub fn driver_type(&self) -> Result<&str, Error>
get driver type
sourcepub async fn acquire(&self) -> Result<RBatisConnExecutor, Error>
pub async fn acquire(&self) -> Result<RBatisConnExecutor, Error>
get an DataBase Connection used for the next step
sourcepub async fn try_acquire(&self) -> Result<RBatisConnExecutor, Error>
pub async fn try_acquire(&self) -> Result<RBatisConnExecutor, Error>
try get an DataBase Connection used for the next step
sourcepub async fn try_acquire_timeout(
&self,
d: Duration,
) -> Result<RBatisConnExecutor, Error>
pub async fn try_acquire_timeout( &self, d: Duration, ) -> Result<RBatisConnExecutor, Error>
try get an DataBase Connection used for the next step
sourcepub async fn acquire_begin(&self) -> Result<RBatisTxExecutor, Error>
pub async fn acquire_begin(&self) -> Result<RBatisTxExecutor, Error>
get an DataBase Connection,and call begin method,used for the next step
sourcepub async fn try_acquire_begin(&self) -> Result<RBatisTxExecutor, Error>
pub async fn try_acquire_begin(&self) -> Result<RBatisTxExecutor, Error>
try get an DataBase Connection,and call begin method,used for the next step
sourcepub fn is_debug_mode(&self) -> bool
pub fn is_debug_mode(&self) -> bool
is debug mode
sourcepub fn get_intercept_dyn(&self, name: &str) -> Option<&dyn Intercept>
pub fn get_intercept_dyn(&self, name: &str) -> Option<&dyn Intercept>
get intercept from name
the default name just like let name = std::any::type_name::<LogInterceptor>()
use std::sync::Arc;
use async_trait::async_trait;
use rbatis::RBatis;
use rbatis::intercept::{Intercept};
#[derive(Debug)]
pub struct MockIntercept {
}
#[async_trait]
impl Intercept for MockIntercept {
}
//use get_intercept_type
let mut rb = RBatis::new();
rb.set_intercepts(vec![Arc::new(MockIntercept{})]);
let name = std::any::type_name::<MockIntercept>();
let intercept = rb.get_intercept_dyn(name);
sourcepub fn get_intercept<T: Intercept>(&self) -> Option<&T>
pub fn get_intercept<T: Intercept>(&self) -> Option<&T>
get intercept from name
use std::sync::Arc;
use async_trait::async_trait;
use rbatis::RBatis;
use rbatis::intercept::{Intercept};
#[derive(Debug)]
pub struct MockIntercept {
}
#[async_trait]
impl Intercept for MockIntercept {
}
//use get_intercept_type
let mut rb = RBatis::new();
rb.set_intercepts(vec![Arc::new(MockIntercept{})]);
let intercept = rb.get_intercept::<MockIntercept>();
sourcepub async fn sync<T: Serialize>(
executor: &dyn Executor,
column_mapper: &dyn ColumMapper,
table: &T,
table_name: &str,
) -> Result<(), Error>
pub async fn sync<T: Serialize>( executor: &dyn Executor, column_mapper: &dyn ColumMapper, table: &T, table_name: &str, ) -> Result<(), Error>
create table if not exists, add column if not exists
use rbatis::executor::Executor;
use rbatis::RBatis;
use rbatis::table_sync::{SqliteTableMapper};
/// let rb = RBatis::new();
/// let conn = rb.acquire().await;
pub async fn do_sync_table(conn: &dyn Executor){
let map = rbs::to_value!{
"id":"INT",
"name":"TEXT",
};
let _ = RBatis::sync(conn,&SqliteTableMapper{},&map,"user").await;
}
sync table struct
use rbatis::executor::Executor;
use rbatis::RBatis;
use rbatis::table_sync::{SqliteTableMapper};
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct User{
pub id:String,
pub name: Option<String>
}
/// let rb = RBatis::new();
/// let conn = rb.acquire().await;
pub async fn do_sync_table(conn: &dyn Executor){
let table = User{id: "".to_string(), name: Some("".to_string())};
let _ = RBatis::sync(conn,&SqliteTableMapper{},&table,"user").await;
}
sync table struct (custom string column type)
use rbatis::executor::Executor;
use rbatis::RBatis;
use rbatis::table_sync::{MysqlTableMapper};
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct User{
pub id:String,
pub name: Option<String>
}
/// let rb = RBatis::new();
/// let conn = rb.acquire().await;
pub async fn do_sync_table_mysql(conn: &dyn Executor){
//empty string: auto create type, "VARCHAR(50)" -> sqlite type
let table = User{id: "".to_string(), name: Some("VARCHAR(50)".to_string())};
let _ = RBatis::sync(conn,&MysqlTableMapper{},&table,"user").await;
}
Trait Implementations§
Auto Trait Implementations§
impl Freeze for RBatis
impl !RefUnwindSafe for RBatis
impl Send for RBatis
impl Sync for RBatis
impl Unpin for RBatis
impl !UnwindSafe for RBatis
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)