Macro include_sqlite_sql::impl_sql
source · [−]macro_rules! impl_sql {
( $sql_name:ident = $( { $kind:tt $name:ident ($($variant:tt $param:ident $ptype:tt)*) $doc:literal $s:tt $( $text:tt )+ } ),+ ) => { ... };
}
Expand description
Generates Rust code to use included SQL.
This macro defines a trait with methods to access data and implements it for rusqlite::Connection
.
This macro recognizes and generates 3 variants of database access methods using the following selectors:
?
- methods that process rows retrieved bySELECT
,!
- methods that execute all other non-SELECT
methods, and->
- methods that executeRETURNING
statements and provide access to returned data.
For SELECT
statements (?
) like:
-- name: get_loaned_books?
-- param: user_id: &str
SELECT book_title FROM library WHERE loaned_to = :user_id;
The method with the following signature is generated:
ⓘ
fn get_loaned_books<F>(
&self,
user_id: &str,
row_callback: F
) -> -> rusqlite::Result<()>
where F: Fn(&rusqlite::Row<'_>) -> rusqlite::Result<()>;
For non-select statements (!
) - INSERT, UPDATE, DELETE, etc. - like:
-- name: loan_books!
-- param: user_id: &str
-- param: book_ids: i32
UPDATE library
SET loaned_to = :user_id
, loaned_on = current_timestamp
WHERE book_id IN (:book_ids);
The method with the following signature is generated:
ⓘ
fn loan_books(
&self,
user_id: &str,
book_ids: &[i32]
) -> rusqlite::Result<usize>;
For DELETE, INSERT, and UPDATE statements that return data via RETURNING
clause (->
) like:
-- name: add_new_book->
-- param: isbn: &str
-- param: book_title: &str
INSERT INTO library (isbn, book_title)
VALUES (:isbn, :book_title)
RETURNING book_id;
The method with the following signature is generated:
ⓘ
fn add_new_book<F,R>(
&self,
isbn: &str,
book_title: &str,
row_callback: F
) -> rusqlite::Result<R>
where F: FnOnce(&rusqlite::Row<'_>) -> rusqlite::Result<R>;