Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions go/vt/vitessdriver/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,19 @@ func (cv *converter) ToNative(v sqltypes.Value) (any, error) {
}

func (cv *converter) BuildBindVariable(v any) (*querypb.BindVariable, error) {
if t, ok := v.(time.Time); ok {
switch t := v.(type) {
case time.Time:
return sqltypes.ValueBindVariable(NewDatetime(t, cv.location)), nil
case []byte:
if t == nil {
return sqltypes.NullBindVariable, nil
}
// sending through string values matches the behavior in go-sql-driver and
// is necessary for json values to be handled without erroring in vttablet.
return sqltypes.StringBindVariable(string(t)), nil
default:
return sqltypes.BuildBindVariable(v)
}
return sqltypes.BuildBindVariable(v)
}

// populateRow populates a row of data using the table's field descriptions.
Expand Down
58 changes: 51 additions & 7 deletions go/vt/vitessdriver/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ limitations under the License.
package vitessdriver

import (
"reflect"
"testing"
"time"

"github.com/stretchr/testify/require"

"vitess.io/vitess/go/sqltypes"
querypb "vitess.io/vitess/go/vt/proto/query"
)

func TestToNative(t *testing.T) {
Expand Down Expand Up @@ -191,11 +193,53 @@ func TestToNative(t *testing.T) {

for _, tcase := range testcases {
v, err := tcase.convert.ToNative(tcase.in)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(v, tcase.out) {
t.Errorf("%v.ToNativeEx = %#v, want %#v", tcase.in, v, tcase.out)
}
require.NoError(t, err)
require.Equal(t, tcase.out, v)
}
}

func TestBuildBindVariable(t *testing.T) {
testcases := []struct {
name string
in any
out *querypb.BindVariable
}{
{
name: "json bytes become varchar",
in: []byte("[]"),
out: sqltypes.StringBindVariable("[]"),
},
{
name: "binary bytes become varchar",
in: []byte{0x00, 0xff},
out: &querypb.BindVariable{
Type: querypb.Type_VARCHAR,
Value: []byte{0x00, 0xff},
},
},
{
name: "nil bytes become null",
in: []byte(nil),
out: sqltypes.NullBindVariable,
},
{
name: "empty bytes become empty varchar",
in: []byte{},
out: sqltypes.StringBindVariable(""),
},
{
name: "time becomes datetime",
in: time.Date(2012, 0o2, 24, 23, 19, 43, 0, time.UTC),
out: sqltypes.ValueBindVariable(sqltypes.TestValue(sqltypes.Datetime, "2012-02-24 23:19:43")),
},
}

convert := &converter{location: time.UTC}
for _, tcase := range testcases {
t.Run(tcase.name, func(t *testing.T) {
bv, err := convert.BuildBindVariable(tcase.in)
require.NoError(t, err)
require.Equal(t, tcase.out, bv)
})
}
}
Loading
Loading