Add FindAndCount method #842

Merged
lunny merged 1 commits from lunny/find_and_count into master 2018-02-07 13:06:14 +00:00
4 changed files with 67 additions and 0 deletions

View File

@ -1453,6 +1453,13 @@ func (engine *Engine) Find(beans interface{}, condiBeans ...interface{}) error {
return session.Find(beans, condiBeans...)
}
// FindAndCount find the results and also return the counts
func (engine *Engine) FindAndCount(rowsSlicePtr interface{}, condiBean ...interface{}) (int64, error) {
session := engine.NewSession()
defer session.Close()
return session.FindAndCount(rowsSlicePtr, condiBean...)
}
// Iterate record by record handle records from table, bean's non-empty fields
// are conditions.
func (engine *Engine) Iterate(bean interface{}, fun IterFunc) error {

View File

@ -30,6 +30,7 @@ type Interface interface {
Exec(string, ...interface{}) (sql.Result, error)
Exist(bean ...interface{}) (bool, error)
Find(interface{}, ...interface{}) error
FindAndCount(interface{}, ...interface{}) (int64, error)
Get(interface{}) (bool, error)
GroupBy(keys string) *Session
ID(interface{}) *Session

View File

@ -29,6 +29,29 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
return session.find(rowsSlicePtr, condiBean...)
}
// FindAndCount find the results and also return the counts
func (session *Session) FindAndCount(rowsSlicePtr interface{}, condiBean ...interface{}) (int64, error) {
if session.isAutoClose {
defer session.Close()
}
session.autoResetStatement = false
err := session.find(rowsSlicePtr, condiBean...)
if err != nil {
return 0, err
}
sliceValue := reflect.Indirect(reflect.ValueOf(rowsSlicePtr))
if sliceValue.Kind() != reflect.Slice && sliceValue.Kind() != reflect.Map {
return 0, errors.New("needs a pointer to a slice or a map")
}
sliceElementType := sliceValue.Type().Elem()
session.autoResetStatement = true
return session.Count(reflect.New(sliceElementType).Interface())
}
func (session *Session) find(rowsSlicePtr interface{}, condiBean ...interface{}) error {
sliceValue := reflect.Indirect(reflect.ValueOf(rowsSlicePtr))
if sliceValue.Kind() != reflect.Slice && sliceValue.Kind() != reflect.Map {

View File

@ -520,3 +520,39 @@ func TestFindMark(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, 2, len(results))
}
func TestFindAndCountOneFunc(t *testing.T) {
type FindAndCountStruct struct {
Id int64
Content string
Msg bool `xorm:"bit"`
}
assert.NoError(t, prepareEngine())
assertSync(t, new(FindAndCountStruct))
cnt, err := testEngine.Insert([]FindAndCountStruct{
{
Content: "111",
Msg: false,
},
{
Content: "222",
Msg: true,
},
})
assert.NoError(t, err)
assert.EqualValues(t, 2, cnt)
var results = make([]FindAndCountStruct, 0, 2)
cnt, err = testEngine.FindAndCount(&results)
assert.NoError(t, err)
assert.EqualValues(t, 2, len(results))
assert.EqualValues(t, 2, cnt)
results = make([]FindAndCountStruct, 0, 1)
cnt, err = testEngine.Where("msg = ?", true).FindAndCount(&results)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(results))
assert.EqualValues(t, 1, cnt)
}