functions.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package xsql
  2. import (
  3. "strings"
  4. )
  5. type FunctionValuer struct{}
  6. func (*FunctionValuer) Value(key string) (interface{}, bool) {
  7. return nil, false
  8. }
  9. var aggFuncMap = map[string]string{"avg": "",
  10. "count": "",
  11. "max": "", "min": "",
  12. "sum": "",
  13. }
  14. var mathFuncMap = map[string]string{"abs": "", "acos": "", "asin": "", "atan": "", "atan2": "",
  15. "bitand": "", "bitor": "", "bitxor": "", "bitnot": "",
  16. "ceil": "", "cos": "", "cosh": "",
  17. "exp": "",
  18. "ln": "", "log": "",
  19. "mod": "",
  20. "power": "",
  21. "rand": "", "round": "",
  22. "sign": "", "sin": "", "sinh": "", "sqrt": "",
  23. "tan": "", "tanh": "",
  24. }
  25. var strFuncMap = map[string]string{"concat": "",
  26. "endswith": "",
  27. "format_time": "",
  28. "indexof": "",
  29. "length": "", "lower": "", "lpad": "", "ltrim": "",
  30. "numbytes": "",
  31. "regexp_matches": "", "regexp_replace": "", "regexp_substr": "", "rpad": "", "rtrim": "",
  32. "substring": "", "startswith": "",
  33. "trim": "",
  34. "upper": "",
  35. }
  36. var convFuncMap = map[string]string{"concat": "", "cast": "", "chr": "",
  37. "encode": "",
  38. "trunc": "",
  39. }
  40. var hashFuncMap = map[string]string{ "md5": "",
  41. "sha1": "", "sha256": "", "sha384": "", "sha512": "",
  42. }
  43. var otherFuncMap = map[string]string{"isNull": "",
  44. "newuuid": "", "timestamp": "",
  45. }
  46. func (*FunctionValuer) Call(name string, args []interface{}) (interface{}, bool) {
  47. lowerName := strings.ToLower(name)
  48. if _, ok := mathFuncMap[lowerName]; ok {
  49. return mathCall(name, args)
  50. } else if _, ok := strFuncMap[lowerName]; ok {
  51. return strCall(lowerName, args)
  52. } else if _, ok := convFuncMap[lowerName]; ok {
  53. return convCall(lowerName, args)
  54. } else if _, ok := hashFuncMap[lowerName]; ok {
  55. return hashCall(lowerName, args)
  56. } else if _, ok := otherFuncMap[lowerName]; ok {
  57. return otherCall(lowerName, args)
  58. }
  59. return nil, false
  60. }