github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/spatial/multipolygon.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 // MultiPolygon is a function that returns a MultiPolygon. 28 type MultiPolygon struct { 29 expression.NaryExpression 30 } 31 32 var _ sql.FunctionExpression = (*MultiPolygon)(nil) 33 var _ sql.CollationCoercible = (*MultiPolygon)(nil) 34 35 // NewMultiPolygon creates a new multipolygon expression. 36 func NewMultiPolygon(args ...sql.Expression) (sql.Expression, error) { 37 if len(args) < 1 { 38 return nil, sql.ErrInvalidArgumentNumber.New("MultiPolygon", "1 or more", len(args)) 39 } 40 return &MultiPolygon{expression.NaryExpression{ChildExpressions: args}}, nil 41 } 42 43 // FunctionName implements sql.FunctionExpression 44 func (p *MultiPolygon) FunctionName() string { 45 return "multipolygon" 46 } 47 48 // Description implements sql.FunctionExpression 49 func (p *MultiPolygon) Description() string { 50 return "returns a new multipolygon." 51 } 52 53 // Type implements the sql.Expression interface. 54 func (p *MultiPolygon) Type() sql.Type { 55 return types.MultiPolygonType{} 56 } 57 58 // CollationCoercibility implements the interface sql.CollationCoercible. 59 func (*MultiPolygon) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { 60 return sql.Collation_binary, 5 61 } 62 63 func (p *MultiPolygon) 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 *MultiPolygon) WithChildren(children ...sql.Expression) (sql.Expression, error) { 73 return NewMultiPolygon(children...) 74 } 75 76 // Eval implements the sql.Expression interface. 77 func (p *MultiPolygon) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 78 var polys = make([]types.Polygon, 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.Polygon: 86 polys[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.MultiPolygon{Polygons: polys}, nil 95 }