diff --git a/pkg/postgres/query/builder.go b/pkg/postgres/query/builder.go index 1de9ef8..28e58e6 100644 --- a/pkg/postgres/query/builder.go +++ b/pkg/postgres/query/builder.go @@ -126,8 +126,9 @@ func (qb *Builder) addPaging(paging *paging.Request) error { return errors.New("missing paging fields, add paging request or remove call to AddSize()") } - if paging.PageIndex > 1 { - qb.query.WriteString(fmt.Sprintf(" OFFSET %d", paging.PageIndex)) + if paging.PageIndex > 0 { + offset := paging.PageIndex * paging.PageSize + qb.query.WriteString(fmt.Sprintf(" OFFSET %d", offset)) } if paging.PageSize > 0 { diff --git a/pkg/postgres/query/builder_test.go b/pkg/postgres/query/builder_test.go index 5cc678e..077a45d 100644 --- a/pkg/postgres/query/builder_test.go +++ b/pkg/postgres/query/builder_test.go @@ -49,7 +49,7 @@ func TestQueryBuilder(t *testing.T) { Operator: filter.LogicOperatorOr, }, Paging: &paging.Request{ - PageIndex: 2, + PageIndex: 1, PageSize: 5, }, Sorting: &sorting.Request{ @@ -57,7 +57,7 @@ func TestQueryBuilder(t *testing.T) { SortDirection: "desc", }, }, - wantQuery: `WHERE "status_col_name" IN (?, ?) OR "source_id_col_name" IN (?, ?, ?) ORDER BY started_col_name DESC OFFSET 2 LIMIT 5`, + wantQuery: `WHERE "status_col_name" IN (?, ?) OR "source_id_col_name" IN (?, ?, ?) ORDER BY started_col_name DESC OFFSET 5 LIMIT 5`, wantArgs: []any{"invalid status", "valid status", "some_source_id", "another_source_id", "third_source_id"}, }, { @@ -105,7 +105,7 @@ func TestQueryBuilder(t *testing.T) { PageSize: 5, }, }, - wantQuery: `WHERE "status_col_name" IN (?) AND "source_id_col_name" IN (?) OFFSET 2 LIMIT 5`, + wantQuery: `WHERE "status_col_name" IN (?) AND "source_id_col_name" IN (?) OFFSET 10 LIMIT 5`, wantArgs: []any{"invalid status", "some_source_id"}, }, { @@ -123,10 +123,10 @@ func TestQueryBuilder(t *testing.T) { }, Paging: &paging.Request{ PageIndex: 2, - PageSize: 5, + PageSize: 10, }, }, - wantQuery: `WHERE "status_col_name" IN (?) OFFSET 2 LIMIT 5`, + wantQuery: `WHERE "status_col_name" IN (?) OFFSET 20 LIMIT 10`, wantArgs: []any{"invalid status"}, }, { @@ -147,7 +147,7 @@ func TestQueryBuilder(t *testing.T) { PageSize: 5, }, }, - wantQuery: `WHERE "status_col_name" IN (?, ?) OFFSET 2 LIMIT 5`, + wantQuery: `WHERE "status_col_name" IN (?, ?) OFFSET 10 LIMIT 5`, wantArgs: []any{"invalid status", "another status"}, }, { @@ -182,7 +182,7 @@ func TestQueryBuilder(t *testing.T) { SortDirection: "desc", }, }, - wantQuery: `WHERE "status_col_name" IN (?, ?) OR "source_id_col_name" IN (?, ?, ?) OR "other_filter_field_col_name" IN (?, ?, ?) ORDER BY started_col_name DESC OFFSET 2 LIMIT 5`, + wantQuery: `WHERE "status_col_name" IN (?, ?) OR "source_id_col_name" IN (?, ?, ?) OR "other_filter_field_col_name" IN (?, ?, ?) ORDER BY started_col_name DESC OFFSET 10 LIMIT 5`, wantArgs: []any{ "invalid status", "valid status", "some_source_id", "another_source_id", "third_source_id", "some_field", "another_field", "third_field", @@ -220,7 +220,7 @@ func TestQueryBuilder(t *testing.T) { SortDirection: "desc", }, }, - wantQuery: `WHERE "status_col_name" IN (?, ?) OR "source_id_col_name" IN (?, ?, ?) OR "other_filter_field_col_name" IN (?, ?, ?) ORDER BY started_col_name DESC OFFSET 2 LIMIT 5`, + wantQuery: `WHERE "status_col_name" IN (?, ?) OR "source_id_col_name" IN (?, ?, ?) OR "other_filter_field_col_name" IN (?, ?, ?) ORDER BY started_col_name DESC OFFSET 10 LIMIT 5`, wantArgs: []any{ "invalid status", "valid status", "some_source_id", "another_source_id", "third_source_id", "some_field", "another_field", "third_field", @@ -244,7 +244,7 @@ func TestQueryBuilder(t *testing.T) { PageSize: 5, }, }, - wantQuery: `WHERE "status_col_name" NOT IN (?, ?) OFFSET 2 LIMIT 5`, + wantQuery: `WHERE "status_col_name" NOT IN (?, ?) OFFSET 10 LIMIT 5`, wantArgs: []any{"invalid status", "another status"}, }, { @@ -263,7 +263,7 @@ func TestQueryBuilder(t *testing.T) { SortDirection: "asc", }, }, - wantQuery: " ORDER BY started_col_name ASC OFFSET 3 LIMIT 10", + wantQuery: " ORDER BY started_col_name ASC OFFSET 30 LIMIT 10", wantArgs: []any{}, }, { @@ -278,7 +278,7 @@ func TestQueryBuilder(t *testing.T) { SortDirection: "asc", }, }, - wantQuery: " ORDER BY started_col_name ASC OFFSET 3 LIMIT 10", + wantQuery: " ORDER BY started_col_name ASC OFFSET 30 LIMIT 10", wantArgs: []any{}, }, {