Skip to content

Commit

Permalink
Merge pull request #167 from hanchuanchuan/feature-session-variables
Browse files Browse the repository at this point in the history
feature: 支持会话级变量设置 (#166)
  • Loading branch information
hanchuanchuan authored Mar 8, 2020
2 parents 59e822c + 8855951 commit e8229c9
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 135 deletions.
29 changes: 8 additions & 21 deletions session/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,15 @@ package session
import (
"fmt"
// "strconv"
"strings"

"github.com/hanchuanchuan/goInception/config"
"github.com/hanchuanchuan/goInception/mysql"
"github.com/hanchuanchuan/goInception/terror"
log "github.com/sirupsen/logrus"
)

//go:generate stringer -type=ErrorCode
type ErrorCode int

var ErrorsMessage = map[ErrorCode]string{}

var (
ErrWrongValueForVar = terror.ClassVariable.New(mysql.ErrWrongValueForVar,
mysql.MySQLErrName[mysql.ErrWrongValueForVar])
Expand Down Expand Up @@ -667,11 +663,14 @@ func GetErrorLevel(code ErrorCode) uint8 {
}
}

func GetErrorMessage(ErrorCode ErrorCode) string {
if v, ok := ErrorsMessage[ErrorCode]; ok {
return v
// GetErrorMessage 获取审核信息,默认为英文
func GetErrorMessage(code ErrorCode, lang string) string {
if lang == "zh_cn" {
if v, ok := ErrorsChinese[code]; ok {
return v
}
}
if v, ok := ErrorsDefault[ErrorCode]; ok {
if v, ok := ErrorsDefault[code]; ok {
return v
}
return "Invalid error code!"
Expand All @@ -691,7 +690,7 @@ func (e *SQLError) Error() string {
// NewErr generates a SQL error, with an error code and default format specifier defined in MySQLErrName.
func NewErr(errCode ErrorCode, args ...interface{}) *SQLError {
e := &SQLError{Code: errCode}
e.Message = fmt.Sprintf(GetErrorMessage(errCode), args...)
e.Message = fmt.Sprintf(GetErrorMessage(errCode, "en_us"), args...)
return e
}

Expand All @@ -702,18 +701,6 @@ func NewErrf(format string, args ...interface{}) *SQLError {
return e
}

func SetLanguage(langStr string) {
lang := strings.Replace(strings.ToLower(langStr), "-", "_", 1)
if lang == "zh_cn" {
ErrorsMessage = ErrorsChinese
} else {
ErrorsMessage = ErrorsDefault
if lang != "en_us" {
log.Warning("Lang set Error! use default en-US.")
}
}
}

func (e ErrorCode) String() string {
switch e {
case ER_ERROR_FIRST:
Expand Down
4 changes: 2 additions & 2 deletions session/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func (s *session) checkFilter(event *replication.RowsEvent,

if currentThreadID == 0 && s.DBType == DBTypeMariaDB {
if record.ErrLevel != 1 {
record.AppendErrorNo(ErrNotFoundThreadId, s.DBVersion)
record.AppendErrorNo(s.Inc.Lang, ErrNotFoundThreadId, s.DBVersion)
}
return true
} else if record.ThreadId != currentThreadID {
Expand Down Expand Up @@ -459,7 +459,7 @@ func (s *session) checkUpdateFilter(event *replication.RowsEvent,

if currentThreadID == 0 && s.DBType == DBTypeMariaDB {
if record.ErrLevel != 1 {
record.AppendErrorNo(ErrNotFoundThreadId, s.DBVersion)
record.AppendErrorNo(s.Inc.Lang, ErrNotFoundThreadId, s.DBVersion)
}
return true, multiTable
} else if record.ThreadId != currentThreadID {
Expand Down
13 changes: 7 additions & 6 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"net"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/hanchuanchuan/goInception/ast"
"github.com/hanchuanchuan/goInception/config"
"github.com/hanchuanchuan/goInception/domain"
Expand Down Expand Up @@ -54,12 +61,6 @@ import (
log "github.com/sirupsen/logrus"
"go.uber.org/zap"
"golang.org/x/net/context"
"net"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/jinzhu/gorm"
)
Expand Down
Loading

0 comments on commit e8229c9

Please sign in to comment.