quaint_forked/ast/
cte.rs

1use std::borrow::Cow;
2
3use super::SelectQuery;
4
5/// A builder for a common table expression (CTE) statement, to be used in the
6/// `WITH` block of a `SELECT` statement.
7///
8/// See [`Select#with`] for usage.
9///
10/// [`Select#with`]: struct.Select.html#method.with
11#[derive(Debug, PartialEq, Clone)]
12pub struct CommonTableExpression<'a> {
13    pub(crate) identifier: Cow<'a, str>,
14    pub(crate) columns: Vec<Cow<'a, str>>,
15    pub(crate) selection: SelectQuery<'a>,
16}
17
18impl<'a> CommonTableExpression<'a> {
19    /// Selects a named value from the nested expresion. The statement selects
20    /// everything if this method is never called.
21    pub fn column(mut self, column: impl Into<Cow<'a, str>>) -> Self {
22        self.columns.push(column.into());
23        self
24    }
25}
26
27/// Conversion into a common table expression.
28///
29/// Used together with the [`Select#with`] method.
30///
31/// [`Select#with`]: struct.Select.html#method.with
32pub trait IntoCommonTableExpression<'a> {
33    fn into_cte(self, identifier: impl Into<Cow<'a, str>>) -> CommonTableExpression<'a>
34    where
35        Self: Into<SelectQuery<'a>>,
36    {
37        CommonTableExpression {
38            identifier: identifier.into(),
39            columns: Vec::new(),
40            selection: self.into(),
41        }
42    }
43}