session_sum.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright 2016 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 "database/sql"
  6. // Count counts the records. bean's non-empty fields
  7. // are conditions.
  8. func (session *Session) Count(bean interface{}) (int64, error) {
  9. defer session.resetStatement()
  10. if session.IsAutoClose {
  11. defer session.Close()
  12. }
  13. var sqlStr string
  14. var args []interface{}
  15. if session.Statement.RawSQL == "" {
  16. sqlStr, args = session.Statement.genCountSQL(bean)
  17. } else {
  18. sqlStr = session.Statement.RawSQL
  19. args = session.Statement.RawParams
  20. }
  21. session.queryPreprocess(&sqlStr, args...)
  22. var err error
  23. var total int64
  24. if session.IsAutoCommit {
  25. err = session.DB().QueryRow(sqlStr, args...).Scan(&total)
  26. } else {
  27. err = session.Tx.QueryRow(sqlStr, args...).Scan(&total)
  28. }
  29. if err == sql.ErrNoRows || err == nil {
  30. return total, nil
  31. }
  32. return 0, err
  33. }
  34. // Sum call sum some column. bean's non-empty fields are conditions.
  35. func (session *Session) Sum(bean interface{}, columnName string) (float64, error) {
  36. defer session.resetStatement()
  37. if session.IsAutoClose {
  38. defer session.Close()
  39. }
  40. var sqlStr string
  41. var args []interface{}
  42. if len(session.Statement.RawSQL) == 0 {
  43. sqlStr, args = session.Statement.genSumSQL(bean, columnName)
  44. } else {
  45. sqlStr = session.Statement.RawSQL
  46. args = session.Statement.RawParams
  47. }
  48. session.queryPreprocess(&sqlStr, args...)
  49. var err error
  50. var res float64
  51. if session.IsAutoCommit {
  52. err = session.DB().QueryRow(sqlStr, args...).Scan(&res)
  53. } else {
  54. err = session.Tx.QueryRow(sqlStr, args...).Scan(&res)
  55. }
  56. if err == sql.ErrNoRows || err == nil {
  57. return res, nil
  58. }
  59. return 0, err
  60. }
  61. // Sums call sum some columns. bean's non-empty fields are conditions.
  62. func (session *Session) Sums(bean interface{}, columnNames ...string) ([]float64, error) {
  63. defer session.resetStatement()
  64. if session.IsAutoClose {
  65. defer session.Close()
  66. }
  67. var sqlStr string
  68. var args []interface{}
  69. if len(session.Statement.RawSQL) == 0 {
  70. sqlStr, args = session.Statement.genSumSQL(bean, columnNames...)
  71. } else {
  72. sqlStr = session.Statement.RawSQL
  73. args = session.Statement.RawParams
  74. }
  75. session.queryPreprocess(&sqlStr, args...)
  76. var err error
  77. var res = make([]float64, len(columnNames), len(columnNames))
  78. if session.IsAutoCommit {
  79. err = session.DB().QueryRow(sqlStr, args...).ScanSlice(&res)
  80. } else {
  81. err = session.Tx.QueryRow(sqlStr, args...).ScanSlice(&res)
  82. }
  83. if err == sql.ErrNoRows || err == nil {
  84. return res, nil
  85. }
  86. return nil, err
  87. }
  88. // SumsInt sum specify columns and return as []int64 instead of []float64
  89. func (session *Session) SumsInt(bean interface{}, columnNames ...string) ([]int64, error) {
  90. defer session.resetStatement()
  91. if session.IsAutoClose {
  92. defer session.Close()
  93. }
  94. var sqlStr string
  95. var args []interface{}
  96. if len(session.Statement.RawSQL) == 0 {
  97. sqlStr, args = session.Statement.genSumSQL(bean, columnNames...)
  98. } else {
  99. sqlStr = session.Statement.RawSQL
  100. args = session.Statement.RawParams
  101. }
  102. session.queryPreprocess(&sqlStr, args...)
  103. var err error
  104. var res = make([]int64, 0, len(columnNames))
  105. if session.IsAutoCommit {
  106. err = session.DB().QueryRow(sqlStr, args...).ScanSlice(&res)
  107. } else {
  108. err = session.Tx.QueryRow(sqlStr, args...).ScanSlice(&res)
  109. }
  110. if err == sql.ErrNoRows || err == nil {
  111. return res, nil
  112. }
  113. return nil, err
  114. }