Support count with cols (#1595)
Support count with cols Reviewed-on: https://gitea.com/xorm/xorm/pulls/1595
This commit is contained in:
parent
537d82a6f7
commit
94fd254638
|
@ -59,6 +59,7 @@ type Interface interface {
|
||||||
QueryString(sqlOrArgs ...interface{}) ([]map[string]string, error)
|
QueryString(sqlOrArgs ...interface{}) ([]map[string]string, error)
|
||||||
Rows(bean interface{}) (*Rows, error)
|
Rows(bean interface{}) (*Rows, error)
|
||||||
SetExpr(string, interface{}) *Session
|
SetExpr(string, interface{}) *Session
|
||||||
|
Select(string) *Session
|
||||||
SQL(interface{}, ...interface{}) *Session
|
SQL(interface{}, ...interface{}) *Session
|
||||||
Sum(bean interface{}, colName string) (float64, error)
|
Sum(bean interface{}, colName string) (float64, error)
|
||||||
SumInt(bean interface{}, colName string) (int64, error)
|
SumInt(bean interface{}, colName string) (int64, error)
|
||||||
|
|
|
@ -153,6 +153,7 @@ func (statement *Statement) GenGetSQL(bean interface{}) (string, []interface{},
|
||||||
return sqlStr, append(statement.joinArgs, condArgs...), nil
|
return sqlStr, append(statement.joinArgs, condArgs...), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GenCountSQL generates the SQL for counting
|
||||||
func (statement *Statement) GenCountSQL(beans ...interface{}) (string, []interface{}, error) {
|
func (statement *Statement) GenCountSQL(beans ...interface{}) (string, []interface{}, error) {
|
||||||
if statement.RawSQL != "" {
|
if statement.RawSQL != "" {
|
||||||
return statement.GenRawSQL(), statement.RawParams, nil
|
return statement.GenRawSQL(), statement.RawParams, nil
|
||||||
|
@ -171,6 +172,8 @@ func (statement *Statement) GenCountSQL(beans ...interface{}) (string, []interfa
|
||||||
if len(selectSQL) <= 0 {
|
if len(selectSQL) <= 0 {
|
||||||
if statement.IsDistinct {
|
if statement.IsDistinct {
|
||||||
selectSQL = fmt.Sprintf("count(DISTINCT %s)", statement.ColumnStr())
|
selectSQL = fmt.Sprintf("count(DISTINCT %s)", statement.ColumnStr())
|
||||||
|
} else if statement.ColumnStr() != "" {
|
||||||
|
selectSQL = fmt.Sprintf("count(%s)", statement.ColumnStr())
|
||||||
} else {
|
} else {
|
||||||
selectSQL = "count(*)"
|
selectSQL = "count(*)"
|
||||||
}
|
}
|
||||||
|
|
|
@ -274,3 +274,27 @@ func TestWithTableName(t *testing.T) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.EqualValues(t, 2, total)
|
assert.EqualValues(t, 2, total)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCountWithSelectCols(t *testing.T) {
|
||||||
|
assert.NoError(t, prepareEngine())
|
||||||
|
|
||||||
|
assertSync(t, new(CountWithTableName))
|
||||||
|
|
||||||
|
_, err := testEngine.Insert(&CountWithTableName{
|
||||||
|
Name: "orderby",
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
_, err = testEngine.Insert(CountWithTableName{
|
||||||
|
Name: "limit",
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
total, err := testEngine.Cols("id").Count(new(CountWithTableName))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, 2, total)
|
||||||
|
|
||||||
|
total, err = testEngine.Select("count(id)").Count(CountWithTableName{})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, 2, total)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue