|
| 1 | +// Copyright 2025 Dolthub, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package function |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "github.com/dolthub/go-mysql-server/sql" |
| 20 | + "github.com/dolthub/go-mysql-server/sql/expression" |
| 21 | + "github.com/dolthub/go-mysql-server/sql/types" |
| 22 | +) |
| 23 | + |
| 24 | +// Oct function provides a string representation for the octal value of N, where N is a decimal number. |
| 25 | +type Oct struct { |
| 26 | + n sql.Expression |
| 27 | +} |
| 28 | + |
| 29 | +var _ sql.FunctionExpression = (*Oct)(nil) |
| 30 | +var _ sql.CollationCoercible = (*Oct)(nil) |
| 31 | + |
| 32 | +func NewOct(n sql.Expression) sql.Expression { return &Oct{n} } |
| 33 | + |
| 34 | +func (o *Oct) FunctionName() string { |
| 35 | + return "oct" |
| 36 | +} |
| 37 | + |
| 38 | +func (o *Oct) Description() string { |
| 39 | + return "returns a string representation for octal value of N, where N is a decimal number." |
| 40 | +} |
| 41 | + |
| 42 | +func (o *Oct) Type() sql.Type { |
| 43 | + return types.LongText |
| 44 | +} |
| 45 | + |
| 46 | +func (o *Oct) IsNullable() bool { |
| 47 | + return o.n.IsNullable() |
| 48 | +} |
| 49 | + |
| 50 | +func (o *Oct) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { |
| 51 | + // Convert a decimal (base 10) number to octal (base 8) |
| 52 | + return NewConv( |
| 53 | + o.n, |
| 54 | + expression.NewLiteral(10, types.Int64), |
| 55 | + expression.NewLiteral(8, types.Int64), |
| 56 | + ).Eval(ctx, row) |
| 57 | +} |
| 58 | + |
| 59 | +func (o *Oct) Resolved() bool { |
| 60 | + return o.n.Resolved() |
| 61 | +} |
| 62 | + |
| 63 | +func (o *Oct) Children() []sql.Expression { |
| 64 | + return []sql.Expression{o.n} |
| 65 | +} |
| 66 | + |
| 67 | +func (o *Oct) WithChildren(children ...sql.Expression) (sql.Expression, error) { |
| 68 | + if len(children) != 1 { |
| 69 | + return nil, sql.ErrInvalidChildrenNumber.New(o, len(children), 1) |
| 70 | + } |
| 71 | + return NewOct(children[0]), nil |
| 72 | +} |
| 73 | + |
| 74 | +func (o *Oct) String() string { |
| 75 | + return fmt.Sprintf("%s(%s)", o.FunctionName(), o.n) |
| 76 | +} |
| 77 | + |
| 78 | +func (*Oct) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { |
| 79 | + return ctx.GetCollation(), 4 // strings with collations |
| 80 | +} |
0 commit comments