Diesel and Rbatis Integrations
Relevant source files
The following files were used as context for generating this wiki page:
SeaQuery provides specialized integration crates for Diesel and Rbatis, allowing developers to leverage SeaQuery's dynamic query building capabilities within these established database frameworks. These integrations bridge the gap between SeaQuery’s abstract syntax tree (AST) and the specific execution requirements of each driver.
The primary purpose of these integrations is to provide binder traits that convert SeaQuery statements into formats compatible with Diesel's backend-specific execution and Rbatis's data-driven model. Sources: sea-query-diesel/Cargo.toml, sea-query-rbatis/Cargo.toml
Diesel Integration
The sea-query-diesel crate provides the DieselBinder trait, which enables the conversion of SeaQuery statements into a SeaQuery<DB> wrapper compatible with Diesel's query execution pipeline.
Architecture and Data Flow
The integration works by implementing DieselBinder for major SeaQuery statement types. It uses Diesel backends to extract builders and transform values into a format Diesel understands.
The flow shows how a standard SeaQuery statement is processed through the DieselBinder to become executable by a Diesel connection. Sources: sea-query-diesel/src/lib.rs:17-36
Key Components
| Component | Description |
|---|---|
DieselBinder | The core trait providing the build_diesel method. |
SeaQuery<DB> | A wrapper struct that holds the SQL string and the bound parameters. |
ExtractBuilder | Internal trait used to obtain the appropriate QueryBuilder for a specific Diesel backend. |
TransformValue | Internal trait responsible for converting SeaQuery Value types into Diesel-compatible values. |
Sources: sea-query-diesel/src/lib.rs:11-20
Supported Statements
The DieselBinder trait is implemented for the following SeaQuery structures:
SelectStatementUpdateStatementInsertStatementDeleteStatementWithQuery
Sources: sea-query-diesel/src/lib.rs:38-42
Configuration and Features
The Diesel integration supports multiple database backends and extended data types through Cargo features.
| Feature | Dependency/Integration |
|---|---|
postgres | diesel/postgres, sea-query/backend-postgres |
mysql | diesel/mysql, sea-query/backend-mysql |
sqlite | diesel/sqlite, sea-query/backend-sqlite |
with-chrono | Integration for Chrono date/time types. |
with-uuid | Integration for UUID types. |
postgres-array | Support for PostgreSQL array types. |
Sources: sea-query-diesel/Cargo.toml:44-55
Rbatis Integration
The sea-query-rbatis crate acts as a driver library to connect SeaQuery with the Rbatis ORM. It primarily focuses on type compatibility and value mapping between the two systems.
Implementation Details
The Rbatis integration relies on the rbs crate for value handling. It enables thread-safe features in SeaQuery to ensure compatibility with Rbatis's asynchronous execution environment. Sources: sea-query-rbatis/Cargo.toml:20-22
The diagram illustrates the transformation of SeaQuery values into rbs values for use within the Rbatis engine. Sources: sea-query-rbatis/Cargo.toml:21
Feature Support
Similar to the Diesel integration, sea-query-rbatis provides a wide range of type support features:
- JSON:
with-jsonusingserde_json. - Decimals:
with-rust_decimalandwith-bigdecimal. - Network:
with-ipnetworkandwith-mac_address. - Postgres Specifics:
postgres-arraysupport.
Sources: sea-query-rbatis/Cargo.toml:32-42
Practical Usage Example (Diesel)
In a typical application, SeaQuery is used to define the schema and the queries, which are then executed via Diesel's RunQueryDsl.
// Constructing a query
let query = Query::select()
.columns([Character::Id, Character::Name])
.from(Character::Table)
.to_owned();
// Binding and executing with Diesel
let rows = query
.build_diesel()
.unwrap()
.get_results::<CharacterStructChrono>(conn)
.unwrap();
Sources: examples/diesel_postgres/src/main.rs:118-124
Summary
The Diesel and Rbatis integrations extend SeaQuery's utility by providing a standardized way to execute dynamically generated SQL. While the Diesel integration focuses on a deep trait-based binding system (DieselBinder), the Rbatis integration ensures type safety and compatibility with the rbs value system used by Rbatis. These modules allow SeaQuery to function as a unified query building layer across different Rust database ecosystems. Sources: sea-query-diesel/src/lib.rs, sea-query-rbatis/src/lib.rs