12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- package xsql
- import (
- "github.com/emqx/kuiper/common"
- "github.com/emqx/kuiper/plugins"
- "github.com/emqx/kuiper/xstream/api"
- "strings"
- )
- type FunctionValuer struct{}
- func (*FunctionValuer) Value(key string) (interface{}, bool) {
- return nil, false
- }
- func (*FunctionValuer) Meta(key string) (interface{}, bool) {
- return nil, false
- }
- var aggFuncMap = map[string]string{"avg": "",
- "count": "",
- "max": "", "min": "",
- "sum": "",
- }
- var mathFuncMap = map[string]string{"abs": "", "acos": "", "asin": "", "atan": "", "atan2": "",
- "bitand": "", "bitor": "", "bitxor": "", "bitnot": "",
- "ceil": "", "cos": "", "cosh": "",
- "exp": "",
- "ln": "", "log": "",
- "mod": "",
- "power": "",
- "rand": "", "round": "",
- "sign": "", "sin": "", "sinh": "", "sqrt": "",
- "tan": "", "tanh": "",
- }
- var strFuncMap = map[string]string{"concat": "",
- "endswith": "",
- "format_time": "",
- "indexof": "",
- "length": "", "lower": "", "lpad": "", "ltrim": "",
- "numbytes": "",
- "regexp_matches": "", "regexp_replace": "", "regexp_substr": "", "rpad": "", "rtrim": "",
- "substring": "", "startswith": "", "split_value": "",
- "trim": "",
- "upper": "",
- }
- var convFuncMap = map[string]string{"concat": "", "cast": "", "chr": "",
- "encode": "",
- "trunc": "",
- }
- var hashFuncMap = map[string]string{"md5": "",
- "sha1": "", "sha256": "", "sha384": "", "sha512": "",
- }
- var otherFuncMap = map[string]string{"isnull": "",
- "newuuid": "", "timestamp": "", "mqtt": "", "meta": "",
- }
- func (*FunctionValuer) Call(name string, args []interface{}) (interface{}, bool) {
- lowerName := strings.ToLower(name)
- if _, ok := mathFuncMap[lowerName]; ok {
- return mathCall(name, args)
- } else if _, ok := strFuncMap[lowerName]; ok {
- return strCall(lowerName, args)
- } else if _, ok := convFuncMap[lowerName]; ok {
- return convCall(lowerName, args)
- } else if _, ok := hashFuncMap[lowerName]; ok {
- return hashCall(lowerName, args)
- } else if _, ok := otherFuncMap[lowerName]; ok {
- return otherCall(lowerName, args)
- } else if _, ok := aggFuncMap[lowerName]; ok {
- return nil, false
- } else {
- common.Log.Debugf("run func %s", name)
- if nf, err := plugins.GetPlugin(name, "functions"); err != nil {
- return err, false
- } else {
- f, ok := nf.(api.Function)
- if !ok {
- return nil, false
- }
- if f.IsAggregate() {
- return nil, false
- }
- result, ok := f.Exec(args)
- common.Log.Debugf("run custom function %s, get result %v", name, result)
- return result, ok
- }
- }
- }
|