logo
pub struct WithQuery { /* private fields */ }
Expand description

A WITH query. A simple SQL query that has a WITH clause (WithClause).

The WithClause 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::cte and the Self::query.

Implementations

Constructs a new empty WithQuery.

Set the whole WithClause.

Set the WithClause::recursive. See that method for more information.

Add the WithClause::search. See that method for more information.

Set the WithClause::cycle. See that method for more information.

Add a CommonTableExpression to the with clause. See WithClause::cte.

Set the query that you execute with the WithClause.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Build corresponding SQL statement into the SqlWriter for certain database backend and collect query parameters

Build corresponding SQL statement for certain database backend and collect query parameters into a vector

Build corresponding SQL statement for certain database backend and collect query parameters

Build corresponding SQL statement for certain database backend and collect query parameters Read more

Build corresponding SQL statement for certain database backend and return SQL string Read more

Build corresponding SQL statement for certain database backend and collect query parameters into a vector Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more