github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/spatial/geometry_collection.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  // GeomColl is a function that returns a GeometryCollection.
    28  type GeomColl struct {
    29  	expression.NaryExpression
    30  }
    31  
    32  var _ sql.FunctionExpression = (*GeomColl)(nil)
    33  var _ sql.CollationCoercible = (*GeomColl)(nil)
    34  
    35  // NewGeomColl creates a new geometrycollection expression.
    36  func NewGeomColl(args ...sql.Expression) (sql.Expression, error) {
    37  	return &GeomColl{expression.NaryExpression{ChildExpressions: args}}, nil
    38  }
    39  
    40  // FunctionName implements sql.FunctionExpression
    41  func (g *GeomColl) FunctionName() string {
    42  	return "geometrycollection"
    43  }
    44  
    45  // Description implements sql.FunctionExpression
    46  func (g *GeomColl) Description() string {
    47  	return "returns a new geometrycollection."
    48  }
    49  
    50  // Type implements the sql.Expression interface.
    51  func (g *GeomColl) Type() sql.Type {
    52  	return types.GeomCollType{}
    53  }
    54  
    55  // CollationCoercibility implements the interface sql.CollationCoercible.
    56  func (*GeomColl) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    57  	return sql.Collation_binary, 5
    58  }
    59  
    60  func (g *GeomColl) String() string {
    61  	var args = make([]string, len(g.ChildExpressions))
    62  	for i, arg := range g.ChildExpressions {
    63  		args[i] = arg.String()
    64  	}
    65  	return fmt.Sprintf("geomcoll(%s)", strings.Join(args, ","))
    66  }
    67  
    68  // WithChildren implements the Expression interface.
    69  func (g *GeomColl) WithChildren(children ...sql.Expression) (sql.Expression, error) {
    70  	return NewGeomColl(children...)
    71  }
    72  
    73  // Eval implements the sql.Expression interface.
    74  func (g *GeomColl) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
    75  	var geoms = make([]types.GeometryValue, len(g.ChildExpressions))
    76  	for i, arg := range g.ChildExpressions {
    77  		val, err := arg.Eval(ctx, row)
    78  		if err != nil {
    79  			return nil, err
    80  		}
    81  		switch v := val.(type) {
    82  		case types.GeometryValue:
    83  			geoms[i] = v
    84  		default:
    85  			return nil, sql.ErrIllegalGISValue.New(v)
    86  		}
    87  	}
    88  
    89  	return types.GeomColl{Geoms: geoms}, nil
    90  }