This repository was archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathlower_upper.go
107 lines (87 loc) · 2.23 KB
/
lower_upper.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package function
import (
"fmt"
"strings"
"github.com/src-d/go-mysql-server/sql"
"github.com/src-d/go-mysql-server/sql/expression"
)
// Lower is a function that returns the lowercase of the text provided.
type Lower struct {
expression.UnaryExpression
}
// NewLower creates a new Lower expression.
func NewLower(e sql.Expression) sql.Expression {
return &Lower{expression.UnaryExpression{Child: e}}
}
// Eval implements the Expression interface.
func (l *Lower) Eval(
ctx *sql.Context,
row sql.Row,
) (interface{}, error) {
v, err := l.Child.Eval(ctx, row)
if err != nil {
return nil, err
}
if v == nil {
return nil, nil
}
v, err = sql.Text.Convert(v)
if err != nil {
return nil, err
}
return strings.ToLower(v.(string)), nil
}
func (l *Lower) String() string {
return fmt.Sprintf("LOWER(%s)", l.Child)
}
// WithChildren implements the Expression interface.
func (l *Lower) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(l, len(children), 1)
}
return NewLower(children[0]), nil
}
// Type implements the Expression interface.
func (l *Lower) Type() sql.Type {
return l.Child.Type()
}
// Upper is a function that returns the UPPERCASE of the text provided.
type Upper struct {
expression.UnaryExpression
}
// NewUpper creates a new Lower expression.
func NewUpper(e sql.Expression) sql.Expression {
return &Upper{expression.UnaryExpression{Child: e}}
}
// Eval implements the Expression interface.
func (u *Upper) Eval(
ctx *sql.Context,
row sql.Row,
) (interface{}, error) {
v, err := u.Child.Eval(ctx, row)
if err != nil {
return nil, err
}
if v == nil {
return nil, nil
}
v, err = sql.Text.Convert(v)
if err != nil {
return nil, err
}
return strings.ToUpper(v.(string)), nil
}
func (u *Upper) String() string {
return fmt.Sprintf("UPPER(%s)", u.Child)
}
// WithChildren implements the Expression interface.
func (u *Upper) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(u, len(children), 1)
}
return NewUpper(children[0]), nil
}
// Type implements the Expression interface.
func (u *Upper) Type() sql.Type {
return u.Child.Type()
}