PageRenderTime 95ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/github.com/jinzhu/gorm/utils.go

https://gitlab.com/prima101112/test-quiz
Go | 264 lines | 213 code | 36 blank | 15 comment | 62 complexity | b7447007402532bbaf0c233f675ed77c MD5 | raw file
  1. package gorm
  2. import (
  3. "bytes"
  4. "database/sql/driver"
  5. "fmt"
  6. "reflect"
  7. "regexp"
  8. "runtime"
  9. "strings"
  10. "sync"
  11. "time"
  12. )
  13. // NowFunc returns current time, this function is exported in order to be able
  14. // to give the flexibility to the developer to customize it according to their
  15. // needs, e.g:
  16. // gorm.NowFunc = func() time.Time {
  17. // return time.Now().UTC()
  18. // }
  19. var NowFunc = func() time.Time {
  20. return time.Now()
  21. }
  22. // Copied from golint
  23. var commonInitialisms = []string{"API", "ASCII", "CPU", "CSS", "DNS", "EOF", "GUID", "HTML", "HTTP", "HTTPS", "ID", "IP", "JSON", "LHS", "QPS", "RAM", "RHS", "RPC", "SLA", "SMTP", "SSH", "TLS", "TTL", "UI", "UID", "UUID", "URI", "URL", "UTF8", "VM", "XML", "XSRF", "XSS"}
  24. var commonInitialismsReplacer *strings.Replacer
  25. func init() {
  26. var commonInitialismsForReplacer []string
  27. for _, initialism := range commonInitialisms {
  28. commonInitialismsForReplacer = append(commonInitialismsForReplacer, initialism, strings.Title(strings.ToLower(initialism)))
  29. }
  30. commonInitialismsReplacer = strings.NewReplacer(commonInitialismsForReplacer...)
  31. }
  32. type safeMap struct {
  33. m map[string]string
  34. l *sync.RWMutex
  35. }
  36. func (s *safeMap) Set(key string, value string) {
  37. s.l.Lock()
  38. defer s.l.Unlock()
  39. s.m[key] = value
  40. }
  41. func (s *safeMap) Get(key string) string {
  42. s.l.RLock()
  43. defer s.l.RUnlock()
  44. return s.m[key]
  45. }
  46. func newSafeMap() *safeMap {
  47. return &safeMap{l: new(sync.RWMutex), m: make(map[string]string)}
  48. }
  49. var smap = newSafeMap()
  50. type strCase bool
  51. const (
  52. lower strCase = false
  53. upper strCase = true
  54. )
  55. // ToDBName convert string to db name
  56. func ToDBName(name string) string {
  57. if v := smap.Get(name); v != "" {
  58. return v
  59. }
  60. if name == "" {
  61. return ""
  62. }
  63. var (
  64. value = commonInitialismsReplacer.Replace(name)
  65. buf = bytes.NewBufferString("")
  66. lastCase, currCase, nextCase strCase
  67. )
  68. for i, v := range value[:len(value)-1] {
  69. nextCase = strCase(value[i+1] >= 'A' && value[i+1] <= 'Z')
  70. if i > 0 {
  71. if currCase == upper {
  72. if lastCase == upper && nextCase == upper {
  73. buf.WriteRune(v)
  74. } else {
  75. if value[i-1] != '_' && value[i+1] != '_' {
  76. buf.WriteRune('_')
  77. }
  78. buf.WriteRune(v)
  79. }
  80. } else {
  81. buf.WriteRune(v)
  82. }
  83. } else {
  84. currCase = upper
  85. buf.WriteRune(v)
  86. }
  87. lastCase = currCase
  88. currCase = nextCase
  89. }
  90. buf.WriteByte(value[len(value)-1])
  91. s := strings.ToLower(buf.String())
  92. smap.Set(name, s)
  93. return s
  94. }
  95. // SQL expression
  96. type expr struct {
  97. expr string
  98. args []interface{}
  99. }
  100. // Expr generate raw SQL expression, for example:
  101. // DB.Model(&product).Update("price", gorm.Expr("price * ? + ?", 2, 100))
  102. func Expr(expression string, args ...interface{}) *expr {
  103. return &expr{expr: expression, args: args}
  104. }
  105. func indirect(reflectValue reflect.Value) reflect.Value {
  106. for reflectValue.Kind() == reflect.Ptr {
  107. reflectValue = reflectValue.Elem()
  108. }
  109. return reflectValue
  110. }
  111. func toQueryMarks(primaryValues [][]interface{}) string {
  112. var results []string
  113. for _, primaryValue := range primaryValues {
  114. var marks []string
  115. for _,_ = range primaryValue {
  116. marks = append(marks, "?")
  117. }
  118. if len(marks) > 1 {
  119. results = append(results, fmt.Sprintf("(%v)", strings.Join(marks, ",")))
  120. } else {
  121. results = append(results, strings.Join(marks, ""))
  122. }
  123. }
  124. return strings.Join(results, ",")
  125. }
  126. func toQueryCondition(scope *Scope, columns []string) string {
  127. var newColumns []string
  128. for _, column := range columns {
  129. newColumns = append(newColumns, scope.Quote(column))
  130. }
  131. if len(columns) > 1 {
  132. return fmt.Sprintf("(%v)", strings.Join(newColumns, ","))
  133. }
  134. return strings.Join(newColumns, ",")
  135. }
  136. func toQueryValues(values [][]interface{}) (results []interface{}) {
  137. for _, value := range values {
  138. for _, v := range value {
  139. results = append(results, v)
  140. }
  141. }
  142. return
  143. }
  144. func fileWithLineNum() string {
  145. for i := 2; i < 15; i++ {
  146. _, file, line, ok := runtime.Caller(i)
  147. if ok && (!regexp.MustCompile(`jinzhu/gorm/.*.go`).MatchString(file) || regexp.MustCompile(`jinzhu/gorm/.*test.go`).MatchString(file)) {
  148. return fmt.Sprintf("%v:%v", file, line)
  149. }
  150. }
  151. return ""
  152. }
  153. func isBlank(value reflect.Value) bool {
  154. return reflect.DeepEqual(value.Interface(), reflect.Zero(value.Type()).Interface())
  155. }
  156. func toSearchableMap(attrs ...interface{}) (result interface{}) {
  157. if len(attrs) > 1 {
  158. if str, ok := attrs[0].(string); ok {
  159. result = map[string]interface{}{str: attrs[1]}
  160. }
  161. } else if len(attrs) == 1 {
  162. if attr, ok := attrs[0].(map[string]interface{}); ok {
  163. result = attr
  164. }
  165. if attr, ok := attrs[0].(interface{}); ok {
  166. result = attr
  167. }
  168. }
  169. return
  170. }
  171. func equalAsString(a interface{}, b interface{}) bool {
  172. return toString(a) == toString(b)
  173. }
  174. func toString(str interface{}) string {
  175. if values, ok := str.([]interface{}); ok {
  176. var results []string
  177. for _, value := range values {
  178. results = append(results, toString(value))
  179. }
  180. return strings.Join(results, "_")
  181. } else if bytes, ok := str.([]byte); ok {
  182. return string(bytes)
  183. } else if reflectValue := reflect.Indirect(reflect.ValueOf(str)); reflectValue.IsValid() {
  184. return fmt.Sprintf("%v", reflectValue.Interface())
  185. }
  186. return ""
  187. }
  188. func makeSlice(elemType reflect.Type) interface{} {
  189. if elemType.Kind() == reflect.Slice {
  190. elemType = elemType.Elem()
  191. }
  192. sliceType := reflect.SliceOf(elemType)
  193. slice := reflect.New(sliceType)
  194. slice.Elem().Set(reflect.MakeSlice(sliceType, 0, 0))
  195. return slice.Interface()
  196. }
  197. func strInSlice(a string, list []string) bool {
  198. for _, b := range list {
  199. if b == a {
  200. return true
  201. }
  202. }
  203. return false
  204. }
  205. // getValueFromFields return given fields's value
  206. func getValueFromFields(value reflect.Value, fieldNames []string) (results []interface{}) {
  207. // If value is a nil pointer, Indirect returns a zero Value!
  208. // Therefor we need to check for a zero value,
  209. // as FieldByName could panic
  210. if indirectValue := reflect.Indirect(value); indirectValue.IsValid() {
  211. for _, fieldName := range fieldNames {
  212. if fieldValue := indirectValue.FieldByName(fieldName); fieldValue.IsValid() {
  213. result := fieldValue.Interface()
  214. if r, ok := result.(driver.Valuer); ok {
  215. result, _ = r.Value()
  216. }
  217. results = append(results, result)
  218. }
  219. }
  220. }
  221. return
  222. }
  223. func addExtraSpaceIfExist(str string) string {
  224. if str != "" {
  225. return " " + str
  226. }
  227. return ""
  228. }