pub struct SqlQuery<'a, T: SqlEntity> { /* private fields */ }Expand description
A query builder.
This is the main structure to build the SQL queries using a templating system.
It is designed to be a composable structure to build the SQL queries
alongside the according parameters.
The query is built using the set_variable method to set the variables
and the add_parameter method to add the parameters.
The query is expanded using the expand method to get the SQL query and
the parameters.
Implementations§
Source§impl<'a, T: SqlEntity> SqlQuery<'a, T>
impl<'a, T: SqlEntity> SqlQuery<'a, T>
Sourcepub fn new(query: &str) -> Self
pub fn new(query: &str) -> Self
Create a new query using the given string as a SQL template.
The template is a string with the variables enclosed in {:variable:}
placeholders.
The default variable is the projection of the entity.
Sourcepub fn set_variable(&mut self, name: &'a str, value: &str) -> &mut Self
pub fn set_variable(&mut self, name: &'a str, value: &str) -> &mut Self
Set a variable in the query. This variable will be replaced by its value in the query.
Sourcepub fn add_parameter(&mut self, parameter: &'a dyn ToSqlAny) -> &mut Self
pub fn add_parameter(&mut self, parameter: &'a dyn ToSqlAny) -> &mut Self
Add a parameter to the query. This parameter will be replaced by its
value in the query. The parameter will be expanded in the $? placeholder.
Sourcepub fn append_parameters(
&mut self,
parameters: Vec<&'a dyn ToSqlAny>,
) -> &mut Self
pub fn append_parameters( &mut self, parameters: Vec<&'a dyn ToSqlAny>, ) -> &mut Self
Append a vec of parameters to the query.
Sourcepub fn set_parameters(&mut self, parameters: Vec<&'a dyn ToSqlAny>) -> &mut Self
pub fn set_parameters(&mut self, parameters: Vec<&'a dyn ToSqlAny>) -> &mut Self
Set the parameters of the query.
Sourcepub fn get_variables(&self) -> &HashMap<&'a str, String>
pub fn get_variables(&self) -> &HashMap<&'a str, String>
Return the variables of the query.
Sourcepub fn get_parameters(&self) -> Vec<&'a dyn ToSqlAny>
pub fn get_parameters(&self) -> Vec<&'a dyn ToSqlAny>
Return the parameters of the query. This method is mostly intended for testing purposes since the parameters are cloned.