include-sqlite-sql

Crates.ioinclude-sqlite-sql
lib.rsinclude-sqlite-sql
version0.2.1
sourcesrc
created_at2022-02-09 23:01:32.218161
updated_at2023-03-07 20:10:23.208693
descriptionA Yesql inspired macro for using SQLite SQL in Rust
homepagehttps://quietboil.github.io/include-sqlite-sql
repositoryhttps://github.com/quietboil/include-sqlite-sql
max_upload_size
id529945
size45,662
(quietboil)

documentation

README

crates.io Documentation MIT

include-sqlite-sql is an extension of include-sql for using SQLite SQL in Rust. It completes include-sql by providing impl_sql macro to generate database access methods from the included SQL. include-sqlite-sql uses Rusqlite for database access.

Example

Write your SQL and save it in a file. For example, let's say the following is saved as library.sql in the project's src folder:

-- name: get_loaned_books ?
--
-- Returns the list of books loaned to a patron
--
-- # Parameters
--
-- param: user_id: &str - user ID
--
SELECT book_title
  FROM library
 WHERE loaned_to = :user_id
 ORDER BY 1

-- name: loan_books!
--
-- Updates the book records to reflect loan to a patron
--
-- # Parameters
--
-- param: book_titles: &str - book titles
-- param: user_id: &str - user ID
--
UPDATE library
   SET loaned_to = :user_id
     , loaned_on = current_timestamp
 WHERE book_title IN (:book_titles)

And then use it in Rust as:

use include_sqlite_sql::{include_sql, impl_sql};
use rusqlite::{Result, Connection};

include_sql!("src/library.sql");

fn main() -> Result<()> {
    let db = Connection::open("library.db")?;

    db.loan_books(&["War and Peace", "Gone With the Wind"], "Sheldon Cooper")?;

    db.get_loaned_books("Sheldon Cooper", |row| {
        let book_title : &str = row.get_ref(0)?.as_str()?;
        println!("{book_title}");
        Ok(())
    })?;

    Ok(())
}

Documentation

The included documentation describes the supported SQL file format and provides additional details on the generated code.

💥 Breaking Changes in 0.2

  • include-sql changed optional statement terminator from ; to /. SQL files that used ; terminator would need to change it to / or remove it completely.

✨ New Features in 0.2

Commit count: 14

cargo fmt