classCreateTrigger
final class CreateTrigger
implements Statement
uses Snapshot
Public API: explicitly declared with
@visibility public.A request to create a trigger: a program of statements that runs when rows of a table change.
Rule: SQLITE-CREATE-TRIGGER-001. The facts are derived by SQLITE-TRIGGER-SCOPE-001. The statements of the program have the forms the trigger grammar admits: a query, or an INSERT, UPDATE or DELETE without a WITH clause and without a correlation name for the written table; an UPDATE or DELETE has no RETURNING clause. The statement executes nothing and provides no relation declaration. Source: https://sqlite.org/lang_createtrigger.html. Status: Implemented.
$trigger = (new \SqlSemantics\Platform\Sqlite\Semantics())->analyze('CREATE TEMP TRIGGER IF NOT EXISTS r AFTER UPDATE OF a ON t FOR EACH ROW WHEN new.a > old.a BEGIN DELETE FROM log; SELECT 1; END');
$statement = $trigger->statement;
[$statement->name->name->value, $statement->temporary, $statement->timing, $statement->columns[0]->value, $statement->forEachRow, count($statement->steps)] // => ['r', true, \SqlSemantics\Platform\Sqlite\Statement\Trigger\TriggerTiming::After, 'a', true, 2]$delete = (new \SqlSemantics\Platform\Sqlite\Semantics())->analyze('DELETE FROM t RETURNING a')->statement;
new \SqlSemantics\Platform\Sqlite\Statement\Trigger\CreateTrigger(new \SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name('r')), \SqlSemantics\Platform\Sqlite\Statement\Trigger\TriggerEvent::Delete, new \SqlSemantics\Platform\Sqlite\Statement\Trigger\TriggerTable(new \SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name('t'))), [$delete]) // throws \SqlSemantics\Platform\Sqlite\Model\Diagnostic\InvalidConstructionProperties§
public non-empty-list<Select|ValuesClause|Compound|WithQuery|InsertRows|InsertSelect|Update|Delete> $stepspublic list<Name> $columnspublic QualifiedName $namepublic TriggerEvent $eventpublic TriggerTable $tablepublic TriggerTiming|null $timing = nullpublic Scalar|null $when = nullpublic bool $forEachRow = falsepublic bool $temporary = falsepublic bool $ifNotExists = falseMethods§
public function __construct(
public QualifiedName $name,
public TriggerEvent $event,
public TriggerTable $table,
list<Select|ValuesClause|Compound|WithQuery|InsertRows|InsertSelect|Update|Delete> $steps,
public TriggerTiming|null $timing = null,
list<Name> $columns = [],
public Scalar|null $when = null,
public bool $forEachRow = false,
public bool $temporary = false,
public bool $ifNotExists = false,
)Parameters
$name | QualifiedName | The trigger name |
$event | TriggerEvent | The change that fires the trigger |
$table | TriggerTable | The watched table |
$steps | list<Select|ValuesClause|Compound|WithQuery|InsertRows|InsertSelect|Update|Delete> | The statements of the program; at least one |
$timing | TriggerTiming|null | The written timing |
$columns | list<Name> | The columns of UPDATE OF |
$when | Scalar|null | The condition of WHEN |
$forEachRow | bool | Whether FOR EACH ROW is written |
$temporary | bool | Whether TEMP or TEMPORARY is written |
$ifNotExists | bool | Whether IF NOT EXISTS is written |
Throws
InvalidConstruction When a program statement is of no admitted formCalls 14
- method-call ClosedList::of() line 87
- new ClosedList line 87
- class-const Select::class() line 87
- class-const ValuesClause::class() line 87
- class-const Compound::class() line 87
- class-const WithQuery::class() line 87
- class-const InsertRows::class() line 87
- class-const InsertSelect::class() line 87
- class-const Update::class() line 87
- class-const Delete::class() line 87
- static-call Check::input() line 90
- static-call Check::listOf() line 96
- class-const Name::class() line 96
- class-const TriggerEvent::Update() line 97
public function deriveStatement(Derivation $derivation): voidDerives the watched table, the condition and the program.
Parameters
$derivation | Derivation |
Calls 2
- method-call TriggerFacts::derive() line 105
- new TriggerFacts line 105
public function render(Output $out): voidWrites the definition.
Parameters
$out | Output |
Calls 8
- method-call Output::keyword() line 113
- method-call Output::name() line 122
- class-const NameUse::Qualifier() line 122
- class-const NameUse::Label() line 124
- function-call
explodeline 126 - method-call Output::symbol() line 130
- class-const NameUse::Column() line 131
- method-call Output::node() line 142
Test cases 35§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 8
CreateTriggerTest::testDeriveStatementChecksTheColumnsOfAnUpdateOfTriggercallsCreateTriggerTest::testDeriveStatementResolvesNewAndOldToTheWatchedTablecallsCreateTriggerTest::testRejectsAnEmptyProgramcallsCreateTriggerTest::testRejectsAnInsertWithACorrelationNamecallsCreateTriggerTest::testRejectsAStepWithAReturningClausecallsCreateTriggerTest::testRejectsAStepWithAWithClausecallsCreateTriggerTest::testRejectsColumnsOnAnEventOtherThanUpdatecallsCreateTriggerTest::testRenderWritesEveryPartInGrammarOrdercalls
Other tests reaching this symbol 27
ExpressionRuleTest::testExpressionLowersRaiseWithAndWithoutAMessagecallsConflictRuleTest::testRaisedLowersTheAlgorithmsRaiseCanNamecallsFlagRuleTest::testIfNotExistsIsTrueOnlyWhenTheClauseIsWrittencallsFlagRuleTest::testTemporaryIsTrueForTempAndTemporaryAndFalseWithoutEithercallsNameRuleTest::testQualifiedLowersAFullnameWithAndWithoutASchemacallsQueryCommandsTest::testCommandLowersACreateTriggerCommandcallsTriggerRuleTest::testCreateLeavesEveryOptionalPartEmptyWhenNotWrittencallsTriggerRuleTest::testCreateLowersTheDeclarationAndTheProgramcallsTriggerRuleTest::testEventLowersDeleteInsertUpdateAndUpdateOfcallsTriggerRuleTest::testProgramLowersEveryStatementInWrittenOrdercallsTriggerRuleTest::testStepLowersAnInsertWithItsUpsertAndADeleteWithItsPredicatecallsTriggerRuleTest::testStepLowersAnUpdateWithEveryClausecallsTriggerRuleTest::testTargetLowersTheWrittenTableWithItsIndexChoiceAndReportsWhatSqliteRefusescallsTriggerRuleTest::testTimingLowersBeforeAfterInsteadOfAndNonecallsTriggerRuleTest::testWhenIsNullWithoutTheClausecallsTriggerFactsTest::testDeriveHidesTheRowThatTheEventDoesNotProvidecallsTriggerFactsTest::testDeriveMakesTheRowsOfTheEventVisibleByQualifierOnlycallsTriggerFactsTest::testDeriveReportsAParameterAnywhereInTheProgramcallsTriggerFactsTest::testDeriveReportsTriggerOnlyMisusesAndTheColumnsOfUpdateOfcallsRaiseActionTest::testFromMatchesTheActionOfAnAnalyzedRaisecallsRaiseTest::testDeriveScalarYieldsNoValueInsideATriggercallsTriggerEventTest::testCasesAreReadFromTheEventWordWhateverItsSpellingcallsTriggerTableTest::testAliasIsAlwaysNullcallsTriggerTableTest::testDeriveRelationResolvesTheWatchedTableInTheContextcallsTriggerTableTest::testNameAnswersTheWatchedTablecallsTriggerTableTest::testRenderWritesTheSchemaAndTheNamecallsTriggerTimingTest::testCasesAreReadFromTheTimingWordsOrAbsentcalls