Skip to content

Commit

Permalink
Handling limit / offset
Browse files Browse the repository at this point in the history
Signed-off-by: Geoffrey Bauduin <geoffrey.bauduin@corp.ovh.com>
  • Loading branch information
geoffreybauduin committed Feb 15, 2018
1 parent 872bd1f commit 15ac2ff
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
6 changes: 6 additions & 0 deletions filterapply.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ func apply(statement squirrel.SelectBuilder, f yaormfilter.Filter, dbp DBProvide
fmt.Sprintf("%s.%s %s", dbp.EscapeValue(applier.tableName), dbp.EscapeValue(orderBy.Field), orderBy.Way),
)
}
if shouldLimit, limit := f.GetLimit(); shouldLimit {
statement = statement.Limit(limit)
}
if shouldOffset, offset := f.GetOffset(); shouldOffset {
statement = statement.Offset(offset)
}
return statement
}

Expand Down
22 changes: 22 additions & 0 deletions filterapply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,25 @@ func TestFilterApplier_ApplyWithOrderBy(t *testing.T) {
assert.Equal(t, models[0].(*testdata.Category).ID, category.ID)
assert.Equal(t, models[1].(*testdata.Category).ID, category2.ID)
}

func TestFilterApplier_ApplyWithLimitAndOffset(t *testing.T) {
killDb, err := testdata.SetupTestDatabase("test")
defer killDb()
assert.Nil(t, err)
dbp, err := yaorm.NewDBProvider(context.TODO(), "test")
assert.Nil(t, err)
category := &testdata.Category{Name: "category"}
saveModel(t, dbp, category)
category2 := &testdata.Category{Name: "category2"}
saveModel(t, dbp, category2)

models, err := yaorm.GenericSelectAll(dbp, testdata.NewCategoryFilter().Limit(1))
assert.Nil(t, err)
assert.Len(t, models, 1)
assert.Equal(t, models[0].(*testdata.Category).ID, category.ID)

models, err = yaorm.GenericSelectAll(dbp, testdata.NewCategoryFilter().Limit(1).Offset(1))
assert.Nil(t, err)
assert.Len(t, models, 1)
assert.Equal(t, models[0].(*testdata.Category).ID, category2.ID)
}
10 changes: 10 additions & 0 deletions testdata/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,13 @@ func (f *CategoryFilter) OrderBy(field string, way yaormfilter.OrderingWay) yaor
f.SetOrderBy(field, way)
return f
}

func (f *CategoryFilter) Limit(limit uint64) yaormfilter.Filter {
f.SetLimit(limit)
return f
}

func (f *CategoryFilter) Offset(offset uint64) yaormfilter.Filter {
f.SetOffset(offset)
return f
}
34 changes: 34 additions & 0 deletions yaormfilter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ type Filter interface {
GetSelectOptions() []RequestOption
OrderBy(field string, way OrderingWay) Filter
GetOrderBy() []*OrderBy
Limit(limit uint64) Filter
Offset(offset uint64) Filter
GetLimit() (bool, uint64)
GetOffset() (bool, uint64)
}

// OrderingWay is a custom type to have ordering
Expand Down Expand Up @@ -41,6 +45,10 @@ type ModelFilter struct {
subqueryload bool
options []RequestOption
orderBy []*OrderBy
shouldLimit bool
limit uint64
shouldOffset bool
offset uint64
}

type OrderBy struct {
Expand Down Expand Up @@ -95,3 +103,29 @@ func (mf *ModelFilter) GetSelectOptions() []RequestOption {
}
return opts
}

func (mf *ModelFilter) Limit(limit uint64) Filter {
panic(errors.NotImplementedf("Limit"))
}

func (mf *ModelFilter) Offset(limit uint64) Filter {
panic(errors.NotImplementedf("Offset"))
}

func (mf *ModelFilter) SetLimit(limit uint64) {
mf.shouldLimit = true
mf.limit = limit
}

func (mf *ModelFilter) SetOffset(offset uint64) {
mf.shouldOffset = true
mf.offset = offset
}

func (mf *ModelFilter) GetLimit() (bool, uint64) {
return mf.shouldLimit, mf.limit
}

func (mf *ModelFilter) GetOffset() (bool, uint64) {
return mf.shouldOffset, mf.offset
}

0 comments on commit 15ac2ff

Please sign in to comment.