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

Commit d63b44e

Browse files
committed
PR feedback: added engine tests, removed Alterable interface, added comments.
Signed-off-by: Zach Musgrave <[email protected]>
1 parent 4b9329e commit d63b44e

File tree

4 files changed

+89
-34
lines changed

4 files changed

+89
-34
lines changed

engine_test.go

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2443,7 +2443,7 @@ func TestAmbiguousColumnResolution(t *testing.T) {
24432443
require.Equal(expected, rs)
24442444
}
24452445

2446-
func TestDDL(t *testing.T) {
2446+
func TestCreateTable(t *testing.T) {
24472447
require := require.New(t)
24482448

24492449
e := newEngine(t)
@@ -2474,6 +2474,83 @@ func TestDDL(t *testing.T) {
24742474
}
24752475

24762476
require.Equal(s, testTable.Schema())
2477+
2478+
testQuery(t, e,
2479+
"CREATE TABLE t2 (a INTEGER NOT NULL PRIMARY KEY, "+
2480+
"b VARCHAR(10) NOT NULL)",
2481+
[]sql.Row(nil),
2482+
)
2483+
2484+
db, err = e.Catalog.Database("mydb")
2485+
require.NoError(err)
2486+
2487+
testTable, ok = db.Tables()["t2"]
2488+
require.True(ok)
2489+
2490+
s = sql.Schema{
2491+
{Name: "a", Type: sql.Int32, Nullable: false, PrimaryKey: true, Source: "t2"},
2492+
{Name: "b", Type: sql.Text, Nullable: false, Source: "t2"},
2493+
}
2494+
2495+
require.Equal(s, testTable.Schema())
2496+
2497+
testQuery(t, e,
2498+
"CREATE TABLE t3(a INTEGER NOT NULL,"+
2499+
"b TEXT NOT NULL,"+
2500+
"c bool, primary key (a,b))",
2501+
[]sql.Row(nil),
2502+
)
2503+
2504+
db, err = e.Catalog.Database("mydb")
2505+
require.NoError(err)
2506+
2507+
testTable, ok = db.Tables()["t3"]
2508+
require.True(ok)
2509+
2510+
s = sql.Schema{
2511+
{Name: "a", Type: sql.Int32, Nullable: false, PrimaryKey: true, Source: "t3"},
2512+
{Name: "b", Type: sql.Text, Nullable: false, PrimaryKey: true, Source: "t3"},
2513+
{Name: "c", Type: sql.Uint8, Nullable: true, Source: "t3"},
2514+
}
2515+
2516+
require.Equal(s, testTable.Schema())
2517+
}
2518+
2519+
func TestDropTable(t *testing.T) {
2520+
require := require.New(t)
2521+
2522+
e := newEngine(t)
2523+
db, err := e.Catalog.Database("mydb")
2524+
require.NoError(err)
2525+
2526+
_, ok := db.Tables()["mytable"]
2527+
require.True(ok)
2528+
2529+
testQuery(t, e,
2530+
"DROP TABLE IF EXISTS mytable, not_exist",
2531+
[]sql.Row(nil),
2532+
)
2533+
2534+
_, ok = db.Tables()["mytable"]
2535+
require.False(ok)
2536+
2537+
_, ok = db.Tables()["othertable"]
2538+
require.True(ok)
2539+
_, ok = db.Tables()["tabletest"]
2540+
require.True(ok)
2541+
2542+
testQuery(t, e,
2543+
"DROP TABLE IF EXISTS othertable, tabletest",
2544+
[]sql.Row(nil),
2545+
)
2546+
2547+
_, ok = db.Tables()["othertable"]
2548+
require.False(ok)
2549+
_, ok = db.Tables()["tabletest"]
2550+
require.False(ok)
2551+
2552+
_, _, err = e.Query(newCtx(), "DROP TABLE not_exist")
2553+
require.Error(err)
24772554
}
24782555

24792556
func TestNaturalJoin(t *testing.T) {

memory/database.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,7 @@ func (d *Database) AddTable(name string, t sql.Table) {
3333
d.tables[name] = t
3434
}
3535

36-
// Create creates a table with the given name and schema
37-
func (d *Database) Create(name string, schema sql.Schema) error {
38-
_, ok := d.tables[name]
39-
if ok {
40-
return sql.ErrTableAlreadyExists.New(name)
41-
}
42-
43-
d.tables[name] = NewTable(name, schema)
44-
return nil
45-
}
46-
47-
// Create creates a table with the given name and schema
36+
// CreateTable creates a table with the given name and schema
4837
func (d *Database) CreateTable(ctx *sql.Context, name string, schema sql.Schema) error {
4938
_, ok := d.tables[name]
5039
if ok {
@@ -55,6 +44,7 @@ func (d *Database) CreateTable(ctx *sql.Context, name string, schema sql.Schema)
5544
return nil
5645
}
5746

47+
// DropTable drops the table with the given name
5848
func (d *Database) DropTable(ctx *sql.Context, name string) error {
5949
_, ok := d.tables[name]
6050
if !ok {

sql/core.go

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

233-
// DEPRECATED. Use TableCreator and TableDropper.
234-
// Alterable should be implemented by databases that can handle DDL statements
235-
type Alterable interface {
236-
Create(name string, schema Schema) error
237-
}
238-
239233
// TableCreator should be implemented by databases that can create new tables.
240234
type TableCreator interface {
241235
CreateTable(ctx *Context, name string, schema Schema) error

sql/plan/ddl.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
// ErrCreateTable is thrown when the database doesn't support table creation
10-
var ErrCreateTable = errors.NewKind("tables cannot be created on database %s")
10+
var ErrCreateTableNotSupported = errors.NewKind("tables cannot be created on database %s")
1111
var ErrDropTableNotSupported = errors.NewKind("tables cannot be dropped on database %s")
1212

1313
// CreateTable is a node describing the creation of some table.
@@ -17,13 +17,6 @@ type CreateTable struct {
1717
schema sql.Schema
1818
}
1919

20-
// DropTable is a node describing dropping a table
21-
type DropTable struct {
22-
db sql.Database
23-
names []string
24-
ifExists bool
25-
}
26-
2720
// NewCreateTable creates a new CreateTable node
2821
func NewCreateTable(db sql.Database, name string, schema sql.Schema) *CreateTable {
2922
for _, s := range schema {
@@ -64,13 +57,7 @@ func (c *CreateTable) RowIter(s *sql.Context) (sql.RowIter, error) {
6457
return sql.RowsToRowIter(), creatable.CreateTable(s, c.name, c.schema)
6558
}
6659

67-
// TODO: phase out this interface
68-
d, ok := c.db.(sql.Alterable)
69-
if !ok {
70-
return nil, ErrCreateTable.New(c.db.Name())
71-
}
72-
73-
return sql.RowsToRowIter(), d.Create(c.name, c.schema)
60+
return nil, ErrCreateTableNotSupported.New(c.db.Name())
7461
}
7562

7663
// Schema implements the Node interface.
@@ -91,6 +78,13 @@ func (c *CreateTable) String() string {
9178
return "CreateTable"
9279
}
9380

81+
// DropTable is a node describing dropping one or more tables
82+
type DropTable struct {
83+
db sql.Database
84+
names []string
85+
ifExists bool
86+
}
87+
9488
// NewDropTable creates a new DropTable node
9589
func NewDropTable(db sql.Database, ifExists bool, tableNames ...string) *DropTable {
9690
return &DropTable{

0 commit comments

Comments
 (0)