add SumInt method
This commit is contained in:
parent
e11235316c
commit
233706969a
|
@ -1443,6 +1443,13 @@ func (engine *Engine) Sum(bean interface{}, colName string) (float64, error) {
|
||||||
return session.Sum(bean, colName)
|
return session.Sum(bean, colName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SumInt sum the records by some column. bean's non-empty fields are conditions.
|
||||||
|
func (engine *Engine) SumInt(bean interface{}, colName string) (int64, error) {
|
||||||
|
session := engine.NewSession()
|
||||||
|
defer session.Close()
|
||||||
|
return session.SumInt(bean, colName)
|
||||||
|
}
|
||||||
|
|
||||||
// Sums sum the records by some columns. bean's non-empty fields are conditions.
|
// Sums sum the records by some columns. bean's non-empty fields are conditions.
|
||||||
func (engine *Engine) Sums(bean interface{}, colNames ...string) ([]float64, error) {
|
func (engine *Engine) Sums(bean interface{}, colNames ...string) ([]float64, error) {
|
||||||
session := engine.NewSession()
|
session := engine.NewSession()
|
||||||
|
|
|
@ -81,6 +81,41 @@ func (session *Session) Sum(bean interface{}, columnName string) (float64, error
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SumInt call sum some column. bean's non-empty fields are conditions.
|
||||||
|
func (session *Session) SumInt(bean interface{}, columnName string) (int64, error) {
|
||||||
|
defer session.resetStatement()
|
||||||
|
if session.IsAutoClose {
|
||||||
|
defer session.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
var sqlStr string
|
||||||
|
var args []interface{}
|
||||||
|
var err error
|
||||||
|
if len(session.Statement.RawSQL) == 0 {
|
||||||
|
sqlStr, args, err = session.Statement.genSumSQL(bean, columnName)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sqlStr = session.Statement.RawSQL
|
||||||
|
args = session.Statement.RawParams
|
||||||
|
}
|
||||||
|
|
||||||
|
session.queryPreprocess(&sqlStr, args...)
|
||||||
|
|
||||||
|
var res int64
|
||||||
|
if session.IsAutoCommit {
|
||||||
|
err = session.DB().QueryRow(sqlStr, args...).Scan(&res)
|
||||||
|
} else {
|
||||||
|
err = session.Tx.QueryRow(sqlStr, args...).Scan(&res)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err == sql.ErrNoRows || err == nil {
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
// Sums call sum some columns. bean's non-empty fields are conditions.
|
// Sums call sum some columns. bean's non-empty fields are conditions.
|
||||||
func (session *Session) Sums(bean interface{}, columnNames ...string) ([]float64, error) {
|
func (session *Session) Sums(bean interface{}, columnNames ...string) ([]float64, error) {
|
||||||
defer session.resetStatement()
|
defer session.resetStatement()
|
||||||
|
|
Loading…
Reference in New Issue