Skip to content

Commit

Permalink
util: fix issue #9532, quote database name before running SQL with it (
Browse files Browse the repository at this point in the history
  • Loading branch information
kennytm authored and eurekaka committed Mar 5, 2019
1 parent d094a0f commit 63b49c9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 4 additions & 2 deletions util/kvencoder/kv_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,15 @@ func (e *kvEncoder) initial(dbName string, idAlloc autoid.Allocator) (err error)
return
}

dbName = strings.Replace(dbName, "`", "``", -1)

se.SetConnectionID(atomic.AddUint64(&mockConnID, 1))
_, err = se.Execute(context.Background(), fmt.Sprintf("create database if not exists %s", dbName))
_, err = se.Execute(context.Background(), fmt.Sprintf("create database if not exists `%s`", dbName))
if err != nil {
err = errors.Trace(err)
return
}
_, err = se.Execute(context.Background(), fmt.Sprintf("use %s", dbName))
_, err = se.Execute(context.Background(), fmt.Sprintf("use `%s`", dbName))
if err != nil {
err = errors.Trace(err)
return
Expand Down
12 changes: 12 additions & 0 deletions util/kvencoder/kv_encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,3 +684,15 @@ func (s *testKvEncoderSuite) TestRefCount(c *C) {
tmp.Close()
c.Assert(refCount, Equals, int64(0))
}

// TestExoticDatabaseName checks if https://github.com/pingcap/tidb/issues/9532
// is fixed.
func (s *testKvEncoderSuite) TestExoticDatabaseName(c *C) {
encoder1, err := New("pay-service_micro_db", nil)
c.Assert(err, IsNil)
encoder1.Close()

encoder2, err := New("㎩¥`𝕊ℯ®Ⅵ₠—🎤肉 ㏈", nil)
c.Assert(err, IsNil)
encoder2.Close()
}

0 comments on commit 63b49c9

Please sign in to comment.