Captured identifiers in SQL strings

Introduce macro_rules `prepare_and_bind` and `prepare_cached_and_bind`
This commit is contained in:
gwenn 2022-12-26 20:00:59 +01:00
parent 5b20201423
commit 78b7c52105

View File

@ -85,6 +85,8 @@ pub use crate::statement::{Statement, StatementStatus};
pub use crate::transaction::{DropBehavior, Savepoint, Transaction, TransactionBehavior}; pub use crate::transaction::{DropBehavior, Savepoint, Transaction, TransactionBehavior};
pub use crate::types::ToSql; pub use crate::types::ToSql;
pub use crate::version::*; pub use crate::version::*;
#[doc(hidden)]
pub use rusqlite_macros::__bind;
mod error; mod error;
@ -219,6 +221,30 @@ macro_rules! named_params {
}; };
} }
/// Captured identifiers in SQL
#[macro_export]
macro_rules! prepare_and_bind {
($conn:expr, $sql:literal) => {{
#[cfg(trick_rust_analyzer_into_highlighting_interpolated_bits)]
format_args!($sql);
let mut stmt = $conn.prepare($sql)?;
$crate::__bind!(stmt, $sql);
stmt
}};
}
/// Captured identifiers in SQL
#[macro_export]
macro_rules! prepare_cached_and_bind {
($conn:expr, $sql:literal) => {{
#[cfg(trick_rust_analyzer_into_highlighting_interpolated_bits)]
format_args!($sql);
let mut stmt = $conn.prepare_cached($sql)?;
$crate::__bind!(stmt, $sql);
stmt
}};
}
/// A typedef of the result returned by many methods. /// A typedef of the result returned by many methods.
pub type Result<T, E = Error> = result::Result<T, E>; pub type Result<T, E = Error> = result::Result<T, E>;
@ -2088,9 +2114,20 @@ mod test {
} }
#[test] #[test]
pub fn db_readonly() -> Result<()> { fn db_readonly() -> Result<()> {
let db = Connection::open_in_memory()?; let db = Connection::open_in_memory()?;
assert!(!db.is_readonly(MAIN_DB)?); assert!(!db.is_readonly(MAIN_DB)?);
Ok(()) Ok(())
} }
#[test]
fn prepare_and_bind() -> Result<()> {
let db = Connection::open_in_memory()?;
let name = "Lisa";
let age = 8;
let mut stmt = prepare_and_bind!(db, "SELECT $name, $age;");
let (v1, v2) = stmt.raw_query().get_expected_row().and_then(|r| Ok((r.get::<_,String>(0)?, r.get::<_,i64>(1)?)))?;
assert_eq!((v1.as_str(), v2), (name, age));
Ok(())
}
} }