2014-01-26 08:41:37 +04:00
|
|
|
package gorm
|
|
|
|
|
2014-01-26 09:51:23 +04:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/jinzhu/gorm/dialect"
|
|
|
|
|
|
|
|
"reflect"
|
|
|
|
)
|
2014-01-26 08:41:37 +04:00
|
|
|
|
|
|
|
type Scope struct {
|
2014-01-26 09:51:23 +04:00
|
|
|
Value interface{}
|
2014-01-26 08:41:37 +04:00
|
|
|
Search *search
|
|
|
|
Sql string
|
|
|
|
SqlVars []interface{}
|
|
|
|
db *DB
|
|
|
|
}
|
|
|
|
|
2014-01-26 09:51:23 +04:00
|
|
|
func (db *DB) newScope(value interface{}) *Scope {
|
|
|
|
return &Scope{db: db, Search: db.search, Value: value}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (scope *Scope) callCallbacks(funcs []*func(s *Scope)) *Scope {
|
|
|
|
for _, f := range funcs {
|
|
|
|
(*f)(scope)
|
|
|
|
}
|
|
|
|
return scope
|
|
|
|
}
|
|
|
|
|
2014-01-26 08:41:37 +04:00
|
|
|
func (scope *Scope) DB() sqlCommon {
|
|
|
|
return scope.db.db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (scope *Scope) Dialect() dialect.Dialect {
|
|
|
|
return scope.db.parent.dialect
|
|
|
|
}
|
|
|
|
|
|
|
|
func (scope *Scope) Err(err error) error {
|
|
|
|
if err != nil {
|
|
|
|
scope.db.err(err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (scope *Scope) HasError() bool {
|
2014-01-26 09:51:23 +04:00
|
|
|
return scope.db.hasError()
|
2014-01-26 08:41:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (scope *Scope) PrimaryKey() string {
|
2014-01-26 09:51:23 +04:00
|
|
|
return "Id"
|
2014-01-26 08:41:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (scope *Scope) HasColumn(name string) bool {
|
2014-01-26 09:51:23 +04:00
|
|
|
data := reflect.Indirect(reflect.ValueOf(scope.Value))
|
|
|
|
|
|
|
|
if data.Kind() == reflect.Struct {
|
|
|
|
return data.FieldByName(name).IsValid()
|
|
|
|
} else if data.Kind() == reflect.Slice {
|
|
|
|
return reflect.New(data.Type().Elem()).Elem().FieldByName(name).IsValid()
|
|
|
|
}
|
2014-01-26 08:41:37 +04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (scope *Scope) SetColumn(column string, value interface{}) {
|
2014-01-26 09:51:23 +04:00
|
|
|
data := reflect.Indirect(reflect.ValueOf(scope.Value))
|
|
|
|
setFieldValue(data.FieldByName(snakeToUpperCamel(column)), value)
|
2014-01-26 08:41:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (scope *Scope) CallMethod(name string) {
|
2014-01-26 09:51:23 +04:00
|
|
|
if fm := reflect.ValueOf(scope.Value).MethodByName(name); fm.IsValid() {
|
|
|
|
fi := fm.Interface()
|
|
|
|
if f, ok := fi.(func()); ok {
|
|
|
|
f()
|
|
|
|
} else if f, ok := fi.(func(s *Scope)); ok {
|
|
|
|
f(scope)
|
|
|
|
} else if f, ok := fi.(func(s *DB)); ok {
|
|
|
|
f(scope.db.new())
|
|
|
|
} else if f, ok := fi.(func() error); ok {
|
|
|
|
scope.Err(f())
|
|
|
|
} else if f, ok := fi.(func(s *Scope) error); ok {
|
|
|
|
scope.Err(f(scope))
|
|
|
|
} else if f, ok := fi.(func(s *DB) error); ok {
|
|
|
|
scope.Err(f(scope.db.new()))
|
|
|
|
} else {
|
|
|
|
scope.Err(errors.New(fmt.Sprintf("unsupported function %v", name)))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
scope.Err(errors.New(fmt.Sprintf("no valid function %v found", name)))
|
|
|
|
}
|
2014-01-26 08:41:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (scope *Scope) CombinedConditionSql() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (scope *Scope) AddToVars(value interface{}) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (scope *Scope) TableName() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (scope *Scope) Raw(sql string, values ...interface{}) {
|
2014-01-26 09:51:23 +04:00
|
|
|
fmt.Println(sql, values)
|
2014-01-26 08:41:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (scope *Scope) Exec() {
|
|
|
|
}
|