Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

rbatis

> 数据库
Open source

Compile Time Async Dynamic SQL ORM

2.5K stars0 likes1 views
WebsiteGitHub

About

Compile Time Async Dynamic SQL ORM

Rbatis

English Documentation | 中文文档

Website | Showcase | Examples

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.

More Supported

  • Full Documentation
  • Changelog
  • rbdc-mcp AI driver for database access
  • rbatis-skil AI driver for ORM useage

Core Advantages

1. High Performance

  • ** Dynamic SQL to Native Rust Translation**: The key advantage - compiles dynamic SQL directly to native Rust code, achieving performance equivalent to hand-written SQL
  • Zero Runtime Overhead: All SQL parsing and optimization happens at compile-time, eliminating runtime interpretation costs
  • 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
  • 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

Database (crates.io) GitHub Link
MySQL rbatis/rbdc-mysql
PostgreSQL rbatis/rbdc-pg
SQLite rbatis/rbdc-sqlite
MSSQL rbatis/rbdc-mssql
Truso rbatis/rbdc-turso
DuckDB rbatis/rbdc-duckdb
MariaDB rbatis/rbdc-mysql
TiDB rbatis/rbdc-mysql
CockroachDB rbatis/rbdc-pg
Oracle chenpengfan/rbdc-oracle
TDengine tdcare/rbdc-tdengine

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:

  1. ** 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.

  2. Compilation Process:

    • Lexical Analysis: Handled by func.rs in rbatis-codegen using Rust's syn and quote crates
    • Syntax Parsing: Performed by parser_html and parser_pysql modules in rbatis-codegen
    • Abstract Syntax Tree: Built using structures defined in the syntax_tree package in rbatis-codegen
    • Intermediate Code Generation: Executed by func.rs, which contains all the code generation functions
  3. 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.

  4. 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.

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

toml
# 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"

Basic Usage

…

Advanced Usage (html_sql)

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

Creating a Custom Database Driver

To implement a custom database driver for Rbatis:

  1. Define your driver project with dependencies:
toml
[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"] }
  1. Implement the required traits:
…

Contact Us

Donations or Contact

WeChat (Please note 'rbatis' when adding as friend)

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rustcockroachdbduckdbmariadbmssql

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库