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

derive-sql

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

11 releases (breaking)

0.9.0 Apr 7, 2024
0.8.1 Mar 18, 2024
0.8.0 Feb 11, 2024
0.6.0 Nov 11, 2023
0.2.1 Nov 19, 2022

#1152 in Database interfaces

Download history 1/week @ 2024-02-09 39/week @ 2024-02-16 68/week @ 2024-02-23 9/week @ 2024-03-01 9/week @ 2024-03-08 109/week @ 2024-03-15 16/week @ 2024-03-22 19/week @ 2024-03-29 102/week @ 2024-04-05

247 downloads per month
Used in 2 crates

MIT license

30KB
560 lines

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

~0.3–18MB
~223K SLoC