当前位置: 首页>>代码示例>>Golang>>正文


Golang DB.Order方法代码示例

本文整理汇总了Golang中github.com/jinzhu/gorm.DB.Order方法的典型用法代码示例。如果您正苦于以下问题:Golang DB.Order方法的具体用法?Golang DB.Order怎么用?Golang DB.Order使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/jinzhu/gorm.DB的用法示例。


在下文中一共展示了DB.Order方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: IndexSelection

// IndexSelection returns list of comments
func IndexSelection(req *http.Request, params martini.Params, r render.Render, db *gorm.DB) {
	limit := 40
	offset := 0
	query := req.URL.Query()
	rawPage, rawMaxResults := query.Get("page"), query.Get("maxResults")
	page, err := strconv.Atoi(rawPage)
	maxResults, err := strconv.Atoi(rawMaxResults)
	if err != nil {
		r.JSON(400, Error{400, "page and maxResults must be integer."})
	}

	limit = maxResults
	offset = (page - 1) * maxResults

	var comments []Selection
	db.Order("id desc").Limit(limit).Offset(offset).Find(&comments)
	r.JSON(200, comments)
}
开发者ID:Rompei,项目名称:qna,代码行数:19,代码来源:selection_controller.go

示例2: TeachersData

func TeachersData(db *gorm.DB) ([]*models.Faculties, []*models.Departments, []*models.Teachers) {
	var faculties []*models.Faculties
	err := db.Find(&faculties).Error
	if err != nil {
		panic(err)
	}
	var departments []*models.Departments
	err = db.Find(&departments).Error
	if err != nil {
		panic(err)
	}
	var teachers []*models.Teachers
	err = db.Order("last_name").Find(&teachers).Error
	if err != nil {
		panic(err)
	}
	return faculties, departments, teachers
}
开发者ID:rtm7777,项目名称:rozklad_cdtu,代码行数:18,代码来源:shared.go

示例3: GetOrderedWindowedQuery

func GetOrderedWindowedQuery(db *gorm.DB, idfield string, id uint, start uint64, end uint64) *gorm.DB {

	var q *gorm.DB

	switch {
	case start > 0 && end > 0:
		q = db.Where(idfield+" = ? AND timestamp >= ? AND timestamp <= ?", id, start, end)
	case end > 0:
		q = db.Where(idfield+" = ? AND timestamp <= ?", id, end)
	case start > 0:
		q = db.Where(idfield+" = ? AND timestamp >= ?", id, start)
	default:
		q = db.Where(idfield+" = ?", id)
	}

	return q.Order("timestamp")

}
开发者ID:b00lduck,项目名称:raspberry-energy-monitor,代码行数:18,代码来源:common.go

示例4: RefreshStats

func RefreshStats(cache *Cache, db *gorm.DB) (err error) {
	m := &runtime.MemStats{}
	runtime.ReadMemStats(m)

	acq := m.Sys / 1024 / 1024
	used := m.Alloc / 1024 / 1024

	m = nil

	stats.GoRoutines = runtime.NumGoroutine()
	stats.MemoryAcq = int(acq)
	stats.MemoryUsed = int(used)

	stats.CacheMB = cache.GetSizeInMb()
	stats.CacheCount = cache.GetSize()

	stats.Rel = nil
	stats.Ta = nil

	err = db.Order("weight desc").Limit(10).Find(&stats.Ta).Error
	if err != nil {
		return
	}

	err = db.Order("hits desc").Limit(10).Find(&stats.Rel).Error
	if err != nil {
		return
	}

	row := db.Raw(HITCOUNT).Row()
	row.Scan(&stats.HitCount)

	row = db.Raw(TAGCOUNT).Row()
	row.Scan(&stats.TagCount)

	row = db.Raw(RELEASECOUNT).Row()
	row.Scan(&stats.ReleaseCount)

	return
}
开发者ID:Kemonozume,项目名称:nzbcrawler,代码行数:40,代码来源:server.go

示例5: 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)
}
开发者ID:rastasheep,项目名称:utisak-worker,代码行数:24,代码来源:query_params.go

示例6: GetChatLogs

