Skip to content

Commit

Permalink
refactor: default restore string without default charset (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
dk-lockdown authored Aug 29, 2022
1 parent 59b9ff1 commit 4d6da29
Show file tree
Hide file tree
Showing 20 changed files with 57 additions and 37 deletions.
4 changes: 4 additions & 0 deletions pkg/constant/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

package constant

import "github.com/cectc/dbpack/third_party/parser/format"

const (
ConfigPathKey = "config"
TransactionTimeout = "transaction-timeout"

DBPackRestoreFormat = format.DefaultRestoreFlags | format.RestoreStringWithoutDefaultCharset
)
5 changes: 2 additions & 3 deletions pkg/executor/read_write_splitting.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/pkg/errors"

"github.com/cectc/dbpack/pkg/config"
"github.com/cectc/dbpack/pkg/constant"
"github.com/cectc/dbpack/pkg/filter"
"github.com/cectc/dbpack/pkg/group"
"github.com/cectc/dbpack/pkg/log"
Expand Down Expand Up @@ -159,9 +160,7 @@ func (executor *ReadWriteSplittingExecutor) ExecutorComQuery(

connectionID := proto.ConnectionID(spanCtx)
queryStmt := proto.QueryStmt(spanCtx)
if err := queryStmt.Restore(format.NewRestoreCtx(
format.DefaultRestoreFlags|
format.RestoreStringWithoutDefaultCharset, &sb)); err != nil {
if err := queryStmt.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
return nil, 0, err
}
newSql := sb.String()
Expand Down
5 changes: 2 additions & 3 deletions pkg/executor/single_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/pkg/errors"

"github.com/cectc/dbpack/pkg/config"
"github.com/cectc/dbpack/pkg/constant"
"github.com/cectc/dbpack/pkg/filter"
"github.com/cectc/dbpack/pkg/log"
"github.com/cectc/dbpack/pkg/proto"
Expand Down Expand Up @@ -154,9 +155,7 @@ func (executor *SingleDBExecutor) ExecutorComQuery(
if queryStmt == nil {
return nil, 0, errors.New("query stmt should not be nil")
}
if err := queryStmt.Restore(format.NewRestoreCtx(
format.DefaultRestoreFlags|
format.RestoreStringWithoutDefaultCharset, &sb)); err != nil {
if err := queryStmt.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
return nil, 0, err
}
sql := sb.String()
Expand Down
5 changes: 3 additions & 2 deletions pkg/filter/dt/exec/prepare_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"strings"

"github.com/cectc/dbpack/pkg/constant"
"github.com/cectc/dbpack/pkg/driver"
"github.com/cectc/dbpack/pkg/dt/schema"
"github.com/cectc/dbpack/pkg/log"
Expand Down Expand Up @@ -89,7 +90,7 @@ func (executor *prepareDeleteExecutor) GetTableMeta(ctx context.Context) (schema

func (executor *prepareDeleteExecutor) GetTableName() string {
var sb strings.Builder
if err := executor.stmt.TableRefs.TableRefs.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)); err != nil {
if err := executor.stmt.TableRefs.TableRefs.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
log.Panic(err)
}
return sb.String()
Expand All @@ -115,7 +116,7 @@ func (executor *prepareDeleteExecutor) buildBeforeImageSql(tableMeta schema.Tabl

func (executor *prepareDeleteExecutor) GetWhereCondition() string {
var sb strings.Builder
if err := executor.stmt.Where.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)); err != nil {
if err := executor.stmt.Where.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
log.Panic(err)
}
return sb.String()
Expand Down
9 changes: 5 additions & 4 deletions pkg/filter/dt/exec/prepare_global_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"
"time"

"github.com/cectc/dbpack/pkg/constant"
"github.com/cectc/dbpack/pkg/driver"
"github.com/cectc/dbpack/pkg/dt"
"github.com/cectc/dbpack/pkg/dt/schema"
Expand Down Expand Up @@ -96,11 +97,11 @@ func (executor *prepareGlobalLockExecutor) GetTableMeta(ctx context.Context) (sc
func (executor *prepareGlobalLockExecutor) GetTableName() string {
var sb strings.Builder
if executor.isUpdate {
if err := executor.updateStmt.TableRefs.TableRefs.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)); err != nil {
if err := executor.updateStmt.TableRefs.TableRefs.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
log.Panic(err)
}
} else {
if err := executor.deleteStmt.TableRefs.TableRefs.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)); err != nil {
if err := executor.deleteStmt.TableRefs.TableRefs.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
log.Panic(err)
}
}
Expand Down Expand Up @@ -148,11 +149,11 @@ func (executor *prepareGlobalLockExecutor) buildBeforeImageSql(tableMeta schema.
func (executor *prepareGlobalLockExecutor) GetWhereCondition() string {
var sb strings.Builder
if executor.isUpdate {
if err := executor.updateStmt.Where.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)); err != nil {
if err := executor.updateStmt.Where.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
log.Panic(err)
}
} else {
if err := executor.deleteStmt.Where.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)); err != nil {
if err := executor.deleteStmt.Where.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
log.Panic(err)
}
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/filter/dt/exec/prepare_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strconv"
"strings"

