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.

98 lines
2.7 KiB

  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 (
  6. "database/sql"
  7. "errors"
  8. "reflect"
  9. )
  10. // Count counts the records. bean's non-empty fields
  11. // are conditions.
  12. func (session *Session) Count(bean ...interface{}) (int64, error) {
  13. if session.isAutoClose {
  14. defer session.Close()
  15. }
  16. var sqlStr string
  17. var args []interface{}
  18. var err error
  19. if session.statement.RawSQL == "" {
  20. sqlStr, args, err = session.statement.genCountSQL(bean...)
  21. if err != nil {
  22. return 0, err
  23. }
  24. } else {
  25. sqlStr = session.statement.RawSQL
  26. args = session.statement.RawParams
  27. }
  28. var total int64
  29. err = session.queryRow(sqlStr, args...).Scan(&total)
  30. if err == sql.ErrNoRows || err == nil {
  31. return total, nil
  32. }
  33. return 0, err
  34. }
  35. // sum call sum some column. bean's non-empty fields are conditions.
  36. func (session *Session) sum(res interface{}, bean interface{}, columnNames ...string) error {
  37. if session.isAutoClose {
  38. defer session.Close()
  39. }
  40. v := reflect.ValueOf(res)
  41. if v.Kind() != reflect.Ptr {
  42. return errors.New("need a pointer to a variable")
  43. }
  44. var isSlice = v.Elem().Kind() == reflect.Slice
  45. var sqlStr string
  46. var args []interface{}
  47. var err error
  48. if len(session.statement.RawSQL) == 0 {
  49. sqlStr, args, err = session.statement.genSumSQL(bean, columnNames...)
  50. if err != nil {
  51. return err
  52. }
  53. } else {
  54. sqlStr = session.statement.RawSQL
  55. args = session.statement.RawParams
  56. }
  57. if isSlice {
  58. err = session.queryRow(sqlStr, args...).ScanSlice(res)
  59. } else {
  60. err = session.queryRow(sqlStr, args...).Scan(res)
  61. }
  62. if err == sql.ErrNoRows || err == nil {
  63. return nil
  64. }
  65. return err
  66. }
  67. // Sum call sum some column. bean's non-empty fields are conditions.
  68. func (session *Session) Sum(bean interface{}, columnName string) (res float64, err error) {
  69. return res, session.sum(&res, bean, columnName)
  70. }
  71. // SumInt call sum some column. bean's non-empty fields are conditions.
  72. func (session *Session) SumInt(bean interface{}, columnName string) (res int64, err error) {
  73. return res, session.sum(&res, bean, columnName)
  74. }
  75. // Sums call sum some columns. bean's non-empty fields are conditions.
  76. func (session *Session) Sums(bean interface{}, columnNames ...string) ([]float64, error) {
  77. var res = make([]float64, len(columnNames), len(columnNames))
  78. return res, session.sum(&res, bean, columnNames...)
  79. }
  80. // SumsInt sum specify columns and return as []int64 instead of []float64
  81. func (session *Session) SumsInt(bean interface{}, columnNames ...string) ([]int64, error) {
  82. var res = make([]int64, len(columnNames), len(columnNames))
  83. return res, session.sum(&res, bean, columnNames...)
  84. }