/
showvariables.go
89 lines (75 loc) · 2.28 KB
/
showvariables.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
87
88
89
package plan
import (
"fmt"
"github.com/mushiyu/go-mysql-server/sql"
"github.com/mushiyu/go-mysql-server/sql/expression"
)
// ShowVariables is a node that shows the global and session variables
type ShowVariables struct {
config map[string]sql.TypedValue
pattern string
}
// NewShowVariables returns a new ShowVariables reference.
// config is a variables lookup table
// like is a "like pattern". If like is an empty string it will return all variables.
func NewShowVariables(config map[string]sql.TypedValue, like string) *ShowVariables {
return &ShowVariables{
config: config,
pattern: like,
}
}
// Resolved implements sql.Node interface. The function always returns true.
func (sv *ShowVariables) Resolved() bool {
return true
}
// WithChildren implements the Node interface.
func (sv *ShowVariables) WithChildren(children ...sql.Node) (sql.Node, error) {
if len(children) != 0 {
return nil, sql.ErrInvalidChildrenNumber.New(sv, len(children), 0)
}
return sv, nil
}
// String implements the Stringer interface.
func (sv *ShowVariables) String() string {
var like string
if sv.pattern != "" {
like = fmt.Sprintf(" LIKE '%s'", sv.pattern)
}
return fmt.Sprintf("SHOW VARIABLES%s", like)
}
// Schema returns a new Schema reference for "SHOW VARIABLES" query.
func (*ShowVariables) Schema() sql.Schema {
return sql.Schema{
&sql.Column{Name: "Variable_name", Type: sql.Text, Nullable: false},
&sql.Column{Name: "Value", Type: sql.Text, Nullable: true},
}
}
// Children implements sql.Node interface. The function always returns nil.
func (*ShowVariables) Children() []sql.Node { return nil }
// RowIter implements the sql.Node interface.
// The function returns an iterator for filtered variables (based on like pattern)
func (sv *ShowVariables) RowIter(ctx *sql.Context) (sql.RowIter, error) {
var (
rows []sql.Row
like sql.Expression
)
if sv.pattern != "" {
like = expression.NewLike(
expression.NewGetField(0, sql.Text, "", false),
expression.NewGetField(1, sql.Text, sv.pattern, false),
)
}
for k, v := range sv.config {
if like != nil {
b, err := like.Eval(ctx, sql.NewRow(k, sv.pattern))
if err != nil {
return nil, err
}
if !b.(bool) {
continue
}
}
rows = append(rows, sql.NewRow(k, v.Value))
}
return sql.RowsToRowIter(rows...), nil
}