| Crates.io | derive-sql |
| lib.rs | derive-sql |
| version | 0.13.0 |
| created_at | 2022-09-26 18:36:19.331268+00 |
| updated_at | 2025-02-09 17:19:12.360286+00 |
| description | Structure the interaction with SQL databases - currently SQLite and MySQL via trait and a procedural macro |
| homepage | |
| repository | https://github.com/juliendecharentenay/derive-sql |
| max_upload_size | |
| id | 674389 |
| size | 160,622 |
This project define an approach to interact with SQL database in Rust [currently only SQLite supported].
A trait Sqlable is defined in the crate with the following functions to interact with the database:
count to provide a count of the number of items in the table.select to return an array of the items in the table.insert to insert a new item in the table.update to update an existing item(s) with the values of the provided item.delete to delete items in the table.delete_table to drop the table.A procedural macro DeriveSqlite is available under the optional feature sqlite. The procedural macro can
be applied to a struct with named fields to implement the Sqlable trait.
The procedural macro is currently tied to rusqlite that needs to be added to your Cargo.toml. If not added, your
project will not compile.
To use this project procedural macro, add the following in your Cargo.toml:
[dependencies]
derive-sql = { version = "0.5", features = [ 'sqlite' ] }
And annotate your struct as follows:
use derive_sql::{Sqlable, DeriveSqlite};
#[derive(DeriveSqlite)]
struct Person {
id: 32,
name: String,
}
And use the generated functions:
cargo doc --openCheckout the example tests using in-memory SQLite database in the extras/derive-sql-sqlite/examples folder:
cargo run --example simple --features sqlite
cargo run --example attributes --features sqlite
Checkout the example tests using a MySQL database in the extras/derive-sql-mysql/examples folder:
cargo run --example simple_mysql --features mysql
cargo run --example attributes_mysql --features mysql
This project is licensed under MIT license.