Skip to content

Commit

Permalink
BREAKING! Use TriggerEventExpr instead of plain TriggerEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
nene committed Feb 12, 2024
1 parent 9e0d309 commit d5ca16d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/cst/Expr.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AllColumns, BaseNode, Empty, Keyword } from "./Base";
import { DataType } from "./DataType";
import { Literal, StringLiteral } from "./Literal";
import { Node, Program } from "./Node";
import { Node, Program, TriggerEventExpr } from "./Node";
import {
LimitClause,
OrderByClause,
Expand Down Expand Up @@ -41,6 +41,7 @@ export type Expr =
| ListExpr
| ParenExpr
| BinaryExpr
| BinaryExpr<TriggerEventExpr, Keyword<"OR">, TriggerEventExpr>
| PrefixOpExpr
| PostfixOpExpr
| FuncCall
Expand Down
15 changes: 13 additions & 2 deletions src/cst/Trigger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { BaseNode, Keyword } from "./Base";
import { RelationKind } from "./CreateTable";
import { Expr, Identifier, ListExpr, EntityName, ParenExpr } from "./Expr";
import {
Expr,
Identifier,
ListExpr,
EntityName,
ParenExpr,
BinaryExpr,
} from "./Expr";
import { BlockStmt } from "./ProceduralLanguage";

export type AllTriggerNodes =
Expand All @@ -27,12 +34,16 @@ export interface CreateTriggerStmt extends BaseNode {
ifNotExistsKw?: [Keyword<"IF">, Keyword<"NOT">, Keyword<"EXISTS">];
name: EntityName;
timeKw?: Keyword<"BEFORE" | "AFTER"> | [Keyword<"INSTEAD">, Keyword<"OF">];
event: TriggerEvent;
event: TriggerEventExpr;
target: TriggerTarget;
clauses: TriggerClause[];
body: BlockStmt | ExecuteClause;
}

export type TriggerEventExpr =
| BinaryExpr<TriggerEventExpr, Keyword<"OR">, TriggerEventExpr>
| TriggerEvent;

export interface TriggerEvent extends BaseNode {
type: "trigger_event";
eventKw: Keyword<"INSERT" | "DELETE" | "UPDATE" | "TRUNCATE">;
Expand Down
7 changes: 6 additions & 1 deletion src/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -3283,7 +3283,7 @@ create_trigger_stmt
ifKw:(if_not_exists __)?
name:(entity_name __)
timeKw:(BEFORE __ / AFTER __ / INSTEAD __ OF __)?
event:(trigger_event __)
event:(trigger_event_expr __)
target:(trigger_target __)
clauses:(trigger_clause __)*
body:trigger_body
Expand Down Expand Up @@ -3312,6 +3312,11 @@ trigger_kind
return loc({ type: "relation_kind", kindKw: kw });
}

trigger_event_expr
= head:trigger_event tail:(__ OR __ trigger_event)* {
return createBinaryExprChain(head, tail);
}

trigger_event
= eventKw:(UPDATE __) ofKw:(OF __) cols:list$column {
return loc({
Expand Down
8 changes: 8 additions & 0 deletions test/ddl/trigger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ describe("trigger", () => {
testWc(`CREATE CONSTRAINT TRIGGER my_trig AFTER INSERT ON my_tbl ${body}`);
});

it("supports multiple events", () => {
testWc(`
CREATE TRIGGER my_trig AFTER INSERT OR UPDATE OF col1, col2 OR DELETE
ON my_tbl
${body}
`);
});

it("supports WHEN clause", () => {
testWc(`CREATE TRIGGER my_trig AFTER INSERT ON my_tbl WHEN (x > 10) ${body}`);
});
Expand Down
3 changes: 2 additions & 1 deletion test/test_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
SelectClause,
Statement,
SubSelect,
TriggerEvent,
Whitespace,
} from "../src/cst/Node";
import {
Expand Down Expand Up @@ -152,7 +153,7 @@ export function showCompoundPrecedence(sql: string): string {
return show(addPrecedenceParens(stmt));
}

function addPrecedenceParens<T extends Expr | SubSelect>(
function addPrecedenceParens<T extends Expr | SubSelect | TriggerEvent>(
expr: T
): ParenExpr<T> | T {
const space: Whitespace[] = [{ type: "space", text: " " }];
Expand Down

0 comments on commit d5ca16d

Please sign in to comment.