Skip to content

Commit

Permalink
Add DROP TABLE (eatonphil#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
krischerven authored Apr 14, 2020
1 parent 2045f36 commit 4e93078
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ type CreateTableStatement struct {
cols *[]*columnDefinition
}

type DropTableStatement struct {
name token
}

type InsertStatement struct {
table token
cols *[]*identifier
Expand All @@ -75,12 +79,14 @@ type AstKind uint
const (
SelectKind AstKind = iota
CreateTableKind
DropTableKind
InsertKind
)

type Statement struct {
SelectStatement *SelectStatement
CreateTableStatement *CreateTableStatement
DropTableStatement *DropTableStatement
InsertStatement *InsertStatement
Kind AstKind
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ repl:
fmt.Println("Error creating table", err)
continue repl
}
case gosql.DropTableKind:
err = mb.DropTable(ast.Statements[0].DropTableStatement)
if err != nil {
fmt.Println("Error dropping table", err)
continue repl
}
case gosql.InsertKind:
err = mb.Insert(stmt.InsertStatement)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
asKeyword keyword = "as"
tableKeyword keyword = "table"
createKeyword keyword = "create"
dropKeyword keyword = "drop"
insertKeyword keyword = "insert"
intoKeyword keyword = "into"
valuesKeyword keyword = "values"
Expand Down Expand Up @@ -204,6 +205,7 @@ func lexKeyword(source string, ic cursor) (*token, cursor, bool) {
valuesKeyword,
tableKeyword,
createKeyword,
dropKeyword,
whereKeyword,
fromKeyword,
intoKeyword,
Expand Down
8 changes: 8 additions & 0 deletions memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,14 @@ func (mb *MemoryBackend) CreateTable(crt *CreateTableStatement) error {
return nil
}

func (mb *MemoryBackend) DropTable(dt *DropTableStatement) error {
if _, ok := mb.tables[dt.name.value]; ok {
delete(mb.tables, dt.name.value)
return nil
}
return ErrTableDoesNotExist
}

func NewMemoryBackend() *MemoryBackend {
return &MemoryBackend{
tables: map[string]*table{},
Expand Down
34 changes: 34 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,32 @@ func parseCreateTableStatement(tokens []*token, initialCursor uint, delimiter to
}, cursor, true
}

func parseDropTableStatement(tokens []*token, initialCursor uint, delimiter token) (*DropTableStatement, uint, bool) {
cursor := initialCursor
ok := false

_, cursor, ok = parseToken(tokens, cursor, tokenFromKeyword(dropKeyword))
if !ok {
return nil, initialCursor, false
}

_, cursor, ok = parseToken(tokens, cursor, tokenFromKeyword(tableKeyword))
if !ok {
return nil, initialCursor, false
}

name, newCursor, ok := parseTokenKind(tokens, cursor, identifierKind)
if !ok {
helpMessage(tokens, cursor, "Expected table name")
return nil, initialCursor, false
}
cursor = newCursor

return &DropTableStatement{
name: *name,
}, cursor, true
}

func parseStatement(tokens []*token, initialCursor uint, delimiter token) (*Statement, uint, bool) {
cursor := initialCursor

Expand Down Expand Up @@ -492,6 +518,14 @@ func parseStatement(tokens []*token, initialCursor uint, delimiter token) (*Stat
}, newCursor, true
}

dpTbl, newCursor, ok := parseDropTableStatement(tokens, cursor, semicolonToken)
if ok {
return &Statement{
Kind: DropTableKind,
DropTableStatement: dpTbl,
}, newCursor, true
}

return nil, initialCursor, false
}

Expand Down

0 comments on commit 4e93078

Please sign in to comment.