예제 #1
0
파일: session.go 프로젝트: pingcap/tidb
// LoadCommonGlobalVariableIfNeeded loads and applies commonly used global variables for the session
// right before creating a transaction for the first time.
func (s *session) loadCommonGlobalVariablesIfNeeded() error {
	vars := s.sessionVars
	if vars.CommonGlobalLoaded {
		return nil
	}
	if s.Value(context.Initing) != nil {
		// When running bootstrap or upgrade, we should not access global storage.
		return nil
	}
	// Set the variable to true to prevent cyclic recursive call.
	vars.CommonGlobalLoaded = true
	rs, err := s.ExecRestrictedSQL(s, loadCommonGlobalVarsSQL)
	if err != nil {
		vars.CommonGlobalLoaded = false
		log.Errorf("Failed to load common global variables.")
		return errors.Trace(err)
	}
	for {
		row, err1 := rs.Next()
		if err1 != nil {
			vars.CommonGlobalLoaded = false
			log.Errorf("Failed to load common global variables.")
			return errors.Trace(err1)
		}
		if row == nil {
			break
		}
		varName := row.Data[0].GetString()
		if d := varsutil.GetSystemVar(vars, varName); d.IsNull() {
			varsutil.SetSystemVar(s.sessionVars, varName, row.Data[1])
		}
	}
	vars.CommonGlobalLoaded = true
	return nil
}
예제 #2
0
파일: show.go 프로젝트: pingcap/tidb
func (e *ShowExec) fetchShowVariables() error {
	sessionVars := e.ctx.GetSessionVars()
	globalVars := sessionVars.GlobalVarsAccessor
	for _, v := range variable.SysVars {
		var err error
		var value string
		if !e.GlobalScope {
			// Try to get Session Scope variable value first.
			sv := varsutil.GetSystemVar(sessionVars, v.Name)
			if sv.IsNull() {
				value, err = globalVars.GetGlobalSysVar(v.Name)
				if err != nil {
					return errors.Trace(err)
				}
				sv.SetString(value)
				err = varsutil.SetSystemVar(sessionVars, v.Name, sv)
				if err != nil {
					return errors.Trace(err)
				}
			}
			value = sv.GetString()
		} else {
			value, err = globalVars.GetGlobalSysVar(v.Name)
			if err != nil {
				return errors.Trace(err)
			}
		}
		row := &Row{Data: types.MakeDatums(v.Name, value)}
		e.rows = append(e.rows, row)
	}
	return nil
}
예제 #3
0
func (s *testSuite) TestSetCharset(c *C) {
	defer testleak.AfterTest(c)()
	tk := testkit.NewTestKit(c, s.store)
	tk.MustExec(`SET NAMES latin1`)

	ctx := tk.Se.(context.Context)
	sessionVars := ctx.GetSessionVars()
	for _, v := range variable.SetNamesVariables {
		sVar := varsutil.GetSystemVar(sessionVars, v)
		c.Assert(sVar.GetString() != "utf8", IsTrue)
	}
	tk.MustExec(`SET NAMES utf8`)
	for _, v := range variable.SetNamesVariables {
		sVar := varsutil.GetSystemVar(sessionVars, v)
		c.Assert(sVar.GetString(), Equals, "utf8")
	}
	sVar := varsutil.GetSystemVar(sessionVars, variable.CollationConnection)
	c.Assert(sVar.GetString(), Equals, "utf8_general_ci")

	// Issue 1523
	tk.MustExec(`SET NAMES binary`)
}
예제 #4
0
파일: helper.go 프로젝트: pingcap/tidb
func getSystemTimestamp(ctx context.Context) (time.Time, error) {
	value := time.Now()

	if ctx == nil {
		return value, nil
	}

	// check whether use timestamp varibale
	sessionVars := ctx.GetSessionVars()
	ts := varsutil.GetSystemVar(sessionVars, "timestamp")
	if !ts.IsNull() && ts.GetString() != "" {
		timestamp, err := ts.ToInt64(ctx.GetSessionVars().StmtCtx)
		if err != nil {
			return time.Time{}, errors.Trace(err)
		}
		if timestamp <= 0 {
			return value, nil
		}
		return time.Unix(timestamp, 0), nil
	}
	return value, nil
}
예제 #5
0
파일: tables.go 프로젝트: pingcap/tidb
func dataForSessionVar(ctx context.Context) (records [][]types.Datum, err error) {
	sessionVars := ctx.GetSessionVars()
	globalVars := sessionVars.GlobalVarsAccessor
	for _, v := range variable.SysVars {
		var value string
		sv := varsutil.GetSystemVar(sessionVars, v.Name)
		if sv.IsNull() {
			value, err = globalVars.GetGlobalSysVar(v.Name)
			if err != nil {
				return nil, errors.Trace(err)
			}
			sv.SetString(value)
			err = varsutil.SetSystemVar(sessionVars, v.Name, sv)
			if err != nil {
				return nil, errors.Trace(err)
			}
		}
		value = sv.GetString()
		row := types.MakeDatums(v.Name, value)
		records = append(records, row)
	}
	return
}
예제 #6
0
파일: evaluator.go 프로젝트: pingcap/tidb
func (e *Evaluator) variable(v *ast.VariableExpr) bool {
	name := strings.ToLower(v.Name)
	sessionVars := e.ctx.GetSessionVars()
	globalVars := sessionVars.GlobalVarsAccessor
	if !v.IsSystem {
		if v.Value != nil && !v.Value.GetDatum().IsNull() {
			strVal, err := v.Value.GetDatum().ToString()
			if err != nil {
				e.err = errors.Trace(err)
				return false
			}
			sessionVars.Users[name] = strings.ToLower(strVal)
			v.SetString(strVal)
			return true
		}
		// user vars
		if value, ok := sessionVars.Users[name]; ok {
			v.SetString(value)
			return true
		}
		// select null user vars is permitted.
		v.SetNull()
		return true
	}

	sysVar, ok := variable.SysVars[name]
	if !ok {
		// select null sys vars is not permitted
		e.err = variable.UnknownSystemVar.GenByArgs(name)
		return false
	}
	if sysVar.Scope == variable.ScopeNone {
		v.SetString(sysVar.Value)
		return true
	}

	if !v.IsGlobal {
		d := varsutil.GetSystemVar(sessionVars, name)
		if d.IsNull() {
			if sysVar.Scope&variable.ScopeGlobal == 0 {
				d.SetString(sysVar.Value)
			} else {
				// Get global system variable and fill it in session.
				globalVal, err := globalVars.GetGlobalSysVar(name)
				if err != nil {
					e.err = errors.Trace(err)
					return false
				}
				d.SetString(globalVal)
				err = varsutil.SetSystemVar(sessionVars, name, d)
				if err != nil {
					e.err = errors.Trace(err)
					return false
				}
			}
		}
		v.SetDatum(d)
		return true
	}
	value, err := globalVars.GetGlobalSysVar(name)
	if err != nil {
		e.err = errors.Trace(err)
		return false
	}

	v.SetString(value)
	return true
}
예제 #7
0
func (er *expressionRewriter) rewriteVariable(v *ast.VariableExpr) {
	stkLen := len(er.ctxStack)
	name := strings.ToLower(v.Name)
	sessionVars := er.b.ctx.GetSessionVars()
	globalVars := sessionVars.GlobalVarsAccessor
	if !v.IsSystem {
		if v.Value != nil {
			er.ctxStack[stkLen-1], er.err = expression.NewFunction(ast.SetVar,
				er.ctxStack[stkLen-1].GetType(),
				datumToConstant(types.NewDatum(name), mysql.TypeString),
				er.ctxStack[stkLen-1])
			return
		}
		if _, ok := sessionVars.Users[name]; ok {
			f, err := expression.NewFunction(ast.GetVar,
				// TODO: Here is wrong, the sessionVars should store a name -> Datum map. Will fix it later.
				types.NewFieldType(mysql.TypeString),
				datumToConstant(types.NewStringDatum(name), mysql.TypeString))
			if err != nil {
				er.err = errors.Trace(err)
				return
			}
			er.ctxStack = append(er.ctxStack, f)
		} else {
			// select null user vars is permitted.
			er.ctxStack = append(er.ctxStack, &expression.Constant{RetType: types.NewFieldType(mysql.TypeNull)})
		}
		return
	}

	sysVar, ok := variable.SysVars[name]
	if !ok {
		// select null sys vars is not permitted
		er.err = variable.UnknownSystemVar.GenByArgs(name)
		return
	}
	if sysVar.Scope == variable.ScopeNone {
		er.ctxStack = append(er.ctxStack, datumToConstant(types.NewDatum(sysVar.Value), mysql.TypeString))
		return
	}

	if v.IsGlobal {
		value, err := globalVars.GetGlobalSysVar(name)
		if err != nil {
			er.err = errors.Trace(err)
			return
		}
		er.ctxStack = append(er.ctxStack, datumToConstant(types.NewDatum(value), mysql.TypeString))
		return
	}
	d := varsutil.GetSystemVar(sessionVars, name)
	if d.IsNull() {
		if sysVar.Scope&variable.ScopeGlobal == 0 {
			d.SetString(sysVar.Value)
		} else {
			// Get global system variable and fill it in session.
			globalVal, err := globalVars.GetGlobalSysVar(name)
			if err != nil {
				er.err = errors.Trace(err)
				return
			}
			d.SetString(globalVal)
			err = varsutil.SetSystemVar(sessionVars, name, d)
			if err != nil {
				er.err = errors.Trace(err)
				return
			}
		}
	}
	er.ctxStack = append(er.ctxStack, datumToConstant(d, mysql.TypeString))
	return
}