a compile-time code generation ORM that balances ease of writing with performance and robustness
It is an ORM, a small compiler, a dynamic SQL languages
- High-performance: Compile time Dynamic SQL,Based on Future/Tokio, Connection Pool
- Reliability: Rust Safe Code,precompile:
#{arg}
, Direct replacement:${arg}
, unify?
placeholders(support all driver) - Productivity: Powerful
Box<dyn Intercept>
(LogInterceptor),#[py_sql]
, Compatible with mybatis3#[html_sql]
,curd!
macro,Box<dyn Driver>
RBDC driver (rbdc-mysql,rbdc-pg,rbdc-sqlite..and more),table_sync_plugin,abs_admin project
Thanks to SQLX, deadpool, Tiberius, MyBatis, xorm
and so on reference design or code implementation. Release of V4 is Inspired and supported by these frameworks.**
Performance
- this bench test is MockTable,MockDriver,MockConnection to Assume that the network I/O time is 0
- run code
rbatis.query_decode::<Vec<i32>>("", vec![]).await;
on benches bench_raw() - run code
MockTable::insert(&rbatis,&t).await;
on benches bench_insert() - run code
MockTable::select_all(&rbatis).await.unwrap();
on benches bench_select()
---- bench_raw stdout ----(windows)
Time: 52.4187ms ,each:524 ns/op
QPS: 1906435 QPS/s
---- bench_insert stdout ----(macos,cpu-M1Max)
Time: 378.186333ms ,each:3781 ns/op
QPS: 264418 QPS/s
---- bench_select stdout ----(macos,cpu-M1Max)
Time: 112.927916ms ,each:1129 ns/op
QPS: 885486 QPS/s
Supported data structures
data structure | is supported |
---|---|
Option | √ |
Vec | √ |
HashMap | √ |
i32,i64,f32,f64,bool,String...more rust type | √ |
rbatis::rbdc::types::{Date,DateTime,Time,Timestamp,Decimal,Json} | √ |
rbatis::plugin::page::{Page, PageRequest} | √ |
rbs::Value* | √ |
serde_json::* | √ |
any serde type | √ |
driver type on package (rdbc-mysql/types,rbdc-pg/types,rbdc-sqlite/types) | √ |
Supported database driver
Supported OS/Platforms by Workflows CI
platform | is supported |
---|---|
Linux(unbutu laster***) | √ |
Apple/MacOS(laster) | √ |
Windows(latest) | √ |
Supported Web Frameworks
- any web Frameworks just like ntex, actix-web, axum, hyper, rocket, tide, warp, salvo and more.
Quick example: QueryWrapper and common usages (see example/crud_test.rs for details)
-
Rust v1.73+ later
-
Cargo.toml
default
#rbatis deps
= { = "4.5"}
= { = "4.5"}
= { = "4.5" }
#rbdc-mysql={version="4.5"}
#rbdc-pg={version="4.5"}
#rbdc-mssql={version="4.5"}
#other deps
= { = "1", = ["derive"] }
= { = "1", = ["full"] }
= "0.4"
= "1.6"
(option) 'native-tls'
= { = "4.5" }
= { = "4.5", = false, = ["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"]}
= { = "4.5", = false, = ["tls-native-tls","default_mode"] }
#other deps
= { = "1", = ["derive"] }
= { = "1", = ["full"] }
= "0.4"
= "1.6"
default use
//#[macro_use] define in 'root crate' or 'mod.rs' or 'main.rs'
extern crate rbatis;
extern crate rbdc;
use DateTime;
crud!;//crud = insert+select_by_column+update_by_column+delete_by_column
impl_select!;
impl_select!;
impl_update!;
impl_delete!;
impl_select_page!;
async
///...more usage,see crud.rs
- raw-sql
pub async
macros
- Important update (pysql removes runtime, directly compiles to static rust code) This means that the performance of SQL generated using py_sql,html_sql is roughly similar to that of handwritten code.
Because of the compile time, the annotations need to declare the database type to be used.
async
- Added html_sql support, a form of organization similar to MyBatis, to facilitate migration of Java systems to Rust( Note that it is also compiled as Rust code at build time and performs close to handwritten code) this is very faster
Because of the compile time, the annotations need to declare the database type to be used
`select * from biz_activity where `
name like #{name}
///select page must have '?:&PageRequest' arg and return 'Page<?>'
async
use Lazy;
pub static RB: = new;
/// Macro generates execution logic based on method definition, similar to @select dynamic SQL of Java/Mybatis
/// RB is the name referenced locally by RBatis, for example DAO ::RB, com:: XXX ::RB... Can be
/// The second parameter is the standard driver SQL. Note that the corresponding database parameter mysql is? , pg is $1...
/// macro auto edit method to 'pub async fn select(name: &str) -> rbatis::core::Result<BizActivity> {}'
///
pub async
//or: pub async fn select(name: &str) -> rbatis::core::Result<BizActivity> {}
pub async
How it works
Rely on rbatis-codegen to create the source code of the corresponding structure from the html file at compile time (with debug_mode(Cargo.toml- rbatis = { features = ["debug_mode"]}
) enabled, you can observe the code-generated function), and call the generated method directly at run time.
We know that compilation is generally divided into three steps, lexes, syntactic analysis, semantic analysis, and intermediate code generation. In rbatis,
Lexical analysis is handled by the dependent func.rs in rbatis-codegen
, which relies on syn and quote.
Parsing is done by parser_html and parser_pysql in rbatis-codegen
The generated syntax tree is a structure defined in the syntax_tree package in rbatis-codegen
Intermediate code generation has func.rs generation function, all supported functions are defined in rbatis-codegen
What is described above occurs during the cargo build phase, which is the compilation phase of the rust procedural macro, where the code generated by rbatis-codegen
is handed back to the rust compiler for LLVM compilation to produce pure machine code
So I think rbatis is Truly zero overhead dynamic SQL compile-time ORM.
Submit PR(Pull Requests)
You are welcome to submit the merge, and make sure that any functionality you add has the appropriate mock unit test function added under the test package.
Changelog
Roadmap
- sqlite table sync plugin(auto create table/column)
- connection pool add more dynamically configured parameters
- More simplified syntax
Contact/donation, or click on star rbatis
联系方式/捐赠,或 rbatis 点star
捐赠
联系方式(添加好友请备注'rbatis') 微信群:先加微信,然后拉进群