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


Golang Context.SetDB方法代碼示例

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


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

示例1: callScopes

func (s *Searcher) callScopes(context *qor.Context) *qor.Context {
	db := context.GetDB()

	// call default scopes
	for _, scope := range s.Resource.scopes {
		if scope.Default {
			db = scope.Handle(db, context)
		}
	}

	// call scopes
	for _, scope := range s.scopes {
		db = scope.Handle(db, context)
	}

	// call filters
	if s.filters != nil {
		for key, value := range s.filters {
			filter := s.Resource.filters[key]
			if filter != nil && filter.Handler != nil {
				db = filter.Handler(key, value, db, context)
			} else {
				db = DefaultHandler(key, value, db, context)
			}
		}
	}

	// add order by
	if order_by := context.Request.Form.Get("order_by"); order_by != "" {
		if regexp.MustCompile("^[a-zA-Z_]+$").MatchString(order_by) {
			if field, ok := db.NewScope(s.Context.Resource.Value).FieldByName(strings.TrimSuffix(order_by, "_desc")); ok {
				if strings.HasSuffix(order_by, "_desc") {
					db = db.Order(field.DBName+" DESC", true)
				} else {
					db = db.Order(field.DBName, true)
				}
			}
		}
	}

	// call search
	var keyword string
	if keyword = context.Request.Form.Get("keyword"); keyword == "" {
		keyword = context.Request.URL.Query().Get("keyword")
	}
	if keyword != "" && s.Resource.SearchHandler != nil {
		context.SetDB(s.Resource.SearchHandler(keyword, context).Debug())
		return context
	}

	context.SetDB(db)
	return context
}
開發者ID:8legd,項目名稱:qor-qor,代碼行數:53,代碼來源:searcher.go

示例2: callScopes

func (s *Searcher) callScopes(context *qor.Context) *qor.Context {
	db := context.GetDB()

	// call default scopes
	for _, scope := range s.Resource.scopes {
		if scope.Default {
			db = scope.Handle(db, context)
		}
	}

	// call scopes
	for _, scope := range s.scopes {
		db = scope.Handle(db, context)
	}

	// call filters
	if s.filters != nil {
		for key, value := range s.filters {
			filter := s.Resource.filters[key]
			if filter != nil && filter.Handler != nil {
				db = filter.Handler(key, value, db, context)
			} else {
				db = DefaultHandler(key, value, db, context)
			}
		}
	}

	// add order by
	if order_by := context.Request.Form.Get("order_by"); order_by != "" {
		if regexp.MustCompile("^[a-zA-Z_]+$").MatchString(order_by) {
			if strings.HasSuffix(order_by, "_desc") {
				db = db.Order(strings.TrimSuffix(order_by, "_desc")+" DESC", true)
			} else {
				db = db.Order(order_by, true)
			}
		}
	}

	// call search
	if keyword := context.Request.Form.Get("keyword"); keyword != "" {
		if s.Resource.SearchHandler != nil {
			context.SetDB(s.Resource.SearchHandler(keyword, context))
			return context
		}
	}

	context.SetDB(db)
	return context
}
開發者ID:pusparajm,項目名稱:qor,代碼行數:49,代碼來源:searcher.go

示例3: callScopes

func (s *Searcher) callScopes(context *qor.Context) *qor.Context {
	db := context.GetDB()

	// call default scopes
	for _, scope := range s.Resource.scopes {
		if scope.Default {
			db = scope.Handle(db, context)
		}
	}

	// call scopes
	for _, scope := range s.scopes {
		db = scope.Handle(db, context)
	}

	// call filters
	if s.filters != nil {
		for key, value := range s.filters {
			filter := s.Resource.filters[key]
			if filter != nil && filter.Handler != nil {
				db = filter.Handler(key, value, db, context)
			} else {
				db = DefaultHandler(key, value, db, context)
			}
		}
	}

	context.SetDB(db)

	// call search
	if keyword := context.Request.Form.Get("keyword"); keyword != "" {
		if s.Resource.SearchHandler != nil {
			context.SetDB(s.Resource.SearchHandler(keyword, context))
		}
	}
	return context
}
開發者ID:nilslice,項目名稱:qor,代碼行數:37,代碼來源:searcher.go


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