forked from go-gorm/gorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
173 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package gorm | ||
|
||
func BeforeCreate(scope *Scope) { | ||
scope.CallMethod("BeforeSave") | ||
scope.CallMethod("BeforeCreate") | ||
} | ||
|
||
func SaveBeforeAssociations(scope *Scope) { | ||
} | ||
|
||
func Create(scope *Scope) { | ||
if !scope.HasError() { | ||
var id interface{} | ||
if scope.Dialect().SupportLastInsertId() { | ||
if sql_result, err := scope.DB().Exec(scope.Sql, scope.SqlVars...); scope.Err(err) == nil { | ||
id, err = sql_result.LastInsertId() | ||
scope.Err(err) | ||
} | ||
} else { | ||
scope.Err(scope.DB().QueryRow(scope.Sql, scope.SqlVars...).Scan(&id)) | ||
} | ||
|
||
scope.SetColumn(scope.PrimaryKey(), id) | ||
} | ||
} | ||
|
||
func AfterCreate(scope *Scope) { | ||
scope.CallMethod("AfterCreate") | ||
scope.CallMethod("AfterSave") | ||
} | ||
|
||
func SaveAfterAssociations(scope *Scope) { | ||
} | ||
|
||
func init() { | ||
DefaultCallback.Create().Register("before_create", BeforeCreate) | ||
DefaultCallback.Create().Register("save_before_associations", SaveBeforeAssociations) | ||
DefaultCallback.Create().Register("create", Create) | ||
DefaultCallback.Create().Register("save_after_associations", SaveAfterAssociations) | ||
DefaultCallback.Create().Register("after_create", AfterCreate) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package gorm | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
func BeforeDelete(scope *Scope) { | ||
scope.CallMethod("BeforeDelete") | ||
} | ||
|
||
func Delete(scope *Scope) { | ||
if scope.HasError() { | ||
return | ||
} | ||
|
||
if !scope.Search.unscope && scope.HasColumn("DeletedAt") { | ||
scope.Raw(fmt.Sprintf("UPDATE %v SET deleted_at=%v %v", scope.TableName(), scope.AddToVars(time.Now()), scope.CombinedConditionSql())) | ||
} else { | ||
scope.Raw(fmt.Sprintf("DELETE FROM %v %v", scope.TableName(), scope.CombinedConditionSql())) | ||
} | ||
scope.Exec() | ||
} | ||
|
||
func AfterDelete(scope *Scope) { | ||
scope.CallMethod("AfterDelete") | ||
} | ||
|
||
func init() { | ||
DefaultCallback.Delete().Register("before_delete", BeforeDelete) | ||
DefaultCallback.Delete().Register("delete", Delete) | ||
DefaultCallback.Delete().Register("after_delete", AfterDelete) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package gorm | ||
|
||
import "github.com/jinzhu/gorm/dialect" | ||
|
||
type Scope struct { | ||
Search *search | ||
Sql string | ||
SqlVars []interface{} | ||
db *DB | ||
} | ||
|
||
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 { | ||
return true | ||
} | ||
|
||
func (scope *Scope) PrimaryKey() string { | ||
return "" | ||
} | ||
|
||
func (scope *Scope) HasColumn(name string) bool { | ||
return false | ||
} | ||
|
||
func (scope *Scope) SetColumn(column string, value interface{}) { | ||
} | ||
|
||
func (scope *Scope) CallMethod(name string) { | ||
} | ||
|
||
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{}) { | ||
} | ||
|
||
func (scope *Scope) Exec() { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters