Compile Time Async Dynamic SQL ORM
Rbatis is a high-performance ORM framework for Rust based on compile-time code generation. It perfectly balances development efficiency, performance, and stability, functioning as both an ORM and a dynamic SQL compiler.
? as a unified placeholder, supporting all drivers#{arg} and direct replacement ${arg}, meeting different scenario requirementsif, for, choose/when/otherwise, bind, trim structures and collection operations (.sql(), .csv())<if>, <where>, <set>, <foreach>), declarative SQL building, and automatic handling of SQL fragments without requiring CDATA| Connection Pool (crates.io) | GitHub Link |
|---|---|
| FastPool (default) | rbatis/fast_pool |
| Deadpool | rbatis/rbdc-pool-deadpool |
| MobcPool | rbatis/rbdc-pool-mobc |
| Data Type | Support |
|---|---|
Option |
✓ |
Vec |
✓ |
HashMap |
✓ |
i32, i64, f32, f64, bool, String, and other Rust base types |
✓ |
rbatis::rbdc::types::{Bytes, Date, DateTime, Time, Timestamp, Decimal, Json} |
✓ |
rbatis::plugin::page::{Page, PageRequest} |
✓ |
rbs::Value |
✓ |
serde_json::Value and other serde types |
✓ |
| Driver-specific types from rbdc-mysql, rbdc-pg, rbdc-sqlite, rbdc-mssql | ✓ |
| crate | GitHub Link |
|---|---|
| rbdc | rbdc |
| rbs | rbs |
Rbatis uses compile-time code generation through the rbatis-codegen crate, which means:
** Direct Translation to Native Rust**: Dynamic SQL is converted to optimized Rust code during compilation, achieving performance identical to hand-written SQL without any runtime interpretation overhead.
Compilation Process:
func.rs in rbatis-codegen using Rust's syn and quote cratesparser_html and parser_pysql modules in rbatis-codegensyntax_tree package in rbatis-codegenfunc.rs, which contains all the code generation functionsBuild Process Integration: The entire process runs during the cargo build phase as part of Rust's procedural macro compilation. The generated code is returned to the Rust compiler for LLVM compilation to produce machine code.
Compile-Time SQL Optimization: Unlike traditional ORMs that interpret dynamic SQL at runtime (causing performance penalties), Rbatis translates SQL to native Rust code at compile-time, delivering hand-written SQL performance while maintaining ORM convenience.
---- bench_raw stdout ----(windows/SingleThread)
Time: 52.4187ms ,each:524 ns/op
QPS: 1906435 QPS/s
---- bench_select stdout ----(macos-M1Cpu/SingleThread)
Time: 112.927916ms ,each:1129 ns/op
QPS: 885486 QPS/s
---- bench_insert stdout ----(macos-M1Cpu/SingleThread)
Time: 346.576666ms ,each:3465 ns/op
QPS: 288531 QPS/s# Cargo.toml
[dependencies]
#drivers
rbdc-sqlite = { version = "4" }
# rbdc-mysql = { version = "4" }
# rbdc-pg = { version = "4" }
# rbdc-mssql = { version = "4" }
rbs = { version = "4"}
rbatis = { version = "4.8"}
# Other dependencies
serde = { version = "1", features = ["derive"] }
tokio = { version = "1", features = ["full"] }
log = "0.4"
fast_log = "1.6"…Use #[rbatis::html_sql("html/file_path")] macro for complex queries like pagination, join queries, etc.:
…Corresponding HTML template file example/example.html:
…Applicable scenarios: paginated queries, join queries, complex dynamic SQL, multi-condition search
To implement a custom database driver for Rbatis:
[features]
default = ["tls-rustls"]
tls-rustls=["rbdc/tls-rustls"]
tls-native-tls=["rbdc/tls-native-tls"]
[dependencies]
rbs = { version = "4"}
rbdc = { version = "4", default-features = false, optional = true }
fastdate = { version = "0.3" }
tokio = { version = "1", features = ["full"] }…WeChat (Please note 'rbatis' when adding as friend)
No open issues yet, or sync has not completed.