Example #1
0
// Search a Client by its condition(s)
func (s *ClientStore) Search(
	q store.Query) store.Result {

	return upperio.NewResult(func() (res db.Result, err error) {
		// get collection
		coll, err := s.Coll()
		if err != nil {
			return
		}

		// retrieve entities by given query conditions
		conds := upperio.Conds(q.GetConds())
		if conds == nil {
			res = coll.Find()
		} else {
			res = coll.Find(conds)
		}

		// add sorting information, if any
		res = res.Sort(upperio.Sort(q)...)

		// handle paging
		if q.GetOffset() != 0 {
			res = res.Skip(uint(q.GetOffset()))
		}
		if q.GetLimit() != 0 {
			res = res.Limit(uint(q.GetLimit()))
		}

		return
	})

}
Example #2
0
func TestSort(t *testing.T) {
	q := store.NewQuery()
	q.GetSorts().Add("para1").Add("-para2").Add("-para3")

	res := upperio.Sort(q)
	expLen := 3
	if l := len(res); l != expLen {
		t.Errorf("result length expected: %d, get: %d", expLen, l)
		t.FailNow()
	}

	if expStr := "para1"; res[0] != expStr {
		t.Errorf("result[0] expected: %d, get: %d", expStr, res[0])
	}
	if expStr := "-para2"; res[1] != expStr {
		t.Errorf("result[1] expected: %d, get: %d", expStr, res[1])
	}
	if expStr := "-para3"; res[2] != expStr {
		t.Errorf("result[2] expected: %d, get: %d", expStr, res[2])
	}
}