From c77c710981711ce1ff625b160f0d87a86b06ae8d Mon Sep 17 00:00:00 2001 From: weizhao Date: Fri, 17 Sep 2021 00:06:37 +0800 Subject: [PATCH 1/3] support insert argument type json.RawMessage --- client/client_test.go | 12 +++++++++++- client/stmt.go | 4 ++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/client/client_test.go b/client/client_test.go index 121051f1b..028d1332b 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -1,6 +1,7 @@ package client import ( + "encoding/json" "flag" "fmt" "strings" @@ -22,7 +23,7 @@ var testHost = flag.String("host", "127.0.0.1", "MySQL server host") // Hint: use docker-compose to start corresponding MySQL docker containers and add the their ports here var testPort = flag.String("port", "3306", "MySQL server port") // choose one or more form 5561,5641,3306,5722,8003,8012,8013, e.g. '3306,5722,8003' var testUser = flag.String("user", "root", "MySQL user") -var testPassword = flag.String("pass", "", "MySQL password") +var testPassword = flag.String("pass", "1", "MySQL password") var testDB = flag.String("db", "test", "MySQL test database") func Test(t *testing.T) { @@ -82,6 +83,7 @@ func (s *clientTestSuite) testConn_CreateTable(c *C) { e enum("test1", "test2"), u tinyint unsigned, i tinyint, + j json, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8` @@ -184,6 +186,14 @@ func (s *clientTestSuite) TestConn_Insert(c *C) { c.Assert(pkg.AffectedRows, Equals, uint64(1)) } +func (s *clientTestSuite) TestConn_Insert2(c *C) { + str := `insert into mixer_test_conn (j) values(?)` + j := json.RawMessage(`[]`) + pkg, err := s.c.Execute(str, j) + c.Assert(err, IsNil) + c.Assert(pkg.AffectedRows, Equals, uint64(1)) +} + func (s *clientTestSuite) TestConn_Select(c *C) { str := `select str, f, e from mixer_test_conn where id = 1` diff --git a/client/stmt.go b/client/stmt.go index 07e13357e..e0d5f1d30 100644 --- a/client/stmt.go +++ b/client/stmt.go @@ -2,6 +2,7 @@ package client import ( "encoding/binary" + "encoding/json" "fmt" "math" @@ -127,6 +128,9 @@ func (s *Stmt) write(args ...interface{}) error { case []byte: paramTypes[i<<1] = MYSQL_TYPE_STRING paramValues[i] = append(PutLengthEncodedInt(uint64(len(v))), v...) + case json.RawMessage: + paramTypes[i<<1] = MYSQL_TYPE_STRING + paramValues[i] = append(PutLengthEncodedInt(uint64(len(v))), v...) default: return fmt.Errorf("invalid argument type %T", args[i]) } From 1d4fdbf8f51e150b672a5c2ff8edc35405b29cbb Mon Sep 17 00:00:00 2001 From: weizhao Date: Fri, 17 Sep 2021 00:10:05 +0800 Subject: [PATCH 2/3] support insert argument type json.RawMessage --- client/client_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/client_test.go b/client/client_test.go index 028d1332b..15e13b3fa 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -23,7 +23,7 @@ var testHost = flag.String("host", "127.0.0.1", "MySQL server host") // Hint: use docker-compose to start corresponding MySQL docker containers and add the their ports here var testPort = flag.String("port", "3306", "MySQL server port") // choose one or more form 5561,5641,3306,5722,8003,8012,8013, e.g. '3306,5722,8003' var testUser = flag.String("user", "root", "MySQL user") -var testPassword = flag.String("pass", "1", "MySQL password") +var testPassword = flag.String("pass", "", "MySQL password") var testDB = flag.String("db", "test", "MySQL test database") func Test(t *testing.T) { @@ -83,7 +83,7 @@ func (s *clientTestSuite) testConn_CreateTable(c *C) { e enum("test1", "test2"), u tinyint unsigned, i tinyint, - j json, + j json, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8` From e92fe48f3c7a3da60a2915955179ec79e1da357c Mon Sep 17 00:00:00 2001 From: weizhao Date: Fri, 17 Sep 2021 00:22:09 +0800 Subject: [PATCH 3/3] support insert argument type json.RawMessage --- client/client_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/client_test.go b/client/client_test.go index 15e13b3fa..8a1b893d4 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -187,9 +187,9 @@ func (s *clientTestSuite) TestConn_Insert(c *C) { } func (s *clientTestSuite) TestConn_Insert2(c *C) { - str := `insert into mixer_test_conn (j) values(?)` + str := `insert into mixer_test_conn (id, j) values(?, ?)` j := json.RawMessage(`[]`) - pkg, err := s.c.Execute(str, j) + pkg, err := s.c.Execute(str, []interface{}{2, j}...) c.Assert(err, IsNil) c.Assert(pkg.AffectedRows, Equals, uint64(1)) }