-
Notifications
You must be signed in to change notification settings - Fork 0
/
tobase64_frombase64.go
executable file
·183 lines (148 loc) · 4.52 KB
/
tobase64_frombase64.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2020-2021 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package function
import (
"encoding/base64"
"fmt"
"reflect"
"strings"
"github.com/Rock-liyi/p2pdb-store/sql"
"github.com/Rock-liyi/p2pdb-store/sql/expression"
)
// ToBase64 is a function to encode a string to the Base64 format
// using the same dialect that MySQL's TO_BASE64 uses
type ToBase64 struct {
expression.UnaryExpression
}
var _ sql.FunctionExpression = (*ToBase64)(nil)
// NewToBase64 creates a new ToBase64 expression.
func NewToBase64(e sql.Expression) sql.Expression {
return &ToBase64{expression.UnaryExpression{Child: e}}
}
// FunctionName implements sql.FunctionExpression
func (t *ToBase64) FunctionName() string {
return "to_base64"
}
// Description implements sql.FunctionExpression
func (t *ToBase64) Description() string {
return "encodes the string str in base64 format."
}
// Eval implements the Expression interface.
func (t *ToBase64) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
str, err := t.Child.Eval(ctx, row)
if err != nil {
return nil, err
}
if str == nil {
return nil, nil
}
str, err = sql.LongText.Convert(str)
if err != nil {
return nil, sql.ErrInvalidType.New(reflect.TypeOf(str))
}
encoded := base64.StdEncoding.EncodeToString([]byte(str.(string)))
lenEncoded := len(encoded)
if lenEncoded <= 76 {
return encoded, nil
}
// Split into max 76 chars lines
var out strings.Builder
start := 0
end := 76
for {
out.WriteString(encoded[start:end] + "\n")
start += 76
end += 76
if end >= lenEncoded {
out.WriteString(encoded[start:lenEncoded])
break
}
}
return out.String(), nil
}
// String implements the fmt.Stringer interface.
func (t *ToBase64) String() string {
return fmt.Sprintf("TO_BASE64(%s)", t.Child)
}
// IsNullable implements the Expression interface.
func (t *ToBase64) IsNullable() bool {
return t.Child.IsNullable()
}
// WithChildren implements the Expression interface.
func (t *ToBase64) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(t, len(children), 1)
}
return NewToBase64(children[0]), nil
}
// Type implements the Expression interface.
func (t *ToBase64) Type() sql.Type {
return sql.LongText
}
// FromBase64 is a function to decode a Base64-formatted string
// using the same dialect that MySQL's FROM_BASE64 uses
type FromBase64 struct {
expression.UnaryExpression
}
var _ sql.FunctionExpression = (*FromBase64)(nil)
// NewFromBase64 creates a new FromBase64 expression.
func NewFromBase64(e sql.Expression) sql.Expression {
return &FromBase64{expression.UnaryExpression{Child: e}}
}
// FunctionName implements sql.FunctionExpression
func (t *FromBase64) FunctionName() string {
return "from_base64"
}
// Description implements sql.FunctionExpression
func (t *FromBase64) Description() string {
return "decodes the base64-encoded string str."
}
// Eval implements the Expression interface.
func (t *FromBase64) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
str, err := t.Child.Eval(ctx, row)
if err != nil {
return nil, err
}
if str == nil {
return nil, nil
}
str, err = sql.LongText.Convert(str)
if err != nil {
return nil, sql.ErrInvalidType.New(reflect.TypeOf(str))
}
decoded, err := base64.StdEncoding.DecodeString(str.(string))
if err != nil {
return nil, err
}
return string(decoded), nil
}
// String implements the fmt.Stringer interface.
func (t *FromBase64) String() string {
return fmt.Sprintf("FROM_BASE64(%s)", t.Child)
}
// IsNullable implements the Expression interface.
func (t *FromBase64) IsNullable() bool {
return t.Child.IsNullable()
}
// WithChildren implements the Expression interface.
func (t *FromBase64) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(t, len(children), 1)
}
return NewFromBase64(children[0]), nil
}
// Type implements the Expression interface.
func (t *FromBase64) Type() sql.Type {
return sql.LongText
}