rbatis

Rust Compile Time ORM robustness,async, pure Rust Dynamic SQL

APACHE-2.0 License

Downloads
1.1M
Stars
2.3K
Committers
25

Bot releases are hidden (Show)

rbatis - v4.5.4

Published by zhuxiujia 11 months ago

v4.5.4

  • add sync method for rbatis
    create table if not exists, add column if not exists

    use rbatis::RBatis;
    use rbatis::table_sync::{SqliteTableMapper};
    
     let rb = RBatis::new();
     let conn = rb.acquire().await;
    pub async fn do_sync_table(rb: &RBatis){
          let map = rbs::to_value!{
                "id":"INT",
                "name":"TEXT",
         };
         let _ = RBatis::sync(&rb,&SqliteTableMapper{},&map,"user").await;
    }
    
    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(rb: &RBatis){
         let table = User{id: "".to_string(), name: Some("".to_string())};
         let _ = RBatis::sync(&rb,&SqliteTableMapper{},&table,"user").await;
    }
    
    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(rb: &RBatis){
         let table = User{id: "".to_string(), name: Some("VARCHAR(50)".to_string())};
         let _ = RBatis::sync(&rb,&MysqlTableMapper{},&table,"user").await;
    }
    
rbatis - v4.5.3

Published by zhuxiujia 11 months ago

v4.5.3

  • fix for #463
rbatis - v4.5.2

Published by zhuxiujia 11 months ago

v4.5.2

  • rbatis remove rbdc fetaures
  • only driver need add features = ["tls-rustls"] or features = ["tls-native-tls"]

just like example

rbs = { version = "4.5" }
rbdc-sqlite = { version = "4.5", default-features = false, features = ["tls-native-tls"] }
#rbdc-mysql={version="4.5", default-features = false, features = ["tls-native-tls"]}
#rbdc-pg={version="4.5", default-features = false, features = ["tls-native-tls"]}
#rbdc-mssql={version="4.5", default-features = false, features = ["tls-native-tls"]}
rbatis = { version = "4.5"}
#other deps
serde = { version = "1", features = ["derive"] }
tokio = { version = "1", features = ["full"] }
log = "0.4"
fast_log = "1.6"
rbatis - v4.5.1

Published by zhuxiujia 12 months ago

  • add Pool Trait, you can design your any Pool!
    for example:
#[derive(Debug)]
pub struct MobcPool {
    pub manager:ConnManager,
    pub inner: mobc::Pool<ConnManager>,
}

unsafe impl Sync for MobcPool {}
unsafe impl Send for MobcPool {}

#[async_trait]
impl Pool for MobcPool {
    fn new(manager: ConnManager) -> Result<Self,Error> where Self: Sized {
        Ok(Self {
            manager:manager.clone(),
            inner: mobc::Pool::new(manager)
        })
    }

    async fn get(&self) -> Result<Box<dyn Connection>, Error> {
        let v = self.inner.get().await.map_err(|e|Error::from(e.to_string()))?;
        Ok(Box::new(v))
    }

    async fn get_timeout(&self, d: Duration) -> Result<Box<dyn Connection>, Error> {
        let v = self.inner.get_timeout(d).await.map_err(|e|Error::from(e.to_string()))?;
        Ok(Box::new(v))
    }

    async fn set_conn_max_lifetime(&self, max_lifetime: Option<Duration>) {
       self.inner.set_conn_max_lifetime(max_lifetime).await;
    }

    async fn set_max_idle_conns(&self, n: u64) {
        self.inner.set_max_idle_conns(n).await;
    }

    async fn set_max_open_conns(&self, n: u64) {
        self.inner.set_max_open_conns(n).await;
    }

    fn driver_type(&self) -> &str {
        self.manager.driver_type()
    }

}

#[async_trait]
impl mobc::Manager for ConnManager {
    type Connection = ConnectionBox;
    type Error = Error;

    async fn connect(&self) -> Result<Self::Connection, Self::Error> {
        self.connect().await
    }

    async fn check(&self, conn: Self::Connection) -> Result<Self::Connection, Self::Error> {
        self.check( conn).await
    }
}


impl Connection for mobc::Connection<ConnManager>{
    fn get_rows(&mut self, sql: &str, params: Vec<Value>) -> BoxFuture<Result<Vec<Box<dyn Row>>, Error>> {
        self.conn.as_mut().unwrap().get_rows(sql,params)
    }

