Skip to content

Commit

Permalink
♻️ refactor: updated codebase query stmt #8
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Oct 1, 2023
1 parent 6ec07b1 commit 133c54d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
23 changes: 23 additions & 0 deletions qb.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,29 @@ func (q *QbDB) Limit(value int64) *QbDB {
return q
}

// Page for pagination
func (q *QbDB) Page(value int64) *QbDB {
if value < 0 {
log.Fatalf("Invalid page: %v", value)
}
if value > 0 {
value = value - 1
}
q.Builder.page = value
return q
}

// Size for pagination
func (q *QbDB) Size(value int64) *QbDB {
if value < 0 {
log.Fatalf("Invalid size(limit): %v", value)
}
q.Builder.size = value
q.Limit(value)
q.Offset(q.Builder.page * value)
return q
}

// Drop drops >=1 tables
func (q *QbDB) Drop(tables string) (sql.Result, error) {
query := fmt.Sprintf("%s%s", "DROP TABLE ", tables)
Expand Down
2 changes: 2 additions & 0 deletions qb_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type qbBuilder struct {
isUnionAll bool
offset int64
limit int64
page int64 // support pagination
size int64 // support pagination
lockForUpdate *string
whereExists string
}
Expand Down

0 comments on commit 133c54d

Please sign in to comment.