当前位置: 首页>>代码示例>>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;未经允许,请勿转载。