"github.com/cectc/dbpack/pkg/constant"
"github.com/cectc/dbpack/pkg/driver"
"github.com/cectc/dbpack/pkg/dt/schema"
"github.com/cectc/dbpack/pkg/log"
Expand Down Expand Up @@ -92,7 +93,7 @@ func (executor *prepareInsertExecutor) GetTableMeta(ctx context.Context) (schema

func (executor *prepareInsertExecutor) GetTableName() string {
var sb strings.Builder
if err := executor.stmt.Table.TableRefs.Left.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)); err != nil {
if err := executor.stmt.Table.TableRefs.Left.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
log.Panic(err)
}
return sb.String()
Expand Down
5 changes: 3 additions & 2 deletions pkg/filter/dt/exec/prepare_select_for_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strings"
"time"

"github.com/cectc/dbpack/pkg/constant"
"github.com/cectc/dbpack/pkg/tracing"

"github.com/cectc/dbpack/pkg/driver"
Expand Down Expand Up @@ -101,14 +102,14 @@ func (executor *prepareSelectForUpdateExecutor) GetTableMeta(ctx context.Context
func (executor *prepareSelectForUpdateExecutor) GetTableName() string {
var sb strings.Builder
table := executor.stmt.From.TableRefs.Left.(*ast.TableSource)
if err := table.Source.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)); err != nil {
if err := table.Source.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
log.Panic(err)
}
return sb.String()
}

func (executor *prepareSelectForUpdateExecutor) GetWhereCondition() string {
var sb strings.Builder
executor.stmt.Where.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb))
executor.stmt.Where.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb))
return sb.String()
}
5 changes: 3 additions & 2 deletions pkg/filter/dt/exec/prepare_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"strings"

"github.com/cectc/dbpack/pkg/constant"
"github.com/cectc/dbpack/pkg/driver"
"github.com/cectc/dbpack/pkg/dt/schema"
"github.com/cectc/dbpack/pkg/log"
Expand Down Expand Up @@ -113,7 +114,7 @@ func (executor *prepareUpdateExecutor) GetTableMeta(ctx context.Context) (schema

func (executor *prepareUpdateExecutor) GetTableName() string {
var sb strings.Builder
if err := executor.stmt.TableRefs.TableRefs.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)); err != nil {
if err := executor.stmt.TableRefs.TableRefs.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
log.Panic(err)
}
return sb.String()
Expand Down Expand Up @@ -159,7 +160,7 @@ func (executor *prepareUpdateExecutor) buildAfterImageSql(tableMeta schema.Table

func (executor *prepareUpdateExecutor) GetWhereCondition() string {
var sb strings.Builder
if err := executor.stmt.Where.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)); err != nil {
if err := executor.stmt.Where.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
log.Error(err)
}
return sb.String()
Expand Down
5 changes: 3 additions & 2 deletions pkg/filter/dt/exec/query_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"strings"

"github.com/cectc/dbpack/pkg/constant"
"github.com/cectc/dbpack/pkg/driver"
"github.com/cectc/dbpack/pkg/dt/schema"
"github.com/cectc/dbpack/pkg/log"
Expand Down Expand Up @@ -78,7 +79,7 @@ func (executor *queryDeleteExecutor) GetTableMeta(ctx context.Context) (schema.T

func (executor *queryDeleteExecutor) GetTableName() string {
var sb strings.Builder
if err := executor.stmt.TableRefs.TableRefs.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)); err != nil {
if err := executor.stmt.TableRefs.TableRefs.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
log.Panic(err)
}
return sb.String()
Expand All @@ -104,7 +105,7 @@ func (executor *queryDeleteExecutor) buildBeforeImageSql(tableMeta schema.TableM

func (executor *queryDeleteExecutor) GetWhereCondition() string {
var sb strings.Builder
if err := executor.stmt.Where.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)); err != nil {
if err := executor.stmt.Where.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
log.Panic(err)
}
return sb.String()
Expand Down
9 changes: 5 additions & 4 deletions pkg/filter/dt/exec/query_global_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"
"time"

