Skip to content

Commit

Permalink
fix: adjust postgres paging query builder (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
endyApina authored Jun 17, 2024
1 parent 9fdfd93 commit a4b3f4d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
5 changes: 3 additions & 2 deletions pkg/postgres/query/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 11 additions & 11 deletions pkg/postgres/query/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ func TestQueryBuilder(t *testing.T) {
Operator: filter.LogicOperatorOr,
},
Paging: &paging.Request{
PageIndex: 2,
PageIndex: 1,
PageSize: 5,
},
Sorting: &sorting.Request{
SortColumn: "started",
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"},
},
{
Expand Down Expand Up @@ -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"},
},
{
Expand All @@ -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"},
},
{
Expand All @@ -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"},
},
{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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"},
},
{
Expand All @@ -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{},
},
{
Expand All @@ -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{},
},
{
Expand Down

0 comments on commit a4b3f4d

Please sign in to comment.