Пример #1
0
func (e *Evaluator) variable(v *ast.VariableExpr) bool {
	name := strings.ToLower(v.Name)
	sessionVars := variable.GetSessionVars(e.ctx)
	globalVars := variable.GetGlobalVarAccessor(e.ctx)
	if !v.IsSystem {
		// user vars
		if value, ok := sessionVars.Users[name]; ok {
			v.SetString(value)
			return true
		}
		// select null user vars is permitted.
		v.SetNull()
		return true
	}

	_, ok := variable.SysVars[name]
	if !ok {
		// select null sys vars is not permitted
		e.err = variable.UnknownSystemVar.Gen("Unknown system variable '%s'", name)
		return false
	}

	if !v.IsGlobal {
		if value, ok := sessionVars.Systems[name]; ok {
			v.SetString(value)
			return true
		}
	}

	value, err := globalVars.GetGlobalSysVar(e.ctx, name)
	if err != nil {
		e.err = errors.Trace(err)
		return false
	}

	v.SetString(value)
	return true
}
Пример #2
0
func (e *Evaluator) variable(v *ast.VariableExpr) bool {
	name := strings.ToLower(v.Name)
	sessionVars := variable.GetSessionVars(e.ctx)
	globalVars := variable.GetGlobalVarAccessor(e.ctx)
	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.Gen("Unknown system variable '%s'", name)
		return false
	}
	if sysVar.Scope == variable.ScopeNone {
		v.SetString(sysVar.Value)
		return true
	}

	if !v.IsGlobal {
		d := sessionVars.GetSystemVar(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(e.ctx, name)
				if err != nil {
					e.err = errors.Trace(err)
					return false
				}
				d.SetString(globalVal)
				err = sessionVars.SetSystemVar(name, d)
				if err != nil {
					e.err = errors.Trace(err)
					return false
				}
			}
		}
		v.SetDatum(d)
		return true
	}
	value, err := globalVars.GetGlobalSysVar(e.ctx, name)
	if err != nil {
		e.err = errors.Trace(err)
		return false
	}

	v.SetString(value)
	return true
}