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 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/core"
  10. )
  11. func setColumnInt(bean interface{}, col *core.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 *core.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 *core.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. func col2NewCols(columns ...string) []string {
  57. newColumns := make([]string, 0, len(columns))
  58. for _, col := range columns {
  59. col = strings.Replace(col, "`", "", -1)
  60. col = strings.Replace(col, `"`, "", -1)
  61. ccols := strings.Split(col, ",")
  62. for _, c := range ccols {
  63. newColumns = append(newColumns, strings.TrimSpace(c))
  64. }
  65. }
  66. return newColumns
  67. }
  68. // Incr provides a query string like "count = count + 1"
  69. func (session *Session) Incr(column string, arg ...interface{}) *Session {
  70. session.statement.Incr(column, arg...)
  71. return session
  72. }
  73. // Decr provides a query string like "count = count - 1"
  74. func (session *Session) Decr(column string, arg ...interface{}) *Session {
  75. session.statement.Decr(column, arg...)
  76. return session
  77. }
  78. // SetExpr provides a query string like "column = {expression}"
  79. func (session *Session) SetExpr(column string, expression interface{}) *Session {
  80. session.statement.SetExpr(column, expression)
  81. return session
  82. }
  83. // Select provides some columns to special
  84. func (session *Session) Select(str string) *Session {
  85. session.statement.Select(str)
  86. return session
  87. }
  88. // Cols provides some columns to special
  89. func (session *Session) Cols(columns ...string) *Session {
  90. session.statement.Cols(columns...)
  91. return session
  92. }
  93. // AllCols ask all columns
  94. func (session *Session) AllCols() *Session {
  95. session.statement.AllCols()
  96. return session
  97. }
  98. // MustCols specify some columns must use even if they are empty
  99. func (session *Session) MustCols(columns ...string) *Session {
  100. session.statement.MustCols(columns...)
  101. return session
  102. }
  103. // UseBool automatically retrieve condition according struct, but
  104. // if struct has bool field, it will ignore them. So use UseBool
  105. // to tell system to do not ignore them.
  106. // If no parameters, it will use all the bool field of struct, or
  107. // it will use parameters's columns
  108. func (session *Session) UseBool(columns ...string) *Session {
  109. session.statement.UseBool(columns...)
  110. return session
  111. }
  112. // Distinct use for distinct columns. Caution: when you are using cache,
  113. // distinct will not be cached because cache system need id,
  114. // but distinct will not provide id
  115. func (session *Session) Distinct(columns ...string) *Session {
  116. session.statement.Distinct(columns...)
  117. return session
  118. }
  119. // Omit Only not use the parameters as select or update columns
  120. func (session *Session) Omit(columns ...string) *Session {
  121. session.statement.Omit(columns...)
  122. return session
  123. }
  124. // Nullable Set null when column is zero-value and nullable for update
  125. func (session *Session) Nullable(columns ...string) *Session {
  126. session.statement.Nullable(columns...)
  127. return session
  128. }
  129. // NoAutoTime means do not automatically give created field and updated field
  130. // the current time on the current session temporarily
  131. func (session *Session) NoAutoTime() *Session {
  132. session.statement.UseAutoTime = false
  133. return session
  134. }