當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Query.Count方法代碼示例

本文整理匯總了Golang中google/golang.org/appengine/datastore.Query.Count方法的典型用法代碼示例。如果您正苦於以下問題:Golang Query.Count方法的具體用法?Golang Query.Count怎麽用?Golang Query.Count使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在google/golang.org/appengine/datastore.Query的用法示例。


在下文中一共展示了Query.Count方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Count

// Count returns the number of results for the query.
func (g *Goon) Count(q *datastore.Query) (int, error) {
	return q.Count(g.Context)
}
開發者ID:laco0416,項目名稱:goon,代碼行數:4,代碼來源:query.go

示例2: handler

func handler(w http.ResponseWriter, r *http.Request) {

	c := appengine.NewContext(r)
	u := user.Current(c)
	log.Infof(c, "Got a visitor to the front page!") //keep log in the imports

	//Check if the request is before or after to create the right query
	//The GET requests for the stories will be based around the SubmitDateTime
	//Using "after" will return stories after a certain date from newest to oldest
	//Using "before" will return stories before a certain date from oldest to newest
	//Default is to use the latest 3 submissions

	afterDate := r.FormValue("after")
	beforeDate := r.FormValue("before")
	returnLimit := 3
	showPrevLink := false

	var q *datastore.Query

	if afterDate != "" {
		showPrevLink = true
		//Get the results in descending order for newest to oldest
		afterDate = strings.Replace(afterDate, "%20", " ", -1) //replace all %20 with " "
		ttime, err := time.Parse(DateTimeDatastoreFormat, afterDate)
		if err != nil {
			serveError(c, w, err)
			return
		}
		q = datastore.NewQuery(WebSubmissionEntityName).
			Filter("SubmitDateTime <", ttime).
			Order("-SubmitDateTime").
			Limit(returnLimit)
	} else if beforeDate != "" {
		showPrevLink = true
		//Get the results is ascending order from oldest to newest
		beforeDate = strings.Replace(beforeDate, "%20", " ", -1) //replace all %20 with " "
		ttime, err := time.Parse(DateTimeDatastoreFormat, beforeDate)
		if err != nil {
			serveError(c, w, err)
			return
		}
		q = datastore.NewQuery(WebSubmissionEntityName).
			Filter("SubmitDateTime >", ttime).
			Order("SubmitDateTime").
			Limit(returnLimit)

		//limit check at the beginning if less than the returnLimit redo from the beginning
		length, cerr := q.Count(c)
		if cerr != nil {
			serveError(c, w, cerr)
		}

		//TODO refactor to not duplicate the default query below
		if length < returnLimit {
			showPrevLink = false
			q = datastore.NewQuery(WebSubmissionEntityName).
				Order("-SubmitDateTime").
				Limit(returnLimit)
		}
	} else {
		q = datastore.NewQuery(WebSubmissionEntityName).
			Order("-SubmitDateTime").
			Limit(returnLimit)

	}

	//Populate the results struct and store the keys
	var pageCon PageContainer

	for t := q.Run(c); ; {
		var x WebSubmission
		key, err := t.Next(&x)
		if err == datastore.Done {
			break
		}
		if err != nil {
			//			serveError(c,w,err)
			fmt.Fprintf(w, "nope %v", err.Error())
			return
		}
		if u == nil {
			pageCon.Stories = append(pageCon.Stories, StoryListData{x, key, false})
		} else {
			pageCon.Stories = append(pageCon.Stories, StoryListData{x, key, u.String() == x.SubmitBy})
		}
	}

	//if we filled up the page with results there are probably more, build the
	//next page link
	length, cerr := q.Count(c)
	if cerr != nil {
		serveError(c, w, cerr)
	}
	if length == returnLimit {
		//get the submit datetime of the last story
		pageCon.AfterLink = pageCon.Stories[returnLimit-1].Story.SubmitDateTime.Format(DateTimeDatastoreFormat)
	}

	//If it was a prev page press reverse the result array to get it back into chronological order
	if length >= 1 && beforeDate != "" {
//.........這裏部分代碼省略.........
開發者ID:SiroDiaz,項目名稱:csuf,代碼行數:101,代碼來源:main.go


注:本文中的google/golang.org/appengine/datastore.Query.Count方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。