classQueryExpression
final class QueryExpression
implements Statement, Query
uses Snapshot
@visibility public.A query expression: a WITH clause, or an ORDER BY and LIMIT that apply to a whole set operation or parenthesized query.
It exists only when it holds a WITH clause, an ordering or a limit. Over a single query block the ordering and the limit belong to the block itself, so a body that is a selection takes no ordering or limit here, unless an ORDER BY follows a block that orders or limits its rows itself: a 5.6 subquery accepts that, and the server orders the rows of the block in a query level of their own (the order_clause action adds a fake query block). The body is not itself a query expression or a query statement, which are merged into or wrapped around this one.
Rule: MYSQL-QUERY-EXPRESSION-001. The facts are derived by MYSQL-QUERY-EXPRESSION-FACTS-001. Source: https://dev.mysql.com/doc/refman/8.4/en/union.html, https://dev.mysql.com/doc/refman/8.4/en/with.html. Status: Implemented.
$query = (new \SqlSemantics\Platform\MySql\Semantics())->analyze('SELECT a FROM t UNION SELECT b FROM u ORDER BY 1 LIMIT 2');
[count($query->statement->orderBy), $query->statement->limit !== null] // => [1, true]new \SqlSemantics\Platform\MySql\Statement\Query\QueryExpression(null, new \SqlSemantics\Platform\MySql\Statement\Query\Select([], [new \SqlSemantics\Platform\MySql\Statement\Query\SelectExpression(new \SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral('1'))])) // throws \SqlSemantics\Platform\MySql\Model\Diagnostic\InvalidConstructionProperties§
public list<OrderItem> $orderBypublic WithClause|null $withpublic Query $bodypublic Limit|null $limit = nullMethods§
public function __construct(
public WithClause|null $with,
public Query $body,
list<OrderItem> $orderBy = [],
public Limit|null $limit = null,
)Parameters
$with | WithClause|null | The WITH clause |
$body | Query | The query the clauses apply to |
$orderBy | list<OrderItem> | The ORDER BY items |
$limit | Limit|null | The LIMIT clause |
Calls 5
- static-call Check::listOf() line 62
- class-const OrderItem::class() line 62
- method-call SortScopes::check() line 63
- new SortScopes line 63
- static-call Check::input() line 64
public function deriveStatement(Derivation $derivation): voidDerives the query expression as a statement root and records its rows as the output.
Parameters
$derivation | Derivation |
Calls 3
- method-call Derivation::output() line 74
- method-call Derivation::query() line 74
- method-call Derivation::environment() line 74
public function deriveQuery(Derivation $derivation, Environment $outer): QueryFactBinds the common tables and derives the body, the ordering and the limit.
Parameters
$derivation | Derivation | |
$outer | Environment |
Returns
Calls 2
- method-call ExpressionFacts::derive() line 82
- new ExpressionFacts line 82
public function render(Output $out): voidWrites the clauses in grammar order.
Parameters
$out | Output |
Calls 3
- method-call Output::node() line 90
- method-call QueryTails::expression() line 91
- new QueryTails line 91
Test cases 17§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 6
QueryExpressionTest::testAnIntegerOrderingItemIsRejectedcallsQueryExpressionTest::testAnOrderingOverASingleBlockIsRejectedcallsQueryExpressionTest::testAWrapperWithoutClausesIsRejectedcallsQueryExpressionTest::testDeriveQueryBindsTheCommonTablesForTheBodycallsQueryExpressionTest::testDeriveQueryResolvesOrdinalsAgainstTheOutputcallsQueryExpressionTest::testDeriveStatementRecordsTheRowsOfTheBodycalls
Other tests reaching this symbol 11
ChainTest::testEnclosedKeepsTheClausesAfterTheFirstParenthesizedOperandcallsChainTest::testQueryLiftsTheClausesOfTheLastBlockcallsUnionRuleTest::testStatementLiftsTheOrderingOfTheLastSelectcallsExpressionRuleTest::testExpressionWrapsANonBlockBodycallsBlockTest::testFinishOrdersTheRowsOfABlockThatLimitsItsRowscallsTrailerTest::testWrapPutsTheOrderingAndTheLockingAroundAQuerycallsExpressionFactsTest::testDeriveResolvesTheOrderingAgainstTheOutputAndBindsTheTablescallsQueryTailsTest::testExpressionWritesTheOrderingAndTheLimitOnlycallsOrderedSetOperationTest::testRenderWritesTheOrderingAfterTheLockingClauseOfTheLastSelectcallsSetOperationTest::testAQueryExpressionOnTheRightIsRejectedcallsWithClauseTest::testBindAnswersTheScopeWithTheCommonTablescalls
Relations§
Instantiated in 3
- new SqlSemantics\Platform\MySql\Lowering\Query\Modern\ExpressionRule::expression() packages/sql-semantics-mysql/src/Lowering/Query/Modern/ExpressionRule.php:122
- new SqlSemantics\Platform\MySql\Lowering\Query\Modern\ExpressionRule::expression() packages/sql-semantics-mysql/src/Lowering/Query/Modern/ExpressionRule.php:124
- new SqlSemantics\Platform\MySql\Lowering\Query\Shared\Trailer::wrap() packages/sql-semantics-mysql/src/Lowering/Query/Shared/Trailer.php:93
Type checks 11
- instanceof SqlSemantics\Platform\MySql\Rules\Dml\GeneratedWrites::valueRows() packages/sql-semantics-mysql/src/Rules/Dml/GeneratedWrites.php:122
- instanceof SqlSemantics\Platform\MySql\Rules\Dml\GeneratedWrites::valueRows() packages/sql-semantics-mysql/src/Rules/Dml/GeneratedWrites.php:123
- instanceof SqlSemantics\Platform\MySql\Rules\Query\CommonTables::operands() packages/sql-semantics-mysql/src/Rules/Query/CommonTables.php:154
- instanceof SqlSemantics\Platform\MySql\Rules\Routine\ProgramRestrictions::into() packages/sql-semantics-mysql/src/Rules/Routine/ProgramRestrictions.php:152
- instanceof SqlSemantics\Platform\MySql\Rules\TableDefinition\EnclosedQuery::accepts() packages/sql-semantics-mysql/src/Rules/TableDefinition/EnclosedQuery.php:46
- instanceof SqlSemantics\Platform\MySql\Rules\TableDefinition\EnclosedQuery::write() packages/sql-semantics-mysql/src/Rules/TableDefinition/EnclosedQuery.php:70
- instanceof SqlSemantics\Platform\MySql\Rules\TableDefinition\ViewColumns::block() packages/sql-semantics-mysql/src/Rules/TableDefinition/ViewColumns.php:134
- instanceof SqlSemantics\Platform\MySql\Statement\Query\QueryStatement::__construct() packages/sql-semantics-mysql/src/Statement/Query/QueryStatement.php:63
- instanceof SqlSemantics\Platform\MySql\Statement\Query\Set\LeadingUnion::__construct() packages/sql-semantics-mysql/src/Statement/Query/Set/LeadingUnion.php:62
- instanceof SqlSemantics\Platform\MySql\Statement\Query\Set\OrderedSetOperation::__construct() packages/sql-semantics-mysql/src/Statement/Query/Set/OrderedSetOperation.php:85
- instanceof SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperation::__construct() packages/sql-semantics-mysql/src/Statement/Query/Set/SetOperation.php:63