Skip to main content

vantage_sql/primitives/
alias.rs

1use super::identifier::Identifier;
2use vantage_expressions::{Expression, Expressive, expr_any};
3
4/// Extension trait that adds `.as_alias()` to any [`Expressive<T>`] type.
5///
6/// Wraps the expression as `expr AS <quoted_alias>`, using [`Identifier`]
7/// for backend-aware quoting.
8///
9/// ```ignore
10/// use vantage_sql::primitives::alias::AliasExt;
11///
12/// Fx::new("count", [mysql_expr!("*")]).as_alias("cnt")
13/// // → COUNT(*) AS `cnt`   (MySQL)
14/// // → COUNT(*) AS "cnt"   (PostgreSQL)
15/// ```
16pub trait AliasExt<T>: Expressive<T> + Sized {
17    #[allow(clippy::wrong_self_convention)]
18    fn as_alias(self, alias: impl Into<String>) -> Expression<T>
19    where
20        Identifier: Expressive<T>,
21    {
22        expr_any!("{} AS {}", (self), (Identifier::new(alias)))
23    }
24}
25
26impl<T, E: Expressive<T> + Sized> AliasExt<T> for E {}