|
| 1 | +package function |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "fmt" |
| 6 | + "reflect" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "gopkg.in/src-d/go-mysql-server.v0/sql" |
| 10 | + "gopkg.in/src-d/go-mysql-server.v0/sql/expression" |
| 11 | +) |
| 12 | + |
| 13 | +// ToBase64 is a function to encode a string to the Base64 format |
| 14 | +// using the same dialect that MySQL's TO_BASE64 uses |
| 15 | +type ToBase64 struct { |
| 16 | + expression.UnaryExpression |
| 17 | +} |
| 18 | + |
| 19 | +// NewToBase64 creates a new ToBase64 expression. |
| 20 | +func NewToBase64(e sql.Expression) sql.Expression { |
| 21 | + return &ToBase64{expression.UnaryExpression{Child: e}} |
| 22 | +} |
| 23 | + |
| 24 | +// Eval implements the Expression interface. |
| 25 | +func (t *ToBase64) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { |
| 26 | + str, err := t.Child.Eval(ctx, row) |
| 27 | + |
| 28 | + if err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + |
| 32 | + if str == nil { |
| 33 | + return nil, nil |
| 34 | + } |
| 35 | + |
| 36 | + str, err = sql.Text.Convert(str) |
| 37 | + if err != nil { |
| 38 | + return nil, sql.ErrInvalidType.New(reflect.TypeOf(str)) |
| 39 | + } |
| 40 | + |
| 41 | + encoded := base64.StdEncoding.EncodeToString([]byte(str.(string))) |
| 42 | + |
| 43 | + lenEncoded := len(encoded) |
| 44 | + if lenEncoded <= 76 { |
| 45 | + return encoded, nil |
| 46 | + } |
| 47 | + |
| 48 | + // Split into max 76 chars lines |
| 49 | + var out strings.Builder |
| 50 | + start := 0 |
| 51 | + end := 76 |
| 52 | + for { |
| 53 | + out.WriteString(encoded[start:end] + "\n") |
| 54 | + start += 76 |
| 55 | + end += 76 |
| 56 | + if end >= lenEncoded { |
| 57 | + out.WriteString(encoded[start:lenEncoded]) |
| 58 | + break |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return out.String(), nil |
| 63 | +} |
| 64 | + |
| 65 | +// String implements the Stringer interface. |
| 66 | +func (t *ToBase64) String() string { |
| 67 | + return fmt.Sprintf("TO_BASE64(%s)", t.Child) |
| 68 | +} |
| 69 | + |
| 70 | +// IsNullable implements the Expression interface. |
| 71 | +func (t *ToBase64) IsNullable() bool { |
| 72 | + return t.Child.IsNullable() |
| 73 | +} |
| 74 | + |
| 75 | +// TransformUp implements the Expression interface. |
| 76 | +func (t *ToBase64) TransformUp(f sql.TransformExprFunc) (sql.Expression, error) { |
| 77 | + child, err := t.Child.TransformUp(f) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + return f(NewToBase64(child)) |
| 82 | +} |
| 83 | + |
| 84 | +// Type implements the Expression interface. |
| 85 | +func (t *ToBase64) Type() sql.Type { |
| 86 | + return sql.Text |
| 87 | +} |
| 88 | + |
| 89 | + |
| 90 | +// FromBase64 is a function to decode a Base64-formatted string |
| 91 | +// using the same dialect that MySQL's FROM_BASE64 uses |
| 92 | +type FromBase64 struct { |
| 93 | + expression.UnaryExpression |
| 94 | +} |
| 95 | + |
| 96 | +// NewFromBase64 creates a new FromBase64 expression. |
| 97 | +func NewFromBase64(e sql.Expression) sql.Expression { |
| 98 | + return &FromBase64{expression.UnaryExpression{Child: e}} |
| 99 | +} |
| 100 | + |
| 101 | +// Eval implements the Expression interface. |
| 102 | +func (t *FromBase64) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { |
| 103 | + str, err := t.Child.Eval(ctx, row) |
| 104 | + |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + |
| 109 | + if str == nil { |
| 110 | + return nil, nil |
| 111 | + } |
| 112 | + |
| 113 | + str, err = sql.Text.Convert(str) |
| 114 | + if err != nil { |
| 115 | + return nil, sql.ErrInvalidType.New(reflect.TypeOf(str)) |
| 116 | + } |
| 117 | + |
| 118 | + decoded, err := base64.StdEncoding.DecodeString(str.(string)) |
| 119 | + if err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + |
| 123 | + return string(decoded), nil |
| 124 | +} |
| 125 | + |
| 126 | +// String implements the Stringer interface. |
| 127 | +func (t *FromBase64) String() string { |
| 128 | + return fmt.Sprintf("FROM_BASE64(%s)", t.Child) |
| 129 | +} |
| 130 | + |
| 131 | +// IsNullable implements the Expression interface. |
| 132 | +func (t *FromBase64) IsNullable() bool { |
| 133 | + return t.Child.IsNullable() |
| 134 | +} |
| 135 | + |
| 136 | +// TransformUp implements the Expression interface. |
| 137 | +func (t *FromBase64) TransformUp(f sql.TransformExprFunc) (sql.Expression, error) { |
| 138 | + child, err := t.Child.TransformUp(f) |
| 139 | + if err != nil { |
| 140 | + return nil, err |
| 141 | + } |
| 142 | + return f(NewFromBase64(child)) |
| 143 | +} |
| 144 | + |
| 145 | +// Type implements the Expression interface. |
| 146 | +func (t *FromBase64) Type() sql.Type { |
| 147 | + return sql.Text |
| 148 | +} |
0 commit comments