vitess.io/vitess@v0.16.2/go/vt/vitessdriver/convert.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package vitessdriver
    18  
    19  import (
    20  	"database/sql/driver"
    21  	"fmt"
    22  	"time"
    23  
    24  	"vitess.io/vitess/go/vt/vtgate/evalengine"
    25  
    26  	"vitess.io/vitess/go/sqltypes"
    27  	querypb "vitess.io/vitess/go/vt/proto/query"
    28  )
    29  
    30  type converter struct {
    31  	location *time.Location
    32  }
    33  
    34  func (cv *converter) ToNative(v sqltypes.Value) (any, error) {
    35  	switch v.Type() {
    36  	case sqltypes.Datetime, sqltypes.Timestamp:
    37  		return DatetimeToNative(v, cv.location)
    38  	case sqltypes.Date:
    39  		return DateToNative(v, cv.location)
    40  	}
    41  	return evalengine.ToNative(v)
    42  }
    43  
    44  func (cv *converter) BuildBindVariable(v any) (*querypb.BindVariable, error) {
    45  	if t, ok := v.(time.Time); ok {
    46  		return sqltypes.ValueBindVariable(NewDatetime(t, cv.location)), nil
    47  	}
    48  	return sqltypes.BuildBindVariable(v)
    49  }
    50  
    51  // populateRow populates a row of data using the table's field descriptions.
    52  // The returned types for "dest" include the list from the interface
    53  // specification at https://golang.org/pkg/database/sql/driver/#Value
    54  // and in addition the type "uint64" for unsigned BIGINT MySQL records.
    55  func (cv *converter) populateRow(dest []driver.Value, row []sqltypes.Value) (err error) {
    56  	for i := range dest {
    57  		dest[i], err = cv.ToNative(row[i])
    58  		if err != nil {
    59  			return
    60  		}
    61  	}
    62  	return
    63  }
    64  
    65  func (cv *converter) buildBindVars(args []driver.Value) (map[string]*querypb.BindVariable, error) {
    66  	bindVars := make(map[string]*querypb.BindVariable, len(args))
    67  	for i, v := range args {
    68  		bv, err := cv.BuildBindVariable(v)
    69  		if err != nil {
    70  			return nil, err
    71  		}
    72  		bindVars[fmt.Sprintf("v%d", i+1)] = bv
    73  	}
    74  	return bindVars, nil
    75  }
    76  
    77  func (cv *converter) bindVarsFromNamedValues(args []driver.NamedValue) (map[string]*querypb.BindVariable, error) {
    78  	bindVars := make(map[string]*querypb.BindVariable, len(args))
    79  	nameUsed := false
    80  	for i, v := range args {
    81  		bv, err := cv.BuildBindVariable(v.Value)
    82  		if err != nil {
    83  			return nil, err
    84  		}
    85  		if i == 0 {
    86  			// Determine if args are based on names or ordinals.
    87  			if v.Name != "" {
    88  				nameUsed = true
    89  			}
    90  		} else {
    91  			// Verify that there's no intermixing.
    92  			if nameUsed && v.Name == "" {
    93  				return nil, errNoIntermixing
    94  			}
    95  			if !nameUsed && v.Name != "" {
    96  				return nil, errNoIntermixing
    97  			}
    98  		}
    99  		if v.Name == "" {
   100  			bindVars[fmt.Sprintf("v%d", i+1)] = bv
   101  		} else {
   102  			if v.Name[0] == ':' || v.Name[0] == '@' {
   103  				bindVars[v.Name[1:]] = bv
   104  			} else {
   105  				bindVars[v.Name] = bv
   106  			}
   107  		}
   108  	}
   109  	return bindVars, nil
   110  }
   111  
   112  func newConverter(cfg *Configuration) (c *converter, err error) {
   113  	c = &converter{
   114  		location: time.UTC,
   115  	}
   116  	if cfg.DefaultLocation != "" {
   117  		c.location, err = time.LoadLocation(cfg.DefaultLocation)
   118  	}
   119  	return
   120  }