github.com/dolthub/go-mysql-server@v0.18.0/sql/types/deferred.go (about)

     1  // Copyright 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 types
    16  
    17  import (
    18  	"reflect"
    19  
    20  	"github.com/dolthub/vitess/go/sqltypes"
    21  	"github.com/dolthub/vitess/go/vt/proto/query"
    22  
    23  	"github.com/dolthub/go-mysql-server/sql"
    24  )
    25  
    26  type deferredType struct {
    27  	bindVar string
    28  }
    29  
    30  var _ sql.DeferredType = (*deferredType)(nil)
    31  var _ sql.CollationCoercible = (*deferredType)(nil)
    32  
    33  func NewDeferredType(name string) sql.Type {
    34  	return &deferredType{bindVar: name}
    35  }
    36  
    37  func (t deferredType) Equals(otherType sql.Type) bool {
    38  	return false
    39  }
    40  
    41  // Compare implements Type interface. Note that while this returns 0 (equals)
    42  // for ordering purposes, in SQL NULL != NULL.
    43  func (t deferredType) Compare(a interface{}, b interface{}) (int, error) {
    44  	return 0, nil
    45  }
    46  
    47  // Convert implements Type interface.
    48  func (t deferredType) Convert(v interface{}) (interface{}, sql.ConvertInRange, error) {
    49  	if v != nil {
    50  		return nil, sql.InRange, ErrValueNotNil.New(v)
    51  	}
    52  
    53  	return nil, sql.InRange, nil
    54  }
    55  
    56  // MaxTextResponseByteLength implements the Type interface
    57  func (t deferredType) MaxTextResponseByteLength(_ *sql.Context) uint32 {
    58  	// deferredType is never actually sent over the wire
    59  	return 0
    60  }
    61  
    62  // MustConvert implements the Type interface.
    63  func (t deferredType) MustConvert(v interface{}) interface{} {
    64  	value, _, err := t.Convert(v)
    65  	if err != nil {
    66  		panic(err)
    67  	}
    68  	return value
    69  }
    70  
    71  // Promote implements the Type interface.
    72  func (t deferredType) Promote() sql.Type {
    73  	return t
    74  }
    75  
    76  // SQL implements Type interface.
    77  func (t deferredType) SQL(ctx *sql.Context, dest []byte, v interface{}) (sqltypes.Value, error) {
    78  	return sqltypes.NULL, nil
    79  }
    80  
    81  // String implements Type interface.
    82  func (t deferredType) String() string {
    83  	return "deferred"
    84  }
    85  
    86  // Type implements Type interface.
    87  func (t deferredType) Type() query.Type {
    88  	return sqltypes.Expression
    89  }
    90  
    91  // ValueType implements Type interface.
    92  func (t deferredType) ValueType() reflect.Type {
    93  	return nil
    94  }
    95  
    96  // Zero implements Type interface.
    97  func (t deferredType) Zero() interface{} {
    98  	return nil
    99  }
   100  
   101  // CollationCoercibility implements sql.CollationCoercible interface.
   102  func (deferredType) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
   103  	return sql.Collation_binary, 7
   104  }
   105  
   106  func (t deferredType) IsDeferred() bool {
   107  	return true
   108  }
   109  
   110  func (t deferredType) Name() string {
   111  	return t.bindVar
   112  }
   113  
   114  func IsDeferredType(t sql.Type) bool {
   115  	_, ok := t.(sql.DeferredType)
   116  	return ok
   117  }