Skip to content

Commit 77e3d1e

Browse files
committed
Drop View
1 parent 228c5b0 commit 77e3d1e

11 files changed

Lines changed: 89 additions & 3 deletions

File tree

Sources/Compiler/Compiler.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ extension CompilerWithSource: StmtSyntaxVisitor {
259259
return typeCheck(stmt, isReadOnly: false)
260260
}
261261

262+
mutating func visit(_ stmt: DropViewStmtSyntax) -> (Statement, Diagnostics)? {
263+
return typeCheck(stmt, isReadOnly: false)
264+
}
265+
262266
mutating func visit(_ stmt: CreateVirtualTableStmtSyntax) -> (Statement, Diagnostics)? {
263267
return typeCheck(stmt, isReadOnly: false)
264268
}
@@ -288,6 +292,7 @@ struct IsValidForMigrations: StmtSyntaxVisitor {
288292
func visit(_ stmt: borrowing DropIndexStmtSyntax) -> Bool { true }
289293
func visit(_ stmt: borrowing ReindexStmtSyntax) -> Bool { true }
290294
func visit(_ stmt: borrowing CreateViewStmtSyntax) -> Bool { true }
295+
func visit(_ stmt: borrowing DropViewStmtSyntax) -> Bool { true }
291296
func visit(_ stmt: borrowing CreateVirtualTableStmtSyntax) -> Bool { true }
292297
func visit(_ stmt: borrowing CreateTriggerStmtSyntax) -> Bool { true }
293298
func visit(_ stmt: borrowing DropTriggerStmtSyntax) -> Bool { true }
@@ -309,6 +314,7 @@ struct IsValidForQueries: StmtSyntaxVisitor {
309314
func visit(_ stmt: borrowing DropIndexStmtSyntax) -> Bool { false }
310315
func visit(_ stmt: borrowing ReindexStmtSyntax) -> Bool { false }
311316
func visit(_ stmt: borrowing CreateViewStmtSyntax) -> Bool { false }
317+
func visit(_ stmt: borrowing DropViewStmtSyntax) -> Bool { false }
312318
func visit(_ stmt: borrowing CreateVirtualTableStmtSyntax) -> Bool { false }
313319
func visit(_ stmt: borrowing CreateTriggerStmtSyntax) -> Bool { false }
314320
func visit(_ stmt: borrowing DropTriggerStmtSyntax) -> Bool { false }
@@ -330,6 +336,7 @@ struct IsAlwaysValid: StmtSyntaxVisitor {
330336
func visit(_ stmt: borrowing DropIndexStmtSyntax) -> Bool { true }
331337
func visit(_ stmt: borrowing ReindexStmtSyntax) -> Bool { true }
332338
func visit(_ stmt: borrowing CreateViewStmtSyntax) -> Bool { true }
339+
func visit(_ stmt: borrowing DropViewStmtSyntax) -> Bool { true }
333340
func visit(_ stmt: borrowing CreateVirtualTableStmtSyntax) -> Bool { true }
334341
func visit(_ stmt: borrowing CreateTriggerStmtSyntax) -> Bool { true }
335342
func visit(_ stmt: borrowing DropTriggerStmtSyntax) -> Bool { true }

Sources/Compiler/Gen/Rewriter.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ extension Rewriter: StmtSyntaxVisitor {
149149

150150
func visit(_ stmt: borrowing CreateViewStmtSyntax) -> [Range<Substring.Index>] { [] }
151151

152+
func visit(_ stmt: borrowing DropViewStmtSyntax) -> [Range<Substring.Index>] { [] }
153+
152154
func visit(_ stmt: borrowing CreateVirtualTableStmtSyntax) -> [Range<Substring.Index>] {
153155
return stmt.arguments.flatMap { argument -> [Range<Substring.Index>] in
154156
guard case let .fts5Column(_, typeName, notNull, _) = argument else { return [] }

Sources/Compiler/Parse/Parsers.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ enum Parsers {
7676
return dropTable(state: &state)
7777
case (.drop, .index):
7878
return dropIndex(state: &state)
79+
case (.drop, .view):
80+
return dropView(state: &state)
7981
case (.reindex, _):
8082
return reindex(state: &state)
8183
case (.with, _):
@@ -372,6 +374,21 @@ enum Parsers {
372374
)
373375
}
374376

377+
/// https://www.sqlite.org/lang_dropview.html
378+
static func dropView(state: inout ParserState) -> DropViewStmtSyntax {
379+
let drop = state.take(.drop)
380+
state.consume(.view)
381+
let ifExists = ifExists(state: &state)
382+
let (schema, view) = tableAndSchemaName(state: &state)
383+
return DropViewStmtSyntax(
384+
id: state.nextId(),
385+
location: state.location(from: drop),
386+
ifExists: ifExists,
387+
schemaName: schema,
388+
viewName: view
389+
)
390+
}
391+
375392
/// Will optionally parse `IF NOT EXISTS` if the first token is `IF`
376393
static func ifNotExists(state: inout ParserState) -> Bool {
377394
guard state.take(if: .if) else { return false }

Sources/Compiler/Sema/CardinalityInferrer.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ extension CardinalityInferrer: StmtSyntaxVisitor {
133133

134134
mutating func visit(_ stmt: borrowing CreateViewStmtSyntax) -> Cardinality { .many }
135135

136+
mutating func visit(_ stmt: borrowing DropViewStmtSyntax) -> Cardinality { .many }
137+
136138
mutating func visit(_ stmt: borrowing CreateVirtualTableStmtSyntax) -> Cardinality { .many }
137139

138140
mutating func visit(_ stmt: borrowing CreateTriggerStmtSyntax) -> Cardinality { .many }

Sources/Compiler/Sema/StmtTypeChecker.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,23 @@ extension StmtTypeChecker: StmtSyntaxVisitor {
231231
return .empty
232232
}
233233

234+
mutating func visit(_ stmt: borrowing DropViewStmtSyntax) -> ResultColumns {
235+
guard let table = schema[stmt.viewName.value] else {
236+
if !stmt.ifExists {
237+
diagnostics.add(.init("View with name does not exist", at: stmt.viewName.location))
238+
}
239+
return .empty
240+
}
241+
242+
if table.kind != .view {
243+
diagnostics.add(.init("Table is not a view", at: stmt.viewName.location))
244+
}
245+
246+
schema[stmt.viewName.value] = nil
247+
248+
return .empty
249+
}
250+
234251
mutating func visit(_ stmt: borrowing CreateVirtualTableStmtSyntax) -> ResultColumns {
235252
if !stmt.ifNotExists, schema[stmt.tableName.name.value] != nil {
236253
diagnostics.add(.tableAlreadyExists(stmt.tableName.name))
@@ -295,7 +312,7 @@ extension StmtTypeChecker: StmtSyntaxVisitor {
295312
schema[trigger: stmt.triggerName.value] = Trigger(
296313
name: stmt.triggerName.value,
297314
targetTable: table.name,
298-
usedTables: usedTableNames
315+
usedTables: usedTableNames.subtracting([table.name])
299316
)
300317

301318
return .empty
@@ -395,6 +412,8 @@ extension StmtTypeChecker {
395412
return .empty
396413
}
397414

415+
usedTableNames.insert(table.name)
416+
398417
let inputType: Type
399418
if let columns = insert.columns {
400419
var columnTypes: [Type] = []
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// DropViewStmtSyntax.swift
3+
// Feather
4+
//
5+
// Created by Wes Wickwire on 5/18/25.
6+
//
7+
8+
struct DropViewStmtSyntax: StmtSyntax {
9+
let id: SyntaxId
10+
let location: SourceLocation
11+
let ifExists: Bool
12+
let schemaName: IdentifierSyntax?
13+
let viewName: IdentifierSyntax
14+
15+
func accept<V>(visitor: inout V) -> V.StmtOutput where V : StmtSyntaxVisitor {
16+
visitor.visit(self)
17+
}
18+
}

Sources/Compiler/Syntax/Statements/StmtSyntax.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ protocol StmtSyntaxVisitor {
2727
mutating func visit(_ stmt: borrowing DropIndexStmtSyntax) -> StmtOutput
2828
mutating func visit(_ stmt: borrowing ReindexStmtSyntax) -> StmtOutput
2929
mutating func visit(_ stmt: borrowing CreateViewStmtSyntax) -> StmtOutput
30+
mutating func visit(_ stmt: borrowing DropViewStmtSyntax) -> StmtOutput
3031
mutating func visit(_ stmt: borrowing CreateVirtualTableStmtSyntax) -> StmtOutput
3132
mutating func visit(_ stmt: borrowing CreateTriggerStmtSyntax) -> StmtOutput
3233
mutating func visit(_ stmt: borrowing DropTriggerStmtSyntax) -> StmtOutput

Tests/CompilerTests/Compiler/CompileInsert.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ CREATE TABLE user (id INTEGER, name TEXT);
1010
-- CHECK: TYPE TEXT?
1111
-- CHECK: INDEX 2
1212
-- CHECK: NAME name
13+
-- CHECK: TABLES
14+
-- CHECK: user
1315
INSERT INTO user (id, name) VALUES (?, ?);
1416

1517
-- CHECK: SIGNATURE
@@ -27,6 +29,8 @@ INSERT INTO user (id, name) VALUES (?, ?);
2729
-- CHECK: OUTPUT
2830
-- CHECK: id INTEGER?
2931
-- CHECK: name TEXT?
32+
-- CHECK: TABLES
33+
-- CHECK: user
3034
INSERT INTO user (id, name) VALUES (?, ?) RETURNING *;
3135

3236
-- CHECK: SIGNATURE
@@ -60,4 +64,6 @@ INSERT INTO user (id, name) VALUES (?, ?) RETURNING *;
6064
-- CHECK: OUTPUT
6165
-- CHECK: id INTEGER?
6266
-- CHECK: name TEXT?
67+
-- CHECK: TABLES
68+
-- CHECK: user
6369
INSERT INTO user (id, name) VALUES (?, ?), (?, ?), (?, ?) RETURNING *;

Tests/CompilerTests/CompilerTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ class CompilerTests: XCTestCase {
3939
}
4040

4141
func testView() throws {
42-
try checkSchema(compile: "CompileView", prefix: "CHECK-SCHEMA")
43-
try checkQueries(compile: "CompileView", prefix: "CHECK-QUERIES")
42+
try checkSchema(compile: "CompileView", prefix: "CHECK-SCHEMA", dump: true)
43+
try checkQueries(compile: "CompileView", prefix: "CHECK-QUERIES", dump: true)
4444
}
4545

4646
func testFTS5() throws {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- CHECK: DROP_VIEW_STMT_SYNTAX
2+
-- CHECK: IF_EXISTS true
3+
-- CHECK: SCHEMA_NAME foo
4+
-- CHECK: VIEW_NAME bar
5+
DROP VIEW IF EXISTS foo.bar;
6+
7+
-- CHECK: DROP_VIEW_STMT_SYNTAX
8+
-- CHECK: IF_EXISTS false
9+
-- CHECK: VIEW_NAME bar
10+
DROP VIEW bar;

0 commit comments

Comments
 (0)