"github.com/cectc/dbpack/pkg/constant"
"github.com/cectc/dbpack/pkg/driver"
"github.com/cectc/dbpack/pkg/dt"
"github.com/cectc/dbpack/pkg/dt/schema"
Expand Down Expand Up @@ -121,11 +122,11 @@ func (executor *queryGlobalLockExecutor) GetTableMeta(ctx context.Context) (sche
func (executor *queryGlobalLockExecutor) GetTableName() string {
var sb strings.Builder
if executor.isUpdate {
if err := executor.updateStmt.TableRefs.TableRefs.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)); err != nil {
if err := executor.updateStmt.TableRefs.TableRefs.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
log.Panic(err)
}
} else {
if err := executor.deleteStmt.TableRefs.TableRefs.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)); err != nil {
if err := executor.deleteStmt.TableRefs.TableRefs.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
log.Panic(err)
}
}
Expand Down Expand Up @@ -166,11 +167,11 @@ func (executor *queryGlobalLockExecutor) buildBeforeImageSql(tableMeta schema.Ta
func (executor *queryGlobalLockExecutor) GetWhereCondition() string {
var sb strings.Builder
if executor.isUpdate {
if err := executor.updateStmt.Where.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)); err != nil {
if err := executor.updateStmt.Where.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
log.Panic(err)
}
} else {
if err := executor.deleteStmt.Where.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)); err != nil {
if err := executor.deleteStmt.Where.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
log.Panic(err)
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/filter/dt/exec/query_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"strings"

"github.com/cectc/dbpack/pkg/constant"
"github.com/cectc/dbpack/pkg/driver"
"github.com/cectc/dbpack/pkg/dt/schema"
"github.com/cectc/dbpack/pkg/log"
Expand Down Expand Up @@ -124,7 +125,7 @@ func (executor *queryInsertExecutor) GetTableMeta(ctx context.Context) (schema.T

func (executor *queryInsertExecutor) GetTableName() string {
var sb strings.Builder
if err := executor.stmt.Table.TableRefs.Left.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)); err != nil {
if err := executor.stmt.Table.TableRefs.Left.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
log.Panic(err)
}
return sb.String()
Expand All @@ -137,7 +138,7 @@ func (executor *queryInsertExecutor) getPKValuesByColumn(ctx context.Context) ([
for i, value := range executor.stmt.Lists[j] {
if i == pkIndex {
var sb strings.Builder
if err := value.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)); err != nil {
if err := value.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
log.Panic(err)
}
pkValues = append(pkValues, sb.String())
Expand Down
5 changes: 3 additions & 2 deletions pkg/filter/dt/exec/query_select_for_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strings"
"time"

"github.com/cectc/dbpack/pkg/constant"
"github.com/cectc/dbpack/pkg/driver"
"github.com/cectc/dbpack/pkg/dt"
"github.com/cectc/dbpack/pkg/dt/schema"
Expand Down Expand Up @@ -97,14 +98,14 @@ func (executor *querySelectForUpdateExecutor) GetTableMeta(ctx context.Context)
func (executor *querySelectForUpdateExecutor) GetTableName() string {
var sb strings.Builder
table := executor.stmt.From.TableRefs.Left.(*ast.TableSource)
if err := table.Source.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)); err != nil {
if err := table.Source.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
log.Panic(err)
}
return sb.String()
}

func (executor *querySelectForUpdateExecutor) GetWhereCondition() string {
var sb strings.Builder
executor.stmt.Where.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb))
executor.stmt.Where.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb))
return sb.String()
}
5 changes: 3 additions & 2 deletions pkg/filter/dt/exec/query_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"strings"