    fn exec(&mut self, sql: &str, params: Vec<Value>) -> BoxFuture<Result<ExecResult, Error>> {
        self.conn.as_mut().unwrap().exec(sql,params)
    }

    fn ping(&mut self) -> BoxFuture<Result<(), Error>> {
        self.conn.as_mut().unwrap().ping()
    }

    fn close(&mut self) -> BoxFuture<Result<(), Error>> {
        self.conn.as_mut().unwrap().close()
    }
}


  • use MobcPool
   use rbatis::RBatis;
    use rbdc::pool::pool_mobc::MobcPool;
    use rbdc_sqlite::{SqliteConnectOptions, SqliteDriver};
    let rb=RBatis::new();
   
    let opts=SqliteConnectOptions::new();
    let rbatis = rb.init_option::<SqliteDriver, SqliteConnectOptions, MobcPool>(SqliteDriver{},opts);
 
rbatis - v4.5.0

Published by zhuxiujia 12 months ago

v4.5.0

  • add Pool Trait, you can design your any Pool!
    for example:
#[derive(Debug)]
pub struct MobcPool {
    pub manager:ConnManager,
    pub inner: mobc::Pool<ConnManager>,
}

unsafe impl Sync for MobcPool {}
unsafe impl Send for MobcPool {}

#[async_trait]
impl Pool for MobcPool {
    fn new(manager: ConnManager) -> Result<Self,Error> where Self: Sized {
        Ok(Self {
            manager:manager.clone(),
            inner: mobc::Pool::new(manager)
        })
    }

    async fn get(&self) -> Result<Box<dyn Connection>, Error> {
        let v = self.inner.get().await.map_err(|e|Error::from(e.to_string()))?;
        Ok(Box::new(v))
    }

    async fn get_timeout(&self, d: Duration) -> Result<Box<dyn Connection>, Error> {
        let v = self.inner.get_timeout(d).await.map_err(|e|Error::from(e.to_string()))?;
        Ok(Box::new(v))
    }

    async fn set_conn_max_lifetime(&self, max_lifetime: Option<Duration>) {
       self.inner.set_conn_max_lifetime(max_lifetime).await;
    }

    async fn set_max_idle_conns(&self, n: u64) {
        self.inner.set_max_idle_conns(n).await;
    }

    async fn set_max_open_conns(&self, n: u64) {
        self.inner.set_max_open_conns(n).await;
    }

    fn driver_type(&self) -> &str {
        self.manager.driver_type()
    }

}

#[async_trait]
impl mobc::Manager for ConnManager {
    type Connection = ConnectionBox;
    type Error = Error;

    async fn connect(&self) -> Result<Self::Connection, Self::Error> {
        self.connect().await
    }

    async fn check(&self, conn: Self::Connection) -> Result<Self::Connection, Self::Error> {
        self.check( conn).await
    }
}


impl Connection for mobc::Connection<ConnManager>{
    fn get_rows(&mut self, sql: &str, params: Vec<Value>) -> BoxFuture<Result<Vec<Box<dyn Row>>, Error>> {
        self.conn.as_mut().unwrap().get_rows(sql,params)
    }

    fn exec(&mut self, sql: &str, params: Vec<Value>) -> BoxFuture<Result<ExecResult, Error>> {
        self.conn.as_mut().unwrap().exec(sql,params)
    }

    fn ping(&mut self) -> BoxFuture<Result<(), Error>> {
        self.conn.as_mut().unwrap().ping()
    }

    fn close(&mut self) -> BoxFuture<Result<(), Error>> {
        self.conn.as_mut().unwrap().close()
    }
}


  • use MobcPool
   use rbatis::RBatis;
    use rbdc::pool::pool_mobc::MobcPool;
    use rbdc_sqlite::{SqliteConnectOptions, SqliteDriver};
    let rb=RBatis::new();
   
    let opts=SqliteConnectOptions::new();
    let rbatis = rb.init_option::<SqliteDriver, SqliteConnectOptions, MobcPool>(SqliteDriver{},opts);
 
rbatis - v4.4.21

Published by zhuxiujia 12 months ago

v4.4.21

  • support get intercept
    for example:
   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 {
    }
