Skip to content

Instantly share code, notes, and snippets.

@elithrar
Created June 12, 2014 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elithrar/b2497b0b473da64932b5 to your computer and use it in GitHub Desktop.
Save elithrar/b2497b0b473da64932b5 to your computer and use it in GitHub Desktop.
Postgres Performance Issues - listings.go
func GetList(page int) (listings []*Listing, paginate bool, exists bool, err error) {
// Pagination settings
limit := opts.PerPage + 1
offset := (page * opts.PerPage) - opts.PerPage
err = db.Select(&listings, "SELECT * FROM listings WHERE expiry_date > current_date ORDER BY expiry_date DESC OFFSET $1 LIMIT $2", offset, limit)
if err != nil {
return listings, false, false, err
}
// Ensure we have results to return
if len(listings) < 1 {
return listings, false, false, nil
}
// Determine if we have more than one page of results.
// If so, trim the extra result off and set pagination = true
if len(listings) > opts.PerPage {
paginate = true
listings = listings[:opts.PerPage]
}
return listings, paginate, true, nil
}
func (l *Listing) Get() (bool, error) {
// Only retrieve the listing if it has not expired
err := db.Get(l, "SELECT * FROM listings WHERE id = $1 AND expiry_date > current_date", l.Id)
if err != nil {
// Delineate between a driver error and no result
if err == sql.ErrNoRows {
return false, nil
}
return false, err
}
return true, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment