|
| 1 | +package function |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "gopkg.in/src-d/go-mysql-server.v0/sql/expression" |
| 8 | + "gopkg.in/src-d/go-mysql-server.v0/sql" |
| 9 | + "gopkg.in/src-d/go-errors.v1" |
| 10 | +) |
| 11 | + |
| 12 | +// Reverse is a function that returns the reverse of the text provided. |
| 13 | +type Reverse struct { |
| 14 | + expression.UnaryExpression |
| 15 | +} |
| 16 | + |
| 17 | +// NewReverse creates a new Reverse expression. |
| 18 | +func NewReverse(e sql.Expression) sql.Expression { |
| 19 | + return &Reverse{expression.UnaryExpression{Child: e}} |
| 20 | +} |
| 21 | + |
| 22 | +// Eval implements the Expression interface. |
| 23 | +func (r *Reverse) Eval( |
| 24 | + ctx *sql.Context, |
| 25 | + row sql.Row, |
| 26 | +) (interface{}, error) { |
| 27 | + v, err := r.Child.Eval(ctx, row) |
| 28 | + if v == nil || err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + |
| 32 | + v, err = sql.Text.Convert(v) |
| 33 | + if err != nil { |
| 34 | + return nil, err |
| 35 | + } |
| 36 | + |
| 37 | + return reverseString(v.(string)), nil |
| 38 | +} |
| 39 | + |
| 40 | +func reverseString(s string) string { |
| 41 | + r := []rune(s) |
| 42 | + for i, j := 0, len(r) - 1; i < j; i, j = i+1, j-1 { |
| 43 | + r[i], r[j] = r[j], r[i] |
| 44 | + } |
| 45 | + return string(r) |
| 46 | +} |
| 47 | + |
| 48 | +func (r *Reverse) String() string { |
| 49 | + return fmt.Sprintf("reverse(%s)", r.Child) |
| 50 | +} |
| 51 | + |
| 52 | +// TransformUp implements the Expression interface. |
| 53 | +func (r *Reverse) TransformUp(f sql.TransformExprFunc) (sql.Expression, error) { |
| 54 | + child, err := r.Child.TransformUp(f) |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + return f(NewReverse(child)) |
| 59 | +} |
| 60 | + |
| 61 | +// Type implements the Expression interface. |
| 62 | +func (r *Reverse) Type() sql.Type { |
| 63 | + return r.Child.Type() |
| 64 | +} |
| 65 | + |
| 66 | +var ErrNegativeRepeatCount = errors.NewKind("negative Repeat count: %v") |
| 67 | + |
| 68 | +// Repeat is a function that returns the string repeated n times. |
| 69 | +type Repeat struct { |
| 70 | + expression.BinaryExpression |
| 71 | +} |
| 72 | + |
| 73 | +// NewRepeat creates a new Repeat expression. |
| 74 | +func NewRepeat(str sql.Expression, count sql.Expression) sql.Expression { |
| 75 | + return &Repeat{expression.BinaryExpression{Left: str, Right: count}} |
| 76 | +} |
| 77 | + |
| 78 | +func (r *Repeat) String() string { |
| 79 | + return fmt.Sprintf("repeat(%s, %s)", r.Left, r.Right) |
| 80 | +} |
| 81 | + |
| 82 | +// Type implements the Expression interface. |
| 83 | +func (r *Repeat) Type() sql.Type { |
| 84 | + return sql.Text |
| 85 | +} |
| 86 | + |
| 87 | +// TransformUp implements the Expression interface. |
| 88 | +func (r *Repeat) TransformUp(f sql.TransformExprFunc) (sql.Expression, error) { |
| 89 | + left, err := r.Left.TransformUp(f) |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + |
| 94 | + right, err := r.Right.TransformUp(f) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + return f(NewRepeat(left, right)) |
| 99 | +} |
| 100 | + |
| 101 | +// Eval implements the Expression interface. |
| 102 | +func (r *Repeat) Eval( |
| 103 | + ctx *sql.Context, |
| 104 | + row sql.Row, |
| 105 | +) (interface{}, error) { |
| 106 | + str, err := r.Left.Eval(ctx, row) |
| 107 | + if str == nil || err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + |
| 111 | + str, err = sql.Text.Convert(str) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + |
| 116 | + count, err := r.Right.Eval(ctx, row) |
| 117 | + if count == nil || err != nil { |
| 118 | + return nil, err |
| 119 | + } |
| 120 | + |
| 121 | + count, err = sql.Int32.Convert(count) |
| 122 | + if err != nil { |
| 123 | + return nil, err |
| 124 | + } |
| 125 | + if count.(int32) < 0 { |
| 126 | + return nil, ErrNegativeRepeatCount.New(count) |
| 127 | + } |
| 128 | + return strings.Repeat(str.(string), int(count.(int32))), nil |
| 129 | +} |
| 130 | + |
| 131 | +// Replace is a function that returns a string with all occurrences of fromStr replaced by the |
| 132 | +// string toStr |
| 133 | +type Replace struct { |
| 134 | + str sql.Expression |
| 135 | + fromStr sql.Expression |
| 136 | + toStr sql.Expression |
| 137 | +} |
| 138 | + |
| 139 | +// NewReplace creates a new Replace expression. |
| 140 | +func NewReplace(str sql.Expression, fromStr sql.Expression, toStr sql.Expression) sql.Expression { |
| 141 | + return &Replace{str, fromStr, toStr} |
| 142 | +} |
| 143 | + |
| 144 | +// Children implements the Expression interface. |
| 145 | +func (r *Replace) Children() []sql.Expression { |
| 146 | + return []sql.Expression{r.str, r.fromStr, r.toStr} |
| 147 | +} |
| 148 | + |
| 149 | +// Resolved implements the Expression interface. |
| 150 | +func (r *Replace) Resolved() bool { |
| 151 | + return r.str.Resolved() && r.fromStr.Resolved() && r.toStr.Resolved() |
| 152 | +} |
| 153 | + |
| 154 | +// IsNullable implements the Expression interface. |
| 155 | +func (r *Replace) IsNullable() bool { |
| 156 | + return r.str.IsNullable() || r.fromStr.IsNullable() || r.toStr.IsNullable() |
| 157 | +} |
| 158 | + |
| 159 | +func (r *Replace) String() string { |
| 160 | + return fmt.Sprintf("replace(%s, %s, %s)", r.str, r.fromStr, r.toStr) |
| 161 | +} |
| 162 | + |
| 163 | +// Type implements the Expression interface. |
| 164 | +func (r *Replace) Type() sql.Type { |
| 165 | + return sql.Text |
| 166 | +} |
| 167 | + |
| 168 | +// TransformUp implements the Expression interface. |
| 169 | +func (r *Replace) TransformUp(f sql.TransformExprFunc) (sql.Expression, error) { |
| 170 | + str, err := r.str.TransformUp(f) |
| 171 | + if err != nil { |
| 172 | + return nil, err |
| 173 | + } |
| 174 | + |
| 175 | + fromStr, err := r.fromStr.TransformUp(f) |
| 176 | + if err != nil { |
| 177 | + return nil, err |
| 178 | + } |
| 179 | + |
| 180 | + toStr, err := r.toStr.TransformUp(f) |
| 181 | + if err != nil { |
| 182 | + return nil, err |
| 183 | + } |
| 184 | + return f(NewReplace(str, fromStr, toStr)) |
| 185 | +} |
| 186 | + |
| 187 | +// Eval implements the Expression interface. |
| 188 | +func (r *Replace) Eval( |
| 189 | + ctx *sql.Context, |
| 190 | + row sql.Row, |
| 191 | +) (interface{}, error) { |
| 192 | + str, err := r.str.Eval(ctx, row) |
| 193 | + if str == nil || err != nil { |
| 194 | + return nil, err |
| 195 | + } |
| 196 | + |
| 197 | + str, err = sql.Text.Convert(str) |
| 198 | + if err != nil { |
| 199 | + return nil, err |
| 200 | + } |
| 201 | + |
| 202 | + fromStr, err := r.fromStr.Eval(ctx, row) |
| 203 | + if fromStr == nil || err != nil { |
| 204 | + return nil, err |
| 205 | + } |
| 206 | + |
| 207 | + fromStr, err = sql.Text.Convert(fromStr) |
| 208 | + if err != nil { |
| 209 | + return nil, err |
| 210 | + } |
| 211 | + |
| 212 | + toStr, err := r.toStr.Eval(ctx, row) |
| 213 | + if toStr == nil || err != nil { |
| 214 | + return nil, err |
| 215 | + } |
| 216 | + |
| 217 | + toStr, err = sql.Text.Convert(toStr) |
| 218 | + if err != nil { |
| 219 | + return nil, err |
| 220 | + } |
| 221 | + |
| 222 | + if fromStr.(string) == "" { |
| 223 | + return str, nil |
| 224 | + } |
| 225 | + |
| 226 | + return strings.Replace(str.(string), fromStr.(string), toStr.(string), -1), nil |
| 227 | +} |
0 commit comments