本文整理汇总了Golang中github.com/jinzhu/gorm.DB.Offset方法的典型用法代码示例。如果您正苦于以下问题:Golang DB.Offset方法的具体用法?Golang DB.Offset怎么用?Golang DB.Offset使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/jinzhu/gorm.DB
的用法示例。
在下文中一共展示了DB.Offset方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: AllMediaForDate
func AllMediaForDate(db gorm.DB, page, year, month, day int) []Media {
var media []Media
offset := (page - 1) * ResultsPerRequest
scope := db.Offset(offset).Limit(ResultsPerRequest)
if year > 0 {
scope = scope.Where("date_part('year', taken_at) = ?", year)
if month > 0 {
scope = scope.Where("date_part('month', taken_at) = ?", month)
if day > 0 {
scope = scope.Where("date_part('day', taken_at) = ?", day)
}
}
}
scope.Find(&media)
return media
}
示例2: getJobsFromDB
func getJobsFromDB(db gorm.DB, start, limit int) (map[string]interface{}, int, error) {
jobs := []application.Job{}
jobsMap := make(map[string]interface{})
var total int
dbQuery := db.Offset(start)
if limit > 0 {
dbQuery = dbQuery.Limit(limit)
}
if err := dbQuery.Find(&jobs).Error; err != nil {
return jobsMap, total, err
}
for _, job := range jobs {
id := strconv.FormatUint(uint64(job.ID), 10)
jobsMap[id] = job
}
if err := db.Find(&[]application.Job{}).Count(&total).Error; err != nil {
return jobsMap, total, err
}
return jobsMap, total, nil
}
示例3: getFreelancersFromDB
func getFreelancersFromDB(db gorm.DB, start, limit int) (map[string]interface{}, int, error) {
freelancersMap := make(map[string]interface{})
freelancers := []application.Freelancer{}
var total int
dbQuery := db.Offset(start)
if limit > 0 {
dbQuery = dbQuery.Limit(limit)
}
if err := dbQuery.Find(&freelancers).Error; err != nil {
return freelancersMap, total, err
}
for _, freelancer := range freelancers {
id := strconv.FormatUint(uint64(freelancer.ID), 10)
freelancersMap[id] = freelancer
}
if err := db.Find(&[]application.Freelancer{}).Count(&total).Error; err != nil {
return freelancersMap, total, err
}
return freelancersMap, total, nil
}
示例4: PrepareQuery
func (p *QueryParams) PrepareQuery(searchRelation *gorm.DB) *gorm.DB {
if len(p.Categories) != 0 && !contains(p.Categories, allCat) {
searchRelation = searchRelation.Where("category_slug IN (?)", p.Categories)
}
if p.Page > 1 {
searchRelation = searchRelation.Offset((p.Page - 1) * perPage)
}
if p.Sort == "newest" {
searchRelation = searchRelation.Order("date desc")
} else {
searchRelation = searchRelation.Order("(total_views / POW(((EXTRACT(EPOCH FROM (now()-date)) / 3600)::integer + 2), 1.5)) desc")
}
if !p.StartDate.IsZero() {
searchRelation = searchRelation.Where("date > ?", p.StartDate)
}
if !p.EndDate.IsZero() {
searchRelation = searchRelation.Where("date < ?", p.EndDate)
}
return searchRelation.Limit(perPage)
}
示例5: LoadPhotos
func LoadPhotos(db *gorm.DB, start int, length int) []Photo {
var photos []Photo
db.Offset(start).Limit(length).Find(&photos)
return photos
}
示例6: Unpaginate
func Unpaginate(db *gorm.DB) *gorm.DB {
return db.Offset(-1).Limit(-1)
}