Rbatis
Introduction
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.
AI-support
Core Advantages
1. High Performance
- Compile-time Dynamic SQL Generation: Converts SQL statements to Rust code during compilation, avoiding runtime overhead
- Based on Tokio Async Model: Fully utilizes Rust's async features to enhance concurrency performance
- Efficient Connection Pools: Built-in multiple connection pool implementations, optimizing database connection management
2. Reliability
- Rust Safety Features: Leverages Rust's ownership and borrowing checks to ensure memory and thread safety
- Unified Parameter Placeholders: Uses
?
as a unified placeholder, supporting all drivers - Two Replacement Modes: Precompiled
#{arg}
and direct replacement${arg}
, meeting different scenario requirements
3. Development Efficiency
- Powerful ORM Capabilities: Automatic mapping between database tables and Rust structures
- Multiple SQL Building Methods:
- py_sql: Python-style dynamic SQL with
if
,for
,choose/when/otherwise
,bind
,trim
structures and collection operations (.sql()
,.csv()
) - html_sql: MyBatis-like XML templates with familiar tag structure (
<if>
,<where>
,<set>
,<foreach>
), declarative SQL building, and automatic handling of SQL fragments without requiring CDATA - Raw SQL: Direct SQL statements
- py_sql: Python-style dynamic SQL with
- CRUD Macros: Generate common CRUD operations with a single line of code
- Interceptor Plugin: Custom extension functionality
- Table Sync Plugin: Automatically create/update table structures
4. Extensibility
- Multiple Database Support: MySQL, PostgreSQL, SQLite, MSSQL, MariaDB, TiDB, CockroachDB, Oracle, TDengine, etc.
- Custom Driver Interface: Implement a simple interface to add support for new databases
- Multiple Connection Pools: FastPool (default), Deadpool, MobcPool
- Compatible with Various Web Frameworks: Seamlessly integrates with ntex, actix-web, axum, hyper, rocket, tide, warp, salvo, and more
Supported Database Drivers
Supported Connection Pools
Connection Pool (crates.io) | GitHub Link |
---|---|
FastPool (default) | rbatis/fast_pool |
Deadpool | rbatis/rbdc-pool-deadpool |
MobcPool | rbatis/rbdc-pool-mobc |
Supported Data Types
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 | ✓ |
Member crates
crate | GitHub Link |
---|---|
rbdc | rbdc |
rbs | rbs |
How Rbatis Works
Rbatis uses compile-time code generation through the rbatis-codegen
crate, which means:
-
Zero Runtime Overhead: Dynamic SQL is converted to Rust code during compilation, not at runtime. This provides performance similar to handwritten code.
-
Compilation Process:
- Lexical Analysis: Handled by
func.rs
inrbatis-codegen
using Rust'ssyn
andquote
crates - Syntax Parsing: Performed by
parser_html
andparser_pysql
modules inrbatis-codegen
- Abstract Syntax Tree: Built using structures defined in the
syntax_tree
package inrbatis-codegen
- Intermediate Code Generation: Executed by
func.rs
, which contains all the code generation functions
- Lexical Analysis: Handled by
-
Build 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. -
Dynamic SQL Without Runtime Cost: Unlike most ORMs that interpret dynamic SQL at runtime, Rbatis performs all this work at compile-time, resulting in efficient and type-safe code.
Performance Benchmarks
---- 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
Quick Start
Dependencies
# Cargo.toml
[]
= { = "4.6"}
= { = "4.6"}
#drivers
= { = "4.6" }
# rbdc-mysql = { version = "4.6" }
# rbdc-pg = { version = "4.6" }
# rbdc-mssql = { version = "4.6" }
# Other dependencies
= { = "1", = ["derive"] }
= { = "1", = ["full"] }
= "0.4"
= "1.6"
Basic Usage
use DateTime;
use value;
use RBatis;
use SqliteDriver;
use ;
use json;
// Automatically generate CRUD methods
crud!;
async
Creating a Custom Database Driver
To implement a custom database driver for Rbatis:
- Define your driver project with dependencies:
[]
= ["tls-rustls"]
=["rbdc/tls-rustls"]
=["rbdc/tls-native-tls"]
[]
= { = "4.6"}
= { = "4.6", = false, = true }
= { = "0.3" }
= { = "1", = ["full"] }
- Implement the required traits:
use ;
// Then use your driver:
async
More Information
Contact Us
Donations or Contact
WeChat (Please note 'rbatis' when adding as friend)