-
-
Save elithrar/b2497b0b473da64932b5 to your computer and use it in GitHub Desktop.
Postgres Performance Issues - listings.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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