You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

session_cols.go 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2017 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. "reflect"
  7. "strings"
  8. "time"
  9. "xorm.io/xorm/schemas"
  10. )
  11. func setColumnInt(bean interface{}, col *schemas.Column, t int64) {
  12. v, err := col.ValueOf(bean)
  13. if err != nil {
  14. return
  15. }
  16. if v.CanSet() {
  17. switch v.Type().Kind() {
  18. case reflect.Int, reflect.Int64, reflect.Int32:
  19. v.SetInt(t)
  20. case reflect.Uint, reflect.Uint64, reflect.Uint32:
  21. v.SetUint(uint64(t))
  22. }
  23. }
  24. }
  25. func setColumnTime(bean interface{}, col *schemas.Column, t time.Time) {
  26. v, err := col.ValueOf(bean)
  27. if err != nil {
  28. return
  29. }
  30. if v.CanSet() {
  31. switch v.Type().Kind() {
  32. case reflect.Struct:
  33. v.Set(reflect.ValueOf(t).Convert(v.Type()))
  34. case reflect.Int, reflect.Int64, reflect.Int32:
  35. v.SetInt(t.Unix())
  36. case reflect.Uint, reflect.Uint64, reflect.Uint32:
  37. v.SetUint(uint64(t.Unix()))
  38. }
  39. }
  40. }
  41. func getFlagForColumn(m map[string]bool, col *schemas.Column) (val bool, has bool) {
  42. if len(m) == 0 {
  43. return false, false
  44. }
  45. n := len(col.Name)
  46. for mk := range m {
  47. if len(mk) != n {
  48. continue
  49. }
  50. if strings.EqualFold(mk, col.Name) {
  51. return m[mk], true
  52. }
  53. }
  54. return false, false
  55. }
  56. // Incr provides a query string like "count = count + 1"
  57. func (session *Session) Incr(column string, arg ...interface{}) *Session {
  58. session.statement.Incr(column, arg...)
  59. return session
  60. }
  61. // Decr provides a query string like "count = count - 1"
  62. func (session *Session) Decr(column string, arg ...interface{}) *Session {
  63. session.statement.Decr(column, arg...)
  64. return session
  65. }
  66. // SetExpr provides a query string like "column = {expression}"
  67. func (session *Session) SetExpr(column string, expression interface{}) *Session {
  68. session.statement.SetExpr(column, expression)
  69. return session
  70. }
  71. // Select provides some columns to special
  72. func (session *Session) Select(str string) *Session {
  73. session.statement.Select(str)
  74. return session
  75. }
  76. // Cols provides some columns to special
  77. func (session *Session) Cols(columns ...string) *Session {
  78. session.statement.Cols(columns...)
  79. return session
  80. }
  81. // AllCols ask all columns
  82. func (session *Session) AllCols() *Session {
  83. session.statement.AllCols()
  84. return session
  85. }
  86. // MustCols specify some columns must use even if they are empty
  87. func (session *Session) MustCols(columns ...string) *Session {
  88. session.statement.MustCols(columns...)
  89. return session
  90. }
  91. // UseBool automatically retrieve condition according struct, but
  92. // if struct has bool field, it will ignore them. So use UseBool
  93. // to tell system to do not ignore them.
  94. // If no parameters, it will use all the bool field of struct, or
  95. // it will use parameters's columns
  96. func (session *Session) UseBool(columns ...string) *Session {
  97. session.statement.UseBool(columns...)
  98. return session
  99. }
  100. // Distinct use for distinct columns. Caution: when you are using cache,
  101. // distinct will not be cached because cache system need id,
  102. // but distinct will not provide id
  103. func (session *Session) Distinct(columns ...string) *Session {
  104. session.statement.Distinct(columns...)
  105. return session
  106. }
  107. // Omit Only not use the parameters as select or update columns
  108. func (session *Session) Omit(columns ...string) *Session {
  109. session.statement.Omit(columns...)
  110. return session
  111. }
  112. // Nullable Set null when column is zero-value and nullable for update
  113. func (session *Session) Nullable(columns ...string) *Session {
  114. session.statement.Nullable(columns...)
  115. return session
  116. }
  117. // NoAutoTime means do not automatically give created field and updated field
  118. // the current time on the current session temporarily
  119. func (session *Session) NoAutoTime() *Session {
  120. session.statement.UseAutoTime = false
  121. return session
  122. }