github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/values.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 function
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/dolthub/go-mysql-server/sql"
    21  	"github.com/dolthub/go-mysql-server/sql/expression"
    22  )
    23  
    24  // Values is used in an ON DUPLICATE KEY UPDATE statement to return the value stated in the to-be-inserted column.
    25  // For example, given the following statement:
    26  // INSERT INTO table (pk, v1, v2) VALUES (1, 3, 5), (2, 4, 6) ON DUPLICATE KEY UPDATE v2 = values(v1) * 10;
    27  // the values inserted into v2 would be 30 and 40.
    28  type Values struct {
    29  	expression.UnaryExpression
    30  	Value interface{}
    31  }
    32  
    33  var _ sql.FunctionExpression = (*Values)(nil)
    34  var _ sql.CollationCoercible = (*Values)(nil)
    35  
    36  // NewValues creates a new Values function.
    37  func NewValues(col sql.Expression) sql.Expression {
    38  	return &Values{
    39  		UnaryExpression: expression.UnaryExpression{Child: col},
    40  		Value:           nil,
    41  	}
    42  }
    43  
    44  // Eval implements sql.FunctionExpression.
    45  func (v *Values) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
    46  	// If Value is never assigned to then it has the nil value. It will only be assigned to in the ON DUPLICATE KEY UPDATE
    47  	// statement, therefore when used in every other context it will return nil, which is the correct and intended behavior.
    48  	return v.Value, nil
    49  }
    50  
    51  // FunctionName implements sql.FunctionExpression.
    52  func (v *Values) FunctionName() string {
    53  	return "values"
    54  }
    55  
    56  // Description implements sql.FunctionExpression.
    57  func (v *Values) Description() string {
    58  	return "defines the values to be used during an INSERT."
    59  }
    60  
    61  // String implements sql.FunctionExpression.
    62  func (v *Values) String() string {
    63  	return fmt.Sprintf("%s(%s)", v.FunctionName(), v.Child.String())
    64  }
    65  
    66  // Type implements sql.FunctionExpression.
    67  func (v *Values) Type() sql.Type {
    68  	return v.Child.Type()
    69  }
    70  
    71  // CollationCoercibility implements the interface sql.CollationCoercible.
    72  func (v *Values) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    73  	return sql.GetCoercibility(ctx, v.Child)
    74  }
    75  
    76  // WithChildren implements sql.FunctionExpression.
    77  func (v *Values) WithChildren(children ...sql.Expression) (sql.Expression, error) {
    78  	if len(children) != 1 {
    79  		return nil, sql.ErrInvalidChildrenNumber.New(v, len(children), 1)
    80  	}
    81  	return NewValues(children[0]), nil
    82  }