#sql-database #proc-macro #mysql #sqlite #traits #procedural #table

derive-sql

Structure the interaction with SQL databases - currently SQLite and MySQL via trait and a procedural macro

17 releases (11 breaking)

0.12.1 Aug 26, 2024
0.11.3 Jul 22, 2024
0.10.0 Jun 9, 2024
0.8.1 Mar 18, 2024
0.2.1 Nov 19, 2022

#238 in Database interfaces

32 downloads per month
Used in 3 crates

MIT license

135KB
2.5K SLoC

derive-sql

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.

How to Use

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:

  • Review the documentation pages;
  • Generate documentation for the generated functions using cargo doc --open

Checkout 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

License

This project is licensed under MIT license.

Dependencies

~2–17MB
~239K SLoC