odbc_api_derive/lib.rs
1mod expand;
2
3use proc_macro::TokenStream;
4use syn::{DeriveInput, parse_macro_input};
5
6use crate::expand::expand;
7
8/// Use this to derive the trait `FetchRow` for structs defined in the application logic.
9///
10/// # Example
11///
12/// ```
13/// use odbc_api_derive::Fetch;
14/// use odbc_api::{Connection, Error, Cursor, parameter::VarCharArray, buffers::RowVec};
15///
16/// #[derive(Default, Clone, Copy, Fetch)]
17/// struct Person {
18/// first_name: VarCharArray<255>,
19/// last_name: VarCharArray<255>,
20/// }
21///
22/// fn send_greetings(conn: &mut Connection) -> Result<(), Error> {
23/// let max_rows_in_batch = 250;
24/// let buffer = RowVec::<Person>::new(max_rows_in_batch);
25/// let mut cursor = conn.execute("SELECT first_name, last_name FROM Persons", (), None)?
26/// .expect("SELECT must yield a result set");
27/// let mut block_cursor = cursor.bind_buffer(buffer)?;
28///
29/// while let Some(batch) = block_cursor.fetch()? {
30/// for person in batch.iter() {
31/// let first = person.first_name.as_str()
32/// .expect("First name must be UTF-8")
33/// .expect("First Name must not be NULL");
34/// let last = person.last_name.as_str()
35/// .expect("Last name must be UTF-8")
36/// .expect("Last Name must not be NULL");
37/// println!("Hello {first} {last}!")
38/// }
39/// }
40/// Ok(())
41/// }
42/// ```
43#[proc_macro_derive(Fetch)]
44pub fn derive_fetch_row(item: TokenStream) -> TokenStream {
45 let input = parse_macro_input!(item as DeriveInput);
46 let output = expand(input);
47 proc_macro::TokenStream::from(output)
48}