let mut rb = RBatis::new();
rb.set_intercepts(vec![Arc::new(MockIntercept{})]);
let intercept = rb.get_intercept::<MockIntercept>();
rbatis - v4.4.20

Published by zhuxiujia 12 months ago

v4.4.20

  • fix #[py_sql(r#"include_str!("example.py")"#)] on debug_mode Reload not Not working
#[py_sql(r#"include_str!("example.py")"#)]
async fn py_select(rb: &dyn Executor, name: &str, ids: &[i32]) -> Result<Vec<Activity>, Error> {
    impled!()
}
rbatis - v4.4.19

Published by zhuxiujia 12 months ago

v4.4.19

  • RBatisRef add rb_ref() fn, rbatis_ref is deprecated
  • RBatisConnExecutor add id field
  • Page. search_count rename to do_count
  • remove some deprecated methods
rbatis - v4.4.18

Published by zhuxiujia 12 months ago

v4.4.18

  • support table sync edit String type of column type
    for example:
pub async fn do_sync_table_mysql(conn: &RBatisConnExecutor){
      let table = User{id: "".to_string(), name: Some("VARCHAR(50)".to_string())};
     sync(conn, &SqliteTableMapper{},to_value!(table),"user").await;
}
rbatis - v4.4.17

Published by zhuxiujia 12 months ago

v4.4.17

  • support sync method
use rbatis::Error;
use rbatis::executor::RBatisConnExecutor;
use rbatis::RBatis;
use rbatis::table_sync::{SqliteTableMapper, sync};
use rbs::to_value;

#[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: &RBatisConnExecutor){
     let table = User{id: "1".to_string(), name: Some("".to_string())};
     sync(conn, &SqliteTableMapper{},to_value!(table),"user").await;
}
rbatis - v4.4.16

Published by zhuxiujia 12 months ago

v4.4.16

  • fix update_by_column_batch for #447
rbatis - v4.4.15

Published by zhuxiujia 12 months ago

v4.4.15

  • use dark_std , remove crossbeam dep.
rbatis - v4.4.13

Published by zhuxiujia 12 months ago

v4.4.13

  • support pysql!, htmlsql define #[py_sql] and #[html_sql]
    for example:
htmlsql!(test_same_id1(rb: &rbatis::RBatis, id: &u64)  -> Result<rbs::Value, rbatis::Error> => "example.html");

htmlsql!(test_same_id2(rb: &RBatis, id: &u64)  -> Result<Value, Error> => r#"<mapper>
            <select id="test_same_id2">
            select ${id},${id},#{id},#{id}
            </select>
            </mapper>"#);

pysql!(test_same_id(rb: &rbatis::RBatis, id: &u64)  -> Result<rbs::Value, rbatis::Error> => "select ${id},${id},#{id},#{id} ");

rbatis - v4.4.11

Published by zhuxiujia 12 months ago

v4.4.11

  • fix page new total
rbatis - v4.4.10

Published by zhuxiujia 12 months ago

v4.4.10

  • support impl_select_page,htmlsql_select_page,pysql_select_page use Owner arg or Ref arg
    for example:
 #[derive(serde::Serialize, serde::Deserialize, Clone)]
    pub struct PySqlSelectPageArg{
        pub name:String
    }

    //  in old version  must be ` item: &PySqlSelectPageArg` ,`item: PySqlSelectPageArg` not allow
    rbatis::pysql_select_page!(pysql_select_page(item: PySqlSelectPageArg) -> MockTable =>
    r#"`select `
      if do_count == true:
        ` count(1) as count `
      if do_count == false:
         ` * `
      `from activity where delete_flag = 0`
        if item.name != '':
           ` and name=#{item.name}`"#);
rbatis - v4.4.9

Published by zhuxiujia almost 1 year ago

v4.4.9

  • impl #445
rbatis - v4.4.8

Published by zhuxiujia about 1 year ago

v4.4.8

  • fix #444
rbatis - v4.4.7

Published by zhuxiujia about 1 year ago

v4.4.7

  • update fastdate=v0.2
rbatis - v4.4.6

Published by zhuxiujia about 1 year ago

v4.4.6

  • default set IPageRequest impl Synd/Sync
rbatis - v4.4.5

Published by zhuxiujia about 1 year ago

v4.4.5

  • crud! macro page macro use IPageRequest trait