Skip to content

add String() for FieldValue #825

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

Merged
merged 3 commits into from
Sep 26, 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
68 changes: 68 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,71 @@ func (s *clientTestSuite) TestStmt_Trans() {
str, _ = r.GetString(0, 0)
require.Equal(s.T(), `abc`, str)
}

func (s *clientTestSuite) TestFieldValueString() {
_, err := s.c.Execute(
`
CREATE TABLE field_value_test (
c_int int,
c_bit bit(8),
c_tinyint_u tinyint unsigned,
c_decimal decimal(10, 5),
c_float float(10),
c_date date,
c_datetime datetime(3),
c_timestamp timestamp(4),
c_time time(5),
c_year year,
c_char char(10),
c_varchar varchar(10),
c_binary binary(10),
c_varbinary varbinary(10),
c_blob blob,
c_enum enum('a', 'b', 'c'),
c_set set('a', 'b', 'c'),
c_json json,
c_null int
)`)
require.NoError(s.T(), err)
s.T().Cleanup(func() {
_, _ = s.c.Execute(
`DROP TABLE field_value_test`)
})

_, err = s.c.Execute(`
INSERT INTO field_value_test VALUES (
1, 2, 3, 4.5, 6.7,
'2019-01-01', '2019-01-01 01:01:01.123', '2019-01-01 01:01:01.1234', '01:01:01.12345', 2019,
'cha\'r', 'varchar', 'binary', 'varbinary', 'blob', 'a', 'a,b', '{"a": 1}',
NULL
)`)
require.NoError(s.T(), err)

result, err := s.c.Execute(`SELECT * FROM field_value_test`)
require.NoError(s.T(), err)
require.Len(s.T(), result.Values, 1)
expected := []string{
`1`, "'\x02'", `3`, `'4.50000'`, `6.7`,
`'2019-01-01'`, `'2019-01-01 01:01:01.123'`, `'2019-01-01 01:01:01.1234'`, `'01:01:01.12345'`, `2019`,
`'cha\'r'`, `'varchar'`, "'binary\x00\x00\x00\x00'", `'varbinary'`, `'blob'`, `'a'`, `'a,b'`, `'{"a": 1}'`,
`NULL`,
}
for i, v := range result.Values[0] {
require.Equal(s.T(), expected[i], v.String())
}

// test can directly use to build a SQL, though it's not safe in most cases
sql := fmt.Sprintf("INSERT INTO field_value_test VALUES (%s)", strings.Join(expected, ","))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhujintao do you think it's OK to use?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it works very well

_, err = s.c.Execute(sql)
require.NoError(s.T(), err)
result, err = s.c.Execute(`SELECT * FROM field_value_test`)
require.NoError(s.T(), err)
// check again, everything is same
require.Len(s.T(), result.Values, 2)
for i, v := range result.Values[0] {
require.Equal(s.T(), expected[i], v.String())
}
for i, v := range result.Values[1] {
require.Equal(s.T(), expected[i], v.String())
}
}
31 changes: 31 additions & 0 deletions mysql/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package mysql

import (
"encoding/binary"
"fmt"
"strconv"
"strings"

"github.com/go-mysql-org/go-mysql/utils"
)
Expand Down Expand Up @@ -210,3 +213,31 @@ func (fv *FieldValue) Value() interface{} {
return nil
}
}

// String returns a MySQL literal string that equals the value.
func (fv *FieldValue) String() string {
switch fv.Type {
case FieldValueTypeNull:
return "NULL"
case FieldValueTypeUnsigned:
return strconv.FormatUint(fv.AsUint64(), 10)
case FieldValueTypeSigned:
return strconv.FormatInt(fv.AsInt64(), 10)
case FieldValueTypeFloat:
return strconv.FormatFloat(fv.AsFloat64(), 'f', -1, 64)
case FieldValueTypeString:
b := strings.Builder{}
b.Grow(len(fv.str) + 2)
b.WriteByte('\'')
for i := range fv.str {
if fv.str[i] == '\'' {
b.WriteByte('\\')
}
b.WriteByte(fv.str[i])
}
b.WriteByte('\'')
return b.String()
default:
return fmt.Sprintf("unknown type %d of FieldValue", fv.Type)
}
}