Add SubQuery method

This commit is contained in:
Jinzhu 2018-02-22 12:04:12 +08:00
parent 58e34726df
commit 48a20a6e9f
1 changed files with 9 additions and 0 deletions

View File

@ -177,6 +177,15 @@ func (s *DB) QueryExpr() *expr {
return Expr(scope.SQL, scope.SQLVars...) return Expr(scope.SQL, scope.SQLVars...)
} }
// SubQuery returns the query as sub query
func (s *DB) SubQuery() *expr {
scope := s.NewScope(s.Value)
scope.InstanceSet("skip_bindvar", true)
scope.prepareQuerySQL()
return Expr(fmt.Sprintf("(%v)", scope.SQL), scope.SQLVars...)
}
// Where return a new relation, filter records with given conditions, accepts `map`, `struct` or `string` as conditions, refer http://jinzhu.github.io/gorm/crud.html#query // Where return a new relation, filter records with given conditions, accepts `map`, `struct` or `string` as conditions, refer http://jinzhu.github.io/gorm/crud.html#query
func (s *DB) Where(query interface{}, args ...interface{}) *DB { func (s *DB) Where(query interface{}, args ...interface{}) *DB {
return s.clone().search.Where(query, args...).db return s.clone().search.Where(query, args...).db