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

     1  // Copyright 2020-2021 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  
    20  	"github.com/dolthub/go-mysql-server/sql"
    21  	"github.com/dolthub/go-mysql-server/sql/expression"
    22  	"github.com/dolthub/go-mysql-server/sql/types"
    23  )
    24  
    25  // SwapXY is a function that returns a spatial type with their X and Y values swapped
    26  type SwapXY struct {
    27  	expression.UnaryExpression
    28  }
    29  
    30  var _ sql.FunctionExpression = (*SwapXY)(nil)
    31  var _ sql.CollationCoercible = (*SwapXY)(nil)
    32  
    33  // NewSwapXY creates a new point expression.
    34  func NewSwapXY(e sql.Expression) sql.Expression {
    35  	return &SwapXY{expression.UnaryExpression{Child: e}}
    36  }
    37  
    38  // FunctionName implements sql.FunctionExpression
    39  func (s *SwapXY) FunctionName() string {
    40  	return "st_swapxy"
    41  }
    42  
    43  // Description implements sql.FunctionExpression
    44  func (s *SwapXY) Description() string {
    45  	return "returns the geometry with the x and y values swapped."
    46  }
    47  
    48  // IsNullable implements the sql.Expression interface.
    49  func (s *SwapXY) IsNullable() bool {
    50  	return s.Child.IsNullable()
    51  }
    52  
    53  // Type implements the sql.Expression interface.
    54  func (s *SwapXY) Type() sql.Type {
    55  	return s.Child.Type()
    56  }
    57  
    58  // CollationCoercibility implements the interface sql.CollationCoercible.
    59  func (*SwapXY) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    60  	return sql.Collation_binary, 4
    61  }
    62  
    63  func (s *SwapXY) String() string {
    64  	return fmt.Sprintf("%s(%s)", s.FunctionName(), s.Child.String())
    65  }
    66  
    67  // WithChildren implements the Expression interface.
    68  func (s *SwapXY) WithChildren(children ...sql.Expression) (sql.Expression, error) {
    69  	if len(children) != 1 {
    70  		return nil, sql.ErrInvalidChildrenNumber.New(s, len(children), 1)
    71  	}
    72  	return NewSwapXY(children[0]), nil
    73  }
    74  
    75  // Eval implements the sql.Expression interface.
    76  func (s *SwapXY) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
    77  	// Evaluate child
    78  	val, err := s.Child.Eval(ctx, row)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	// Return nil if geometry is nil
    84  	if val == nil {
    85  		return nil, nil
    86  	}
    87  
    88  	// Expect one of the geometry types
    89  	switch v := val.(type) {
    90  	case types.GeometryValue:
    91  		return v.Swap(), nil
    92  	default:
    93  		return nil, sql.ErrInvalidGISData.New(s.FunctionName())
    94  	}
    95  }