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

Commit 77bee7f

Browse files
authored
Merge pull request #592 from erizocosmico/feature/databaser
sql: add Databaser interface for cleaner analyzer rules
2 parents 65f04e7 + 933dc11 commit 77bee7f

File tree

8 files changed

+126
-81
lines changed

8 files changed

+126
-81
lines changed

sql/analyzer/assign_catalog_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestAssignCatalog(t *testing.T) {
4545

4646
si, ok := node.(*plan.ShowIndexes)
4747
require.True(ok)
48-
require.Equal(db, si.Database)
48+
require.Equal(db, si.Database())
4949
require.Equal(c.IndexRegistry, si.Registry)
5050

5151
node, err = f.Apply(sql.NewEmptyContext(), a, plan.NewShowProcessList())

sql/analyzer/resolve_database.go

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package analyzer
22

33
import (
44
"gopkg.in/src-d/go-mysql-server.v0/sql"
5-
"gopkg.in/src-d/go-mysql-server.v0/sql/plan"
65
)
76

87
func resolveDatabase(ctx *sql.Context, a *Analyzer, n sql.Node) (sql.Node, error) {
@@ -12,59 +11,27 @@ func resolveDatabase(ctx *sql.Context, a *Analyzer, n sql.Node) (sql.Node, error
1211
a.Log("resolve database, node of type: %T", n)
1312

1413
return n.TransformUp(func(n sql.Node) (sql.Node, error) {
15-
switch v := n.(type) {
16-
case *plan.ShowIndexes:
17-
db, err := a.Catalog.Database(a.Catalog.CurrentDatabase())
18-
if err != nil {
19-
return nil, err
20-
}
21-
22-
nc := *v
23-
nc.Database = db
24-
return &nc, nil
25-
case *plan.ShowTables:
26-
var dbName = v.Database.Name()
27-
if dbName == "" {
28-
dbName = a.Catalog.CurrentDatabase()
29-
}
30-
31-
db, err := a.Catalog.Database(dbName)
32-
if err != nil {
33-
return nil, err
34-
}
35-
36-
nc := *v
37-
nc.Database = db
38-
return &nc, nil
39-
case *plan.CreateTable:
40-
db, err := a.Catalog.Database(a.Catalog.CurrentDatabase())
41-
if err != nil {
42-
return nil, err
43-
}
14+
d, ok := n.(sql.Databaser)
15+
if !ok {
16+
return n, nil
17+
}
4418

45-
nc := *v
46-
nc.Database = db
47-
return &nc, nil
48-
case *plan.Use:
49-
db, err := a.Catalog.Database(v.Database.Name())
50-
if err != nil {
51-
return nil, err
19+
var dbName = a.Catalog.CurrentDatabase()
20+
if db := d.Database(); db != nil {
21+
if _, ok := db.(sql.UnresolvedDatabase); !ok {
22+
return n, nil
5223
}
5324

54-
nc := *v
55-
nc.Database = db
56-
return &nc, nil
57-
case *plan.ShowCreateDatabase:
58-
db, err := a.Catalog.Database(v.Database.Name())
59-
if err != nil {
60-
return nil, err
25+
if db.Name() != "" {
26+
dbName = db.Name()
6127
}
28+
}
6229

63-
nc := *v
64-
nc.Database = db
65-
return &nc, nil
66-
default:
67-
return n, nil
30+
db, err := a.Catalog.Database(dbName)
31+
if err != nil {
32+
return nil, err
6833
}
34+
35+
return d.WithDatabase(db)
6936
})
7037
}

sql/core.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ type Expressioner interface {
117117
TransformExpressions(TransformExprFunc) (Node, error)
118118
}
119119

120+
// Databaser is a node that contains a reference to a database.
121+
type Databaser interface {
122+
// Database the current database.
123+
Database() Database
124+
// WithDatabase returns a new node instance with the database replaced with
125+
// the one given as parameter.
126+
WithDatabase(Database) (Node, error)
127+
}
128+
120129
// Partition represents a partition from a SQL table.
121130
type Partition interface {
122131
Key() []byte

sql/plan/ddl.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ var ErrCreateTable = errors.NewKind("tables cannot be created on database %s")
1010

1111
// CreateTable is a node describing the creation of some table.
1212
type CreateTable struct {
13-
Database sql.Database
14-
name string
15-
schema sql.Schema
13+
db sql.Database
14+
name string
15+
schema sql.Schema
1616
}
1717

1818
// NewCreateTable creates a new CreateTable node
@@ -22,23 +22,37 @@ func NewCreateTable(db sql.Database, name string, schema sql.Schema) *CreateTabl
2222
}
2323

2424
return &CreateTable{
25-
Database: db,
26-
name: name,
27-
schema: schema,
25+
db: db,
26+
name: name,
27+
schema: schema,
2828
}
2929
}
3030

31+
var _ sql.Databaser = (*CreateTable)(nil)
32+
33+
// Database implements the sql.Databaser interface.
34+
func (c *CreateTable) Database() sql.Database {
35+
return c.db
36+
}
37+
38+
// WithDatabase implements the sql.Databaser interface.
39+
func (c *CreateTable) WithDatabase(db sql.Database) (sql.Node, error) {
40+
nc := *c
41+
nc.db = db
42+
return &nc, nil
43+
}
44+
3145
// Resolved implements the Resolvable interface.
3246
func (c *CreateTable) Resolved() bool {
33-
_, ok := c.Database.(sql.UnresolvedDatabase)
47+
_, ok := c.db.(sql.UnresolvedDatabase)
3448
return !ok
3549
}
3650

3751
// RowIter implements the Node interface.
3852
func (c *CreateTable) RowIter(s *sql.Context) (sql.RowIter, error) {
39-
d, ok := c.Database.(sql.Alterable)
53+
d, ok := c.db.(sql.Alterable)
4054
if !ok {
41-
return nil, ErrCreateTable.New(c.Database.Name())
55+
return nil, ErrCreateTable.New(c.db.Name())
4256
}
4357

4458
return sql.RowsToRowIter(), d.Create(c.name, c.schema)
@@ -52,7 +66,7 @@ func (c *CreateTable) Children() []sql.Node { return nil }
5266

5367
// TransformUp implements the Transformable interface.
5468
func (c *CreateTable) TransformUp(f sql.TransformNodeFunc) (sql.Node, error) {
55-
return f(NewCreateTable(c.Database, c.name, c.schema))
69+
return f(NewCreateTable(c.db, c.name, c.schema))
5670
}
5771

5872
// TransformExpressionsUp implements the Transformable interface.

sql/plan/show_create_database.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
// ShowCreateDatabase returns the SQL for creating a database.
1111
type ShowCreateDatabase struct {
12-
Database sql.Database
12+
db sql.Database
1313
IfNotExists bool
1414
}
1515

@@ -25,9 +25,23 @@ func NewShowCreateDatabase(db sql.Database, ifNotExists bool) *ShowCreateDatabas
2525
return &ShowCreateDatabase{db, ifNotExists}
2626
}
2727

28+
var _ sql.Databaser = (*ShowCreateDatabase)(nil)
29+
30+
// Database implements the sql.Databaser interface.
31+
func (s *ShowCreateDatabase) Database() sql.Database {
32+
return s.db
33+
}
34+
35+
// WithDatabase implements the sql.Databaser interface.
36+
func (s *ShowCreateDatabase) WithDatabase(db sql.Database) (sql.Node, error) {
37+
nc := *s
38+
nc.db = db
39+
return &nc, nil
40+
}
41+
2842
// RowIter implements the sql.Node interface.
2943
func (s *ShowCreateDatabase) RowIter(ctx *sql.Context) (sql.RowIter, error) {
30-
var name = s.Database.Name()
44+
var name = s.db.Name()
3145

3246
var buf bytes.Buffer
3347

@@ -56,15 +70,15 @@ func (s *ShowCreateDatabase) Schema() sql.Schema {
5670
}
5771

5872
func (s *ShowCreateDatabase) String() string {
59-
return fmt.Sprintf("SHOW CREATE DATABASE %s", s.Database.Name())
73+
return fmt.Sprintf("SHOW CREATE DATABASE %s", s.db.Name())
6074
}
6175

6276
// Children implements the sql.Node interface.
6377
func (s *ShowCreateDatabase) Children() []sql.Node { return nil }
6478

6579
// Resolved implements the sql.Node interface.
6680
func (s *ShowCreateDatabase) Resolved() bool {
67-
_, ok := s.Database.(sql.UnresolvedDatabase)
81+
_, ok := s.db.(sql.UnresolvedDatabase)
6882
return !ok
6983
}
7084

sql/plan/show_indexes.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
// ShowIndexes is a node that shows the indexes on a table.
1111
type ShowIndexes struct {
12-
Database sql.Database
12+
db sql.Database
1313
Table string
1414
Registry *sql.IndexRegistry
1515
}
@@ -19,15 +19,29 @@ func NewShowIndexes(db sql.Database, table string, registry *sql.IndexRegistry)
1919
return &ShowIndexes{db, table, registry}
2020
}
2121

22+
var _ sql.Databaser = (*ShowIndexes)(nil)
23+
24+
// Database implements the sql.Databaser interface.
25+
func (n *ShowIndexes) Database() sql.Database {
26+
return n.db
27+
}
28+
29+
// WithDatabase implements the sql.Databaser interface.
30+
func (n *ShowIndexes) WithDatabase(db sql.Database) (sql.Node, error) {
31+
nc := *n
32+
nc.db = db
33+
return &nc, nil
34+
}
35+
2236
// Resolved implements the Resolvable interface.
2337
func (n *ShowIndexes) Resolved() bool {
24-
_, ok := n.Database.(sql.UnresolvedDatabase)
38+
_, ok := n.db.(sql.UnresolvedDatabase)
2539
return !ok
2640
}
2741

2842
// TransformUp implements the Transformable interface.
2943
func (n *ShowIndexes) TransformUp(f sql.TransformNodeFunc) (sql.Node, error) {
30-
return f(NewShowIndexes(n.Database, n.Table, n.Registry))
44+
return f(NewShowIndexes(n.db, n.Table, n.Registry))
3145
}
3246

3347
// TransformExpressionsUp implements the Transformable interface.
@@ -67,7 +81,7 @@ func (n *ShowIndexes) Children() []sql.Node { return nil }
6781
// RowIter implements the Node interface.
6882
func (n *ShowIndexes) RowIter(*sql.Context) (sql.RowIter, error) {
6983
return &showIndexesIter{
70-
db: n.Database,
84+
db: n.db,
7185
table: n.Table,
7286
registry: n.Registry,
7387
}, nil

sql/plan/show_tables.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88

99
// ShowTables is a node that shows the database tables.
1010
type ShowTables struct {
11-
Database sql.Database
12-
Full bool
11+
db sql.Database
12+
Full bool
1313
}
1414

1515
var showTablesSchema = sql.Schema{
@@ -24,14 +24,28 @@ var showTablesFullSchema = sql.Schema{
2424
// NewShowTables creates a new show tables node given a database.
2525
func NewShowTables(database sql.Database, full bool) *ShowTables {
2626
return &ShowTables{
27-
Database: database,
28-
Full: full,
27+
db: database,
28+
Full: full,
2929
}
3030
}
3131

32+
var _ sql.Databaser = (*ShowTables)(nil)
33+
34+
// Database implements the sql.Databaser interface.
35+
func (p *ShowTables) Database() sql.Database {
36+
return p.db
37+
}
38+
39+
// WithDatabase implements the sql.Databaser interface.
40+
func (p *ShowTables) WithDatabase(db sql.Database) (sql.Node, error) {
41+
nc := *p
42+
nc.db = db
43+
return &nc, nil
44+
}
45+
3246
// Resolved implements the Resolvable interface.
3347
func (p *ShowTables) Resolved() bool {
34-
_, ok := p.Database.(sql.UnresolvedDatabase)
48+
_, ok := p.db.(sql.UnresolvedDatabase)
3549
return !ok
3650
}
3751

@@ -52,7 +66,7 @@ func (p *ShowTables) Schema() sql.Schema {
5266
// RowIter implements the Node interface.
5367
func (p *ShowTables) RowIter(ctx *sql.Context) (sql.RowIter, error) {
5468
tableNames := []string{}
55-
for key := range p.Database.Tables() {
69+
for key := range p.db.Tables() {
5670
tableNames = append(tableNames, key)
5771
}
5872

@@ -72,7 +86,7 @@ func (p *ShowTables) RowIter(ctx *sql.Context) (sql.RowIter, error) {
7286

7387
// TransformUp implements the Transformable interface.
7488
func (p *ShowTables) TransformUp(f sql.TransformNodeFunc) (sql.Node, error) {
75-
return f(NewShowTables(p.Database, p.Full))
89+
return f(NewShowTables(p.db, p.Full))
7690
}
7791

7892
// TransformExpressionsUp implements the Transformable interface.

sql/plan/use.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,36 @@ import (
88

99
// Use changes the current database.
1010
type Use struct {
11-
Database sql.Database
12-
Catalog *sql.Catalog
11+
db sql.Database
12+
Catalog *sql.Catalog
1313
}
1414

1515
// NewUse creates a new Use node.
1616
func NewUse(db sql.Database) *Use {
17-
return &Use{Database: db}
17+
return &Use{db: db}
1818
}
1919

2020
var _ sql.Node = (*Use)(nil)
21+
var _ sql.Databaser = (*Use)(nil)
22+
23+
// Database implements the sql.Databaser interface.
24+
func (u *Use) Database() sql.Database {
25+
return u.db
26+
}
27+
28+
// WithDatabase implements the sql.Databaser interface.
29+
func (u *Use) WithDatabase(db sql.Database) (sql.Node, error) {
30+
nc := *u
31+
nc.db = db
32+
return &nc, nil
33+
}
2134

2235
// Children implements the sql.Node interface.
2336
func (Use) Children() []sql.Node { return nil }
2437

2538
// Resolved implements the sql.Node interface.
2639
func (u *Use) Resolved() bool {
27-
_, ok := u.Database.(sql.UnresolvedDatabase)
40+
_, ok := u.db.(sql.UnresolvedDatabase)
2841
return !ok
2942
}
3043

@@ -33,7 +46,7 @@ func (Use) Schema() sql.Schema { return nil }
3346

3447
// RowIter implements the sql.Node interface.
3548
func (u *Use) RowIter(ctx *sql.Context) (sql.RowIter, error) {
36-
u.Catalog.SetCurrentDatabase(u.Database.Name())
49+
u.Catalog.SetCurrentDatabase(u.db.Name())
3750
return sql.RowsToRowIter(), nil
3851
}
3952

@@ -49,5 +62,5 @@ func (u *Use) TransformExpressionsUp(f sql.TransformExprFunc) (sql.Node, error)
4962

5063
// String implements the sql.Node interface.
5164
func (u *Use) String() string {
52-
return fmt.Sprintf("USE(%s)", u.Database.Name())
65+
return fmt.Sprintf("USE(%s)", u.db.Name())
5366
}

0 commit comments

Comments
 (0)