@@ -169,18 +169,24 @@ extension StmtTypeChecker: StmtSyntaxVisitor {
169169 _ = typeCheck ( whereExpr)
170170 }
171171
172+ schema [ index: stmt. name. value] = Index (
173+ name: stmt. name. value,
174+ table: table. name
175+ )
176+
172177 return . empty
173178 }
174179
175180 mutating func visit( _ stmt: borrowing DropIndexStmtSyntax ) -> ResultColumns {
176- // Indices are not stored at the moment, so there is nothign to do.
181+ if !stmt. ifExists, schema [ index: stmt. name. value] == nil {
182+ diagnostics. add ( . init( " Index does not exist " , at: stmt. name. location) )
183+ }
184+
185+ schema [ index: stmt. name. value] = nil
177186 return . empty
178187 }
179188
180189 mutating func visit( _ stmt: borrowing ReindexStmtSyntax ) -> ResultColumns {
181- // Indices are not stored at the moment, so there is nothign to do.
182- // We cant really even validate the name since it can be the
183- // index name and not just the table
184190 return . empty
185191 }
186192
@@ -246,6 +252,13 @@ extension StmtTypeChecker: StmtSyntaxVisitor {
246252 return . empty
247253 }
248254
255+ if !stmt. ifNotExists, schema [ trigger: stmt. triggerName. value] != nil {
256+ diagnostics. add ( . init(
257+ " Trigger with name already exists " ,
258+ at: stmt. triggerName. location
259+ ) )
260+ }
261+
249262 switch stmt. action {
250263 case . delete:
251264 insertTableAndColumnsIntoEnv ( table, as: " old " , globallyAddColumns: false )
@@ -279,11 +292,21 @@ extension StmtTypeChecker: StmtSyntaxVisitor {
279292 }
280293 }
281294
295+ schema [ trigger: stmt. triggerName. value] = Trigger (
296+ name: stmt. triggerName. value,
297+ targetTable: table. name,
298+ usedTables: usedTableNames
299+ )
300+
282301 return . empty
283302 }
284303
285- func visit( _ stmt: borrowing DropTriggerStmtSyntax ) -> ResultColumns {
286- // TODO: Track triggers
304+ mutating func visit( _ stmt: borrowing DropTriggerStmtSyntax ) -> ResultColumns {
305+ if !stmt. ifExists, schema [ trigger: stmt. triggerName. value] == nil {
306+ diagnostics. add ( . init( " Trigger with name does not exist " , at: stmt. triggerName. location) )
307+ }
308+
309+ schema [ trigger: stmt. triggerName. value] = nil
287310 return . empty
288311 }
289312}
@@ -942,6 +965,24 @@ extension StmtTypeChecker {
942965 diagnostics. add ( . tableDoesNotExist( dropTable. tableName. name) )
943966 }
944967
968+ for trigger in schema. triggers. values {
969+ if trigger. targetTable == dropTable. tableName. name. value {
970+ // Dropping a table automatically removes any trigger its the target of.
971+ schema [ trigger: trigger. name] = nil
972+ } else {
973+ guard !trigger. usedTables. contains ( dropTable. tableName. name. value) else { continue }
974+
975+ // SQLite seemingly from my tests will allow this to happen but I swear I've
976+ // had errors from it before. But error if the table is used in a trigger.
977+ // Any trigger where its the target table will automatically be deleted
978+ // so those dont matter
979+ diagnostics. add ( . init(
980+ " Table referenced in statements of trigger ' \( trigger. name) ' " ,
981+ at: dropTable. location
982+ ) )
983+ }
984+ }
985+
945986 schema [ dropTable. tableName. name. value] = nil
946987 }
947988
0 commit comments