Skip to content

Commit

Permalink
[ISSUE arana-db#93] Feat show var (arana-db#154)
Browse files Browse the repository at this point in the history
* feat: none

* feat: support show var

* feat: support show variables

* style: 移除无效代码

* feat: 逻辑修改

* fix: 修复PR中的comment
  • Loading branch information
chuntaojun authored May 8, 2022
1 parent a24aedb commit 5935d1c
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ vendor/
/docker/data/
/docker/mysqld/
/dist
/default.etcd
/default.etcd

.codecc
.vscode
13 changes: 12 additions & 1 deletion pkg/executor/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,18 @@ func (executor *RedirectExecutor) ExecutorComQuery(ctx *proto.Context) (proto.Re
}
}
case *ast.ShowStmt:
if !schemaless || stmt.Tp == ast.ShowDatabases { // only SHOW DATABASES is allowed in schemaless mode
allowSchemaless := func(stmt *ast.ShowStmt) bool {
if stmt.Tp == ast.ShowDatabases {
return true
}
if stmt.Tp == ast.ShowVariables {
return true
}

return false
}

if !schemaless || allowSchemaless(stmt) { // only SHOW DATABASES is allowed in schemaless mode
res, warn, err = rt.Execute(ctx)
} else {
err = errNoDatabaseSelected
Expand Down
10 changes: 10 additions & 0 deletions pkg/runtime/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,16 @@ func (cc *convCtx) convShowStmt(node *ast.ShowStmt) Statement {
ret.like.Valid, ret.like.String = true, like
}
return ret
case ast.ShowVariables:
ret := &ShowVariables{}
if node.Full {
ret.flag |= scFlagFull
}
if like, ok := toLike(node); ok {
ret.like.Valid = true
ret.like.String = like
}
return ret
default:
panic(fmt.Sprintf("unimplement: show type %v!", node.Tp))
}
Expand Down
1 change: 1 addition & 0 deletions pkg/runtime/ast/drop_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package ast
import (
"strings"
)

import (
"github.com/pkg/errors"
)
Expand Down
38 changes: 38 additions & 0 deletions pkg/runtime/ast/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,41 @@ func (sh *ShowColumns) TableFormat() string {
}
return "FROM"
}

type ShowVariables struct {
flag showColumnsFlag
like sql.NullString
}

func (s *ShowVariables) Validate() error {
return nil
}

func (s *ShowVariables) Restore(flag RestoreFlag, sb *strings.Builder, args *[]int) error {
sb.WriteString("SHOW VARIABLES ")

if s.like.Valid {
sb.WriteString(" LIKE ")

sb.WriteByte('\'')
sb.WriteString(s.like.String)
sb.WriteByte('\'')
}

return nil
}

func (s *ShowVariables) Like() (string, bool) {
if s.like.Valid {
return s.like.String, true
}
return "", false
}

func (s *ShowVariables) CntParams() int {
return 0
}

func (s *ShowVariables) Mode() SQLType {
return Squery
}
8 changes: 8 additions & 0 deletions pkg/runtime/optimize/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ func (o optimizer) doOptimize(ctx context.Context, conn proto.VConn, stmt rast.S
return o.optimizeTruncate(ctx, t, args)
case *rast.DropTableStatement:
return o.optimizeDropTable(ctx, t, args)
case *rast.ShowVariables:
return o.optimizeShowVariables(ctx, t, args)
}

//TODO implement all statements
Expand Down Expand Up @@ -554,6 +556,12 @@ func (o optimizer) optimizeTruncate(ctx context.Context, stmt *rast.TruncateStat
return ret, nil
}

func (o optimizer) optimizeShowVariables(ctx context.Context, stmt *rast.ShowVariables, args []interface{}) (proto.Plan, error) {
ret := plan.NewShowVariablesPlan(stmt)
ret.BindArgs(args)
return ret, nil
}

func (o optimizer) computeShards(ru *rule.Rule, table rast.TableName, where rast.ExpressionNode, args []interface{}) (rule.DatabaseTables, error) {
vt, ok := ru.VTable(table.Suffix())
if !ok {
Expand Down
1 change: 1 addition & 0 deletions pkg/runtime/plan/drop_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"strings"
)

import (
"github.com/arana-db/arana/pkg/mysql"
"github.com/arana-db/arana/pkg/proto"
Expand Down
66 changes: 66 additions & 0 deletions pkg/runtime/plan/variables.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 plan

import (
"context"
"strings"
)

import (
"github.com/pkg/errors"
)

import (
"github.com/arana-db/arana/pkg/proto"
"github.com/arana-db/arana/pkg/runtime/ast"
)

var _ proto.Plan = (*ShowVariablesPlan)(nil)

type ShowVariablesPlan struct {
basePlan
stmt *ast.ShowVariables
}

func NewShowVariablesPlan(stmt *ast.ShowVariables) *ShowVariablesPlan {
return &ShowVariablesPlan{stmt: stmt}
}

func (s *ShowVariablesPlan) Type() proto.PlanType {
return proto.PlanTypeQuery
}

func (s *ShowVariablesPlan) ExecIn(ctx context.Context, vConn proto.VConn) (proto.Result, error) {

var (
sb strings.Builder
args []int
)

if err := s.stmt.Restore(ast.RestoreDefault, &sb, &args); err != nil {
return nil, errors.Wrap(err, "failed to execute DELETE statement")
}

ret, err := vConn.Query(ctx, "", sb.String(), s.toArgs(args)...)
if err != nil {
return nil, err
}

return ret, nil
}
1 change: 1 addition & 0 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

import (
_ "github.com/go-sql-driver/mysql" // register mysql

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
Expand Down

0 comments on commit 5935d1c

Please sign in to comment.