varsutil: tiny cleanup (#2884)

Extract a function to check if the option is on.
This commit is contained in:
Shen Li
2017-03-19 16:09:55 +08:00
committed by Han Fei
parent d0699bded8
commit e55534f1e9
2 changed files with 32 additions and 5 deletions

View File

@ -108,24 +108,29 @@ func SetSessionSystemVar(vars *variable.SessionVars, name string, value types.Da
return errors.Trace(err)
}
case variable.AutocommitVar:
isAutocommit := strings.EqualFold(sVal, "ON") || sVal == "1"
isAutocommit := tidbOptOn(sVal)
vars.SetStatusFlag(mysql.ServerStatusAutocommit, isAutocommit)
if isAutocommit {
vars.SetStatusFlag(mysql.ServerStatusInTrans, false)
}
case variable.TiDBSkipConstraintCheck:
vars.SkipConstraintCheck = (sVal == "1")
vars.SkipConstraintCheck = tidbOptOn(sVal)
case variable.TiDBSkipDDLWait:
vars.SkipDDLWait = (sVal == "1")
vars.SkipDDLWait = tidbOptOn(sVal)
case variable.TiDBOptAggPushDown:
vars.AllowAggPushDown = strings.EqualFold(sVal, "ON") || sVal == "1"
vars.AllowAggPushDown = tidbOptOn(sVal)
case variable.TiDBOptInSubqUnFolding:
vars.AllowInSubqueryUnFolding = strings.EqualFold(sVal, "ON") || sVal == "1"
vars.AllowInSubqueryUnFolding = tidbOptOn(sVal)
}
vars.Systems[name] = sVal
return nil
}
// For all tidb session variable options, we use "ON"/1 to turn on the options.
func tidbOptOn(opt string) bool {
return strings.EqualFold(opt, "ON") || opt == "1"
}
func parseTimeZone(s string) *time.Location {
if s == "SYSTEM" {
// TODO: Support global time_zone variable, it should be set to global time_zone value.

View File

@ -33,6 +33,28 @@ var _ = Suite(&testVarsutilSuite{})
type testVarsutilSuite struct {
}
func (s *testVarsutilSuite) TestTiDBOptOn(c *C) {
defer testleak.AfterTest(c)()
tbl := []struct {
val string
on bool
}{
{"ON", true},
{"on", true},
{"On", true},
{"1", true},
{"off", false},
{"No", false},
{"0", false},
{"1.1", false},
{"", false},
}
for _, t := range tbl {
on := tidbOptOn(t.val)
c.Assert(on, Equals, t.on)
}
}
func (s *testVarsutilSuite) TestVarsutil(c *C) {
defer testleak.AfterTest(c)()
v := variable.NewSessionVars()