use sqlx::postgres::PgArguments;
use crate::Params;
use crate::Result;
#[derive(Clone, Debug)]
#[must_use]
#[non_exhaustive]
pub struct BuiltQuery {
pub sql: String,
pub params: Params,
pub cacheable: bool,
}
impl BuiltQuery {
pub fn arguments(&self) -> Result<PgArguments> {
self.params.arguments()
}
}
#[cfg(test)]
mod tests {
use crate::{BuiltQuery, Param, Params};
#[test]
fn built_query_arguments_delegate_to_stored_params() {
let built = BuiltQuery {
sql: "select $1".to_owned(),
params: Params::from_vec(vec![Param::typed(1_i32)]),
cacheable: true,
};
built.arguments().unwrap();
}
#[test]
fn built_query_clone_keeps_sql_params_and_cacheability() {
let built = BuiltQuery {
sql: "select $1".to_owned(),
params: Params::from_vec(vec![Param::typed("x".to_owned())]),
cacheable: false,
};
let cloned = built.clone();
assert_eq!(cloned.sql, "select $1");
assert_eq!(cloned.params.len(), 1);
assert!(!cloned.cacheable);
}
#[test]
fn built_query_stream_methods_type_check() {
fn assert_type_checks(pool: sqlx::PgPool, built: BuiltQuery) {
let rows = built.fetch_stream(&pool).unwrap();
drop(rows);
let typed_rows = built.fetch_stream_as::<(i64,)>(&pool).unwrap();
drop(typed_rows);
let scalars = built.fetch_stream_scalar::<i64>(&pool).unwrap();
drop(scalars);
let owned_rows = built.clone().fetch_stream_pool(pool.clone()).unwrap();
drop(owned_rows);
let owned_typed_rows = built
.clone()
.fetch_stream_pool_as::<(i64,)>(pool.clone())
.unwrap();
drop(owned_typed_rows);
let owned_scalars = built.fetch_stream_pool_scalar::<i64>(pool).unwrap();
drop(owned_scalars);
}
let _ = assert_type_checks as fn(sqlx::PgPool, BuiltQuery);
}
}