Expand description
rdbi-codegen: Generate Rust structs and rdbi DAO functions from MySQL schema DDL
This crate provides both a CLI tool and a library for generating Rust code
from MySQL schema files. It parses SQL DDL using sqlparser-rs and generates:
- Serde-compatible structs with
#[derive(Serialize, Deserialize, rdbi::FromRow, rdbi::ToParams)] - Async DAO functions using rdbi with index-aware query methods
§Usage in build.rs (Recommended)
Configure in your Cargo.toml:
[package.metadata.rdbi-codegen]
schema_file = "schema.sql"
include_tables = ["users", "orders"]
exclude_tables = ["migrations"]Then use a minimal build.rs:
ⓘ
fn main() {
rdbi_codegen::generate_from_cargo_metadata()
.expect("Failed to generate rdbi code");
}§Alternative: Programmatic Configuration
ⓘ
use std::env;
use std::path::PathBuf;
fn main() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
rdbi_codegen::CodegenBuilder::new("schema.sql")
.output_dir(&out_dir)
.generate()
.expect("Failed to generate rdbi code");
println!("cargo:rerun-if-changed=schema.sql");
}§CLI Usage
rdbi-codegen --schema schema.sql --output ./generatedRe-exports§
pub use config::CodegenConfig;pub use error::CodegenError;pub use error::Result;
Modules§
- codegen
- Code generation module
- config
- Configuration module for rdbi-codegen
- error
- Error types for rdbi-codegen
- parser
- SQL schema parser module using sqlparser-rs
Structs§
- Codegen
Builder - Builder pattern for easy configuration in build.rs
Functions§
- generate
- Main entry point for code generation
- generate_
from_ cargo_ metadata - Generate code from
[package.metadata.rdbi-codegen]in Cargo.toml