classCompound
final class Compound
implements Statement, Query
uses Snapshot
@visibility public.A compound query: arms combined from left to right by UNION, UNION ALL, INTERSECT and EXCEPT.
An ORDER BY and a LIMIT written after the last arm order and limit the combined rows, so they belong to the compound and not to that arm. The grammar also admits them on an earlier arm, which SQLite rejects; there they stay on the arm.
Rule: SQLITE-COMPOUND-001. The facts are derived by SQLITE-COMPOUND-SCOPE-001. Source: https://sqlite.org/lang_select.html#compound_select_statements. Status: Implemented.
$query = (new \SqlSemantics\Platform\Sqlite\Semantics())->analyze('SELECT 1 AS a UNION SELECT 2 ORDER BY a LIMIT 1');
[count($query->statement->steps), count($query->statement->orderBy), $query->statement->steps[0]->query->orderBy, $query->toString()] // => [1, 1, [], 'SELECT 1 AS a UNION SELECT 2 ORDER BY a LIMIT 1']$ordered = (new \SqlSemantics\Platform\Sqlite\Semantics())->analyze('SELECT 1 AS a ORDER BY a')->statement;
new \SqlSemantics\Platform\Sqlite\Statement\Query\Compound(new \SqlSemantics\Platform\Sqlite\Statement\Query\ValuesClause([new \SqlSemantics\Platform\Sqlite\Statement\Query\ValueRow([new \SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\NullLiteral()])]), [new \SqlSemantics\Platform\Sqlite\Statement\Query\CompoundStep(\SqlSemantics\Platform\Sqlite\Statement\Query\CompoundOperator::Union, $ordered)]) // throws \SqlSemantics\Platform\Sqlite\Model\Diagnostic\InvalidConstructionProperties§
public non-empty-list<CompoundStep> $stepspublic list<SortTerm> $orderBypublic Select|ValuesClause $firstpublic Limit|null $limit = nullMethods§
public function __construct(
public Select|ValuesClause $first,
list<CompoundStep> $steps,
list<SortTerm> $orderBy = [],
public Limit|null $limit = null,
)Parameters
$first | Select|ValuesClause | The first arm |
$steps | list<CompoundStep> | The further arms in written order; at least one |
$orderBy | list<SortTerm> | The ORDER BY terms of the combined rows |
$limit | Limit|null | The LIMIT clause of the combined rows |
Calls 7
- static-call Check::listOf() line 63
- class-const CompoundStep::class() line 63
- class-const SortTerm::class() line 64
- function-call
countline 65 - static-call Check::input() line 66
- method-call Ordinals::value() line 69
- new Ordinals line 69
public function deriveStatement(Derivation $derivation): voidDerives the compound query as a statement root and records its rows as the output.
Parameters
$derivation | Derivation |
Calls 5
- method-call ProgramOnly::outsideProgram() line 78
- new ProgramOnly line 78
- method-call Derivation::output() line 79
- method-call Derivation::query() line 79
- method-call Derivation::environment() line 79
public function deriveQuery(Derivation $derivation, Environment $outer): QueryFactDerives the arms, the ordering and the output fields.
Parameters
$derivation | Derivation | |
$outer | Environment |
Returns
Calls 2
- method-call CompoundFacts::derive() line 87
- new CompoundFacts line 87
public function render(Output $out): voidWrites the arms, then the ordering and the limit.
Parameters
$out | Output |
Calls 2
- method-call Output::node() line 95
- method-call Output::keyword() line 100
Test cases 35§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 7
CompoundTest::testDeriveQueryOrdersTheCombinedRowsByTheNamesOfEveryArmcallsCompoundTest::testDeriveStatementNamesTheOutputAfterTheFirstArmcallsCompoundTest::testRenderRefusesAnIntegerConstantAsAnOrdinaryOrderByTermcallsCompoundTest::testRenderRefusesAnOrderingAfterAValuesArmcallsCompoundTest::testRenderRefusesAnOrderingLeftOnTheLastArmcallsCompoundTest::testRenderRefusesASingleArmcallsCompoundTest::testRenderWritesANewlyBuiltCompoundcalls
Other tests reaching this symbol 28
MutationRuleTest::testInsertLowersWrittenRowsAndEveryOtherSourceAsAQuerycallsQueryCommandsTest::testCommandLowersEveryFormOfASelectCommandcallsSelectRuleTest::testArmLowersASelectionOrAValuesClausecallsSelectRuleTest::testBodyLeavesACompoundThatEndsInValuesWithoutOrderByAndLimitcallsSelectRuleTest::testBodyLowersACompoundQueryWithItsArmsInOrderAndTheTrailingOrderByAndLimitcallsSelectRuleTest::testOperatorLowersEveryCompoundOperatorAsWrittencallsSelectRuleTest::testSelectLowersAPlainQueryAndAQueryWithCommonTablescallsColumnTypingTest::testLeftmostReadsTheLeftmostArmOfADefiningQuerycallsExpressionAffinityTest::testArmsFlattensWithClausesCompoundsAndValuesRowscallsExpressionAffinityTest::testColumnAdjustsACompoundByTheOtherArmscallsExpressionAffinityTest::testColumnIsNotDeterminedWhenAnArmDependsOnAMissingDeclarationcallsQueryColumnsTest::testNamedGivesTheNamesSqliteGivesAfterResolutioncallsQueryColumnsTest::testTableColumnsRecordACompoundFromItsArmscallsQueryColumnsTest::testViewColumnsRecordACompoundFromItsArmscallsCompoundFactsTest::testAliasesNamesTheOutputAfterEveryArmcallsCompoundFactsTest::testCombinedAnswersAnOpenStarWhenTheFirstArmIsOpencallsCompoundFactsTest::testCombinedDependsOnAnOpenLaterArmcallsCompoundFactsTest::testCombinedTakesTheNamesOfTheFirstArmAndReportsAnotherWidthOncecallsCompoundFactsTest::testDeriveCombinesTheArmsAndOrdersByTheNamesOfEveryArmcallsCompoundFactsTest::testDeriveReportsAnOrderingOrALimitOnAnEarlierArmcallsCompoundFactsTest::testReboundGivesTheRecursiveReferenceTheNamesOfTheColumnListAndAnyStorageClasscallsCompoundFactsTest::testReboundLeavesAnEnvironmentWithoutTheTableUntouchedcallsCompoundFactsTest::testReboundNamesTheColumnsAfterTheFirstArmWithoutAColumnListcallsResultNamesTest::testLeftmostFindsTheArmThatNamesTheColumnscallsCompoundStepTest::testRenderWritesIntersectcallsCompoundStepTest::testRenderWritesTheOperatorBeforeTheArmcallsOutputOrdinalTest::testDeriveScalarCountsTheColumnsOfACompoundcallsWithQueryTest::testDeriveQueryTypesARecursiveColumnAsAnyStorageClassInTheRecursiveArmcalls
Relations§
Instantiated in 2
Constant reads 1
Type checks 2
- instanceof SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ExpressionAffinity::arms() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ExpressionAffinity.php:205
- instanceof SqlSemantics\Platform\Sqlite\Rules\Query\ResultNames::leftmost() packages/sql-semantics-sqlite/src/Rules/Query/ResultNames.php:209
Type declarations 6
- type SqlSemantics\Platform\Sqlite\Lowering\Query\SelectRule packages/sql-semantics-sqlite/src/Lowering/Query/SelectRule.php:61
- type SqlSemantics\Platform\Sqlite\Lowering\Query\SelectRule packages/sql-semantics-sqlite/src/Lowering/Query/SelectRule.php:78
- type SqlSemantics\Platform\Sqlite\Lowering\Trigger\TriggerRule packages/sql-semantics-sqlite/src/Lowering/Trigger/TriggerRule.php:155
- type SqlSemantics\Platform\Sqlite\Rules\Query\CompoundFacts packages/sql-semantics-sqlite/src/Rules/Query/CompoundFacts.php:62
- type SqlSemantics\Platform\Sqlite\Rules\Query\CompoundFacts packages/sql-semantics-sqlite/src/Rules/Query/CompoundFacts.php:173
- type SqlSemantics\Platform\Sqlite\Statement\Query\With\WithQuery packages/sql-semantics-sqlite/src/Statement/Query/With/WithQuery.php:41