Struct starfish::migrator::prelude::CommonTableExpression
[−]pub struct CommonTableExpression { /* private fields */ }
Expand description
A table definition inside a WITH clause (WithClause).
A WITH clause can contain one or multiple common table expressions (CommonTableExpression).
These named queries can act as a “query local table” that are materialized during execution and then can be used by the query prefixed with the WITH clause.
A WITH clause can contain multiple of these CommonTableExpression. (Except in the case of recursive WITH query which can only contain one CommonTableExpression).
A CommonTableExpression is a name, column names and a query returning data for those columns.
Some databases (like sqlite) restrict the acceptable kinds of queries inside of the WITH clause common table expressions. These databases only allow SelectStatements to form a common table expression.
Other databases like postgres allow modification queries (UPDATE, DELETE) inside of the WITH clause but they have to return a table. (They must have a RETURNING clause).
sea-query doesn’t check this or restrict the kind of CommonTableExpression that you can create in rust. This means that you can put an UPDATE or DELETE queries into WITH clause and sea-query will succeed in generating that kind of sql query but the execution inside the database will fail because they are invalid.
It is your responsibility to ensure that the kind of WITH clause that you put together makes sense and valid for that database that you are using.
NOTE that for recursive WITH queries (in sql: “WITH RECURSIVE”) you can only have a single CommonTableExpression inside of the WITH clause. That query must match certain requirements:
- It is a query of UNION or UNION ALL of two queries.
- The first part of the query (the left side of the UNION) must be executable first in itself. It must be non-recursive. (Cannot contain self reference)
- The self reference must appear in the right hand side of the UNION.
- The query can only have a single self-reference.
- Recursive data-modifying statements are not supported, but you can use the results of a recursive SELECT query in a data-modifying statement. (like so: WITH RECURSIVE cte_name(a,b,c,d) AS (SELECT … UNION SELECT … FROM … JOIN cte_name ON … WHERE …) DELETE FROM table WHERE table.a = cte_name.a)
It is mandatory to set the Self::table_name and the Self::query.
Implementations
impl CommonTableExpression
impl CommonTableExpression
pub fn new() -> CommonTableExpression
pub fn new() -> CommonTableExpression
Construct a new CommonTableExpression
pub fn table_name<T>(&mut self, table_name: T) -> &mut CommonTableExpression where
T: IntoIden,
pub fn table_name<T>(&mut self, table_name: T) -> &mut CommonTableExpression where
T: IntoIden,
Sets the CTE table name of the query.
pub fn column<C>(&mut self, col: C) -> &mut CommonTableExpression where
C: IntoIden,
pub fn column<C>(&mut self, col: C) -> &mut CommonTableExpression where
C: IntoIden,
Adds a named column to the CTE table definition.
pub fn materialized(&mut self, materialized: bool) -> &mut CommonTableExpression
pub fn materialized(&mut self, materialized: bool) -> &mut CommonTableExpression
Some databases allow you to put “MATERIALIZED” or “NOT MATERIALIZED” in the CTE definition. This will affect how during the execution of WithQuery the CTE in the WithClause will be executed. If the database doesn’t support this syntax this option specified here will be ignored and not appear in the generated sql.
pub fn query<Q>(&mut self, query: Q) -> &mut CommonTableExpression where
Q: QueryStatementBuilder,
pub fn query<Q>(&mut self, query: Q) -> &mut CommonTableExpression where
Q: QueryStatementBuilder,
Set the query generating the CTE content. The query’s result must match the defined columns.
pub fn from_select(select: SelectStatement) -> CommonTableExpression
pub fn from_select(select: SelectStatement) -> CommonTableExpression
Create a CTE from a SelectStatement if the selections are named columns then this will return a CommonTableExpression that has the column names set. The Self::table_name is not set.
pub fn try_set_cols_from_select(&mut self, select: &SelectStatement) -> bool
pub fn try_set_cols_from_select(&mut self, select: &SelectStatement) -> bool
Set up the columns of the CTE to match the given SelectStatement selected columns. This will fail if the select contains non named columns like expressions of wildcards.
Returns true if the column setup from the select query was successful. If the returned value is false the columns are untouched.
Trait Implementations
impl Clone for CommonTableExpression
impl Clone for CommonTableExpression
fn clone(&self) -> CommonTableExpression
fn clone(&self) -> CommonTableExpression
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl Debug for CommonTableExpression
impl Debug for CommonTableExpression
impl Default for CommonTableExpression
impl Default for CommonTableExpression
fn default() -> CommonTableExpression
fn default() -> CommonTableExpression
Returns the “default value” for a type. Read more
Auto Trait Implementations
impl !RefUnwindSafe for CommonTableExpression
impl Send for CommonTableExpression
impl Sync for CommonTableExpression
impl Unpin for CommonTableExpression
impl !UnwindSafe for CommonTableExpression
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
fn instrument(self, span: Span) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
sourcefn in_current_span(self) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
fn in_current_span(self) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T> where
T: Future, type Output = <T as Future>::Output;
where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T> where
T: Future, type Output = <T as Future>::Output;
where
S: Into<Dispatch>,
T: Future, type Output = <T as Future>::Output;
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T> where
T: Future, type Output = <T as Future>::Output;
fn with_current_subscriber(self) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T> where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more