mirror of https://github.com/go-gorm/gorm.git
Refactor Search API
This commit is contained in:
parent
9f2959959b
commit
6e5d46bf37
|
@ -8,7 +8,7 @@ func BeforeDelete(scope *Scope) {
|
||||||
|
|
||||||
func Delete(scope *Scope) {
|
func Delete(scope *Scope) {
|
||||||
if !scope.HasError() {
|
if !scope.HasError() {
|
||||||
if !scope.Search.Unscope && scope.HasColumn("DeletedAt") {
|
if !scope.Search.unscoped && scope.HasColumn("DeletedAt") {
|
||||||
scope.Raw(
|
scope.Raw(
|
||||||
fmt.Sprintf("UPDATE %v SET deleted_at=%v %v",
|
fmt.Sprintf("UPDATE %v SET deleted_at=%v %v",
|
||||||
scope.QuotedTableName(),
|
scope.QuotedTableName(),
|
||||||
|
|
|
@ -23,7 +23,7 @@ func Query(scope *Scope) {
|
||||||
|
|
||||||
if orderBy, ok := scope.InstanceGet("gorm:order_by_primary_key"); ok {
|
if orderBy, ok := scope.InstanceGet("gorm:order_by_primary_key"); ok {
|
||||||
if primaryKey := scope.PrimaryKey(); primaryKey != "" {
|
if primaryKey := scope.PrimaryKey(); primaryKey != "" {
|
||||||
scope.Search = scope.Search.clone().order(fmt.Sprintf("%v.%v %v", scope.QuotedTableName(), primaryKey, orderBy))
|
scope.Search.Order(fmt.Sprintf("%v.%v %v", scope.QuotedTableName(), primaryKey, orderBy))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
44
main.go
44
main.go
|
@ -94,7 +94,7 @@ func (s *DB) New() *DB {
|
||||||
func (db *DB) NewScope(value interface{}) *Scope {
|
func (db *DB) NewScope(value interface{}) *Scope {
|
||||||
dbClone := db.clone()
|
dbClone := db.clone()
|
||||||
dbClone.Value = value
|
dbClone.Value = value
|
||||||
return &Scope{db: dbClone, Search: dbClone.search, Value: value}
|
return &Scope{db: dbClone, Search: dbClone.search.clone(), Value: value}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommonDB Return the underlying sql.DB or sql.Tx instance.
|
// CommonDB Return the underlying sql.DB or sql.Tx instance.
|
||||||
|
@ -128,43 +128,43 @@ func (s *DB) SingularTable(enable bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) Or(query interface{}, args ...interface{}) *DB {
|
func (s *DB) Or(query interface{}, args ...interface{}) *DB {
|
||||||
return s.clone().search.or(query, args...).db
|
return s.clone().search.Or(query, args...).db
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) Not(query interface{}, args ...interface{}) *DB {
|
func (s *DB) Not(query interface{}, args ...interface{}) *DB {
|
||||||
return s.clone().search.not(query, args...).db
|
return s.clone().search.Not(query, args...).db
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) Limit(value interface{}) *DB {
|
func (s *DB) Limit(value interface{}) *DB {
|
||||||
return s.clone().search.limit(value).db
|
return s.clone().search.Limit(value).db
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) Offset(value interface{}) *DB {
|
func (s *DB) Offset(value interface{}) *DB {
|
||||||
return s.clone().search.offset(value).db
|
return s.clone().search.Offset(value).db
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) Order(value string, reorder ...bool) *DB {
|
func (s *DB) Order(value string, reorder ...bool) *DB {
|
||||||
return s.clone().search.order(value, reorder...).db
|
return s.clone().search.Order(value, reorder...).db
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) Select(query interface{}, args ...interface{}) *DB {
|
func (s *DB) Select(query interface{}, args ...interface{}) *DB {
|
||||||
return s.clone().search.selects(query, args...).db
|
return s.clone().search.Selects(query, args...).db
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) Group(query string) *DB {
|
func (s *DB) Group(query string) *DB {
|
||||||
return s.clone().search.group(query).db
|
return s.clone().search.Group(query).db
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) Having(query string, values ...interface{}) *DB {
|
func (s *DB) Having(query string, values ...interface{}) *DB {
|
||||||
return s.clone().search.having(query, values...).db
|
return s.clone().search.Having(query, values...).db
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) Joins(query string) *DB {
|
func (s *DB) Joins(query string) *DB {
|
||||||
return s.clone().search.joins(query).db
|
return s.clone().search.Joins(query).db
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) Scopes(funcs ...func(*DB) *DB) *DB {
|
func (s *DB) Scopes(funcs ...func(*DB) *DB) *DB {
|
||||||
|
@ -175,27 +175,27 @@ func (s *DB) Scopes(funcs ...func(*DB) *DB) *DB {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) Unscoped() *DB {
|
func (s *DB) Unscoped() *DB {
|
||||||
return s.clone().search.unscoped().db
|
return s.clone().search.Unscoped().db
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) Attrs(attrs ...interface{}) *DB {
|
func (s *DB) Attrs(attrs ...interface{}) *DB {
|
||||||
return s.clone().search.attrs(attrs...).db
|
return s.clone().search.Attrs(attrs...).db
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) Assign(attrs ...interface{}) *DB {
|
func (s *DB) Assign(attrs ...interface{}) *DB {
|
||||||
return s.clone().search.assign(attrs...).db
|
return s.clone().search.Assign(attrs...).db
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) First(out interface{}, where ...interface{}) *DB {
|
func (s *DB) First(out interface{}, where ...interface{}) *DB {
|
||||||
newScope := s.clone().NewScope(out)
|
newScope := s.clone().NewScope(out)
|
||||||
newScope.Search = newScope.Search.clone().limit(1)
|
newScope.Search.Limit(1)
|
||||||
return newScope.InstanceSet("gorm:order_by_primary_key", "ASC").
|
return newScope.InstanceSet("gorm:order_by_primary_key", "ASC").
|
||||||
inlineCondition(where...).callCallbacks(s.parent.callback.queries).db
|
inlineCondition(where...).callCallbacks(s.parent.callback.queries).db
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) Last(out interface{}, where ...interface{}) *DB {
|
func (s *DB) Last(out interface{}, where ...interface{}) *DB {
|
||||||
newScope := s.clone().NewScope(out)
|
newScope := s.clone().NewScope(out)
|
||||||
newScope.Search = newScope.Search.clone().limit(1)
|
newScope.Search.Limit(1)
|
||||||
return newScope.InstanceSet("gorm:order_by_primary_key", "DESC").
|
return newScope.InstanceSet("gorm:order_by_primary_key", "DESC").
|
||||||
inlineCondition(where...).callCallbacks(s.parent.callback.queries).db
|
inlineCondition(where...).callCallbacks(s.parent.callback.queries).db
|
||||||
}
|
}
|
||||||
|
@ -226,7 +226,7 @@ func (s *DB) FirstOrInit(out interface{}, where ...interface{}) *DB {
|
||||||
}
|
}
|
||||||
c.NewScope(out).inlineCondition(where...).initialize()
|
c.NewScope(out).inlineCondition(where...).initialize()
|
||||||
} else {
|
} else {
|
||||||
c.NewScope(out).updatedAttrsWithValues(convertInterfaceToMap(s.search.AssignAttrs), false)
|
c.NewScope(out).updatedAttrsWithValues(convertInterfaceToMap(s.search.assignAttrs), false)
|
||||||
}
|
}
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
@ -238,8 +238,8 @@ func (s *DB) FirstOrCreate(out interface{}, where ...interface{}) *DB {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
c.NewScope(out).inlineCondition(where...).initialize().callCallbacks(s.parent.callback.creates)
|
c.NewScope(out).inlineCondition(where...).initialize().callCallbacks(s.parent.callback.creates)
|
||||||
} else if len(c.search.AssignAttrs) > 0 {
|
} else if len(c.search.assignAttrs) > 0 {
|
||||||
c.NewScope(out).InstanceSet("gorm:update_interface", s.search.AssignAttrs).callCallbacks(s.parent.callback.updates)
|
c.NewScope(out).InstanceSet("gorm:update_interface", s.search.assignAttrs).callCallbacks(s.parent.callback.updates)
|
||||||
}
|
}
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
@ -284,7 +284,7 @@ func (s *DB) Delete(value interface{}, where ...interface{}) *DB {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) Raw(sql string, values ...interface{}) *DB {
|
func (s *DB) Raw(sql string, values ...interface{}) *DB {
|
||||||
return s.clone().search.raw(true).where(sql, values...).db
|
return s.clone().search.Raw(true).Where(sql, values...).db
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) Exec(sql string, values ...interface{}) *DB {
|
func (s *DB) Exec(sql string, values ...interface{}) *DB {
|
||||||
|
@ -315,7 +315,7 @@ func (s *DB) Count(value interface{}) *DB {
|
||||||
|
|
||||||
func (s *DB) Table(name string) *DB {
|
func (s *DB) Table(name string) *DB {
|
||||||
clone := s.clone()
|
clone := s.clone()
|
||||||
clone.search.table(name)
|
clone.search.Table(name)
|
||||||
clone.Value = nil
|
clone.Value = nil
|
||||||
return clone
|
return clone
|
||||||
}
|
}
|
||||||
|
@ -447,7 +447,7 @@ func (s *DB) Association(column string) *Association {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) Preload(column string, conditions ...interface{}) *DB {
|
func (s *DB) Preload(column string, conditions ...interface{}) *DB {
|
||||||
return s.clone().search.preload(column, conditions...).db
|
return s.clone().search.Preload(column, conditions...).db
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set set value by name
|
// Set set value by name
|
||||||
|
|
|
@ -23,8 +23,8 @@ func Preload(scope *Scope) {
|
||||||
fields := scope.Fields()
|
fields := scope.Fields()
|
||||||
isSlice := scope.IndirectValue().Kind() == reflect.Slice
|
isSlice := scope.IndirectValue().Kind() == reflect.Slice
|
||||||
|
|
||||||
if scope.Search.Preload != nil {
|
if scope.Search.preload != nil {
|
||||||
for key, conditions := range scope.Search.Preload {
|
for key, conditions := range scope.Search.preload {
|
||||||
for _, field := range fields {
|
for _, field := range fields {
|
||||||
if field.Name == key && field.Relationship != nil {
|
if field.Name == key && field.Relationship != nil {
|
||||||
results := makeSlice(field.Struct.Type)
|
results := makeSlice(field.Struct.Type)
|
||||||
|
|
16
scope.go
16
scope.go
|
@ -10,15 +10,15 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Scope struct {
|
type Scope struct {
|
||||||
Value interface{}
|
|
||||||
indirectValue *reflect.Value
|
|
||||||
Search *search
|
Search *search
|
||||||
|
Value interface{}
|
||||||
Sql string
|
Sql string
|
||||||
SqlVars []interface{}
|
SqlVars []interface{}
|
||||||
db *DB
|
db *DB
|
||||||
skipLeft bool
|
indirectValue *reflect.Value
|
||||||
primaryKeyField *Field
|
|
||||||
instanceId string
|
instanceId string
|
||||||
|
primaryKeyField *Field
|
||||||
|
skipLeft bool
|
||||||
fields map[string]*Field
|
fields map[string]*Field
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -225,15 +225,15 @@ func (scope *Scope) AddToVars(value interface{}) string {
|
||||||
|
|
||||||
// TableName get table name
|
// TableName get table name
|
||||||
func (scope *Scope) TableName() string {
|
func (scope *Scope) TableName() string {
|
||||||
if scope.Search != nil && len(scope.Search.TableName) > 0 {
|
if scope.Search != nil && len(scope.Search.tableName) > 0 {
|
||||||
return scope.Search.TableName
|
return scope.Search.tableName
|
||||||
}
|
}
|
||||||
return scope.GetModelStruct().TableName
|
return scope.GetModelStruct().TableName
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) QuotedTableName() (name string) {
|
func (scope *Scope) QuotedTableName() (name string) {
|
||||||
if scope.Search != nil && len(scope.Search.TableName) > 0 {
|
if scope.Search != nil && len(scope.Search.tableName) > 0 {
|
||||||
return scope.Quote(scope.Search.TableName)
|
return scope.Quote(scope.Search.tableName)
|
||||||
} else {
|
} else {
|
||||||
return scope.Quote(scope.TableName())
|
return scope.Quote(scope.TableName())
|
||||||
}
|
}
|
||||||
|
|
|
@ -159,7 +159,7 @@ func (scope *Scope) buildSelectQuery(clause map[string]interface{}) (str string)
|
||||||
func (scope *Scope) whereSql() (sql string) {
|
func (scope *Scope) whereSql() (sql string) {
|
||||||
var primaryConditions, andConditions, orConditions []string
|
var primaryConditions, andConditions, orConditions []string
|
||||||
|
|
||||||
if !scope.Search.Unscope && scope.Fields()["deleted_at"] != nil {
|
if !scope.Search.unscoped && scope.Fields()["deleted_at"] != nil {
|
||||||
sql := fmt.Sprintf("(%v.deleted_at IS NULL OR %v.deleted_at <= '0001-01-02')", scope.QuotedTableName(), scope.QuotedTableName())
|
sql := fmt.Sprintf("(%v.deleted_at IS NULL OR %v.deleted_at <= '0001-01-02')", scope.QuotedTableName(), scope.QuotedTableName())
|
||||||
primaryConditions = append(primaryConditions, sql)
|
primaryConditions = append(primaryConditions, sql)
|
||||||
}
|
}
|
||||||
|
@ -168,19 +168,19 @@ func (scope *Scope) whereSql() (sql string) {
|
||||||
primaryConditions = append(primaryConditions, scope.primaryCondition(scope.AddToVars(scope.PrimaryKeyValue())))
|
primaryConditions = append(primaryConditions, scope.primaryCondition(scope.AddToVars(scope.PrimaryKeyValue())))
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, clause := range scope.Search.WhereConditions {
|
for _, clause := range scope.Search.whereConditions {
|
||||||
if sql := scope.buildWhereCondition(clause); sql != "" {
|
if sql := scope.buildWhereCondition(clause); sql != "" {
|
||||||
andConditions = append(andConditions, sql)
|
andConditions = append(andConditions, sql)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, clause := range scope.Search.OrConditions {
|
for _, clause := range scope.Search.orConditions {
|
||||||
if sql := scope.buildWhereCondition(clause); sql != "" {
|
if sql := scope.buildWhereCondition(clause); sql != "" {
|
||||||
orConditions = append(orConditions, sql)
|
orConditions = append(orConditions, sql)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, clause := range scope.Search.NotConditions {
|
for _, clause := range scope.Search.notConditions {
|
||||||
if sql := scope.buildNotCondition(clause); sql != "" {
|
if sql := scope.buildNotCondition(clause); sql != "" {
|
||||||
andConditions = append(andConditions, sql)
|
andConditions = append(andConditions, sql)
|
||||||
}
|
}
|
||||||
|
@ -208,76 +208,76 @@ func (scope *Scope) whereSql() (sql string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) selectSql() string {
|
func (scope *Scope) selectSql() string {
|
||||||
if len(scope.Search.Selects) == 0 {
|
if len(scope.Search.selects) == 0 {
|
||||||
return "*"
|
return "*"
|
||||||
}
|
}
|
||||||
return scope.buildSelectQuery(scope.Search.Selects)
|
return scope.buildSelectQuery(scope.Search.selects)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) orderSql() string {
|
func (scope *Scope) orderSql() string {
|
||||||
if len(scope.Search.Orders) == 0 {
|
if len(scope.Search.orders) == 0 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return " ORDER BY " + strings.Join(scope.Search.Orders, ",")
|
return " ORDER BY " + strings.Join(scope.Search.orders, ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) limitSql() string {
|
func (scope *Scope) limitSql() string {
|
||||||
if !scope.Dialect().HasTop() {
|
if !scope.Dialect().HasTop() {
|
||||||
if len(scope.Search.Limit) == 0 {
|
if len(scope.Search.limit) == 0 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return " LIMIT " + scope.Search.Limit
|
return " LIMIT " + scope.Search.limit
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) topSql() string {
|
func (scope *Scope) topSql() string {
|
||||||
if scope.Dialect().HasTop() && len(scope.Search.Offset) == 0 {
|
if scope.Dialect().HasTop() && len(scope.Search.offset) == 0 {
|
||||||
if len(scope.Search.Limit) == 0 {
|
if len(scope.Search.limit) == 0 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return " TOP(" + scope.Search.Limit + ")"
|
return " TOP(" + scope.Search.limit + ")"
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) offsetSql() string {
|
func (scope *Scope) offsetSql() string {
|
||||||
if len(scope.Search.Offset) == 0 {
|
if len(scope.Search.offset) == 0 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
if scope.Dialect().HasTop() {
|
if scope.Dialect().HasTop() {
|
||||||
sql := " OFFSET " + scope.Search.Offset + " ROW "
|
sql := " OFFSET " + scope.Search.offset + " ROW "
|
||||||
if len(scope.Search.Limit) > 0 {
|
if len(scope.Search.limit) > 0 {
|
||||||
sql += "FETCH NEXT " + scope.Search.Limit + " ROWS ONLY"
|
sql += "FETCH NEXT " + scope.Search.limit + " ROWS ONLY"
|
||||||
}
|
}
|
||||||
return sql
|
return sql
|
||||||
}
|
}
|
||||||
return " OFFSET " + scope.Search.Offset
|
return " OFFSET " + scope.Search.offset
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) groupSql() string {
|
func (scope *Scope) groupSql() string {
|
||||||
if len(scope.Search.Group) == 0 {
|
if len(scope.Search.group) == 0 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return " GROUP BY " + scope.Search.Group
|
return " GROUP BY " + scope.Search.group
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) havingSql() string {
|
func (scope *Scope) havingSql() string {
|
||||||
if scope.Search.HavingCondition == nil {
|
if scope.Search.havingCondition == nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return " HAVING " + scope.buildWhereCondition(scope.Search.HavingCondition)
|
return " HAVING " + scope.buildWhereCondition(scope.Search.havingCondition)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) joinsSql() string {
|
func (scope *Scope) joinsSql() string {
|
||||||
return scope.Search.Joins + " "
|
return scope.Search.joins + " "
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) prepareQuerySql() {
|
func (scope *Scope) prepareQuerySql() {
|
||||||
if scope.Search.Raw {
|
if scope.Search.raw {
|
||||||
scope.Raw(strings.TrimSuffix(strings.TrimPrefix(scope.CombinedConditionSql(), " WHERE ("), ")"))
|
scope.Raw(strings.TrimSuffix(strings.TrimPrefix(scope.CombinedConditionSql(), " WHERE ("), ")"))
|
||||||
} else {
|
} else {
|
||||||
scope.Raw(fmt.Sprintf("SELECT %v %v FROM %v %v", scope.topSql(), scope.selectSql(), scope.QuotedTableName(), scope.CombinedConditionSql()))
|
scope.Raw(fmt.Sprintf("SELECT %v %v FROM %v %v", scope.topSql(), scope.selectSql(), scope.QuotedTableName(), scope.CombinedConditionSql()))
|
||||||
|
@ -287,7 +287,7 @@ func (scope *Scope) prepareQuerySql() {
|
||||||
|
|
||||||
func (scope *Scope) inlineCondition(values ...interface{}) *Scope {
|
func (scope *Scope) inlineCondition(values ...interface{}) *Scope {
|
||||||
if len(values) > 0 {
|
if len(values) > 0 {
|
||||||
scope.Search = scope.Search.clone().where(values[0], values[1:]...)
|
scope.Search.Where(values[0], values[1:]...)
|
||||||
}
|
}
|
||||||
return scope
|
return scope
|
||||||
}
|
}
|
||||||
|
@ -348,17 +348,17 @@ func (scope *Scope) rows() (*sql.Rows, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) initialize() *Scope {
|
func (scope *Scope) initialize() *Scope {
|
||||||
for _, clause := range scope.Search.WhereConditions {
|
for _, clause := range scope.Search.whereConditions {
|
||||||
scope.updatedAttrsWithValues(convertInterfaceToMap(clause["query"]), false)
|
scope.updatedAttrsWithValues(convertInterfaceToMap(clause["query"]), false)
|
||||||
}
|
}
|
||||||
scope.updatedAttrsWithValues(convertInterfaceToMap(scope.Search.InitAttrs), false)
|
scope.updatedAttrsWithValues(convertInterfaceToMap(scope.Search.initAttrs), false)
|
||||||
scope.updatedAttrsWithValues(convertInterfaceToMap(scope.Search.AssignAttrs), false)
|
scope.updatedAttrsWithValues(convertInterfaceToMap(scope.Search.assignAttrs), false)
|
||||||
return scope
|
return scope
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) pluck(column string, value interface{}) *Scope {
|
func (scope *Scope) pluck(column string, value interface{}) *Scope {
|
||||||
dest := reflect.Indirect(reflect.ValueOf(value))
|
dest := reflect.Indirect(reflect.ValueOf(value))
|
||||||
scope.Search = scope.Search.clone().selects(column)
|
scope.Search.Selects(column)
|
||||||
if dest.Kind() != reflect.Slice {
|
if dest.Kind() != reflect.Slice {
|
||||||
scope.Err(errors.New("results should be a slice"))
|
scope.Err(errors.New("results should be a slice"))
|
||||||
return scope
|
return scope
|
||||||
|
@ -377,7 +377,7 @@ func (scope *Scope) pluck(column string, value interface{}) *Scope {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) count(value interface{}) *Scope {
|
func (scope *Scope) count(value interface{}) *Scope {
|
||||||
scope.Search = scope.Search.clone().selects("count(*)")
|
scope.Search.Selects("count(*)")
|
||||||
scope.Err(scope.row().Scan(value))
|
scope.Err(scope.row().Scan(value))
|
||||||
return scope
|
return scope
|
||||||
}
|
}
|
||||||
|
|
134
search.go
134
search.go
|
@ -4,129 +4,129 @@ import "fmt"
|
||||||
|
|
||||||
type search struct {
|
type search struct {
|
||||||
db *DB
|
db *DB
|
||||||
WhereConditions []map[string]interface{}
|
whereConditions []map[string]interface{}
|
||||||
OrConditions []map[string]interface{}
|
orConditions []map[string]interface{}
|
||||||
NotConditions []map[string]interface{}
|
notConditions []map[string]interface{}
|
||||||
HavingCondition map[string]interface{}
|
havingCondition map[string]interface{}
|
||||||
InitAttrs []interface{}
|
initAttrs []interface{}
|
||||||
AssignAttrs []interface{}
|
assignAttrs []interface{}
|
||||||
Selects map[string]interface{}
|
selects map[string]interface{}
|
||||||
Orders []string
|
orders []string
|
||||||
Joins string
|
joins string
|
||||||
Preload map[string][]interface{}
|
preload map[string][]interface{}
|
||||||
Offset string
|
offset string
|
||||||
Limit string
|
limit string
|
||||||
Group string
|
group string
|
||||||
TableName string
|
tableName string
|
||||||
Unscope bool
|
unscoped bool
|
||||||
Raw bool
|
raw bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *search) clone() *search {
|
func (s *search) clone() *search {
|
||||||
return &search{
|
return &search{
|
||||||
Preload: s.Preload,
|
preload: s.preload,
|
||||||
WhereConditions: s.WhereConditions,
|
whereConditions: s.whereConditions,
|
||||||
OrConditions: s.OrConditions,
|
orConditions: s.orConditions,
|
||||||
NotConditions: s.NotConditions,
|
notConditions: s.notConditions,
|
||||||
HavingCondition: s.HavingCondition,
|
havingCondition: s.havingCondition,
|
||||||
InitAttrs: s.InitAttrs,
|
initAttrs: s.initAttrs,
|
||||||
AssignAttrs: s.AssignAttrs,
|
assignAttrs: s.assignAttrs,
|
||||||
Selects: s.Selects,
|
selects: s.selects,
|
||||||
Orders: s.Orders,
|
orders: s.orders,
|
||||||
Joins: s.Joins,
|
joins: s.joins,
|
||||||
Offset: s.Offset,
|
offset: s.offset,
|
||||||
Limit: s.Limit,
|
limit: s.limit,
|
||||||
Group: s.Group,
|
group: s.group,
|
||||||
TableName: s.TableName,
|
tableName: s.tableName,
|
||||||
Unscope: s.Unscope,
|
unscoped: s.unscoped,
|
||||||
Raw: s.Raw,
|
raw: s.raw,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *search) where(query interface{}, values ...interface{}) *search {
|
func (s *search) Where(query interface{}, values ...interface{}) *search {
|
||||||
s.WhereConditions = append(s.WhereConditions, map[string]interface{}{"query": query, "args": values})
|
s.whereConditions = append(s.whereConditions, map[string]interface{}{"query": query, "args": values})
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *search) not(query interface{}, values ...interface{}) *search {
|
func (s *search) Not(query interface{}, values ...interface{}) *search {
|
||||||
s.NotConditions = append(s.NotConditions, map[string]interface{}{"query": query, "args": values})
|
s.notConditions = append(s.notConditions, map[string]interface{}{"query": query, "args": values})
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *search) or(query interface{}, values ...interface{}) *search {
|
func (s *search) Or(query interface{}, values ...interface{}) *search {
|
||||||
s.OrConditions = append(s.OrConditions, map[string]interface{}{"query": query, "args": values})
|
s.orConditions = append(s.orConditions, map[string]interface{}{"query": query, "args": values})
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *search) attrs(attrs ...interface{}) *search {
|
func (s *search) Attrs(attrs ...interface{}) *search {
|
||||||
s.InitAttrs = append(s.InitAttrs, toSearchableMap(attrs...))
|
s.initAttrs = append(s.initAttrs, toSearchableMap(attrs...))
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *search) assign(attrs ...interface{}) *search {
|
func (s *search) Assign(attrs ...interface{}) *search {
|
||||||
s.AssignAttrs = append(s.AssignAttrs, toSearchableMap(attrs...))
|
s.assignAttrs = append(s.assignAttrs, toSearchableMap(attrs...))
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *search) order(value string, reorder ...bool) *search {
|
func (s *search) Order(value string, reorder ...bool) *search {
|
||||||
if len(reorder) > 0 && reorder[0] {
|
if len(reorder) > 0 && reorder[0] {
|
||||||
s.Orders = []string{value}
|
s.orders = []string{value}
|
||||||
} else {
|
} else {
|
||||||
s.Orders = append(s.Orders, value)
|
s.orders = append(s.orders, value)
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *search) selects(query interface{}, args ...interface{}) *search {
|
func (s *search) Selects(query interface{}, args ...interface{}) *search {
|
||||||
s.Selects = map[string]interface{}{"query": query, "args": args}
|
s.selects = map[string]interface{}{"query": query, "args": args}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *search) limit(value interface{}) *search {
|
func (s *search) Limit(value interface{}) *search {
|
||||||
s.Limit = s.getInterfaceAsSql(value)
|
s.limit = s.getInterfaceAsSql(value)
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *search) offset(value interface{}) *search {
|
func (s *search) Offset(value interface{}) *search {
|
||||||
s.Offset = s.getInterfaceAsSql(value)
|
s.offset = s.getInterfaceAsSql(value)
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *search) group(query string) *search {
|
func (s *search) Group(query string) *search {
|
||||||
s.Group = s.getInterfaceAsSql(query)
|
s.group = s.getInterfaceAsSql(query)
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *search) having(query string, values ...interface{}) *search {
|
func (s *search) Having(query string, values ...interface{}) *search {
|
||||||
s.HavingCondition = map[string]interface{}{"query": query, "args": values}
|
s.havingCondition = map[string]interface{}{"query": query, "args": values}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *search) joins(query string) *search {
|
func (s *search) Joins(query string) *search {
|
||||||
s.Joins = query
|
s.joins = query
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *search) preload(column string, values ...interface{}) *search {
|
func (s *search) Preload(column string, values ...interface{}) *search {
|
||||||
if s.Preload == nil {
|
if s.preload == nil {
|
||||||
s.Preload = map[string][]interface{}{}
|
s.preload = map[string][]interface{}{}
|
||||||
}
|
}
|
||||||
s.Preload[column] = values
|
s.preload[column] = values
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *search) raw(b bool) *search {
|
func (s *search) Raw(b bool) *search {
|
||||||
s.Raw = b
|
s.raw = b
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *search) unscoped() *search {
|
func (s *search) Unscoped() *search {
|
||||||
s.Unscope = true
|
s.unscoped = true
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *search) table(name string) *search {
|
func (s *search) Table(name string) *search {
|
||||||
s.TableName = name
|
s.tableName = name
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,20 +7,20 @@ import (
|
||||||
|
|
||||||
func TestCloneSearch(t *testing.T) {
|
func TestCloneSearch(t *testing.T) {
|
||||||
s := new(search)
|
s := new(search)
|
||||||
s.where("name = ?", "jinzhu").order("name").attrs("name", "jinzhu").selects("name, age")
|
s.Where("name = ?", "jinzhu").Order("name").Attrs("name", "jinzhu").Selects("name, age")
|
||||||
|
|
||||||
s1 := s.clone()
|
s1 := s.clone()
|
||||||
s1.where("age = ?", 20).order("age").attrs("email", "a@e.org").selects("email")
|
s1.Where("age = ?", 20).Order("age").Attrs("email", "a@e.org").Selects("email")
|
||||||
|
|
||||||
if reflect.DeepEqual(s.WhereConditions, s1.WhereConditions) {
|
if reflect.DeepEqual(s.whereConditions, s1.whereConditions) {
|
||||||
t.Errorf("Where should be copied")
|
t.Errorf("Where should be copied")
|
||||||
}
|
}
|
||||||
|
|
||||||
if reflect.DeepEqual(s.Orders, s1.Orders) {
|
if reflect.DeepEqual(s.orders, s1.orders) {
|
||||||
t.Errorf("Order should be copied")
|
t.Errorf("Order should be copied")
|
||||||
}
|
}
|
||||||
|
|
||||||
if reflect.DeepEqual(s.InitAttrs, s1.InitAttrs) {
|
if reflect.DeepEqual(s.initAttrs, s1.initAttrs) {
|
||||||
t.Errorf("InitAttrs should be copied")
|
t.Errorf("InitAttrs should be copied")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue