github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/sessionctx/autocommit/autocommit.go (about) 1 // Copyright 2015 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package autocommit 15 16 import ( 17 "github.com/insionng/yougam/libraries/pingcap/tidb/context" 18 ) 19 20 // Checker is the interface checks if it should autocommit in the context. 21 // TODO: Choose a better name. 22 type Checker interface { 23 // ShouldAutocommit returns true if it should autocommit in the context. 24 ShouldAutocommit(ctx context.Context) bool 25 } 26 27 // keyType is a dummy type to avoid naming collision in context. 28 type keyType int 29 30 // String defines a Stringer function for debugging and pretty printing. 31 func (k keyType) String() string { 32 return "autocommit_checker" 33 } 34 35 const key keyType = 0 36 37 // BindAutocommitChecker binds autocommit checker to context. 38 func BindAutocommitChecker(ctx context.Context, checker Checker) { 39 ctx.SetValue(key, checker) 40 } 41 42 // ShouldAutocommit gets checker from ctx and checks if it should autocommit. 43 func ShouldAutocommit(ctx context.Context) bool { 44 v, ok := ctx.Value(key).(Checker) 45 if !ok { 46 panic("Miss autocommit checker") 47 } 48 return v.ShouldAutocommit(ctx) 49 }