| Crates.io | rbdc |
| lib.rs | rbdc |
| version | 4.6.1 |
| created_at | 2022-08-06 09:45:07.789624+00 |
| updated_at | 2025-08-15 04:56:01.152038+00 |
| description | The Rust SQL Toolkit and ORM Library. An async, pure Rust SQL crate featuring compile-time Dynamic SQL |
| homepage | https://rbatis.github.io/rbatis.io |
| repository | https://github.com/rbatis/rbatis |
| max_upload_size | |
| id | 639781 |
| size | 149,359 |
RBDC driver abstract
rbdc is safe code(#![forbid(unsafe_code)])rbatisjust only impl this traits(6)
use rbdc::db::{Driver, MetaData, Row, Connection, ConnectOptions, Placeholder};
pub struct YourDriver{}
impl Driver for YourDriver{}
pub struct YourMetaData{}
impl MetaData for YourMetaData{}
pub struct YourRow{}
impl Row for YourRow{}
pub struct YourConnection{}
impl Connection for YourConnection{}
pub struct YourConnectOptions{}
impl ConnectOptions for YourConnectOptions{}
pub struct YourPlaceholder{}
impl Placeholder for YourPlaceholder{}
use rbdc_sqlite::SqliteDriver;
use rbdc::db::{Connection};
use rbdc::Error;
use rbdc::pool::ConnManager;
use rbdc::pool::Pool;
use rbdc_pool_fast::FastPool;
#[tokio::main]
async fn main() -> Result<(), Error> {
let pool = FastPool::new(ConnManager::new(SqliteDriver {}, "sqlite://target/test.db")?)?;
let mut conn = pool.get().await?;
// select
let v = conn.get_values("select * from sqlite_master", vec![]).await?;
println!("{}", rbs::Value::Array(v));
// update/delete
let r = conn.exec("update table set name='a' where id = 1", vec![]).await?;
println!("{}", r);
Ok(())
}
For database drivers with blocking APIs, follow the pattern in rbdc-sqlite using the flume channel library:
// Key components:
// 1. Dedicated worker thread per connection
// 2. Command channels for communication
pub struct YourConnection {
worker: ConnectionWorker,
}
struct ConnectionWorker {
command_tx: flume::Sender<Command>,
}
enum Command {
Execute { /* ... */ },
Query { /* ... */ },
}
Benefits:
Connection require both Send and Sync?Connection: Send + Sync is required because:
When implementing for non-thread-safe databases:
// SAFETY: YourConnection is thread-safe because:
// 1. Database operations run on a dedicated worker thread
// 2. Communication uses thread-safe channels
unsafe impl Sync for YourConnection {}
Improper implementation can cause data races and undefined behavior.