Skip to content

Commit 021d100

Browse files
committed
sets default
1 parent 7df02dd commit 021d100

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

server/context.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,13 @@ func (s *SessionManager) getOrCreateSession(ctx context.Context, conn *mysql.Con
235235
return sess, nil
236236
}
237237

238-
// InitSessionVariable sets a value to a parameter of a session at start.
239-
func (s *SessionManager) InitSessionVariable(ctx context.Context, conn *mysql.Conn, name, value string) error {
238+
// InitSessionDefaultVariable sets a default value to a parameter of a session at start.
239+
func (s *SessionManager) InitSessionDefaultVariable(ctx context.Context, conn *mysql.Conn, name, value string) error {
240240
sess, err := s.getOrCreateSession(ctx, conn)
241241
if err != nil {
242242
return err
243243
}
244-
return sess.InitSessionVariable(s.ctxFactory(ctx, sql.WithSession(sess)), name, value)
244+
return sess.InitSessionDefaultVariable(s.ctxFactory(ctx, sql.WithSession(sess)), name, value)
245245
}
246246

247247
// NewContextWithQuery creates a new context for the session at the given conn.

sql/base_session.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,30 @@ func (s *BaseSession) InitSessionVariable(ctx *Context, sysVarName string, value
159159
return s.setSessVar(ctx, sysVar, value, true)
160160
}
161161

162+
// InitSessionDefaultVariable implements the Session interface and is used to initialize variables (Including read-only variables)
163+
func (s *BaseSession) InitSessionDefaultVariable(ctx *Context, sysVarName string, value interface{}) error {
164+
sysVar, _, ok := SystemVariables.GetGlobal(sysVarName)
165+
if !ok {
166+
return ErrUnknownSystemVariable.New(sysVarName)
167+
}
168+
169+
sysVarName = strings.ToLower(sysVarName)
170+
val, ok := s.systemVars[sysVarName]
171+
if ok && val.Val != sysVar.GetDefault() {
172+
return ErrSystemVariableReinitialized.New(sysVarName)
173+
}
174+
svv, err := sysVar.InitValue(ctx, "", value, false)
175+
if err != nil {
176+
return err
177+
}
178+
svv.Var.SetDefault(value)
179+
s.systemVars[sysVarName] = svv
180+
if sysVarName == characterSetResultsSysVarName {
181+
s.charset = CharacterSet_Unspecified
182+
}
183+
return nil
184+
}
185+
162186
func (s *BaseSession) setSessVar(ctx *Context, sysVar SystemVariable, newVal interface{}, init bool) error {
163187
var svv SystemVarValue
164188
var err error

sql/session.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ type Session interface {
6666
Client() Client
6767
// SetClient returns a new session with the given client.
6868
SetClient(Client)
69+
// InitSessionDefaultVariable sets the given value as the default value of given system variable for this session.
70+
InitSessionDefaultVariable(ctx *Context, sysVarName string, value interface{}) error
6971
// SetSessionVariable sets the given system variable to the value given for this session.
7072
SetSessionVariable(ctx *Context, sysVarName string, value interface{}) error
7173
// InitSessionVariable sets the given system variable to the value given for this session and will allow for

0 commit comments

Comments
 (0)