github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/set.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 expression
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"gopkg.in/src-d/go-errors.v1"
    21  
    22  	"github.com/dolthub/go-mysql-server/sql"
    23  	"github.com/dolthub/go-mysql-server/sql/types"
    24  )
    25  
    26  var errCannotSetField = errors.NewKind("Expected GetField expression on left but got %T")
    27  
    28  // SetField updates the value of a field or a system variable
    29  type SetField struct {
    30  	BinaryExpressionStub
    31  }
    32  
    33  var _ sql.Expression = (*SetField)(nil)
    34  var _ sql.CollationCoercible = (*SetField)(nil)
    35  
    36  // NewSetField creates a new SetField expression.
    37  func NewSetField(left, expr sql.Expression) sql.Expression {
    38  	return &SetField{BinaryExpressionStub{LeftChild: left, RightChild: expr}}
    39  }
    40  
    41  func (s *SetField) String() string {
    42  	return fmt.Sprintf("SET %s = %s", s.LeftChild, s.RightChild)
    43  }
    44  
    45  func (s *SetField) DebugString() string {
    46  	return fmt.Sprintf("SET %s = %s", sql.DebugString(s.LeftChild), sql.DebugString(s.RightChild))
    47  }
    48  
    49  // Type implements the Expression interface.
    50  func (s *SetField) Type() sql.Type {
    51  	return s.LeftChild.Type()
    52  }
    53  
    54  // CollationCoercibility implements the interface sql.CollationCoercible.
    55  func (s *SetField) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    56  	return sql.GetCoercibility(ctx, s.LeftChild)
    57  }
    58  
    59  // Eval implements the Expression interface.
    60  // Returns a copy of the given row with an updated value.
    61  func (s *SetField) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
    62  	getField, ok := s.LeftChild.(*GetField)
    63  	if !ok {
    64  		return nil, errCannotSetField.New(s.LeftChild)
    65  	}
    66  
    67  	if getField.fieldIndex < 0 || getField.fieldIndex >= len(row) {
    68  		return nil, ErrIndexOutOfBounds.New(getField.fieldIndex, len(row))
    69  	}
    70  	val, err := s.RightChild.Eval(ctx, row)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  	if val != nil {
    75  		convertedVal, _, err := getField.fieldType.Convert(val)
    76  		if err != nil {
    77  			// Fill in error with information
    78  			if types.ErrLengthBeyondLimit.Is(err) {
    79  				return nil, sql.NewWrappedTypeConversionError(val, getField.fieldIndex, types.ErrLengthBeyondLimit.New(val, getField.Name()))
    80  			}
    81  			return nil, sql.NewWrappedTypeConversionError(val, getField.fieldIndex, err)
    82  		}
    83  		val = convertedVal
    84  	}
    85  	updatedRow := row.Copy()
    86  	updatedRow[getField.fieldIndex] = val
    87  	return updatedRow, nil
    88  }
    89  
    90  // WithChildren implements the Expression interface.
    91  func (s *SetField) WithChildren(children ...sql.Expression) (sql.Expression, error) {
    92  	if len(children) != 2 {
    93  		return nil, sql.ErrInvalidChildrenNumber.New(s, len(children), 2)
    94  	}
    95  	return NewSetField(children[0], children[1]), nil
    96  }