( schemaName: string, sql: string )
| 7 | import { CursorV2, parseColumnList } from "./sql-parse-table"; |
| 8 | |
| 9 | export function parseCreateTriggerScript( |
| 10 | schemaName: string, |
| 11 | sql: string |
| 12 | ): DatabaseTriggerSchema { |
| 13 | const cursor = new CursorV2(tokenizeSql(sql, "sqlite")); |
| 14 | |
| 15 | cursor.expectToken("CREATE"); |
| 16 | cursor.expectTokenOptional("TEMP"); |
| 17 | cursor.expectTokenOptional("TEMPORARY"); |
| 18 | cursor.expectToken("TRIGGER"); |
| 19 | cursor.expectTokensOptional(["IF", "NOT", "EXIST"]); |
| 20 | const name = cursor.consumeIdentifier(); |
| 21 | |
| 22 | let when: TriggerWhen = "BEFORE"; |
| 23 | |
| 24 | if (cursor.match("BEFORE")) { |
| 25 | cursor.next(); |
| 26 | } else if (cursor.match("AFTER")) { |
| 27 | when = "AFTER"; |
| 28 | cursor.next(); |
| 29 | } else if (cursor.match("INSTEAD")) { |
| 30 | cursor.expectTokens(["INSTEAD", "OF"]); |
| 31 | when = "INSTEAD_OF"; |
| 32 | } |
| 33 | |
| 34 | let operation: TriggerOperation = "INSERT"; |
| 35 | let columnNames; |
| 36 | |
| 37 | if (cursor.match("DELETE")) { |
| 38 | operation = "DELETE"; |
| 39 | cursor.next(); |
| 40 | } else if (cursor.match("INSERT")) { |
| 41 | operation = "INSERT"; |
| 42 | cursor.next(); |
| 43 | } else if (cursor.match("UPDATE")) { |
| 44 | operation = "UPDATE"; |
| 45 | cursor.next(); |
| 46 | if (cursor.match("OF")) { |
| 47 | cursor.next(); |
| 48 | columnNames = parseColumnList(cursor); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | cursor.expectToken("ON"); |
| 53 | const tableName = cursor.consumeIdentifier(); |
| 54 | cursor.expectTokensOptional(["FOR", "EACH", "ROW"]); |
| 55 | |
| 56 | let whenExpression = ""; |
| 57 | const fromExpression = cursor.getPointer(); |
| 58 | let toExpression; |
| 59 | |
| 60 | if (cursor.match("WHEN")) { |
| 61 | // Loop till the end or meet the BEGIN |
| 62 | cursor.next(); |
| 63 | |
| 64 | while (!cursor.end()) { |
| 65 | toExpression = cursor.getPointer(); |
| 66 | if (cursor.match("BEGIN")) break; |
no test coverage detected
searching dependent graphs…