uitil.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package db
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. // key and value extract
  7. func whereExactKeyValues(where map[string]interface{}) (keys string, values []interface{}) {
  8. var i = 1
  9. if where != nil && len(where) > 0 {
  10. for k, v := range where {
  11. if keys != "" {
  12. keys = keys + " and "
  13. }
  14. if vv, ok := v.(string); ok {
  15. values = append(values, vv) // value
  16. keys = fmt.Sprintf("%s %s = $%d", keys, k, i) // key like $#
  17. } else {
  18. values = append(values, v) // value
  19. keys = fmt.Sprintf("%s %s = $%d", keys, k, i) // key = $#
  20. }
  21. i = i + 1
  22. }
  23. }
  24. return
  25. }
  26. //convert milli time to date format
  27. func TimestampDate(m int64) time.Time {
  28. return time.Unix(m/1e3, (m%1e3)*int64(time.Millisecond)/int64(time.Nanosecond))
  29. }
  30. // make time to milli format
  31. func TimestampMilli(t time.Time) int64 {
  32. return unixMilli(t)
  33. }
  34. // make milli time
  35. func unixMilli(t time.Time) int64 {
  36. return t.Round(time.Millisecond).UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond))
  37. }
  38. // return true if float64 type , else false
  39. func IsFloat64(value interface{}) bool {
  40. if _, ok := value.(float64); ok {
  41. return true
  42. }
  43. return false
  44. }
  45. // return true if float32 type , else false
  46. func IsFloat32(value interface{}) bool {
  47. if _, ok := value.(float32); ok {
  48. return true
  49. }
  50. return false
  51. }
  52. // return true if bool type , else false
  53. func IsBoolean(value interface{}) bool {
  54. if _, ok := value.(bool); ok {
  55. return true
  56. }
  57. return false
  58. }
  59. // return true if int type , else false
  60. func IsInt(value interface{}) bool {
  61. if _, ok := value.(int); ok {
  62. return true
  63. }
  64. return false
  65. }
  66. // return true if int64 type , else false
  67. func IsInt64(value interface{}) bool {
  68. if _, ok := value.(int64); ok {
  69. return true
  70. }
  71. return false
  72. }
  73. // return true if map[string]interface{} type , else false
  74. func IsMap(value interface{}) bool {
  75. if _, ok := value.(map[string]interface{}); ok {
  76. return true
  77. }
  78. return false
  79. }