func GetChatLogs(w http.ResponseWriter, r *http.Request) {
	chatLogsTempl, err := template.ParseFiles("views/admin/templates/chatlogs.html")
	if err != nil {
		logrus.Error(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	var messages []*models.ChatMessage
	values := r.URL.Query()

	room, err := strconv.Atoi(values.Get("room"))
	if err != nil && values.Get("room") != "" {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	steamID := values.Get("steamid")
	var from, to time.Time

	if values.Get("from") != "" {
		from, err = timestamp(values.Get("from"))
		if err != nil {
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
	} else {
		from = time.Time{}
	}

	if values.Get("to") != "" {
		to, err = timestamp(values.Get("to"))
		if err != nil {
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
	} else {
		to = time.Now()
	}

	order := values.Get("order")
	var results *gorm.DB

	if values.Get("room") == "" { //Retrieve all messages sent by a specific player
		if steamID == "" {
			http.Error(w, "No Steam ID given.", http.StatusBadRequest)
			return
		}

		playerID := getPlayerID(steamID)
		if playerID == 0 {
			http.Error(w, fmt.Sprintf("Couldn't find player with Steam ID %s", steamID), http.StatusNotFound)
			return
		}

		results = db.DB.Where("player_id = ? AND room = ? AND created_at >= ? AND created_at <= ?", playerID, room, from, to)
	} else if steamID == "" { //Retrieve all messages sent to a specfic room
		results = db.DB.Where("room = ? AND (created_at >= ? AND created_at <= ?)", room, from, to)
	} else { //Retrieve all messages sent to a specific room and a speficic player
		playerID := getPlayerID(steamID)
		if playerID == 0 {
			http.Error(w, fmt.Sprintf("Couldn't find player with Steam ID %s", steamID), http.StatusNotFound)
			return
		}

		results = db.DB.Where("player_id = ? AND room = ? AND created_at >= ? AND created_at <= ?", playerID, room, from, to)
	}

	if order == "Ascending" {
		err = results.Order("id").Find(&messages).Error
	} else if order == "Descending" {
		err = results.Order("id desc").Find(&messages).Error
	}

	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	for _, message := range messages {
		//err := db.DB.DB().QueryRow("SELECT name, profileurl FROM players WHERE id = $1", message.PlayerID).Scan(&message.Player.Name, &message.Player.ProfileURL)
		err := db.DB.DB().QueryRow("SELECT name, profileurl FROM players WHERE id = $1", message.PlayerID).Scan(&message.Player.Name, &message.Player.ProfileURL)
		if err != nil {
			logrus.Warning(err.Error())
		}
	}

	err = chatLogsTempl.Execute(w, messages)
	if err != nil {
		logrus.Error(err.Error())
		return
	}
}
开发者ID:gpittarelli,项目名称:Helen,代码行数:93,代码来源:chatlogs.go

示例7: AllItems

func (uc ItemController) AllItems(db gorm.DB) []model.Element {
	var o []model.Element
	db.Order("id desc").Find(&o)
	return o
}
开发者ID:oblank,项目名称:go-todo,代码行数:5,代码来源:main.go

示例8: GetChatLogs

func GetChatLogs(w http.ResponseWriter, r *http.Request) {
	var messages []*chat.ChatMessage
	values := r.URL.Query()

	room, err := strconv.Atoi(values.Get("room"))
	if err != nil && values.Get("room") != "" {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	steamID := values.Get("steamid")
	var from, to time.Time

	if values.Get("from") != "" { //2006-01-02
		from, err = time.Parse("2006-01-02", values.Get("from"))
		if err != nil {
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
	} else {
		from = time.Time{}
	}

	if values.Get("to") != "" {
		to, err = time.Parse("2006-01-02", values.Get("to"))
		if err != nil {
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
	} else {
		to = time.Now()
	}

	order := values.Get("order")
	var results *gorm.DB

	if values.Get("room") == "" { //Retrieve all messages sent by a specific player
		if steamID == "" {
			http.Error(w, "No Steam ID given.", http.StatusBadRequest)
			return
		}

		playerID := getPlayerID(steamID)
		if playerID == 0 {
			http.Error(w, fmt.Sprintf("Couldn't find player with Steam ID %s", steamID), http.StatusNotFound)
			return
		}

		results = db.DB.Preload("Player").Where("player_id = ? AND room = ? AND created_at >= ? AND created_at <= ?", playerID, room, from, to)
	} else if steamID == "" { //Retrieve all messages sent to a specfic room
		results = db.DB.Preload("Player").Where("room = ? AND (created_at >= ? AND created_at <= ?)", room, from, to)
	} else { //Retrieve all messages sent to a specific room and a speficic player
		playerID := getPlayerID(steamID)
		if playerID == 0 {
			http.Error(w, fmt.Sprintf("Couldn't find player with Steam ID %s", steamID), http.StatusNotFound)
			return
		}

		results = db.DB.Preload("Player").Where("player_id = ? AND room = ? AND created_at >= ? AND created_at <= ?", playerID, room, from, to)
	}

	if order == "Ascending" {
		err = results.Order("id").Find(&messages).Error
	} else if order == "Descending" {
		err = results.Order("id desc").Find(&messages).Error
	}

	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	err = chatLogsTempl.Execute(w, messages)
	if err != nil {
		logrus.Error(err)
	}
}
开发者ID:TF2Stadium,项目名称:Helen,代码行数:77,代码来源:chatlogs.go

示例9: PopularIndex

func PopularIndex(db gorm.DB, r render.Render) {
	media := []models.Media{}
	db.Order("played desc").Limit(15).Find(&media)
	r.JSON(http.StatusOK, media)
}
开发者ID:RadioTerminal,项目名称:podcaster,代码行数:5,代码来源:latest.go

示例10: LatestIndex

func LatestIndex(db gorm.DB, r render.Render) {
	media := []models.Media{}
	db.Order("created_at desc").Limit(15).Find(&media)
	r.JSON(http.StatusOK, media)
}
开发者ID:RadioTerminal,项目名称:podcaster,代码行数:5,代码来源:latest.go

示例11: RetrieveOrdered

func (b *Box) RetrieveOrdered(db *gorm.DB) ([]Box, error) {
	var bs []Box
	err := db.Order("start_date asc").Where(b).Find(&bs, b.Base.BuildQuery()).Error

	return bs, err
}
开发者ID:asvins,项目名称:operations,代码行数:6,代码来源:box.go

示例12: getCurrentQuestionWithTX

// getCurrentQuestionWithTX returns the current question using database transaction.
func getCurrentQuestionWithTX(tx *gorm.DB) (*Question, error) {
	question := &Question{}
	err := tx.Order("started_at").Last(question).Error
	return question, err
}
开发者ID:PepperSalt42,项目名称:api,代码行数:6,代码来源:question.go

示例13: GetAll

/*获取所有记录*/
func (p *Point) GetAll(db gorm.DB) []Point {
	points := []Point{}
	db.Order("create_at desc").Find(&points)
	return points
}
开发者ID:highsoul,项目名称:zmy,代码行数:6,代码来源:point.go


注:本文中的github.com/jinzhu/gorm.DB.Order方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。