session_cols.go 4.5 KB

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