Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Commit 9af52bf

Browse files
committed
Added a context to DropTable and CreateTable, and declared a new interface for create table statements, deprecating Alterable (which only supports Create)
Signed-off-by: Zach Musgrave <zach@liquidata.co>
1 parent a514766 commit 9af52bf

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

memory/database.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,18 @@ func (d *Database) Create(name string, schema sql.Schema) error {
4444
return nil
4545
}
4646

47-
func (d *Database) DropTable(name string) error {
47+
// Create creates a table with the given name and schema
48+
func (d *Database) CreateTable(ctx *sql.Context, name string, schema sql.Schema) error {
49+
_, ok := d.tables[name]
50+
if ok {
51+
return sql.ErrTableAlreadyExists.New(name)
52+
}
53+
54+
d.tables[name] = NewTable(name, schema)
55+
return nil
56+
}
57+
58+
func (d *Database) DropTable(ctx *sql.Context, name string) error {
4859
_, ok := d.tables[name]
4960
if !ok {
5061
return sql.ErrTableNotFound.New(name)

sql/core.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,20 @@ type Database interface {
230230
Tables() map[string]Table
231231
}
232232

233+
// DEPRECATED. Use TableCreator and TableDropper.
233234
// Alterable should be implemented by databases that can handle DDL statements
234235
type Alterable interface {
235236
Create(name string, schema Schema) error
236237
}
237238

238-
// Droppable should be implemented by databases that can drop tables.
239-
type Droppable interface {
240-
DropTable(name string) error
239+
// TableCreator should be implemented by databases that can create new tables.
240+
type TableCreator interface {
241+
CreateTable(ctx *Context, name string, schema Schema) error
242+
}
243+
244+
// TableDropper should be implemented by databases that can drop tables.
245+
type TableDropper interface {
246+
DropTable(ctx *Context, name string) error
241247
}
242248

243249
// Lockable should be implemented by tables that can be locked and unlocked.

sql/plan/ddl.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ func (c *CreateTable) Resolved() bool {
5959

6060
// RowIter implements the Node interface.
6161
func (c *CreateTable) RowIter(s *sql.Context) (sql.RowIter, error) {
62+
creatable, ok := c.db.(sql.TableCreator)
63+
if ok {
64+
return sql.RowsToRowIter(), creatable.CreateTable(s, c.name, c.schema)
65+
}
66+
67+
// TODO: phase out this interface
6268
d, ok := c.db.(sql.Alterable)
6369
if !ok {
6470
return nil, ErrCreateTable.New(c.db.Name())
@@ -116,7 +122,7 @@ func (d *DropTable) Resolved() bool {
116122

117123
// RowIter implements the Node interface.
118124
func (d *DropTable) RowIter(s *sql.Context) (sql.RowIter, error) {
119-
droppable, ok := d.db.(sql.Droppable)
125+
droppable, ok := d.db.(sql.TableDropper)
120126
if !ok {
121127
return nil, ErrDropTableNotSupported.New(d.db.Name())
122128
}
@@ -130,7 +136,7 @@ func (d *DropTable) RowIter(s *sql.Context) (sql.RowIter, error) {
130136
}
131137
return nil, sql.ErrTableNotFound.New(tableName)
132138
}
133-
err = droppable.DropTable(tableName)
139+
err = droppable.DropTable(s, tableName)
134140
if err != nil {
135141
break
136142
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy