Skip to content

Commit

Permalink
extension: import extension for tidb-server (#42651)
Browse files Browse the repository at this point in the history
ref #38493
  • Loading branch information
CbcWestwolf authored Mar 29, 2023
1 parent 04b1652 commit ab26d16
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
1 change: 1 addition & 0 deletions extension/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_import/generated-*.go
8 changes: 8 additions & 0 deletions extension/_import/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "_import",
srcs = ["import.go"],
importpath = "github.com/pingcap/tidb/extension/_import",
visibility = ["//visibility:public"],
)
18 changes: 18 additions & 0 deletions extension/_import/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2023 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package extensionimport

// This package is used to import extensions outside the tidb repo
// The autogenerated files registering each extension will be placed in this folder
16 changes: 9 additions & 7 deletions server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ func (cc *clientConn) openSessionAndDoAuth(authData []byte, authPlugin string) e
}
cc.ctx.SetPort(port)
if cc.dbname != "" {
err = cc.useDB(context.Background(), cc.dbname)
_, err = cc.useDB(context.Background(), cc.dbname)
if err != nil {
return err
}
Expand Down Expand Up @@ -1322,7 +1322,9 @@ func (cc *clientConn) dispatch(ctx context.Context, data []byte) error {
case mysql.ComQuit:
return io.EOF
case mysql.ComInitDB:
if err := cc.useDB(ctx, dataStr); err != nil {
node, err := cc.useDB(ctx, dataStr)
cc.onExtensionStmtEnd(node, false, err)
if err != nil {
return err
}
return cc.writeOK(ctx)
Expand Down Expand Up @@ -1405,19 +1407,19 @@ func (cc *clientConn) writeStats(ctx context.Context) error {
return cc.flush(ctx)
}

func (cc *clientConn) useDB(ctx context.Context, db string) (err error) {
func (cc *clientConn) useDB(ctx context.Context, db string) (node ast.StmtNode, err error) {
// if input is "use `SELECT`", mysql client just send "SELECT"
// so we add `` around db.
stmts, err := cc.ctx.Parse(ctx, "use `"+db+"`")
if err != nil {
return err
return nil, err
}
_, err = cc.ctx.ExecuteStmt(ctx, stmts[0])
if err != nil {
return err
return nil, err
}
cc.dbname = db
return
return stmts[0], err
}

func (cc *clientConn) flush(ctx context.Context) error {
Expand Down Expand Up @@ -2492,7 +2494,7 @@ func (cc *clientConn) handleResetConnection(ctx context.Context) error {
return errors.New("Could not reset connection")
}
if cc.dbname != "" { // Restore the current DB
err = cc.useDB(context.Background(), cc.dbname)
_, err = cc.useDB(context.Background(), cc.dbname)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions tidb-server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go_library(
"//domain/infosync",
"//executor",
"//extension",
"//extension/_import",
"//keyspace",
"//kv",
"//metrics",
Expand Down
1 change: 1 addition & 0 deletions tidb-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/pingcap/tidb/domain/infosync"
"github.com/pingcap/tidb/executor"
"github.com/pingcap/tidb/extension"
_ "github.com/pingcap/tidb/extension/_import"
"github.com/pingcap/tidb/keyspace"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/metrics"
Expand Down

0 comments on commit ab26d16

Please sign in to comment.