Cleanup some codes

This commit is contained in:
Jinzhu 2013-11-11 13:53:04 +08:00
parent 1c49c4ef85
commit 0327deec14
4 changed files with 25 additions and 30 deletions

View File

@ -30,6 +30,7 @@ type Chain struct {
func (s *Chain) driver() string {
return s.d.driver
}
func (s *Chain) err(err error) error {
if err != nil {
s.Errors = append(s.Errors, err)

28
do.go
View File

@ -37,9 +37,7 @@ type Do struct {
func (s *Do) tableName() string {
if len(s.specifiedTableName) == 0 {
var err error
s.guessedTableName, err = s.model.tableName()
s.err(err)
s.guessedTableName = s.model.tableName()
return s.guessedTableName
} else {
return s.specifiedTableName
@ -172,8 +170,8 @@ func (s *Do) saveAfterAssociations() {
}
func (s *Do) create() (i interface{}) {
s.err(s.model.callMethod("BeforeCreate"))
s.err(s.model.callMethod("BeforeSave"))
s.model.callMethod("BeforeCreate")
s.model.callMethod("BeforeSave")
s.saveBeforeAssociations()
s.prepareCreateSql()
@ -197,8 +195,8 @@ func (s *Do) create() (i interface{}) {
}
s.saveAfterAssociations()
s.err(s.model.callMethod("AfterCreate"))
s.err(s.model.callMethod("AfterSave"))
s.model.callMethod("AfterCreate")
s.model.callMethod("AfterSave")
}
return id
}
@ -265,8 +263,8 @@ func (s *Do) update() (i int64) {
}
}
s.err(s.model.callMethod("BeforeUpdate"))
s.err(s.model.callMethod("BeforeSave"))
s.model.callMethod("BeforeUpdate")
s.model.callMethod("BeforeSave")
s.saveBeforeAssociations()
s.prepareUpdateSql(update_attrs)
@ -275,10 +273,8 @@ func (s *Do) update() (i int64) {
s.exec()
s.saveAfterAssociations()
if !s.hasError() {
s.err(s.model.callMethod("AfterUpdate"))
s.err(s.model.callMethod("AfterSave"))
}
s.model.callMethod("AfterUpdate")
s.model.callMethod("AfterSave")
}
return s.model.primaryKeyValue()
@ -290,7 +286,7 @@ func (s *Do) prepareDeleteSql() {
}
func (s *Do) delete() {
s.err(s.model.callMethod("BeforeDelete"))
s.model.callMethod("BeforeDelete")
if !s.hasError() {
if !s.unscoped && s.model.hasColumn("DeletedAt") {
@ -301,9 +297,7 @@ func (s *Do) delete() {
s.prepareDeleteSql()
s.exec()
}
if !s.hasError() {
s.err(s.model.callMethod("AfterDelete"))
}
s.model.callMethod("AfterDelete")
}
return
}

View File

@ -1218,25 +1218,25 @@ func TestTableName(t *testing.T) {
var table string
model := &Model{data: Order{}}
table, _ = model.tableName()
table = model.tableName()
if table != "orders" {
t.Errorf("Order table name should be orders")
}
db.SingularTable(true)
table, _ = model.tableName()
table = model.tableName()
if table != "order" {
t.Errorf("Order's singular table name should be order")
}
model2 := &Model{data: Cart{}}
table, _ = model2.tableName()
table = model2.tableName()
if table != "shopping_cart" {
t.Errorf("Cart's singular table name should be shopping_cart")
}
model3 := &Model{data: &Cart{}}
table, _ = model3.tableName()
table = model3.tableName()
if table != "shopping_cart" {
t.Errorf("Cart's singular table name should be shopping_cart")
}

View File

@ -277,9 +277,9 @@ func (m *Model) typeName() string {
return typ.Name()
}
func (m *Model) tableName() (str string, err error) {
func (m *Model) tableName() (str string) {
if m.data == nil {
err = errors.New("Model haven't been set")
m.do.err(errors.New("Model haven't been set"))
return
}
@ -288,7 +288,7 @@ func (m *Model) tableName() (str string, err error) {
v := fm.Call([]reflect.Value{})
if len(v) > 0 {
if result, ok := v[0].Interface().(string); ok {
return result, nil
return result
}
}
}
@ -300,7 +300,7 @@ func (m *Model) tableName() (str string, err error) {
for key, value := range pluralMap {
reg := regexp.MustCompile(key + "$")
if reg.MatchString(str) {
return reg.ReplaceAllString(str, value), err
return reg.ReplaceAllString(str, value)
}
}
}
@ -308,9 +308,9 @@ func (m *Model) tableName() (str string, err error) {
return
}
func (m *Model) callMethod(method string) error {
if m.data == nil {
return nil
func (m *Model) callMethod(method string) {
if m.data == nil || m.do.hasError() {
return
}
fm := reflect.ValueOf(m.data).MethodByName(method)
@ -318,11 +318,11 @@ func (m *Model) callMethod(method string) error {
v := fm.Call([]reflect.Value{})
if len(v) > 0 {
if verr, ok := v[0].Interface().(error); ok {
return verr
m.do.err(verr)
}
}
}
return nil
return
}
func (m *Model) returningStr() (str string) {