sqlx_askama_template/
lib.rs

1#![doc = include_str!("../README.md")]
2
3use sqlx_core::{Error, database::Database};
4mod v3;
5pub use askama;
6pub use sqlx_askama_template_macro::*;
7pub use v3::*;
8
9/// SQL template trait
10///
11/// Defines basic operations for rendering SQL from templates
12pub trait SqlTemplate<'q, DB>: Sized + Clone
13where
14    DB: Database,
15{
16    /// Renders the SQL template using a custom placeholder encoding function
17    ///
18    /// Writes the rendered SQL to the provided buffer and handles parameter encoding.
19    /// The placeholder function (if provided) formats parameter placeholders (e.g., $1, ?)
20    /// based on their index.
21    ///
22    /// # Parameters
23    /// - `f`: Optional function to format parameter placeholders (receives index and buffer)
24    /// - `sql_buffer`: Mutable string buffer to store the rendered SQL
25    ///
26    /// # Returns
27    /// Encoded database arguments (None if no parameters) or an error if rendering fails
28    fn render_sql_with_encode_placeholder_fn(
29        self,
30        f: Option<fn(usize, &mut String)>,
31        sql_buffer: &mut String,
32    ) -> Result<Option<DB::Arguments<'q>>, Error>;
33    /// Renders SQL template and returns query string with parameters
34    fn render_sql(self) -> Result<(String, Option<DB::Arguments<'q>>), Error> {
35        let mut sql_buff = String::new();
36        let arg = self.render_sql_with_encode_placeholder_fn(None, &mut sql_buff)?;
37        Ok((sql_buff, arg))
38    }
39
40    /// Renders SQL template and returns executable query result
41    fn render_executable(
42        self,
43        sql_buffer: &'q mut String,
44    ) -> Result<SqlTemplateExecute<'q, DB>, Error> {
45        let (sql, arguments) = self.render_sql()?;
46        *sql_buffer = sql;
47        Ok(SqlTemplateExecute {
48            sql: sql_buffer,
49
50            arguments,
51
52            persistent: true,
53        })
54    }
55    #[deprecated(note = "use `adapter_render` instead")]
56    fn render_db_adapter_manager(
57        self,
58        _sql_buff: &'q mut String,
59    ) -> DBAdapterManager<'q, DB, Self> {
60        self.adapter_render()
61    }
62    /// Creates a database adapter manager for the template
63    ///
64    /// Provides an adapter pattern interface for managing template rendering
65    /// in database-specific scenarios.
66    ///
67    /// # Returns
68    /// A new `DBAdapterManager` instance wrapping the template
69    fn adapter_render(self) -> DBAdapterManager<'q, DB, Self> {
70        DBAdapterManager::new(self)
71    }
72}