forked from src-d/go-mysql-server
/
show_tables.go
86 lines (69 loc) · 1.77 KB
/
show_tables.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package plan
import (
"io"
"sort"
"gopkg.in/src-d/go-mysql-server.v0/sql"
)
// ShowTables is a node that shows the database tables.
type ShowTables struct {
Database sql.Database
}
// NewShowTables creates a new show tables node given a database.
func NewShowTables(database sql.Database) *ShowTables {
return &ShowTables{
Database: database,
}
}
// Resolved implements the Resolvable interface.
func (p *ShowTables) Resolved() bool {
_, ok := p.Database.(*sql.UnresolvedDatabase)
return !ok
}
// Children implements the Node interface.
func (*ShowTables) Children() []sql.Node {
return nil
}
// Schema implements the Node interface.
func (*ShowTables) Schema() sql.Schema {
return sql.Schema{{
Name: "table",
Type: sql.Text,
Nullable: false,
}}
}
// RowIter implements the Node interface.
func (p *ShowTables) RowIter(ctx *sql.Context) (sql.RowIter, error) {
tableNames := []string{}
for key := range p.Database.Tables() {
tableNames = append(tableNames, key)
}
sort.Strings(tableNames)
return &showTablesIter{tableNames: tableNames}, nil
}
// TransformUp implements the Transformable interface.
func (p *ShowTables) TransformUp(f sql.TransformNodeFunc) (sql.Node, error) {
return f(NewShowTables(p.Database))
}
// TransformExpressionsUp implements the Transformable interface.
func (p *ShowTables) TransformExpressionsUp(f sql.TransformExprFunc) (sql.Node, error) {
return p, nil
}
func (p ShowTables) String() string {
return "ShowTables"
}
type showTablesIter struct {
tableNames []string
idx int
}
func (i *showTablesIter) Next() (sql.Row, error) {
if i.idx >= len(i.tableNames) {
return nil, io.EOF
}
row := sql.NewRow(i.tableNames[i.idx])
i.idx++
return row, nil
}
func (i *showTablesIter) Close() error {
i.tableNames = nil
return nil
}