github.com/qxnw/lib4go@v0.0.0-20180426074627-c80c7e84b925/db/tpl/tpl.m.go (about) 1 package tpl 2 3 import ( 4 "fmt" 5 "regexp" 6 "strings" 7 ) 8 9 //MTPLContext SQLite模板 10 type MTPLContext struct { 11 name string 12 } 13 14 //GetSQLContext 获取查询串 15 func (o MTPLContext) GetSQLContext(tpl string, input map[string]interface{}) (query string, args []interface{}) { 16 f := func() string { 17 return "?" 18 } 19 return AnalyzeTPLFromCache(o.name, tpl, input, f) 20 } 21 22 //GetSPContext 获取存储过程 23 func (o MTPLContext) GetSPContext(tpl string, input map[string]interface{}) (query string, args []interface{}) { 24 return o.GetSQLContext(tpl, input) 25 } 26 27 //Replace 替换SQL中的占位符 28 func (o MTPLContext) Replace(sql string, args []interface{}) (r string) { 29 if strings.EqualFold(sql, "") || args == nil { 30 return sql 31 } 32 word, _ := regexp.Compile(`\?([,|\ ;)]|$)`) 33 index := -1 34 sql = word.ReplaceAllStringFunc(sql, func(s string) string { 35 index++ 36 if index >= len(args) { 37 return "NULL" + s[1:] 38 } 39 return fmt.Sprintf("'%v'%s", args[index], s[1:]) 40 }) 41 return sql 42 } 43 func init() { 44 // Register("sqlite", MTPLContext{name: "sqlite"}) 45 }