use crate::expr::Expr;
use crate::expr::write_expr;
use crate::types::IntoColumnRef;
use crate::writer::SqlWriter;
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
#[expect(missing_docs)]
pub enum Returning {
All,
Exprs(Vec<Expr>),
}
impl Returning {
pub fn all() -> Self {
Self::All
}
pub fn column<T>(col: T) -> Self
where
T: IntoColumnRef,
{
Self::Exprs(vec![Expr::column(col)])
}
pub fn columns<T, I>(cols: I) -> Self
where
I: IntoIterator<Item = T>,
T: IntoColumnRef,
{
Self::Exprs(cols.into_iter().map(Expr::column).collect())
}
pub fn exprs<T, I>(exprs: I) -> Self
where
I: IntoIterator<Item = T>,
T: Into<Expr>,
{
Self::Exprs(exprs.into_iter().map(Into::into).collect())
}
}
pub(crate) fn write_returning<W: SqlWriter>(w: &mut W, returning: &Returning) {
w.push_str(" RETURNING ");
match returning {
Returning::All => w.push_char('*'),
Returning::Exprs(exprs) => {
for (i, expr) in exprs.iter().enumerate() {
if i > 0 {
w.push_str(", ");
}
write_expr(w, expr);
}
}
}
}