github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/spatial/multilinestring.go (about)

     1  // Copyright 2020-2022 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 spatial
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/dolthub/go-mysql-server/sql/expression"
    22  	"github.com/dolthub/go-mysql-server/sql/types"
    23  
    24  	"github.com/dolthub/go-mysql-server/sql"
    25  )
    26  
    27  // MultiLineString is a function that returns a MultiLineString.
    28  type MultiLineString struct {
    29  	expression.NaryExpression
    30  }
    31  
    32  var _ sql.FunctionExpression = (*MultiLineString)(nil)
    33  var _ sql.CollationCoercible = (*MultiLineString)(nil)
    34  
    35  // NewMultiLineString creates a new multilinestring expression.
    36  func NewMultiLineString(args ...sql.Expression) (sql.Expression, error) {
    37  	if len(args) < 1 {
    38  		return nil, sql.ErrInvalidArgumentNumber.New("MultiLineString", "1 or more", len(args))
    39  	}
    40  	return &MultiLineString{expression.NaryExpression{ChildExpressions: args}}, nil
    41  }
    42  
    43  // FunctionName implements sql.FunctionExpression
    44  func (p *MultiLineString) FunctionName() string {
    45  	return "multilinestring"
    46  }
    47  
    48  // Description implements sql.FunctionExpression
    49  func (p *MultiLineString) Description() string {
    50  	return "returns a new multilinestring."
    51  }
    52  
    53  // Type implements the sql.Expression interface.
    54  func (p *MultiLineString) Type() sql.Type {
    55  	return types.MultiLineStringType{}
    56  }
    57  
    58  // CollationCoercibility implements the interface sql.CollationCoercible.
    59  func (*MultiLineString) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    60  	return sql.Collation_binary, 5
    61  }
    62  
    63  func (p *MultiLineString) String() string {
    64  	var args = make([]string, len(p.ChildExpressions))
    65  	for i, arg := range p.ChildExpressions {
    66  		args[i] = arg.String()
    67  	}
    68  	return fmt.Sprintf("%s(%s)", p.FunctionName(), strings.Join(args, ","))
    69  }
    70  
    71  // WithChildren implements the Expression interface.
    72  func (p *MultiLineString) WithChildren(children ...sql.Expression) (sql.Expression, error) {
    73  	return NewMultiLineString(children...)
    74  }
    75  
    76  // Eval implements the sql.Expression interface.
    77  func (p *MultiLineString) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
    78  	var lines = make([]types.LineString, len(p.ChildExpressions))
    79  	for i, arg := range p.ChildExpressions {
    80  		val, err := arg.Eval(ctx, row)
    81  		if err != nil {
    82  			return nil, err
    83  		}
    84  		switch v := val.(type) {
    85  		case types.LineString:
    86  			lines[i] = v
    87  		case types.GeometryValue:
    88  			return nil, sql.ErrInvalidArgumentDetails.New(p.FunctionName(), v)
    89  		default:
    90  			return nil, sql.ErrIllegalGISValue.New(v)
    91  		}
    92  	}
    93  
    94  	return types.MultiLineString{Lines: lines}, nil
    95  }