Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ErrBinaryCollation in queries with []byte argument #1906

Merged
merged 3 commits into from
Jul 28, 2023
Merged
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
22 changes: 13 additions & 9 deletions driver/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,31 @@ func TestQuery(t *testing.T) {
var numbers []any
var created time.Time
var count int
var blob []byte

cases := []struct {
Name, Query string
Args []any
Pointers Pointers
Expect Records
}{
{"Select All", "SELECT * FROM db.person", []any{&id, &name, &email, &numbers, &created}, records},
{"Select First", "SELECT * FROM db.person LIMIT 1", []any{&id, &name, &email, &numbers, &created}, records.Rows(0)},
{"Select Name", "SELECT name FROM db.person", []any{&name}, records.Columns(1)},
{"Select Count", "SELECT COUNT(1) FROM db.person", []any{&count}, Records{{len(records)}}},
{"Select All", "SELECT * FROM db.person", nil, []any{&id, &name, &email, &numbers, &created}, records},
{"Select First", "SELECT * FROM db.person LIMIT 1", nil, []any{&id, &name, &email, &numbers, &created}, records.Rows(0)},
{"Select Name", "SELECT name FROM db.person", nil, []any{&name}, records.Columns(1)},
{"Select Count", "SELECT COUNT(1) FROM db.person", nil, []any{&count}, Records{{len(records)}}},

{"Insert", `INSERT INTO db.person (name, email, phone_numbers, created_at) VALUES ('foo', 'bar', '["baz"]', NOW())`, []any{}, Records{}},
{"Select Inserted", "SELECT name, email, phone_numbers FROM db.person WHERE name = 'foo'", []any{&name, &email, &numbers}, Records{{"foo", "bar", []any{"baz"}}}},
{"Insert", `INSERT INTO db.person (name, email, phone_numbers, created_at) VALUES ('foo', 'bar', '["baz"]', NOW())`, nil, []any{}, Records{}},
{"Select Inserted", "SELECT name, email, phone_numbers FROM db.person WHERE name = 'foo'", nil, []any{&name, &email, &numbers}, Records{{"foo", "bar", []any{"baz"}}}},

{"Update", "UPDATE db.person SET name = 'asdf' WHERE name = 'foo'", []any{}, Records{}},
{"Delete", "DELETE FROM db.person WHERE name = 'asdf'", []any{}, Records{}},
{"Update", "UPDATE db.person SET name = 'asdf' WHERE name = 'foo'", nil, []any{}, Records{}},
{"Delete", "DELETE FROM db.person WHERE name = 'asdf'", nil, []any{}, Records{}},

{"Select Binary Args", `SELECT ?`, []any{[]byte{1, 2, 3}}, []any{&blob}, Records{{[]byte{1, 2, 3}}}},
}

for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
rows, err := db.Query(c.Query)
rows, err := db.Query(c.Query, c.Args...)
require.NoError(t, err, "Query")

var i int
Expand Down
2 changes: 1 addition & 1 deletion driver/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func valueToExpr(v driver.Value) (sql.Expression, error) {
case bool:
typ = types.Boolean
case []byte:
typ, err = types.CreateStringWithDefaults(sqltypes.Blob, int64(len(v)))
typ, err = types.CreateBinary(sqltypes.Blob, int64(len(v)))
case string:
typ, err = types.CreateStringWithDefaults(sqltypes.Text, int64(len(v)))
case time.Time:
Expand Down
4 changes: 2 additions & 2 deletions sql/types/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var (
// ErrLengthTooLarge is thrown when a string's length is too large given the other parameters.
ErrLengthTooLarge = errors.NewKind("length is %v but max allowed is %v")
ErrLengthBeyondLimit = errors.NewKind("string '%v' is too large for column '%v'")
ErrBinaryCollation = errors.NewKind("binary types must have the binary collation")
ErrBinaryCollation = errors.NewKind("binary types must have the binary collation: %v")

TinyText = MustCreateStringWithDefaults(sqltypes.Text, TinyTextBlobMax)
Text = MustCreateStringWithDefaults(sqltypes.Text, TextBlobMax)
Expand Down Expand Up @@ -99,7 +99,7 @@ func CreateString(baseType query.Type, length int64, collation sql.CollationID)
switch baseType {
case sqltypes.Binary, sqltypes.VarBinary, sqltypes.Blob:
if collation != sql.Collation_binary {
return nil, ErrBinaryCollation.New(collation.Name, sql.Collation_binary)
return nil, ErrBinaryCollation.New(collation.Name())
}
}

Expand Down