"github.com/cectc/dbpack/pkg/constant"
"github.com/cectc/dbpack/pkg/driver"
"github.com/cectc/dbpack/pkg/dt/schema"
"github.com/cectc/dbpack/pkg/log"
Expand Down Expand Up @@ -98,7 +99,7 @@ func (executor *queryUpdateExecutor) GetTableMeta(ctx context.Context) (schema.T

func (executor *queryUpdateExecutor) GetTableName() string {
var sb strings.Builder
if err := executor.stmt.TableRefs.TableRefs.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)); err != nil {
if err := executor.stmt.TableRefs.TableRefs.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
log.Panic(err)
}
return sb.String()
Expand Down Expand Up @@ -156,7 +157,7 @@ func (executor *queryUpdateExecutor) buildAfterImageSql(tableMeta schema.TableMe

func (executor *queryUpdateExecutor) GetWhereCondition() string {
var sb strings.Builder
if err := executor.stmt.Where.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)); err != nil {
if err := executor.stmt.Where.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
log.Panic(err)
}
return sb.String()
Expand Down
6 changes: 5 additions & 1 deletion pkg/misc/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ func MysqlAppendInParamWithValue(values []interface{}) string {
for i, value := range values {
switch val := value.(type) {
case string:
sb.WriteString(fmt.Sprintf("'%s'", val))
if strings.HasPrefix(val, "'") {
sb.WriteString(fmt.Sprintf("%s", val))
} else {
sb.WriteString(fmt.Sprintf("'%s'", val))
}
case []byte:
sb.WriteString(fmt.Sprintf("'%s'", val))
default:
Expand Down
6 changes: 5 additions & 1 deletion pkg/misc/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,14 @@ func TestMysqlAppendInParamWithValue(t *testing.T) {
in []interface{}
out string
}{
"1": {
"0": {
in: []interface{}{"abc", "xyz"},
out: "('abc','xyz')",
},
"1": {
in: []interface{}{"'abc'", "'xyz'"},
out: "('abc','xyz')",
},
"2": {
in: []interface{}{[]byte("abc"), []byte("xyz")},
out: "('abc','xyz')",
Expand Down
2 changes: 1 addition & 1 deletion pkg/optimize/optimize_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func getPkValue(ctx context.Context, stmt *ast.InsertStmt, pkIndex int, args []i
switch commandType {
case constant.ComQuery:
var sb strings.Builder
ctx := format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)
ctx := format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)
if err := stmt.Lists[0][pkIndex].Restore(ctx); err != nil {
log.Panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/plan/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (p *DeletePlan) Execute(ctx context.Context, hints ...*ast.TableOptimizerHi
}

func (p *DeletePlan) generate(sb *strings.Builder, table string, hints ...*ast.TableOptimizerHint) error {
ctx := format.NewRestoreCtx(format.DefaultRestoreFlags|format.RestoreStringWithoutDefaultCharset, sb)
ctx := format.NewRestoreCtx(constant.DBPackRestoreFormat, sb)
ctx.WriteKeyWord("DELETE ")

if len(hints) != 0 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/plan/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (p *InsertPlan) Execute(ctx context.Context, hints ...*ast.TableOptimizerHi
}

func (p *InsertPlan) generate(sb *strings.Builder) (err error) {
ctx := format.NewRestoreCtx(format.DefaultRestoreFlags|format.RestoreStringWithoutDefaultCharset, sb)
ctx := format.NewRestoreCtx(constant.DBPackRestoreFormat, sb)

ctx.WriteKeyWord("INSERT ")
ctx.WriteKeyWord("INTO ")
Expand Down
4 changes: 2 additions & 2 deletions pkg/plan/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (p *QueryOnSingleDBPlan) generate(ctx context.Context, sb *strings.Builder,
}
sb.WriteString(") t ")
if p.Stmt.OrderBy != nil {
restoreCtx := format.NewRestoreCtx(format.DefaultRestoreFlags|format.RestoreStringWithoutDefaultCharset, sb)
restoreCtx := format.NewRestoreCtx(constant.DBPackRestoreFormat, sb)
if err := p.Stmt.OrderBy.Restore(restoreCtx); err != nil {
return errors.WithStack(err)
}
Expand Down Expand Up @@ -240,7 +240,7 @@ func (p *QueryOnMultiDBPlan) Execute(ctx context.Context, _ ...*ast.TableOptimiz
}

func generateSelect(table string, stmt *ast.SelectStmt, sb *strings.Builder, limit *Limit) error {
ctx := format.NewRestoreCtx(format.DefaultRestoreFlags|format.RestoreStringWithoutDefaultCharset, sb)
ctx := format.NewRestoreCtx(constant.DBPackRestoreFormat, sb)
ctx.WriteKeyWord(stmt.Kind.String())
ctx.WritePlain(" ")

Expand Down
Loading

0 comments on commit 4d6da29

Please sign in to comment.