persistant_macros/lib.rs
1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! The compile-time SQL macro behind `persistant::sql!` (feature `sql-macros` of the `persistant`
3//! crate). A proc-macro crate cannot be extended, only wrapped, so this is PersistAnt's own thin
4//! wrapper over `unisql-macro-core`; see `docs/COMPILE-TIME-SQL.md`.
5//!
6//! ```text
7//! const FIND: persistant::Sql = persistant::sql!(
8//! source = postgres, targets = [postgres, sqlite, mysql],
9//! "SELECT id, email FROM users WHERE id = $1"
10//! );
11//! ```
12//!
13//! Arguments, in any order, then the statement as the last argument (a string literal):
14//! `source` (required), `targets` (required, at least one), `allow`, `requires`, `forbids`, and
15//! `crate = path` (where the expansion finds `Query`, default `::persistant::embed`). The statement is
16//! parsed in the source dialect and translated for every named target at build time; whatever cannot
17//! be is a compile error. Nothing is looked up in a database and no schema is read yet (the
18//! catalog-based checks are a later stage).
19
20mod checker;
21mod imp;
22mod rows;
23
24/// Check and translate a literal statement at build time. See the crate documentation.
25#[proc_macro]
26pub fn sql(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
27 imp::sql(input.into()).into()
28}
29
30/// Derive `FromRow` for a struct with named fields, each of a type the crate can read from a cell
31/// (`i64`, `i32`, `f64`, `bool`, `String`, `Vec<u8>`, or `Option` of those). The struct is then read from
32/// a result row by position, and `sql!(as T, ..)` can check a `SELECT` against it at build time.
33/// `#[persistant(crate = path)]` names the crate the impl refers to (default `::persistant::relational`).
34#[proc_macro_derive(FromRow, attributes(persistant))]
35pub fn derive_from_row(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
36 imp::derive_from_row(input.into()).into()
37}