helpers.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. // Copyright 2015 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "errors"
  7. "fmt"
  8. "reflect"
  9. "sort"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/go-xorm/core"
  14. )
  15. // str2PK convert string value to primary key value according to tp
  16. func str2PKValue(s string, tp reflect.Type) (reflect.Value, error) {
  17. var err error
  18. var result interface{}
  19. var defReturn = reflect.Zero(tp)
  20. switch tp.Kind() {
  21. case reflect.Int:
  22. result, err = strconv.Atoi(s)
  23. if err != nil {
  24. return defReturn, fmt.Errorf("convert %s as int: %s", s, err.Error())
  25. }
  26. case reflect.Int8:
  27. x, err := strconv.Atoi(s)
  28. if err != nil {
  29. return defReturn, fmt.Errorf("convert %s as int8: %s", s, err.Error())
  30. }
  31. result = int8(x)
  32. case reflect.Int16:
  33. x, err := strconv.Atoi(s)
  34. if err != nil {
  35. return defReturn, fmt.Errorf("convert %s as int16: %s", s, err.Error())
  36. }
  37. result = int16(x)
  38. case reflect.Int32:
  39. x, err := strconv.Atoi(s)
  40. if err != nil {
  41. return defReturn, fmt.Errorf("convert %s as int32: %s", s, err.Error())
  42. }
  43. result = int32(x)
  44. case reflect.Int64:
  45. result, err = strconv.ParseInt(s, 10, 64)
  46. if err != nil {
  47. return defReturn, fmt.Errorf("convert %s as int64: %s", s, err.Error())
  48. }
  49. case reflect.Uint:
  50. x, err := strconv.ParseUint(s, 10, 64)
  51. if err != nil {
  52. return defReturn, fmt.Errorf("convert %s as uint: %s", s, err.Error())
  53. }
  54. result = uint(x)
  55. case reflect.Uint8:
  56. x, err := strconv.ParseUint(s, 10, 64)
  57. if err != nil {
  58. return defReturn, fmt.Errorf("convert %s as uint8: %s", s, err.Error())
  59. }
  60. result = uint8(x)
  61. case reflect.Uint16:
  62. x, err := strconv.ParseUint(s, 10, 64)
  63. if err != nil {
  64. return defReturn, fmt.Errorf("convert %s as uint16: %s", s, err.Error())
  65. }
  66. result = uint16(x)
  67. case reflect.Uint32:
  68. x, err := strconv.ParseUint(s, 10, 64)
  69. if err != nil {
  70. return defReturn, fmt.Errorf("convert %s as uint32: %s", s, err.Error())
  71. }
  72. result = uint32(x)
  73. case reflect.Uint64:
  74. result, err = strconv.ParseUint(s, 10, 64)
  75. if err != nil {
  76. return defReturn, fmt.Errorf("convert %s as uint64: %s", s, err.Error())
  77. }
  78. case reflect.String:
  79. result = s
  80. default:
  81. return defReturn, errors.New("unsupported convert type")
  82. }
  83. return reflect.ValueOf(result).Convert(tp), nil
  84. }
  85. func str2PK(s string, tp reflect.Type) (interface{}, error) {
  86. v, err := str2PKValue(s, tp)
  87. if err != nil {
  88. return nil, err
  89. }
  90. return v.Interface(), nil
  91. }
  92. func splitTag(tag string) (tags []string) {
  93. tag = strings.TrimSpace(tag)
  94. var hasQuote = false
  95. var lastIdx = 0
  96. for i, t := range tag {
  97. if t == '\'' {
  98. hasQuote = !hasQuote
  99. } else if t == ' ' {
  100. if lastIdx < i && !hasQuote {
  101. tags = append(tags, strings.TrimSpace(tag[lastIdx:i]))
  102. lastIdx = i + 1
  103. }
  104. }
  105. }
  106. if lastIdx < len(tag) {
  107. tags = append(tags, strings.TrimSpace(tag[lastIdx:]))
  108. }
  109. return
  110. }
  111. type zeroable interface {
  112. IsZero() bool
  113. }
  114. func isZero(k interface{}) bool {
  115. switch k.(type) {
  116. case int:
  117. return k.(int) == 0
  118. case int8:
  119. return k.(int8) == 0
  120. case int16:
  121. return k.(int16) == 0
  122. case int32:
  123. return k.(int32) == 0
  124. case int64:
  125. return k.(int64) == 0
  126. case uint:
  127. return k.(uint) == 0
  128. case uint8:
  129. return k.(uint8) == 0
  130. case uint16:
  131. return k.(uint16) == 0
  132. case uint32:
  133. return k.(uint32) == 0
  134. case uint64:
  135. return k.(uint64) == 0
  136. case float32:
  137. return k.(float32) == 0
  138. case float64:
  139. return k.(float64) == 0
  140. case bool:
  141. return k.(bool) == false
  142. case string:
  143. return k.(string) == ""
  144. case zeroable:
  145. return k.(zeroable).IsZero()
  146. }
  147. return false
  148. }
  149. func isStructZero(v reflect.Value) bool {
  150. if !v.IsValid() {
  151. return true
  152. }
  153. for i := 0; i < v.NumField(); i++ {
  154. field := v.Field(i)
  155. switch field.Kind() {
  156. case reflect.Ptr:
  157. field = field.Elem()
  158. fallthrough
  159. case reflect.Struct:
  160. if !isStructZero(field) {
  161. return false
  162. }
  163. default:
  164. if field.CanInterface() && !isZero(field.Interface()) {
  165. return false
  166. }
  167. }
  168. }
  169. return true
  170. }
  171. func isArrayValueZero(v reflect.Value) bool {
  172. if !v.IsValid() || v.Len() == 0 {
  173. return true
  174. }
  175. for i := 0; i < v.Len(); i++ {
  176. if !isZero(v.Index(i).Interface()) {
  177. return false
  178. }
  179. }
  180. return true
  181. }
  182. func int64ToIntValue(id int64, tp reflect.Type) reflect.Value {
  183. var v interface{}
  184. kind := tp.Kind()
  185. if kind == reflect.Ptr {
  186. kind = tp.Elem().Kind()
  187. }
  188. switch kind {
  189. case reflect.Int16:
  190. temp := int16(id)
  191. v = &temp
  192. case reflect.Int32:
  193. temp := int32(id)
  194. v = &temp
  195. case reflect.Int:
  196. temp := int(id)
  197. v = &temp
  198. case reflect.Int64:
  199. temp := id
  200. v = &temp
  201. case reflect.Uint16:
  202. temp := uint16(id)
  203. v = &temp
  204. case reflect.Uint32:
  205. temp := uint32(id)
  206. v = &temp
  207. case reflect.Uint64:
  208. temp := uint64(id)
  209. v = &temp
  210. case reflect.Uint:
  211. temp := uint(id)
  212. v = &temp
  213. }
  214. if tp.Kind() == reflect.Ptr {
  215. return reflect.ValueOf(v).Convert(tp)
  216. }
  217. return reflect.ValueOf(v).Elem().Convert(tp)
  218. }
  219. func int64ToInt(id int64, tp reflect.Type) interface{} {
  220. return int64ToIntValue(id, tp).Interface()
  221. }
  222. func isPKZero(pk core.PK) bool {
  223. for _, k := range pk {
  224. if isZero(k) {
  225. return true
  226. }
  227. }
  228. return false
  229. }
  230. func indexNoCase(s, sep string) int {
  231. return strings.Index(strings.ToLower(s), strings.ToLower(sep))
  232. }
  233. func splitNoCase(s, sep string) []string {
  234. idx := indexNoCase(s, sep)
  235. if idx < 0 {
  236. return []string{s}
  237. }
  238. return strings.Split(s, s[idx:idx+len(sep)])
  239. }
  240. func splitNNoCase(s, sep string, n int) []string {
  241. idx := indexNoCase(s, sep)
  242. if idx < 0 {
  243. return []string{s}
  244. }
  245. return strings.SplitN(s, s[idx:idx+len(sep)], n)
  246. }
  247. func makeArray(elem string, count int) []string {
  248. res := make([]string, count)
  249. for i := 0; i < count; i++ {
  250. res[i] = elem
  251. }
  252. return res
  253. }
  254. func rValue(bean interface{}) reflect.Value {
  255. return reflect.Indirect(reflect.ValueOf(bean))
  256. }
  257. func rType(bean interface{}) reflect.Type {
  258. sliceValue := reflect.Indirect(reflect.ValueOf(bean))
  259. //return reflect.TypeOf(sliceValue.Interface())
  260. return sliceValue.Type()
  261. }
  262. func structName(v reflect.Type) string {
  263. for v.Kind() == reflect.Ptr {
  264. v = v.Elem()
  265. }
  266. return v.Name()
  267. }
  268. func col2NewCols(columns ...string) []string {
  269. newColumns := make([]string, 0, len(columns))
  270. for _, col := range columns {
  271. col = strings.Replace(col, "`", "", -1)
  272. col = strings.Replace(col, `"`, "", -1)
  273. ccols := strings.Split(col, ",")
  274. for _, c := range ccols {
  275. newColumns = append(newColumns, strings.TrimSpace(c))
  276. }
  277. }
  278. return newColumns
  279. }
  280. func sliceEq(left, right []string) bool {
  281. if len(left) != len(right) {
  282. return false
  283. }
  284. sort.Sort(sort.StringSlice(left))
  285. sort.Sort(sort.StringSlice(right))
  286. for i := 0; i < len(left); i++ {
  287. if left[i] != right[i] {
  288. return false
  289. }
  290. }
  291. return true
  292. }
  293. func setColumnInt(bean interface{}, col *core.Column, t int64) {
  294. v, err := col.ValueOf(bean)
  295. if err != nil {
  296. return
  297. }
  298. if v.CanSet() {
  299. switch v.Type().Kind() {
  300. case reflect.Int, reflect.Int64, reflect.Int32:
  301. v.SetInt(t)
  302. case reflect.Uint, reflect.Uint64, reflect.Uint32:
  303. v.SetUint(uint64(t))
  304. }
  305. }
  306. }
  307. func setColumnTime(bean interface{}, col *core.Column, t time.Time) {
  308. v, err := col.ValueOf(bean)
  309. if err != nil {
  310. return
  311. }
  312. if v.CanSet() {
  313. switch v.Type().Kind() {
  314. case reflect.Struct:
  315. v.Set(reflect.ValueOf(t).Convert(v.Type()))
  316. case reflect.Int, reflect.Int64, reflect.Int32:
  317. v.SetInt(t.Unix())
  318. case reflect.Uint, reflect.Uint64, reflect.Uint32:
  319. v.SetUint(uint64(t.Unix()))
  320. }
  321. }
  322. }
  323. func genCols(table *core.Table, session *Session, bean interface{}, useCol bool, includeQuote bool) ([]string, []interface{}, error) {
  324. colNames := make([]string, 0, len(table.ColumnsSeq()))
  325. args := make([]interface{}, 0, len(table.ColumnsSeq()))
  326. for _, col := range table.Columns() {
  327. if useCol && !col.IsVersion && !col.IsCreated && !col.IsUpdated {
  328. if _, ok := getFlagForColumn(session.Statement.columnMap, col); !ok {
  329. continue
  330. }
  331. }
  332. if col.MapType == core.ONLYFROMDB {
  333. continue
  334. }
  335. fieldValuePtr, err := col.ValueOf(bean)
  336. if err != nil {
  337. return nil, nil, err
  338. }
  339. fieldValue := *fieldValuePtr
  340. if col.IsAutoIncrement {
  341. switch fieldValue.Type().Kind() {
  342. case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int, reflect.Int64:
  343. if fieldValue.Int() == 0 {
  344. continue
  345. }
  346. case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint, reflect.Uint64:
  347. if fieldValue.Uint() == 0 {
  348. continue
  349. }
  350. case reflect.String:
  351. if len(fieldValue.String()) == 0 {
  352. continue
  353. }
  354. case reflect.Ptr:
  355. if fieldValue.Pointer() == 0 {
  356. continue
  357. }
  358. }
  359. }
  360. if col.IsDeleted {
  361. continue
  362. }
  363. if session.Statement.ColumnStr != "" {
  364. if _, ok := getFlagForColumn(session.Statement.columnMap, col); !ok {
  365. continue
  366. }
  367. }
  368. if session.Statement.OmitStr != "" {
  369. if _, ok := getFlagForColumn(session.Statement.columnMap, col); ok {
  370. continue
  371. }
  372. }
  373. // !evalphobia! set fieldValue as nil when column is nullable and zero-value
  374. if _, ok := getFlagForColumn(session.Statement.nullableMap, col); ok {
  375. if col.Nullable && isZero(fieldValue.Interface()) {
  376. var nilValue *int
  377. fieldValue = reflect.ValueOf(nilValue)
  378. }
  379. }
  380. if (col.IsCreated || col.IsUpdated) && session.Statement.UseAutoTime /*&& isZero(fieldValue.Interface())*/ {
  381. // if time is non-empty, then set to auto time
  382. val, t := session.Engine.NowTime2(col.SQLType.Name)
  383. args = append(args, val)
  384. var colName = col.Name
  385. session.afterClosures = append(session.afterClosures, func(bean interface{}) {
  386. col := table.GetColumn(colName)
  387. setColumnTime(bean, col, t)
  388. })
  389. } else if col.IsVersion && session.Statement.checkVersion {
  390. args = append(args, 1)
  391. } else {
  392. arg, err := session.value2Interface(col, fieldValue)
  393. if err != nil {
  394. return colNames, args, err
  395. }
  396. args = append(args, arg)
  397. }
  398. if includeQuote {
  399. colNames = append(colNames, session.Engine.Quote(col.Name)+" = ?")
  400. } else {
  401. colNames = append(colNames, col.Name)
  402. }
  403. }
  404. return colNames, args, nil
  405. }
  406. func indexName(tableName, idxName string) string {
  407. return fmt.Sprintf("IDX_%v_%v", tableName, idxName)
  408. }
  409. func getFlagForColumn(m map[string]bool, col *core.Column) (val bool, has bool) {
  410. if len(m) == 0 {
  411. return false, false
  412. }
  413. n := len(col.Name)
  414. for mk := range m {
  415. if len(mk) != n {
  416. continue
  417. }
  418. if strings.EqualFold(mk, col.Name) {
  419. return m[mk], true
  420. }
  421. }
  422. return false, false
  423. }