session_sum.go 3.4 KB

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