session_cols.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. "github.com/xormplus/core"
  10. )
  11. type incrParam struct {
  12. colName string
  13. arg interface{}
  14. }
  15. type decrParam struct {
  16. colName string
  17. arg interface{}
  18. }
  19. type exprParam struct {
  20. colName string
  21. expr string
  22. }
  23. type columnMap []string
  24. func (m columnMap) contain(colName string) bool {
  25. if len(m) == 0 {
  26. return false
  27. }
  28. n := len(colName)
  29. for _, mk := range m {
  30. if len(mk) != n {
  31. continue
  32. }
  33. if strings.EqualFold(mk, colName) {
  34. return true
  35. }
  36. }
  37. return false
  38. }
  39. func (m *columnMap) add(colName string) bool {
  40. if m.contain(colName) {
  41. return false
  42. }
  43. *m = append(*m, colName)
  44. return true
  45. }
  46. func setColumnInt(bean interface{}, col *core.Column, t int64) {
  47. v, err := col.ValueOf(bean)
  48. if err != nil {
  49. return
  50. }
  51. if v.CanSet() {
  52. switch v.Type().Kind() {
  53. case reflect.Int, reflect.Int64, reflect.Int32:
  54. v.SetInt(t)
  55. case reflect.Uint, reflect.Uint64, reflect.Uint32:
  56. v.SetUint(uint64(t))
  57. }
  58. }
  59. }
  60. func setColumnTime(bean interface{}, col *core.Column, t time.Time) {
  61. v, err := col.ValueOf(bean)
  62. if err != nil {
  63. return
  64. }
  65. if v.CanSet() {
  66. switch v.Type().Kind() {
  67. case reflect.Struct:
  68. v.Set(reflect.ValueOf(t).Convert(v.Type()))
  69. case reflect.Int, reflect.Int64, reflect.Int32:
  70. v.SetInt(t.Unix())
  71. case reflect.Uint, reflect.Uint64, reflect.Uint32:
  72. v.SetUint(uint64(t.Unix()))
  73. }
  74. }
  75. }
  76. func getFlagForColumn(m map[string]bool, col *core.Column) (val bool, has bool) {
  77. if len(m) == 0 {
  78. return false, false
  79. }
  80. n := len(col.Name)
  81. for mk := range m {
  82. if len(mk) != n {
  83. continue
  84. }
  85. if strings.EqualFold(mk, col.Name) {
  86. return m[mk], true
  87. }
  88. }
  89. return false, false
  90. }
  91. func col2NewCols(columns ...string) []string {
  92. newColumns := make([]string, 0, len(columns))
  93. for _, col := range columns {
  94. col = strings.Replace(col, "`", "", -1)
  95. col = strings.Replace(col, `"`, "", -1)
  96. ccols := strings.Split(col, ",")
  97. for _, c := range ccols {
  98. newColumns = append(newColumns, strings.TrimSpace(c))
  99. }
  100. }
  101. return newColumns
  102. }
  103. // Incr provides a query string like "count = count + 1"
  104. func (session *Session) Incr(column string, args ...interface{}) *Session {
  105. session.statement.Incr(column, args...)
  106. return session
  107. }
  108. // Decr provides a query string like "count = count - 1"
  109. func (session *Session) Decr(column string, args ...interface{}) *Session {
  110. session.statement.Decr(column, args...)
  111. return session
  112. }
  113. // SetExpr provides a query string like "column = {expression}"
  114. func (session *Session) SetExpr(column string, expression string) *Session {
  115. session.statement.SetExpr(column, expression)
  116. return session
  117. }
  118. // Select provides some columns to special
  119. func (session *Session) Select(str string) *Session {
  120. session.statement.Select(str)
  121. return session
  122. }
  123. // Cols provides some columns to special
  124. func (session *Session) Cols(columns ...string) *Session {
  125. session.statement.Cols(columns...)
  126. return session
  127. }
  128. // AllCols ask all columns
  129. func (session *Session) AllCols() *Session {
  130. session.statement.AllCols()
  131. return session
  132. }
  133. // MustCols specify some columns must use even if they are empty
  134. func (session *Session) MustCols(columns ...string) *Session {
  135. session.statement.MustCols(columns...)
  136. return session
  137. }
  138. // UseBool automatically retrieve condition according struct, but
  139. // if struct has bool field, it will ignore them. So use UseBool
  140. // to tell system to do not ignore them.
  141. // If no parameters, it will use all the bool field of struct, or
  142. // it will use parameters's columns
  143. func (session *Session) UseBool(columns ...string) *Session {
  144. session.statement.UseBool(columns...)
  145. return session
  146. }
  147. // Distinct use for distinct columns. Caution: when you are using cache,
  148. // distinct will not be cached because cache system need id,
  149. // but distinct will not provide id
  150. func (session *Session) Distinct(columns ...string) *Session {
  151. session.statement.Distinct(columns...)
  152. return session
  153. }
  154. // Omit Only not use the parameters as select or update columns
  155. func (session *Session) Omit(columns ...string) *Session {
  156. session.statement.Omit(columns...)
  157. return session
  158. }
  159. // Nullable Set null when column is zero-value and nullable for update
  160. func (session *Session) Nullable(columns ...string) *Session {
  161. session.statement.Nullable(columns...)
  162. return session
  163. }
  164. // NoAutoTime means do not automatically give created field and updated field
  165. // the current time on the current session temporarily
  166. func (session *Session) NoAutoTime() *Session {
  167. session.statement.UseAutoTime = false
  168. return session
  169. }