Struct rbatis::rbatis::RBatis

source ·
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

source

pub fn new() -> Self

create an RBatis add intercept use LogInterceptor

self.init(driver, url)? and self.try_acquire().await? a connection. DefaultPool use FastPool

source

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

source

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);
source

pub fn init_pool<Pool: Pool + 'static>(&self, pool: Pool) -> Result<(), Error>

source

pub fn init_opt<Driver: Driver + 'static, ConnectOptions: ConnectOptions>( &self, driver: Driver, options: ConnectOptions ) -> Result<(), Error>

👎Deprecated: please use init_option()
source

pub fn set_intercepts(&mut self, arg: Vec<Arc<dyn Intercept>>)

set_intercepts for many

source

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;
}
source

pub fn driver_type(&self) -> Result<&str, Error>

get driver type

source

pub async fn acquire(&self) -> Result<RBatisConnExecutor, Error>

get an DataBase Connection used for the next step

source

pub async fn try_acquire(&self) -> Result<RBatisConnExecutor, Error>

try get an DataBase Connection used for the next step

source

pub async fn try_acquire_timeout( &self, d: Duration ) -> Result<RBatisConnExecutor, Error>

try get an DataBase Connection used for the next step

source

pub async fn acquire_begin(&self) -> Result<RBatisTxExecutor, Error>

get an DataBase Connection,and call begin method,used for the next step

source

pub async fn try_acquire_begin(&self) -> Result<RBatisTxExecutor, Error>

try get an DataBase Connection,and call begin method,used for the next step

source

pub fn is_debug_mode(&self) -> bool

is debug mode

source

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);
source

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>();
source

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;
}
source§

impl RBatis

source

pub async fn exec( &self, sql: &str, args: Vec<Value> ) -> Result<ExecResult, Error>

exec sql

source

pub async fn query(&self, sql: &str, args: Vec<Value>) -> Result<Value, Error>

query raw Value

source

pub async fn query_decode<T>( &self, sql: &str, args: Vec<Value> ) -> Result<T, Error>

query and decode

Trait Implementations§

source§

impl Clone for RBatis

source§

fn clone(&self) -> RBatis

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for RBatis

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for RBatis

source§

fn default() -> RBatis

Returns the “default value” for a type. Read more
source§

impl Executor for RBatis

source§

fn exec( &self, sql: &str, args: Vec<Value> ) -> BoxFuture<'_, Result<ExecResult, Error>>

source§

fn query( &self, sql: &str, args: Vec<Value> ) -> BoxFuture<'_, Result<Value, Error>>

source§

fn name(&self) -> &str

source§

impl RBatisRef for &RBatis

source§

fn rb_ref(&self) -> &RBatis

source§

fn driver_type(&self) -> Result<&str>

source§

impl RBatisRef for RBatis

source§

fn rb_ref(&self) -> &RBatis

source§

fn driver_type(&self) -> Result<&str>

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V